Skip to content

Commit

Permalink
Fast api user post and delete (#541)
Browse files Browse the repository at this point in the history
* Create user.py

* Delete user.py

* Create flasktest.py

* passed user post test

* test case for delete

* all test cases

* change based on comments

* Update .gitignore

* Delete yacs.n.iml

* Delete workspace.xml

* Delete modules.xml

* Delete vcs.xml

* Delete runConfigurations.xml

* removed .idea/.gitignore

Co-authored-by: Liam Haining <[email protected]>
  • Loading branch information
franchen28 and lhain08 authored Mar 18, 2022
1 parent d524b7f commit dba5d56
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ node_modules/
.python-version
*.ipynb*
courses20.xml
.coverage
.coverage
71 changes: 71 additions & 0 deletions src/api/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from .util import Client
TEST_USER = { 'email': '[email protected]',
'password': '123456' }

TEST_USER_SIGNUP = { 'email': '[email protected]',
'name': 'TestName',
'phone': '',
'password': '123456',
'degree': 'Undergraduate',
'major': 'CSCI' }

# pytest -s tests/test_user.py
def test_user_post_success(post_user, client: Client):
'''
add a valid new user into the session
'''
r = client.post("/api/user", json= {"name": "test2", "email":"[email protected]","phone": "", "password":"test123", "major":"", "degree":""} )
data = r.json()
assert r.status_code == 200
assert data['content'] is not None
assert data['content']['msg'] == "User added successfully."
r = client.post('/api/session', json={"email":"[email protected]", "password":"test123"})
assert r.status_code == 200
client.delete("/api/user", json={"sessionID":r.json()['content']['sessionID'], 'password':'test123'})

def test_user_post_failure(post_user, client: Client):
'''
add a invalid new user into the session
'''
r = client.post("/api/user", json= {"name": "test1", "email":"test1","phone": "", "password":"123", "major":"", "degree":""})
data = r.json()
assert data['content'] is None
assert r.status_code == 200

def test_user_delete_success(post_user, client: Client):
'''
delete a valid user in the session
'''
r1 = client.post("/api/session", json=TEST_USER) # {'email':'[email protected]', 'password': 'test123'})
data = r1.json()
sessionid = data['content']['sessionID']
r2 = client.delete("/api/user", json= {"sessionID": sessionid, "password": "123456"})
assert r2.status_code == 200
r=client.post('/api/user', json=TEST_USER_SIGNUP)
assert r.status_code == 200
data = r.json()
# assert data['content'] is not None
# assert data['content']['msg'] == "User added successfully."

def test_user_delete_failure(post_user, client: Client):
'''
delete a not exist user in the session
'''
r1 = client.post("/api/session", json=TEST_USER) # {'email':'[email protected]', 'password': 'test123'})
data = r1.json()
sessionid = data['content']['sessionID']
r2 = client.delete("/api/user", json= {"sessionID": sessionid, "password": "12345"})
assert r2.status_code == 200
data2 = r2.json()
assert data2['errMsg'] == "Wrong password."

def test_user_delete_failure2(post_user, client: Client):
'''
delete the session, then try to delete user
'''
sess = client.post("/api/session", json=TEST_USER).json()
sessID = sess['content']['sessionID']
r = client.delete('/api/session', json={'sessionID': sessID})
assert r.status_code == 200
r1 = client.delete("/api/user", json= {"sessionID": sessID, "password": "12345"})
assert r1.status_code == 403

0 comments on commit dba5d56

Please sign in to comment.