Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/filterhits/test/api_tests.py

Issue 29344572: Noissue - Removed obsolete sitescripts.filterhits package (Closed)
Patch Set: Created May 26, 2016, 12:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/filterhits/test/__init__.py ('k') | sitescripts/filterhits/test/db_tests.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2016 Eyeo GmbH
5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 import json
19 import sys
20 import StringIO
21 import unittest
22 from urllib import urlencode
23
24 from sitescripts.filterhits.test import test_helpers
25 from sitescripts.filterhits import db
26 from sitescripts.filterhits.web.query import query_handler
27 from sitescripts.filterhits.web.submit import submit as submit_handler
28
29 valid_data = """{
30 "version": 1,
31 "timeSincePush": 12345,
32 "addonName": "adblockplus",
33 "addonVersion": "2.3.4",
34 "application": "firefox",
35 "applicationVersion": "31",
36 "platform": "gecko",
37 "platformVersion": "31",
38 "filters": {
39 "||example.com^": {
40 "firstParty": {
41 "example.com": {"hits": 12, "latest": 1414817340948},
42 "example.org": {"hits": 4, "latest": 1414859271125}
43 },
44 "thirdParty": {
45 "example.com": {"hits": 5, "latest": 1414916268769}
46 },
47 "subscriptions": ["EasyList", "EasyList Germany+EasyList"]
48 }
49 }
50 }"""
51
52
53 class APITestCase(test_helpers.FilterhitsTestCase):
54 def assertResponse(self, handler, expected_response, expected_result=None, e xpected_headers=None, **environ):
55 def check_response(response, headers):
56 self.assertEqual(response, expected_response)
57 if not expected_headers is None:
58 self.assertEqual(headers, expected_headers)
59
60 if "body" in environ:
61 environ["CONTENT_LENGTH"] = len(environ["body"])
62 environ["wsgi.input"] = StringIO.StringIO(environ["body"])
63 del environ["body"]
64
65 if "get_params" in environ:
66 environ["QUERY_STRING"] = urlencode(environ["get_params"])
67 del environ["get_params"]
68
69 environ["wsgi.errors"] = sys.stderr
70 result = handler(environ, check_response)
71 if not expected_result is None:
72 self.assertEqual(json.loads("".join(result)), expected_result)
73
74 def test_submit(self):
75 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0)
76 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0)
77 # Ensure missing or invalid JSON results in an error
78 self.assertResponse(submit_handler, "400 Processing Error",
79 REQUEST_METHOD="POST", body="")
80 self.assertResponse(submit_handler, "400 Processing Error",
81 REQUEST_METHOD="POST", body="Oops...")
82 self.assertResponse(submit_handler, "400 Processing Error",
83 REQUEST_METHOD="POST", body="{123:]")
84 self.assertResponse(submit_handler, "400 Processing Error",
85 REQUEST_METHOD="POST", body="1")
86 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0)
87 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0)
88 # Ensure even an empty object, or one with the wrong fields returns succ essfully
89 self.assertResponse(submit_handler, "204 No Content",
90 REQUEST_METHOD="POST", body="{}")
91 self.assertResponse(submit_handler, "204 No Content",
92 REQUEST_METHOD="POST", body="{\"hello\": \"world\"}" )
93 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0)
94 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0)
95 # Now some actually valid data
96 self.assertResponse(submit_handler, "204 No Content",
97 REQUEST_METHOD="POST", body=valid_data)
98 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2)
99 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1)
100 # Now make sure apparently valid data with timestamps that cause geometr ical
101 # mean calculations to fail with MySQL errors return OK but don't change DB
102 invalid_data = json.loads(valid_data)
103 invalid_data["filters"]["||example.com^"]["firstParty"]["example.com"][" latest"] = 3
104 invalid_data = json.dumps(invalid_data)
105 self.assertResponse(submit_handler, "204 No Content",
106 REQUEST_METHOD="POST", body=invalid_data)
107 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2)
108 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1)
109
110 def test_query(self):
111 # Basic query with no data, should return successfully
112 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "r esults": [], "echo": 0})
113 # If echo parameter is passed and is integer it should be returned
114 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "r esults": [], "echo": 1337},
115 get_params={"echo": 1337})
116 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "r esults": [], "echo": 0},
117 get_params={"echo": "naughty"})
118 # Now let's submit some data so we can query it back out
119 test_data = [json.loads(valid_data), json.loads(valid_data), json.loads( valid_data)]
120 test_data[1]["filters"]["##Second-Filter|"] = test_data[1]["filters"].po p("||example.com^")
121 test_data[2]["filters"]["##Third-Filter|"] = test_data[2]["filters"].pop ("||example.com^")
122 for data in test_data:
123 self.assertResponse(submit_handler, "204 No Content",
124 REQUEST_METHOD="POST", body=json.dumps(data))
125 # Ordering parameters should be respected
126 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6,
127 "results": [{"domain": "ex ample.com",
128 "filter": "|| example.com^",
129 "frequency": 0}], "echo": 0},
130 get_params={"order_by": "filter", "order": "desc", " take": "1"})
131 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6,
132 "results": [{"domain": "ex ample.com",
133 "filter": "## Second-Filter|",
134 "frequency": 0}], "echo": 0},
135 get_params={"order_by": "filter", "order": "asc", "t ake": "1"})
136 # As should filtering parameters
137 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 3,
138 "results": [{"domain": "ex ample.com",
139 "filter": "## Third-Filter|",
140 "frequency": 0}], "echo": 0},
141 get_params={"domain": "example.com", "take": "1"})
142 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 2,
143 "results": [{"domain": "ex ample.org",
144 "filter": "## Third-Filter|",
145 "frequency": 4}], "echo": 0},
146 get_params={"filter": "Third", "take": 1})
147 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 1,
148 "results": [{"domain": "ex ample.com",
149 "filter": "## Third-Filter|",
150 "frequency": 0}], "echo": 0},
151 get_params={"domain": "example.com", "filter": "Thir d", "take": "1"})
152 # ... and pagination parameters
153 self.maxDiff = None
154 self.assertResponse(query_handler, "200 OK", {"count": 2, "total": 6,
155 "results": [{"domain": "ex ample.org",
156 "filter": "|| example.com^",
157 "frequency": 4},
158 {"domain": "ex ample.org",
159 "filter": "## Second-Filter|",
160 "frequency": 4}], "echo": 0},
161 get_params={"skip": "1", "take": "2"})
162 self.assertResponse(query_handler, "200 OK", {"count": 2, "total": 6,
163 "results": [{"domain": "ex ample.org",
164 "filter": "## Second-Filter|",
165 "frequency": 4},
166 {"domain": "ex ample.com",
167 "filter": "## Third-Filter|",
168 "frequency": 0}], "echo": 0},
169 get_params={"skip": "2", "take": "2"})
170
171 if __name__ == "__main__":
172 unittest.main()
OLDNEW
« no previous file with comments | « sitescripts/filterhits/test/__init__.py ('k') | sitescripts/filterhits/test/db_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld