1
+ from .util import Client
2
+ TEST_USER = {
'email' :
'[email protected] ' ,
3
+ 'password' : '123456' }
4
+
5
+ TEST_USER_SIGNUP = {
'email' :
'[email protected] ' ,
6
+ 'name' : 'TestName' ,
7
+ 'phone' : '' ,
8
+ 'password' : '123456' ,
9
+ 'degree' : 'Undergraduate' ,
10
+ 'major' : 'CSCI' }
11
+
12
+ # pytest -s tests/test_user.py
13
+ def test_user_post_success (post_user , client : Client ):
14
+ '''
15
+ add a valid new user into the session
16
+ '''
17
+ r = client .
post (
"/api/user" ,
json = {
"name" :
"test2" ,
"email" :
"[email protected] " ,
"phone" :
"" ,
"password" :
"test123" ,
"major" :
"" ,
"degree" :
"" } )
18
+ data = r .json ()
19
+ assert r .status_code == 200
20
+ assert data ['content' ] is not None
21
+ assert data ['content' ]['msg' ] == "User added successfully."
22
+ r = client .
post (
'/api/session' ,
json = {
"email" :
"[email protected] " ,
"password" :
"test123" })
23
+ assert r .status_code == 200
24
+ client .delete ("/api/user" , json = {"sessionID" :r .json ()['content' ]['sessionID' ], 'password' :'test123' })
25
+
26
+ def test_user_post_failure (post_user , client : Client ):
27
+ '''
28
+ add a invalid new user into the session
29
+ '''
30
+ r = client .post ("/api/user" , json = {"name" : "test1" , "email" :"test1" ,"phone" : "" , "password" :"123" , "major" :"" , "degree" :"" })
31
+ data = r .json ()
32
+ assert data ['content' ] is None
33
+ assert r .status_code == 200
34
+
35
+ def test_user_delete_success (post_user , client : Client ):
36
+ '''
37
+ delete a valid user in the session
38
+ '''
39
+ r1 = client .
post (
"/api/session" ,
json = TEST_USER )
# {'email':'[email protected] ', 'password': 'test123'})
40
+ data = r1 .json ()
41
+ sessionid = data ['content' ]['sessionID' ]
42
+ r2 = client .delete ("/api/user" , json = {"sessionID" : sessionid , "password" : "123456" })
43
+ assert r2 .status_code == 200
44
+ r = client .post ('/api/user' , json = TEST_USER_SIGNUP )
45
+ assert r .status_code == 200
46
+ data = r .json ()
47
+ # assert data['content'] is not None
48
+ # assert data['content']['msg'] == "User added successfully."
49
+
50
+ def test_user_delete_failure (post_user , client : Client ):
51
+ '''
52
+ delete a not exist user in the session
53
+ '''
54
+ r1 = client .
post (
"/api/session" ,
json = TEST_USER )
# {'email':'[email protected] ', 'password': 'test123'})
55
+ data = r1 .json ()
56
+ sessionid = data ['content' ]['sessionID' ]
57
+ r2 = client .delete ("/api/user" , json = {"sessionID" : sessionid , "password" : "12345" })
58
+ assert r2 .status_code == 200
59
+ data2 = r2 .json ()
60
+ assert data2 ['errMsg' ] == "Wrong password."
61
+
62
+ def test_user_delete_failure2 (post_user , client : Client ):
63
+ '''
64
+ delete the session, then try to delete user
65
+ '''
66
+ sess = client .post ("/api/session" , json = TEST_USER ).json ()
67
+ sessID = sess ['content' ]['sessionID' ]
68
+ r = client .delete ('/api/session' , json = {'sessionID' : sessID })
69
+ assert r .status_code == 200
70
+ r1 = client .delete ("/api/user" , json = {"sessionID" : sessID , "password" : "12345" })
71
+ assert r1 .status_code == 403
0 commit comments