OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # |
| 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 |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 import base64 |
| 17 import sys |
| 18 import urllib2 |
| 19 |
| 20 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
| 21 remove_wsgi_intercept) |
| 22 |
| 23 # Fake import M2Crypto so that we don't need to install the real thing for this |
| 24 # test where we don't want encryption anyway. |
| 25 sys.modules['M2Crypto'] = sys |
| 26 |
| 27 from sitescripts.testpages.web import sitekey_frame |
| 28 |
| 29 |
| 30 # Request parameters. |
| 31 HOST = 'test.local' |
| 32 SCRIPT_NAME = 'sitekey-frame' |
| 33 USER_AGENT = 'foobar' |
| 34 |
| 35 # This is the string that should be signed by the web handler. |
| 36 TO_SIGN = '/{}\x00{}\x00{}'.format(SCRIPT_NAME, HOST, USER_AGENT) |
| 37 |
| 38 |
| 39 class FakeTemplate: |
| 40 """Jinja template replacement for testing.""" |
| 41 |
| 42 def __init__(self, *args, **kw): |
| 43 pass |
| 44 |
| 45 def render(self, params): |
| 46 """Stringify the parameters.""" |
| 47 return str(params) |
| 48 |
| 49 |
| 50 class FakeM2Crypto: |
| 51 """M2Crypto replacement for testing.""" |
| 52 |
| 53 class EVP: |
| 54 @staticmethod |
| 55 def load_key(path): |
| 56 return FakeM2Crypto() |
| 57 |
| 58 def sign_init(self): |
| 59 pass |
| 60 |
| 61 def sign_update(self, data): |
| 62 self.data = data |
| 63 |
| 64 def as_der(self): |
| 65 return 'key' |
| 66 |
| 67 def final(self): |
| 68 return self.data |
| 69 |
| 70 |
| 71 MODULE = 'sitescripts.testpages.web.sitekey_frame' |
| 72 |
| 73 |
| 74 def test_sitekey_frame(mocker): |
| 75 mocker.patch(MODULE + '.get_template', FakeTemplate) |
| 76 mocker.patch(MODULE + '.M2Crypto', FakeM2Crypto) |
| 77 |
| 78 urllib_intercept.install_opener() |
| 79 add_wsgi_intercept(HOST, 80, lambda: sitekey_frame.sitekey_frame, |
| 80 script_name=SCRIPT_NAME) |
| 81 try: |
| 82 response = urllib2.urlopen(urllib2.Request( |
| 83 'http://{}/{}'.format(HOST, SCRIPT_NAME), |
| 84 headers={'User-Agent': 'foobar'}, |
| 85 )) |
| 86 assert response.code == 200 |
| 87 data = eval(response.read()) |
| 88 assert base64.b64decode(data['signature']) == TO_SIGN |
| 89 assert data['http_path'] == '/' + SCRIPT_NAME |
| 90 assert data['http_host'] == HOST |
| 91 assert data['http_ua'] == USER_AGENT |
| 92 finally: |
| 93 remove_wsgi_intercept() |
OLD | NEW |