From 6d424787f79c711f5ddf44617c45fd769d397113 Mon Sep 17 00:00:00 2001 From: Matt Peachey Date: Fri, 28 Feb 2020 13:00:31 -0400 Subject: [PATCH 1/2] Add tests for user-sign-in --- api/tests/test_user_sign_in.py | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 api/tests/test_user_sign_in.py diff --git a/api/tests/test_user_sign_in.py b/api/tests/test_user_sign_in.py new file mode 100644 index 0000000000..114352b22d --- /dev/null +++ b/api/tests/test_user_sign_in.py @@ -0,0 +1,71 @@ +import sys +import os +from os.path import dirname, join, expanduser, normpath, realpath + +import pyotp +import pytest +from flask_bcrypt import Bcrypt +from graphene.test import Client + + +# This is the only way I could get imports to work for unit testing. +PACKAGE_PARENT = '..' +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) + +from manage import seed, remove_seed +seed() +from db import * +from app import app +from queries import schema +from models import Users +from functions.error_messages import * +remove_seed() + + +@pytest.fixture(scope='class') +def user_schema_test_db_init(): + db.init_app(app) + bcrypt = Bcrypt(app) + + with app.app_context(): + test_user = Users( + display_name="testuser", + user_name="testuser@testemail.ca", + user_password=bcrypt.generate_password_hash( + password="testpassword123").decode("UTF-8"), + ) + db.session.add(test_user) + db.session.commit() + + yield + + with app.app_context(): + Users.query.delete() + db.session.commit() + + +## +# This class of tests works within the 'createUser' api endpoint +@pytest.mark.usefixtures('user_schema_test_db_init') +class TestCreateUser: + def test_successful_creation(self): + """Test that ensures a user can be signed in""" + with app.app_context(): + client = Client(schema) + executed = client.execute( + ''' + mutation{ + signIn(userName:"testuser@testemail.ca", password:"testpassword123"){ + user{ + userName + } + } + } + ''') + assert executed['data'] + assert executed['data']['signIn'] + assert executed['data']['signIn']['user'] + assert executed['data']['signIn']['user']['userName'] == "testuser@testemail.ca" + + From d8fb628bbe08f870f8dd27b176fde1752dc746e2 Mon Sep 17 00:00:00 2001 From: Matt Peachey Date: Fri, 28 Feb 2020 13:10:44 -0400 Subject: [PATCH 2/2] Add invalid credentials test --- api/tests/test_user_sign_in.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/api/tests/test_user_sign_in.py b/api/tests/test_user_sign_in.py index 114352b22d..80d1adb161 100644 --- a/api/tests/test_user_sign_in.py +++ b/api/tests/test_user_sign_in.py @@ -19,7 +19,9 @@ from app import app from queries import schema from models import Users -from functions.error_messages import * +from functions.error_messages import( + error_invalid_credentials +) remove_seed() @@ -48,8 +50,8 @@ def user_schema_test_db_init(): ## # This class of tests works within the 'createUser' api endpoint @pytest.mark.usefixtures('user_schema_test_db_init') -class TestCreateUser: - def test_successful_creation(self): +class TestSignInUser: + def test_successful_sign_in(self): """Test that ensures a user can be signed in""" with app.app_context(): client = Client(schema) @@ -68,4 +70,23 @@ def test_successful_creation(self): assert executed['data']['signIn']['user'] assert executed['data']['signIn']['user']['userName'] == "testuser@testemail.ca" + def test_invalid_credentials(self): + """Test that ensures a user can be signed in""" + with app.app_context(): + client = Client(schema) + executed = client.execute( + ''' + mutation{ + signIn(userName:"testuser@testemail.ca", password:"testpassword1234"){ + user{ + userName + } + } + } + ''') + assert executed['errors'] + assert executed['errors'][0] + assert executed['errors'][0]['message'] + assert executed['errors'][0]['message'] == error_invalid_credentials() +