-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.py
288 lines (195 loc) · 7.01 KB
/
unit_test.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import json
import pytest
from flask import Flask
from flask_migrate import Migrate
from controllers.note_routes import blp as note_blp
from controllers.user_routes import blp as user_blp
from database import db
@pytest.fixture(scope="session")
def app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
migrate = Migrate(app, db)
app.register_blueprint(user_blp, url_prefix='/api/v1/users')
app.register_blueprint(note_blp, url_prefix='/api/v1/notes')
with app.app_context():
db.create_all()
yield app
with app.app_context():
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
# User tests
def test_create_user_successful(client):
data = {
"email": "[email protected]",
"username": "testuser"
}
response = client.post('/api/v1/users/create', json=data)
assert response.status_code == 201
data = json.loads(response.data)
assert "message" in data
assert "user_id" in data
def test_get_all_users_successful(client):
response = client.get('/api/v1/users')
assert response.status_code == 200
data = json.loads(response.data)
assert "users" in data
def test_get_user_by_id(client):
response = client.get('/api/v1/users/1')
assert response.status_code == 200
data = json.loads(response.data)
assert "user" in data
def test_get_user_by_id_404(client): # User not found
response = client.get('/api/v1/users/5')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_update_user_successful(client):
data = {
"email": "[email protected]",
"username": "updateduser"
}
response = client.put('/api/v1/users/1', json=data)
assert response.status_code == 200
data = json.loads(response.data)
assert "message" in data
def test_update_user_404(client): # User not found
data = {
"email": "[email protected]",
"username": "updateduser"
}
response = client.put('/api/v1/users/5', json=data)
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_delete_user_successful(client):
response = client.delete('/api/v1/users/1')
assert response.status_code == 200
data = json.loads(response.data)
assert "message" in data
def test_delete_user_404(client):
response = client.delete('/api/v1/users/5')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
# to check correctness of notes
def test_create_user(client):
data1 = {
"email": "[email protected]",
"username": "testuser"
}
data2 = {
"email": "[email protected]",
"username": "testuser1"
}
response = client.post('/api/v1/users/create', json=data1)
assert response.status_code == 201
response1 = client.post('/api/v1/users/create', json=data2)
assert response1.status_code == 201
# Notes tests
def test_create_note_successful(client):
data = {
"title": "Test Note",
"text": "This is a test note",
"user_id": 1
}
response = client.post('/api/v1/notes/create', json=data)
assert response.status_code == 201
data = json.loads(response.data)
assert "message" in data
assert "note_id" in data
def test_create_note_404(client):
data = {
"title": "Test Note",
"text": "This is a test note",
"user_id": 123
}
response = client.post('/api/v1/notes/create', json=data)
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_get_all_notes_successful(client):
response = client.get('/api/v1/notes')
assert response.status_code == 200
data = json.loads(response.data)
assert "notes" in data
def test_get_note_by_id_successful(client):
response = client.get('/api/v1/notes/1')
assert response.status_code == 200
data = json.loads(response.data)
assert "note" in data
def test_get_note_by_id_404(client):
response = client.get('/api/v1/notes/645338')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "Note not found"
def test_get_all_notes_by_user_id_successful(client):
response = client.get('/api/v1/users/1/notes')
assert response.status_code == 200
data = json.loads(response.data)
assert "Notes by user" in data
def test_get_all_notes_by_user_id_404(client):
response = client.get('/api/v1/users/123/notes')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_update_note_successful(client):
data = {
"title": "Updated Note",
"text": "This note has been updated"
}
response = client.put('/api/v1/notes/1/1', json=data)
assert response.status_code == 200
data = json.loads(response.data)
assert "message" in data
def test_update_note_404_1(client): # Note not found
data = {
"title": "Updated Note",
"text": "This note has been updated"
}
response = client.put('/api/v1/notes/123/1', json=data)
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "Note not found"
def test_update_note_404_2(client): # User not found
data = {
"title": "Updated Note",
"text": "This note has been updated"
}
response = client.put('/api/v1/notes/1/123', json=data)
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_update_note_403(client): # Access forbidden
data = {
"title": "Updated Note",
"text": "This note has been updated"
}
response = client.put('/api/v1/notes/1/2', json=data)
assert response.status_code == 403
data = json.loads(response.data)
assert data["error"] == "You are not allowed to perform this action"
def test_delete_note_404_1(client): # Note not found
response = client.delete('/api/v1/notes/123/1')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "Note not found"
def test_delete_note_404_2(client): # User not found
response = client.delete('/api/v1/notes/1/123')
assert response.status_code == 404
data = json.loads(response.data)
assert data["error"] == "User not found"
def test_delete_note_403(client): # Access denied
response = client.delete('/api/v1/notes/1/2')
assert response.status_code == 403
data = json.loads(response.data)
assert data["error"] == "You are not allowed to perform this action"
def test_delete_note_successful(client):
response = client.delete('/api/v1/notes/1/1')
assert response.status_code == 200
data = json.loads(response.data)
assert "message" in data