forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_stats.py
129 lines (119 loc) · 3.69 KB
/
test_stats.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
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
"""Tests for the /api/v1/stats endpoints."""
from pytest_mongo import factories
from pytest_redis import factories
from .common import ( # noqa (fixture)
ADMIN_DEMOGRAPHICS,
clear_db,
client,
decode_response,
get_csrf_token,
register_test_accounts,
TEACHER_DEMOGRAPHICS,
STUDENT_DEMOGRAPHICS,
STUDENT_2_DEMOGRAPHICS,
OTHER_USER_DEMOGRAPHICS,
load_sample_problems,
get_conn,
ensure_within_competition,
enable_sample_problems,
get_problem_key,
cache,
RATE_LIMIT_BYPASS_KEY,
update_all_scoreboards,
)
import api
def test_registration_stats(mongo_proc, redis_proc, client):
"""Test the /stats/registration endpoint."""
clear_db()
register_test_accounts()
# Get the initial registration count
res = client.get("/api/v1/stats/registration")
assert res.status_code == 200
expected_response = {
"groups": 0,
"teamed_users": 0,
"teams": 0,
"users": 5,
"teachers": 1,
}
assert res.json == expected_response
# Try adding a new user
res = client.post(
"/api/v1/users",
json={
"email": "[email protected]",
"firstname": "Third",
"lastname": "Testuser",
"password": "testuser3",
"username": "testuser3",
"affiliation": "Testing",
"usertype": "other",
"country": "US",
"demo": {"age": "18+"},
},
)
assert res.status_code == 201
cache(api.stats.get_registration_count)
res = client.get("/api/v1/stats/registration")
assert res.status_code == 200
expected_response["users"] += 1
assert res.json == expected_response
# Have a user create a team
client.post(
"api/v1/user/login",
json={
"username": STUDENT_DEMOGRAPHICS["username"],
"password": STUDENT_DEMOGRAPHICS["password"],
},
)
res = client.post(
"/api/v1/teams", json={"team_name": "newteam", "team_password": "newteam"}
)
assert res.status_code == 201
cache(api.stats.get_registration_count)
res = client.get("/api/v1/stats/registration")
assert res.status_code == 200
expected_response["teams"] += 1
expected_response["teamed_users"] += 1
assert res.json == expected_response
# Have another user join that team
api.config.get_settings()
db = get_conn()
db.settings.find_one_and_update({}, {"$set": {"max_team_size": 2}})
client.get("/api/v1/user/logout")
client.post(
"/api/v1/user/login",
json={
"username": STUDENT_2_DEMOGRAPHICS["username"],
"password": STUDENT_2_DEMOGRAPHICS["password"],
},
)
res = client.post(
"/api/v1/team/join", json={"team_name": "newteam", "team_password": "newteam"}
)
assert res.status_code == 200
cache(api.stats.get_registration_count)
res = client.get("/api/v1/stats/registration")
assert res.status_code == 200
expected_response["teamed_users"] += 1
assert res.json == expected_response
# Create a group
client.get("/api/v1/user/logout")
res = client.post(
"/api/v1/user/login",
json={
"username": TEACHER_DEMOGRAPHICS["username"],
"password": TEACHER_DEMOGRAPHICS["password"],
},
)
csrf_t = get_csrf_token(res)
res = client.post(
"/api/v1/groups", json={"name": "newgroup"}, headers=[("X-CSRF-Token", csrf_t)]
)
print(res.json)
assert res.status_code == 201
cache(api.stats.get_registration_count)
res = client.get("/api/v1/stats/registration")
assert res.status_code == 200
expected_response["groups"] += 1
assert res.json == expected_response