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
|
import pytest
from flask import g, session, json, url_for, request
from sw.db import get_db
from sw.auth import login_required, specific_user_required, json_input
from werkzeug.datastructures import Headers
def test_register(client, app):
response = client.post(
"/auth/register",
data=json.dumps({"username": "a", "password": "a",}),
content_type="application/json",
)
# TODO assert "/u/a" in response.headers["Location"]
with app.app_context():
db_result = (
get_db().execute("select * from users where username='a'",).fetchone()
)
assert db_result is not None
def test_login_right_credentials(client, auth):
assert auth.login().status_code == 200
with client:
client.get("/")
assert session["user_id"] == 1
assert g.user["username"] == "test"
@pytest.mark.parametrize(
("username", "password", "message"),
(
(None, "", b"Username required"),
("", "", b"Username must not be empty"),
("a", None, b"Password required"),
("a", "", b"Password must not be empty"),
("test", "test", b"Username test is taken"),
),
)
def test_register_validate_input(client, username, password, message):
response = client.post(
"/auth/register",
data=json.dumps({"username": username, "password": password,}),
headers={"Content-Type": "application/json"},
)
assert message in response.data
def test_login_required_no_user(client, app):
test_endpoint = "/test/dummy_endpoint"
@app.route(test_endpoint)
@login_required
def dummy_endpoint():
return "Should be unreachable", 200
response = client.get(test_endpoint)
assert response.status_code == 403
assert b"Should be unreachable" not in response.data
def test_login_required_with_user(client, app, auth):
test_endpoint = "/test/dummy_endpoint"
@app.route(test_endpoint)
@login_required
def dummy_endpoint():
return "Should succeed", 200
auth.login()
with client:
response = client.get(test_endpoint)
assert response.status_code == 200
assert b"Should succeed" in response.data
def test_specific_user_required_no_user(client, app):
test_endpoint = "/test/dummy_endpoint/<username>"
@app.route(test_endpoint)
@specific_user_required
def dummy_endpoint(username):
return "Should be unreachable", 200
response = client.get("/test/dummy_endpoint/test")
assert response.status_code == 403
assert b"Should be unreachable" not in response.data
def test_specific_user_required_wrong_user(client, app, auth):
test_endpoint = "/test/dummy_endpoint/<username>"
@app.route(test_endpoint)
@specific_user_required
def dummy_endpoint(username):
return "Should be unreachable", 200
auth.login()
with client:
response = client.get("/test/dummy_endpoint/other")
assert response.status_code == 403
assert b"Should be unreachable" not in response.data
def test_specific_user_required_right_user(client, app, auth):
test_endpoint = "/test/dummy_endpoint/<username>"
@app.route(test_endpoint)
@specific_user_required
def dummy_endpoint(username):
return "Should succeed", 200
auth.login()
with client:
response = client.get("/test/dummy_endpoint/test")
assert response.status_code == 200
assert b"Should succeed" in response.data
@pytest.mark.parametrize(
("input_data", "content_type", "expected_response"),
(
(None, None, 422),
("", None, 422),
("invalid JSON string", None, 422),
('{ "valid": "JSON"}', None, 422),
(None, "", 422),
("", "", 422),
("invalid JSON string", "", 422),
('{ "valid": "JSON"}', "", 422),
(None, "application/json", 400),
("", "application/json", 400),
("invalid JSON string", "application/json", 400),
('{ "valid": "JSON"}', "application/json", 201),
(None, "application/ld+json", 400),
("", "application/ld+json", 400),
("invalid JSON string", "application/ld+json", 400),
('{ "valid": "JSON"}', "application/ld+json", 201),
),
)
def test_json_input_empty_input(
client, app, input_data, content_type, expected_response
):
test_endpoint = "/test/dummy_endpoint"
@app.route(test_endpoint, methods=["POST"])
@json_input
def dummy_endpoint():
data = request.json
return data, 201
with client:
response = client.post(
test_endpoint, data=input_data, headers={"Content-Type": content_type}
)
assert response.status_code == expected_response
|