aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_application.py
blob: 20cbeace14e2c1a38c3dcd06f3f2edc3d190f340 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from base64 import b64decode, b64encode

import pytest
from webtest import TestApp

import paste.db
from paste import __main__, application
from paste.store import Store

DB = "file::memory:?cache=shared"


@pytest.fixture
def db():
    with paste.db.connect(DB) as d:
        yield d


@pytest.fixture
def app(db):
    _ = db
    app = TestApp(application, extra_environ={"HTTP_HOST": "localhost", "PASTE_DB": DB})
    yield app


@pytest.fixture
def token(db):
    return b64encode(Store(db).generate_token()).decode()


@pytest.mark.parametrize("method", ["put", "post", "delete"])
def test_without_apikey(app, method):
    res = getattr(app, method)(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain"},
        expect_errors=True,
    )
    assert res.status == "401 Unauthorized"
    assert res.headers["WWW-Authenticate"] == "APIKey"


@pytest.mark.parametrize("method", ["put", "post", "delete"])
def test_malformed_authorization_header(app, method):
    res = getattr(app, method)(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": "malformed"},
        expect_errors=True,
    )
    assert res.status == "401 Unauthorized"
    assert res.headers["WWW-Authenticate"] == "APIKey"


@pytest.mark.parametrize("method", ["put", "post", "delete"])
def test_malformed_apikey(app, method):
    res = getattr(app, method)(
        "/test_key" "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": "APIKey malformed"},
        expect_errors=True,
    )
    assert res.status == "401 Unauthorized"
    assert res.headers["WWW-Authenticate"] == "APIKey"


@pytest.mark.parametrize("method", ["put", "post", "delete"])
def test_invalid_apikey(app, method, token):
    invalid = b64encode(bytes(b ^ 13 for b in b64decode(token))).decode()
    res = getattr(app, method)(
        "/test_key" "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {invalid}"},
        expect_errors=True,
    )
    assert res.status == "401 Unauthorized"
    assert res.headers["WWW-Authenticate"] == "APIKey"


def test_put(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url


def test_put_twice(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "204 No Content"
    assert res.headers["Location"] == res.request.url


@pytest.mark.parametrize("method", ["get", "head", "delete"])
def test_method_nonexistent_fails(app, method, token):
    headers = {}
    if method == "delete":
        headers = {"Authorization": f"APIKey {token}"}
    res = getattr(app, method)("/test_key", headers=headers, expect_errors=True)
    assert res.status == "404 Not Found"


def test_put_then_get_then_head(app, token):
    BODY = "Hello, World!"
    res = app.put(
        "/test_key",
        BODY,
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    res = app.get("/test_key")
    assert res.status == "200 OK"
    assert res.headers["ETag"] == etag
    assert res.text == BODY
    res = app.head("/test_key")
    assert res.status == "200 OK"
    assert res.headers["ETag"] == etag
    assert res.text == ""


def test_if_none_match(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    res = app.get("/test_key", headers={"If-None-Match": etag})
    assert res.status == "304 Not Modified"
    assert res.headers["ETag"] == etag


def test_if_none_match_other_etag(app, token):
    BODY = "Hello, World!"
    res = app.put(
        "/test_key",
        BODY,
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    res = app.get("/test_key", headers={"If-None-Match": '"not a real etag"'})
    assert res.status == "200 OK"
    assert res.headers["ETag"] == etag
    assert res.text == BODY


def test_if_none_match_malformed_etag(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    res = app.get(
        "/test_key", headers={"If-None-Match": "malformed"}, expect_errors=True
    )
    assert res.status == "400 Bad Request"


@pytest.mark.parametrize(
    "etags",
    [
        [None, "a"],
        ["a", None],
        [None, "a", "b"],
        ["a", None, "b"],
        ["a", "b", None],
    ],
)
def test_if_none_match_list(app, etags, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    etags_str = ", ".join(f'"{e}"' if e else etag for e in etags)
    res = app.get("/test_key", headers={"If-None-Match": etags_str})
    assert res.status == "304 Not Modified"
    assert res.headers["ETag"] == etag


def test_put_update(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    etag = res.headers["ETag"]
    res = app.put(
        "/test_key",
        "Hello, Updated World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "204 No Content"
    assert res.headers["ETag"] != etag
    res = app.get("/test_key")
    assert res.status == "200 OK"
    assert res.text == "Hello, Updated World!"


def test_delete(app, token):
    res = app.put(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] == res.request.url
    res = app.delete("/test_key", expect_errors=True)
    assert res.status == "401 Unauthorized"
    res = app.delete("/test_key", headers={"Authorization": f"APIKey {token}"})
    assert res.status == "204 No Content"
    res = app.delete(
        "/test_key", headers={"Authorization": f"APIKey {token}"}, expect_errors=True
    )
    assert res.status == "404 Not Found"


def test_post(app, token):
    res = app.post(
        "/test_key",
        "Hello, World!",
        headers={"Content-Type": "text/plain", "Authorization": f"APIKey {token}"},
    )
    assert res.status == "201 Created"
    assert res.headers["Location"] != res.request.url
    print(res.headers["Location"])
    print(res.request.url)
    assert res.headers["Location"].startswith(res.request.url)
    etag = res.headers["ETag"]
    res = app.get(res.headers["Location"])
    assert res.status == "200 OK"
    assert res.headers["ETag"] == etag