Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2017-present eyeo GmbH | 2 # Copyright (C) 2017-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 # We are mocking the functions that use MySQLdb, so it is not needed. This | 24 # We are mocking the functions that use MySQLdb, so it is not needed. This |
25 # is to prevent the tests from crashing when they try to import it. | 25 # is to prevent the tests from crashing when they try to import it. |
26 sys.modules['MySQLdb'] = sys | 26 sys.modules['MySQLdb'] = sys |
27 from sitescripts.reports.web.updateReport import handleRequest | 27 from sitescripts.reports.web.updateReport import handleRequest |
28 | 28 |
29 LOCAL_HOST = 'test.local' | 29 LOCAL_HOST = 'test.local' |
30 REMOTE_HOST = 'reports.adblockplus.org' | 30 REMOTE_HOST = 'reports.adblockplus.org' |
31 PORT = 80 | 31 PORT = 80 |
32 PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc' | 32 PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc' |
33 UR_PATH = 'sitescripts.reports.web.updateReport.' | 33 UR_PATH = 'sitescripts.reports.web.updateReport.' |
Vasily Kuznetsov
2019/02/07 20:19:16
Nice idea to make this a constant to avoid those s
rhowell
2019/02/08 01:34:51
Thanks. :)
| |
34 | 34 |
35 | 35 |
36 def intercept_fn(environ, start_response): | 36 def intercept_fn(environ, start_response): |
37 assert environ['SERVER_NAME'] == REMOTE_HOST | 37 assert environ['SERVER_NAME'] == REMOTE_HOST |
38 assert PLAINTEXT_GUID in environ['PATH_INFO'] | 38 assert PLAINTEXT_GUID in environ['PATH_INFO'] |
39 return 'Intercepted!' | 39 return 'Intercepted!' |
40 | 40 |
41 | 41 |
42 @pytest.fixture | 42 @pytest.fixture |
43 def response_for(): | 43 def response_for(): |
(...skipping 22 matching lines...) Expand all Loading... | |
66 'notify': 'test NOTIFY', | 66 'notify': 'test NOTIFY', |
67 'message': 'test MESSAGE', | 67 'message': 'test MESSAGE', |
68 'subject': 'test SUBJECT', | 68 'subject': 'test SUBJECT', |
69 'name': 'test NAME', | 69 'name': 'test NAME', |
70 } | 70 } |
71 | 71 |
72 | 72 |
73 @pytest.mark.parametrize('field,message', [ | 73 @pytest.mark.parametrize('field,message', [ |
74 (('guid', 'badGUID'), 'Invalid or missing report GUID'), | 74 (('guid', 'badGUID'), 'Invalid or missing report GUID'), |
75 (('secret', 'badSECRET'), 'Wrong secret value'), | 75 (('secret', 'badSECRET'), 'Wrong secret value'), |
76 (('name', ''), 'Missing fields'), | |
77 (('email', 'bad email'), 'Invalid email address'), | |
78 ]) | 76 ]) |
79 def test_http_errs(field, message, response_for, form_data, mocker): | 77 def test_http_errs(field, message, response_for, form_data, mocker): |
80 mocker.patch(UR_PATH + 'getReport', new=lambda *args: {'usefulness': 1}) | 78 mocker.patch(UR_PATH + 'getReport', new=lambda *args: {'usefulness': 1}) |
81 key, value = field | 79 key, value = field |
82 form_data[key] = value | 80 form_data[key] = value |
83 with pytest.raises(HTTPError) as error: | 81 with pytest.raises(HTTPError) as error: |
84 response_for(form_data) | 82 response_for(form_data) |
85 | 83 |
86 assert message in error.value.read() | 84 assert message in error.value.read() |
87 | 85 |
88 | 86 |
89 def test_success(response_for, form_data, mocker): | 87 def test_success(response_for, form_data, mocker): |
90 # These methods are patched to avoid the need for a MySQL database | 88 # These methods are patched to avoid the need for a MySQL database |
91 mocker.patch(UR_PATH + 'getReport', new=lambda *args: {'usefulness': 1, | 89 mocker.patch(UR_PATH + 'getReport', new=lambda *args: {'usefulness': 1, |
92 'email': 'jane_doe@example.com'}) | 90 'email': 'jane_doe@example.com'}) |
93 sr_mock = mocker.patch(UR_PATH + 'saveReport') | 91 sr_mock = mocker.patch(UR_PATH + 'saveReport') |
94 uu_mock = mocker.patch(UR_PATH + 'updateUserUsefulness') | 92 uuu_mock = mocker.patch(UR_PATH + 'updateUserUsefulness') |
Vasily Kuznetsov
2019/02/07 20:19:15
Maybe this should be uuu_mock to follow the same c
rhowell
2019/02/08 01:34:51
Right! I had it like that at first, but this line
| |
95 sun_mock = mocker.patch(UR_PATH + 'sendUpdateNotification') | 93 sun_mock = mocker.patch(UR_PATH + 'sendUpdateNotification') |
96 | 94 |
97 assert response_for(form_data) == (200, '\nIntercepted!') | 95 assert response_for(form_data) == (200, '\nIntercepted!') |
98 | 96 |
99 assert sr_mock.call_count == 1 | 97 assert sr_mock.call_count == 1 |
100 for key in ['usefulness', 'email']: | 98 for key in ['usefulness', 'email']: |
101 assert key in sr_mock.call_args[0][1] | 99 assert key in sr_mock.call_args[0][1] |
102 assert sr_mock.call_args[0][1][key] == str(form_data[key]) | 100 assert sr_mock.call_args[0][1][key] == str(form_data[key]) |
103 | 101 |
104 assert '0' in uu_mock.call_args[0] and 1 in uu_mock.call_args[0] | 102 assert '0' in uuu_mock.call_args[0] and 1 in uuu_mock.call_args[0] |
105 | 103 |
106 for key in ['email', 'status']: | 104 for key in ['email', 'status']: |
107 assert key in sun_mock.call_args[0][0] | 105 assert key in sun_mock.call_args[0][0] |
108 assert sun_mock.call_args[0][0]['email'] == form_data['email'] | 106 assert sun_mock.call_args[0][0]['email'] == form_data['email'] |
109 | 107 |
110 # These should not be equal, because updateReport.py strips characters | 108 # These should not be equal, because updateReport.py strips characters |
111 # over 1024, and form_data['status'] has 1025. | 109 # over 1024, and form_data['status'] has 1025. |
112 assert str(sr_mock.call_args[0][1]['status']) != form_data['status'] | 110 assert str(sr_mock.call_args[0][1]['status']) != form_data['status'] |
113 assert str(sun_mock.call_args[0][0]['status']) != form_data['status'] | 111 assert str(sun_mock.call_args[0][0]['status']) != form_data['status'] |
114 | 112 |
115 | 113 |
116 def test_get_report_error(response_for, form_data, mocker): | 114 def test_get_report_error(response_for, form_data, mocker): |
117 mocker.patch(UR_PATH + 'getReport', new=lambda *args: None) | 115 mocker.patch(UR_PATH + 'getReport', new=lambda *args: None) |
118 with pytest.raises(HTTPError) as error: | 116 with pytest.raises(HTTPError) as error: |
119 response_for(form_data) | 117 response_for(form_data) |
120 | 118 |
121 assert 'Report does not exist' in error.value.read() | 119 assert 'Report does not exist' in error.value.read() |
LEFT | RIGHT |