-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest_notes.py
164 lines (147 loc) · 5.67 KB
/
test_notes.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
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
from flask import json
from lms.lmsdb.models import Exercise, User
from tests import conftest
class TestNotes:
@staticmethod
def test_notes(
staff_user: User,
student_user: User,
):
staff_user2 = conftest.create_staff_user(index=1)
private_note, _ = conftest.create_note(
creator=staff_user,
user=student_user,
note_text='private note',
privacy=0,
)
staff_note, _ = conftest.create_note(
creator=staff_user,
user=student_user,
note_text='staff note',
privacy=1,
)
user_note, _ = conftest.create_note(
creator=staff_user,
user=student_user,
note_text='user note',
privacy=2,
)
public_note, _ = conftest.create_note(
creator=staff_user,
user=student_user,
note_text='public note',
privacy=3,
)
client = conftest.get_logged_user(staff_user2.username)
# Trying to remove a private note of another staff client
private_note_response = client.get(
f'/notes/{student_user.id}',
query_string={'noteId': private_note.id},
data=json.dumps({'act': 'delete'}),
content_type='application/json',
)
assert private_note_response.status_code == 403
# Removing a staff note of another staff user
staff_note_response = client.get(
f'/notes/{student_user.id}',
query_string={'noteId': staff_note.id},
data=json.dumps({'act': 'delete'}),
content_type='application/json',
)
assert staff_note_response.status_code == 200
# Another staff user can see only the remaining user and public comment
user_page_notes = client.get(
f'notes/{student_user.id}', query_string={'act': 'fetch'},
content_type='application/json',
)
json_user_page_notes = json.loads(
user_page_notes.get_data(as_text=True),
)
assert len(json_user_page_notes) == 2
# Staff trying unknown act
unknown_act_note_response = client.post(
f'/notes/{student_user.id}',
data=json.dumps({'act': 'unknown'}),
content_type='application/json',
)
assert unknown_act_note_response.status_code == 400
conftest.logout_user(client)
client2 = conftest.get_logged_user(student_user.username)
# User can see only the remaining user and public comment
user_page_notes = client2.get(
f'notes/{student_user.id}', query_string={'act': 'fetch'},
content_type='application/json',
)
json_user_page_notes = json.loads(
user_page_notes.get_data(as_text=True),
)
assert len(json_user_page_notes) == 2
# Trying to remove a public note
public_note_response = client2.get(
f'/notes/{student_user.id}',
query_string={'noteId': public_note.id},
data=json.dumps({'act': 'delete'}),
content_type='application/json',
)
assert public_note_response.status_code == 403
@staticmethod
def test_user_notes(student_user: User):
client = conftest.get_logged_user(student_user.username)
# User trying to create a note, doesn't matter what
new_note_response = client.post(
f'/notes/{student_user.id}',
data=json.dumps({'act': 'create'}),
content_type='application/json',
)
assert new_note_response.status_code == 403
# Trying to reach not exist user
not_exist_user_note_response = client.get(
'/notes/99', data=json.dumps({'act': 'fetch'}),
content_type='application/json',
)
assert not_exist_user_note_response.status_code == 404
@staticmethod
def test_create_note(
student_user: User,
staff_user: User,
exercise: Exercise,
):
client = conftest.get_logged_user(staff_user.username)
# Trying to create note with no text
new_note_response = client.post(
f'/notes/{student_user.id}',
data=json.dumps({'act': 'create'}),
query_string={'note': ''},
content_type='application/json',
)
assert new_note_response.status_code == 422
# Creating a staff note
staff_note_response = client.post(
f'/notes/{student_user.id}',
data=json.dumps({'act': 'create'}),
query_string={'note': 'staff note', 'privacy': '1'},
content_type='application/json',
)
assert staff_note_response.status_code == 200
# Creating a private note
private_note = {'note': 'private note', 'exercise': exercise.subject}
private_note_response = client.post(
f'/notes/{student_user.id}',
data=json.dumps({'act': 'create'}),
query_string=private_note,
content_type='application/json',
)
assert private_note_response.status_code == 200
# Fetching notes
user_page_notes = client.get(
f'notes/{student_user.id}', query_string={'act': 'fetch'},
content_type='application/json',
)
json_user_page_notes = json.loads(
user_page_notes.get_data(as_text=True),
)
staff_note, private_note = json_user_page_notes
assert staff_note.get('privacy') == 30
assert private_note.get('privacy') == 40
assert private_note.get('subject') == exercise.subject
assert staff_note.get('fullname') == staff_user.fullname