1 | """ |
---|
2 | Tests for ``allmydata.web.private``. |
---|
3 | |
---|
4 | Ported to Python 3. |
---|
5 | """ |
---|
6 | |
---|
7 | from testtools.matchers import ( |
---|
8 | Equals, |
---|
9 | ) |
---|
10 | from testtools.twistedsupport import ( |
---|
11 | succeeded, |
---|
12 | ) |
---|
13 | |
---|
14 | from twisted.web.http import ( |
---|
15 | UNAUTHORIZED, |
---|
16 | NOT_FOUND, |
---|
17 | ) |
---|
18 | from twisted.web.http_headers import ( |
---|
19 | Headers, |
---|
20 | ) |
---|
21 | |
---|
22 | from treq.client import ( |
---|
23 | HTTPClient, |
---|
24 | ) |
---|
25 | from treq.testing import ( |
---|
26 | RequestTraversalAgent, |
---|
27 | ) |
---|
28 | |
---|
29 | from ..common import ( |
---|
30 | SyncTestCase, |
---|
31 | ) |
---|
32 | |
---|
33 | from ...web.private import ( |
---|
34 | SCHEME, |
---|
35 | create_private_tree, |
---|
36 | ) |
---|
37 | |
---|
38 | from .matchers import ( |
---|
39 | has_response_code, |
---|
40 | ) |
---|
41 | |
---|
42 | class PrivacyTests(SyncTestCase): |
---|
43 | """ |
---|
44 | Tests for the privacy features of the resources created by ``create_private_tree``. |
---|
45 | """ |
---|
46 | def setUp(self): |
---|
47 | self.token = b"abcdef" |
---|
48 | self.resource = create_private_tree(lambda: self.token) |
---|
49 | self.agent = RequestTraversalAgent(self.resource) |
---|
50 | self.client = HTTPClient(self.agent) |
---|
51 | return super(PrivacyTests, self).setUp() |
---|
52 | |
---|
53 | def _authorization(self, scheme, value): |
---|
54 | value = str(value, "utf-8") |
---|
55 | return Headers({ |
---|
56 | u"authorization": [u"{} {}".format(scheme, value)], |
---|
57 | }) |
---|
58 | |
---|
59 | def test_unauthorized(self): |
---|
60 | """ |
---|
61 | A request without an *Authorization* header receives an *Unauthorized* response. |
---|
62 | """ |
---|
63 | self.assertThat( |
---|
64 | self.client.head(b"http:///foo/bar"), |
---|
65 | succeeded(has_response_code(Equals(UNAUTHORIZED))), |
---|
66 | ) |
---|
67 | |
---|
68 | def test_wrong_scheme(self): |
---|
69 | """ |
---|
70 | A request with an *Authorization* header not containing the Tahoe-LAFS |
---|
71 | scheme receives an *Unauthorized* response. |
---|
72 | """ |
---|
73 | self.assertThat( |
---|
74 | self.client.head( |
---|
75 | b"http:///foo/bar", |
---|
76 | headers=self._authorization(u"basic", self.token), |
---|
77 | ), |
---|
78 | succeeded(has_response_code(Equals(UNAUTHORIZED))), |
---|
79 | ) |
---|
80 | |
---|
81 | def test_wrong_token(self): |
---|
82 | """ |
---|
83 | A request with an *Authorization* header not containing the expected token |
---|
84 | receives an *Unauthorized* response. |
---|
85 | """ |
---|
86 | self.assertThat( |
---|
87 | self.client.head( |
---|
88 | b"http:///foo/bar", |
---|
89 | headers=self._authorization(str(SCHEME, "utf-8"), b"foo bar"), |
---|
90 | ), |
---|
91 | succeeded(has_response_code(Equals(UNAUTHORIZED))), |
---|
92 | ) |
---|
93 | |
---|
94 | def test_authorized(self): |
---|
95 | """ |
---|
96 | A request with an *Authorization* header containing the expected scheme |
---|
97 | and token does not receive an *Unauthorized* response. |
---|
98 | """ |
---|
99 | self.assertThat( |
---|
100 | self.client.head( |
---|
101 | b"http:///foo/bar", |
---|
102 | headers=self._authorization(str(SCHEME, "utf-8"), self.token), |
---|
103 | ), |
---|
104 | # It's a made up URL so we don't get a 200, either, but a 404. |
---|
105 | succeeded(has_response_code(Equals(NOT_FOUND))), |
---|
106 | ) |
---|