Skip to content

Commit 8906e41

Browse files
authored
Add unit tests for user sign in (canada-ca#143)
* Add tests for user-sign-in * Add invalid credentials test
1 parent 9cfcaf5 commit 8906e41

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

api/tests/test_user_sign_in.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import sys
2+
import os
3+
from os.path import dirname, join, expanduser, normpath, realpath
4+
5+
import pyotp
6+
import pytest
7+
from flask_bcrypt import Bcrypt
8+
from graphene.test import Client
9+
10+
11+
# This is the only way I could get imports to work for unit testing.
12+
PACKAGE_PARENT = '..'
13+
SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__))))
14+
sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT)))
15+
16+
from manage import seed, remove_seed
17+
seed()
18+
from db import *
19+
from app import app
20+
from queries import schema
21+
from models import Users
22+
from functions.error_messages import(
23+
error_invalid_credentials
24+
)
25+
remove_seed()
26+
27+
28+
@pytest.fixture(scope='class')
29+
def user_schema_test_db_init():
30+
db.init_app(app)
31+
bcrypt = Bcrypt(app)
32+
33+
with app.app_context():
34+
test_user = Users(
35+
display_name="testuser",
36+
user_name="testuser@testemail.ca",
37+
user_password=bcrypt.generate_password_hash(
38+
password="testpassword123").decode("UTF-8"),
39+
)
40+
db.session.add(test_user)
41+
db.session.commit()
42+
43+
yield
44+
45+
with app.app_context():
46+
Users.query.delete()
47+
db.session.commit()
48+
49+
50+
##
51+
# This class of tests works within the 'createUser' api endpoint
52+
@pytest.mark.usefixtures('user_schema_test_db_init')
53+
class TestSignInUser:
54+
def test_successful_sign_in(self):
55+
"""Test that ensures a user can be signed in"""
56+
with app.app_context():
57+
client = Client(schema)
58+
executed = client.execute(
59+
'''
60+
mutation{
61+
signIn(userName:"testuser@testemail.ca", password:"testpassword123"){
62+
user{
63+
userName
64+
}
65+
}
66+
}
67+
''')
68+
assert executed['data']
69+
assert executed['data']['signIn']
70+
assert executed['data']['signIn']['user']
71+
assert executed['data']['signIn']['user']['userName'] == "testuser@testemail.ca"
72+
73+
def test_invalid_credentials(self):
74+
"""Test that ensures a user can be signed in"""
75+
with app.app_context():
76+
client = Client(schema)
77+
executed = client.execute(
78+
'''
79+
mutation{
80+
signIn(userName:"testuser@testemail.ca", password:"testpassword1234"){
81+
user{
82+
userName
83+
}
84+
}
85+
}
86+
''')
87+
assert executed['errors']
88+
assert executed['errors'][0]
89+
assert executed['errors'][0]['message']
90+
assert executed['errors'][0]['message'] == error_invalid_credentials()
91+
92+

0 commit comments

Comments
 (0)