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