OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 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 MySQLdb |
| 19 import StringIO |
| 20 import json |
| 21 import sys |
| 22 import unittest |
| 23 from urllib import urlencode |
| 24 |
| 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 class APITestCase(unittest.TestCase): |
| 53 def clear_rows(self): |
| 54 if self.db: |
| 55 db.write(self.db, (("DELETE FROM filters",), |
| 56 ("DELETE FROM frequencies",))) |
| 57 |
| 58 def setUp(self): |
| 59 try: |
| 60 db.testing = True |
| 61 self.db = db.connect() |
| 62 except MySQLdb.Error: |
| 63 self.db = None |
| 64 self.clear_rows() |
| 65 |
| 66 def tearDown(self): |
| 67 if self.db: |
| 68 self.clear_rows() |
| 69 self.db.close() |
| 70 self.db = None |
| 71 |
| 72 def assertResponse(self, handler, expected_response, expected_result=None, exp
ected_headers=None, **environ): |
| 73 def check_response(response, headers): |
| 74 self.assertEqual(response, expected_response) |
| 75 if not expected_headers is None: |
| 76 self.assertEqual(headers, expected_headers) |
| 77 |
| 78 if "body" in environ: |
| 79 environ["CONTENT_LENGTH"] = len(environ["body"]) |
| 80 environ["wsgi.input"] = StringIO.StringIO(environ["body"]) |
| 81 del environ["body"] |
| 82 |
| 83 if "get_params" in environ: |
| 84 environ["QUERY_STRING"] = urlencode(environ["get_params"]) |
| 85 del environ["get_params"] |
| 86 |
| 87 environ["wsgi.errors"] = sys.stderr |
| 88 result = handler(environ, check_response) |
| 89 if not expected_result is None: |
| 90 self.assertEqual(json.loads("".join(result)), expected_result) |
| 91 |
| 92 def test_submit(self): |
| 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 # Ensure missing or invalid JSON results in an error |
| 96 self.assertResponse(submit_handler, "400 Processing Error", |
| 97 REQUEST_METHOD="POST", body="") |
| 98 self.assertResponse(submit_handler, "400 Processing Error", |
| 99 REQUEST_METHOD="POST", body="Oops...") |
| 100 self.assertResponse(submit_handler, "400 Processing Error", |
| 101 REQUEST_METHOD="POST", body="{123:]") |
| 102 self.assertResponse(submit_handler, "400 Processing Error", |
| 103 REQUEST_METHOD="POST", body="1") |
| 104 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0) |
| 105 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0) |
| 106 # Ensure even an empty object, or one with the wrong fields returns OK |
| 107 self.assertResponse(submit_handler, "200 OK", |
| 108 REQUEST_METHOD="POST", body="{}") |
| 109 self.assertResponse(submit_handler, "200 OK", |
| 110 REQUEST_METHOD="POST", body="{\"hello\": \"world\"}") |
| 111 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0) |
| 112 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0) |
| 113 # Now some actually valid data |
| 114 self.assertResponse(submit_handler, "200 OK", |
| 115 REQUEST_METHOD="POST", body=valid_data) |
| 116 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2) |
| 117 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1) |
| 118 # Now make sure apparently valid data with timestamps that cause geometrical |
| 119 # mean calculations to fail with MySQL errors return OK but don't change DB |
| 120 invalid_data = json.loads(valid_data) |
| 121 invalid_data["filters"]["||example.com^"]["firstParty"]["example.com"]["late
st"] = 3 |
| 122 invalid_data = json.dumps(invalid_data) |
| 123 self.assertResponse(submit_handler, "200 OK", |
| 124 REQUEST_METHOD="POST", body=invalid_data) |
| 125 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2) |
| 126 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1) |
| 127 |
| 128 def test_query(self): |
| 129 # Basic query with no data, should return OK |
| 130 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul
ts": [], "echo": 0}) |
| 131 # If echo parameter is passed and is integer it should be returned |
| 132 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul
ts": [], "echo": 1337}, |
| 133 get_params={ "echo": 1337 }) |
| 134 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul
ts": [], "echo": 0}, |
| 135 get_params={ "echo": "naughty" }) |
| 136 # Now let's submit some data so we can query it back out |
| 137 test_data = [json.loads(valid_data), json.loads(valid_data), json.loads(vali
d_data)] |
| 138 test_data[1]["filters"]["##Second-Filter|"] = test_data[1]["filters"].pop("|
|example.com^") |
| 139 test_data[2]["filters"]["##Third-Filter|"] = test_data[2]["filters"].pop("||
example.com^") |
| 140 for data in test_data: |
| 141 self.assertResponse(submit_handler, "200 OK", |
| 142 REQUEST_METHOD="POST", body=json.dumps(data)) |
| 143 # Ordering parameters should be respected |
| 144 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6, |
| 145 "results": [{'domain': 'exampl
e.com', |
| 146 'filter': '||exam
ple.com^', |
| 147 'hits': 0}], "ech
o": 0}, |
| 148 get_params={ "order_by": "filter", "order": "desc", "tak
e": "1" }) |
| 149 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6, |
| 150 "results": [{'domain': 'exampl
e.com', |
| 151 'filter': '##Seco
nd-Filter|', |
| 152 'hits': 0}], "ech
o": 0}, |
| 153 get_params={ "order_by": "filter", "order": "asc", "take
": "1" }) |
| 154 # As should filtering parameters |
| 155 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 3, |
| 156 "results": [{'domain': 'exampl
e.com', |
| 157 'filter': '##Thir
d-Filter|', |
| 158 'hits': 0}], "ech
o": 0}, |
| 159 get_params={ "domain": "example.com", "take": "1" }) |
| 160 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 2, |
| 161 "results": [{'domain': 'exampl
e.org', |
| 162 'filter': '##Thir
d-Filter|', |
| 163 'hits': 4}], "ech
o": 0}, |
| 164 get_params={ "filter": "Third", "take": 1 }) |
| 165 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 1, |
| 166 "results": [{'domain': 'exampl
e.com', |
| 167 'filter': '##Thir
d-Filter|', |
| 168 'hits': 0}], "ech
o": 0}, |
| 169 get_params={ "domain": "example.com", "filter": "Third",
"take": "1" }) |
| 170 # ... and pagination parameters |
| 171 self.maxDiff = None |
| 172 self.assertResponse(query_handler, "200 OK", {"count": 2, "total": 6, |
| 173 "results": [{'domain': 'exampl
e.org', |
| 174 'filter': '||exam
ple.com^', |
| 175 'hits': 4}, |
| 176 {'domain': 'exampl
e.org', |
| 177 'filter': '##Seco
nd-Filter|', |
| 178 'hits': 4}], "ech
o": 0}, |
| 179 get_params={ "skip": "1", "take": "2" }) |
| 180 self.assertResponse(query_handler, "200 OK", {"count": 2, "total": 6, |
| 181 "results": [{'domain': 'exampl
e.org', |
| 182 'filter': '##Seco
nd-Filter|', |
| 183 'hits': 4}, |
| 184 {'domain': 'exampl
e.com', |
| 185 'filter': '##Thir
d-Filter|', |
| 186 'hits': 0}], "ech
o": 0}, |
| 187 get_params={ "skip": "2", "take": "2" }) |
| 188 |
| 189 if __name__ == '__main__': |
| 190 unittest.main() |
OLD | NEW |