forked from bloomberg/pybossa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_http_signer.py
65 lines (52 loc) · 2.18 KB
/
test_http_signer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2015 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PYBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>.
from requests import Request
from pybossa.http_signer import HttpSigner
SECRET = 'my-secret-key'
HEADER = 'X-Sig'
class TestHttpSigner(object):
def setUp(self):
self.none_signer = HttpSigner(None, HEADER)
self.http_signer = HttpSigner(SECRET, HEADER)
def test_none_signer_does_not_sign(self):
req = Request('GET', 'http://example.com')
req = self.none_signer(req)
assert HEADER not in req.headers
assert not self.http_signer.valid(req)
def test_none_signer_does_not_sign_request_auth(self):
req = Request(
'GET', 'http://example.com', auth=self.none_signer)
prepared = req.prepare()
assert HEADER not in prepared.headers
assert not self.http_signer.valid(req)
def test_signs_request_valid(self):
req = Request('GET', 'http://example.com')
req = self.http_signer(req)
assert req.headers.get(HEADER) == SECRET
assert self.http_signer.valid(req)
def test_signs_request_invalid(self):
req = Request('GET', 'http://example.com')
req.headers[HEADER] = 'not-my-secret-key'
assert req.headers.get(HEADER) != SECRET
assert not self.http_signer.valid(req)
def test_signs_request_auth(self):
req = Request(
'GET', 'http://example.com', auth=self.http_signer)
prepared = req.prepare()
assert prepared.headers.get(HEADER) == SECRET
assert self.http_signer.valid(prepared)