forked from tal-training/project-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
141 lines (117 loc) · 3.88 KB
/
test_api.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
import pytest
from app import app, User
import json
from flask_login import FlaskLoginClient
app.test_client_class = FlaskLoginClient
client = app.test_client(user=User.get_by_id(1))
def test_create_user():
# creates a user
response = client.post(
"/api/user",
json={'username': 'test1', 'password': 'test1', 'admin': 'False'}
)
res = json.loads(response.data)
assert 'User Created Successfully' in res['result']
user_id = res['result'][-1]
# deletes a user
response_delete = client.delete(
f"/api/user?user_id={user_id}"
)
res = json.loads(response_delete.data)
assert 'User Deleted Successfully' == res['result']
def test_user_already_created():
name = "ron"
# creates a user
response_create = client.post(
"/api/user",
json={'username': name, 'password': 'test', 'admin': 'False'}
)
res = json.loads(response_create.data)
assert 'User Created Successfully' in res['result']
# creates user with the same name
user_id = res['result'][-1]
response_create_duplicate = client.post(
"/api/user",
json={'username': name, 'password': '123', 'admin': 'False'}
)
res = json.loads(response_create_duplicate.data)
assert 'User Already Exists' in res['result']
# deletes the user
response_delete = client.delete(
f"/api/user?user_id={user_id}"
)
res = json.loads(response_delete.data)
assert 'User Deleted Successfully' == res['result']
def test_user_password_update():
# creates a user
response_create = client.post(
"/api/user",
json={'username': 'test9', 'password': 'test', 'admin': 'False'}
)
res = json.loads(response_create.data)
assert 'User Created Successfully' in res['result']
# update password
user_id = res['result'][-1]
response_password_update = client.patch(
"/api/user",
json={'user_id': user_id, 'password': '123'}
)
res = json.loads(response_password_update.data)
assert 'User Password Updated Successfully' in res['result']
# delete a user
response_delete = client.delete(
f"/api/user?user_id={user_id}"
)
res = json.loads(response_delete.data)
assert 'User Deleted Successfully' == res['result']
def test_user_delete():
# create a user
response_create = client.post(
"/api/user",
json={'username': 'test5', 'password': 'test', 'admin': 'False'}
)
res = json.loads(response_create.data)
user_id = res['result'][-1]
assert 'User Created Successfully' in res['result']
# delete a user
response_delete = client.delete(
f"/api/user?user_id={user_id}"
)
res = json.loads(response_delete.data)
assert 'User Deleted Successfully' == res['result']
def test_note_actions():
# create users
response = client.post(
"/api/user",
json={'username': 'test12', 'password': 'test1', 'admin': 'False'}
)
res = json.loads(response.data)
user_id = res['result'][-1]
assert 'User Created Successfully' in res['result']
# create post
response = client.post(
"/api/note",
json={'text': 'some text', 'user_id': user_id}
)
res = json.loads(response.data)
note_id = res['result'][-1]
assert 'Note Created With id' in res['result']
# update post
response = client.patch(
"/api/note",
json={'text': 'some text', 'note_id': note_id}
)
res = json.loads(response.data)
assert f'Note {note_id} Updated' in res['result']
# delete post
response = client.delete(
f"/api/note?note_id={note_id}"
)
res = json.loads(response.data)
assert f'Note {note_id} Deleted' in res['result']
# deletes a user
response_delete = client.delete(
f"/api/user?user_id={user_id}"
)
res = json.loads(response_delete.data)
assert 'User Deleted Successfully' == res['result']