-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement first test in a module
- Loading branch information
1 parent
809fdc6
commit 4850bc9
Showing
14 changed files
with
216 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM mariadb:latest | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y mariadb-client && \ | ||
rm -rf /var/lib/apt/lists/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from flask_login import login_user | ||
|
||
from app.blueprints.auth.models import User | ||
|
||
|
||
class AuthenticationService: | ||
|
||
@staticmethod | ||
def login(email, password, remember=True): | ||
user = User.get_by_email(email) | ||
if user is not None and user.check_password(password): | ||
login_user(user, remember=remember) | ||
return True | ||
return False |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pytest | ||
from flask import url_for | ||
from app import create_app, db | ||
from app.blueprints.auth.models import User | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def test_client(): | ||
flask_app = create_app('testing') | ||
|
||
with flask_app.test_client() as testing_client: | ||
with flask_app.app_context(): | ||
db.create_all() | ||
|
||
user_test = User(email='[email protected]') | ||
user_test.set_password('test1234') | ||
db.session.add(user_test) | ||
db.session.commit() | ||
|
||
yield testing_client | ||
|
||
db.session.remove() | ||
db.drop_all() | ||
|
||
|
||
def login(test_client, email, password): | ||
""" | ||
Authenticates the user with the credentials provided. | ||
Args: | ||
test_client: Flask test client. | ||
email (str): User's email address. | ||
password (str): User's password. | ||
Returns: | ||
response: POST login request response. | ||
""" | ||
response = test_client.post('/login', data=dict( | ||
email=email, | ||
password=password | ||
), follow_redirects=True) | ||
return response | ||
|
||
|
||
def logout(test_client): | ||
""" | ||
Logs out the user. | ||
Args: | ||
test_client: Flask test client. | ||
Returns: | ||
response: Response to GET request to log out. | ||
""" | ||
return test_client.get('/logout', follow_redirects=True) | ||
|
||
|
||
def test_login_success(test_client): | ||
response = test_client.post('/login', data=dict( | ||
email='[email protected]', | ||
password='test1234' | ||
), follow_redirects=True) | ||
|
||
assert response.request.path != url_for('auth.login'), "Login was unsuccessful" | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
|
||
|
||
def test_login_unsuccessful_bad_email(test_client): | ||
response = test_client.post('/login', data=dict( | ||
email='[email protected]', | ||
password='test1234' | ||
), follow_redirects=True) | ||
|
||
assert response.request.path == url_for('auth.login'), "Login was unsuccessful" | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
|
||
|
||
def test_login_unsuccessful_bad_password(test_client): | ||
response = test_client.post('/login', data=dict( | ||
email='[email protected]', | ||
password='basspassword' | ||
), follow_redirects=True) | ||
|
||
assert response.request.path == url_for('auth.login'), "Login was unsuccessful" | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
|
||
|
||
def test_edit_profile_page_get(test_client): | ||
""" | ||
Tests access to the profile editing page via a GET request. | ||
""" | ||
login_response = login(test_client, '[email protected]', 'test1234') | ||
assert login_response.status_code == 200, "Login was unsuccessful." | ||
|
||
response = test_client.get('/profile/edit') | ||
assert response.status_code == 200, "The profile editing page could not be accessed." | ||
assert b"Edit profile" in response.data, "The expected content is not present on the page" | ||
|
||
logout(test_client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
echo "(testing) Hostname: $MARIADB_HOSTNAME, Port: $MARIADB_PORT, User: $MARIADB_USER, Test DB: $MARIADB_TEST_DATABASE" | ||
|
||
echo "MariaDB is up - creating test database if it doesn't exist" | ||
|
||
# Create the test database if it does not exist | ||
mariadb -h "$MARIADB_HOSTNAME" -P "$MARIADB_PORT" -u root -p"$MARIADB_ROOT_PASSWORD" -e "CREATE DATABASE IF NOT EXISTS \`${MARIADB_TEST_DATABASE}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; GRANT ALL PRIVILEGES ON \`${MARIADB_TEST_DATABASE}\`.* TO '$MARIADB_USER'@'%'; FLUSH PRIVILEGES;" | ||
|
||
|
||
echo "Test database created and privileges granted" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters