Skip to content

Commit

Permalink
Add new methods and data for testing the files
Browse files Browse the repository at this point in the history
  • Loading branch information
yeboah326 committed Dec 9, 2021
1 parent 68a7021 commit d1e775b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
7 changes: 7 additions & 0 deletions backend/api/tests/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
super_user = {
"name": "Super User",
"email": "superu@email.com",
"type": "super",
"password": "testing1234"
}

test_user_1 = {
"name": "John Doe",
"email": "johndoe@email.com",
Expand Down
20 changes: 20 additions & 0 deletions backend/api/tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from api.auth.models import User, UserSchema
from api.reminder.models import Contact
from api.tests.data import (
super_user,
test_user_1,
test_user_2
)
Expand All @@ -16,6 +17,25 @@ def reset_db():
Contact.query.delete()
db.session.commit()

def create_super_user(client):
# Create super user
client.post(
"/api/auth/users",
json=super_user
)

super = User.find_by_email(super_user['email'])

# Login the super user
response = client.post(
"/api/auth/login",
json={"email": super_user['email'], "password": super_user['password']}
)

return response.json



def create_one_test_user(client):
# Create first user
client.post(
Expand Down
36 changes: 31 additions & 5 deletions backend/api/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
from api.tests.setup import (
reset_db,
create_super_user,
create_one_test_user,
create_two_test_users
)
Expand Down Expand Up @@ -31,20 +32,41 @@ def test_auth_create_new_user(client):
# Reset the database after running all tests
reset_db()

def test_auth_login_user(client):

# Reset the database before the test begins
reset_db()

# Create a single user
create_one_test_user(client)

response = client.post(
"/api/auth/login",
json={"email": test_user_1['email'], "password": test_user_1['password']}
)

assert response.status_code == 200
assert response.json['token']
assert response.json['user_type'] == 'normal'

def test_auth_get_all_users(client):
# Reset the database before the test begins
reset_db()

# Create super user
super = create_super_user(client)

create_two_test_users(client)

response = client.get(
"/api/auth/users"
"/api/auth/users",
headers={"Authorization": f"Bearer {super['token']}"}
)

assert response.status_code == 200
assert len(response.json) == 2
assert response.json[0]['email'] == test_user_1['email']
assert response.json[1]['email'] == test_user_2['email']
assert len(response.json) == 3
assert response.json[1]['email'] == test_user_1['email']
assert response.json[2]['email'] == test_user_2['email']

# Reset the database before the test begins
reset_db()
Expand All @@ -54,10 +76,14 @@ def test_auth_get_user_by_id(client):
# Reset the database before the test begins
reset_db()

# Create super user
super = create_super_user(client)
print(super)
user = create_one_test_user(client)

response = client.get(
f"/api/auth/users/{user['id']}"
f"/api/auth/users/{user['id']}",
headers={"Authorization": f"Bearer {super['token']}"}
)

assert response.status_code == 200
Expand Down

0 comments on commit d1e775b

Please sign in to comment.