-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from diverso-lab/develop
Develop
- Loading branch information
Showing
30 changed files
with
1,285 additions
and
1,051 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[flake8] | ||
max-line-length = 120 | ||
max-line-length = 120 |
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
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,21 @@ | ||
from app.blueprints.auth.models import User | ||
from core.repositories.BaseRepository import BaseRepository | ||
|
||
|
||
class UserRepository(BaseRepository): | ||
def __init__(self): | ||
super().__init__(User) | ||
|
||
def create(self, commit: bool = True, **kwargs): | ||
password = kwargs.pop("password") | ||
instance = self.model(**kwargs) | ||
instance.set_password(password) | ||
self.session.add(instance) | ||
if commit: | ||
self.session.commit() | ||
else: | ||
self.session.flush() | ||
return instance | ||
|
||
def get_by_email(self, email: str): | ||
return self.model.query.filter_by(email=email).first() |
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 |
---|---|---|
@@ -1,14 +1,63 @@ | ||
from flask_login import login_user | ||
|
||
from app.blueprints.auth.models import User | ||
from app.blueprints.auth.repositories import UserRepository | ||
from app.blueprints.profile.repositories import UserProfileRepository | ||
from core.services.BaseService import BaseService | ||
|
||
|
||
class AuthenticationService: | ||
class AuthenticationService(BaseService): | ||
def __init__(self): | ||
super().__init__(UserRepository()) | ||
self.user_profile_repository = UserProfileRepository() | ||
|
||
@staticmethod | ||
def login(email, password, remember=True): | ||
user = User.get_by_email(email) | ||
def login(self, email, password, remember=True): | ||
user = self.repository.get_by_email(email) | ||
if user is not None and user.check_password(password): | ||
login_user(user, remember=remember) | ||
return True | ||
return False | ||
|
||
def is_email_available(self, email: str) -> bool: | ||
return self.repository.get_by_email(email) is None | ||
|
||
def create_with_profile(self, **kwargs): | ||
try: | ||
email = kwargs.pop("email", None) | ||
password = kwargs.pop("password", None) | ||
name = kwargs.pop("name", None) | ||
surname = kwargs.pop("surname", None) | ||
|
||
if not email: | ||
raise ValueError("Email is required.") | ||
if not password: | ||
raise ValueError("Password is required.") | ||
if not name: | ||
raise ValueError("Name is required.") | ||
if not surname: | ||
raise ValueError("Surname is required.") | ||
|
||
user_data = { | ||
"email": email, | ||
"password": password | ||
} | ||
|
||
profile_data = { | ||
"name": name, | ||
"surname": surname, | ||
} | ||
|
||
user = self.create(commit=False, **user_data) | ||
profile_data["user_id"] = user.id | ||
self.user_profile_repository.create(**profile_data) | ||
self.repository.session.commit() | ||
except Exception as exc: | ||
self.repository.session.rollback() | ||
raise exc | ||
return user | ||
|
||
def update_profile(self, user_profile_id, form): | ||
if form.validate(): | ||
updated_instance = self.update(user_profile_id, **form.data) | ||
return updated_instance, None | ||
|
||
return None, form.errors |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import pytest | ||
from flask import url_for | ||
|
||
from app.blueprints.auth.services import AuthenticationService | ||
from app.blueprints.auth.repositories import UserRepository | ||
from app.blueprints.profile.repositories import UserProfileRepository | ||
|
||
@pytest.fixture(scope='module') | ||
|
||
@pytest.fixture(scope="module") | ||
def test_client(test_client): | ||
""" | ||
Extends the test_client fixture to add additional specific data for module testing. | ||
for module testing (por example, new users) | ||
""" | ||
with test_client.application.app_context(): | ||
# Add HERE new elements to the database that you want to exist in the test context. | ||
|
@@ -17,33 +20,100 @@ def test_client(test_client): | |
|
||
|
||
def test_login_success(test_client): | ||
response = test_client.post('/login', data=dict( | ||
email='[email protected]', | ||
password='test1234' | ||
), follow_redirects=True) | ||
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" | ||
assert response.request.path != url_for("auth.login"), "Login was unsuccessful" | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
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) | ||
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" | ||
assert response.request.path == url_for("auth.login"), "Login was unsuccessful" | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
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) | ||
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_signup_user_no_name(test_client): | ||
response = test_client.post( | ||
"/signup", data=dict(surname="Foo", email="[email protected]", password="test1234"), follow_redirects=True | ||
) | ||
assert response.request.path == url_for("auth.show_signup_form"), "Signup was unsuccessful" | ||
assert b"This field is required" in response.data, response.data | ||
|
||
|
||
def test_signup_user_unsuccessful(test_client): | ||
email = "[email protected]" | ||
response = test_client.post( | ||
"/signup", data=dict(name="Test", surname="Foo", email=email, password="test1234"), follow_redirects=True | ||
) | ||
assert response.request.path == url_for("auth.show_signup_form"), "Signup was unsuccessful" | ||
assert f"Email {email} in use".encode("utf-8") in response.data | ||
|
||
|
||
def test_signup_user_successful(test_client): | ||
response = test_client.post( | ||
"/signup", | ||
data=dict(name="Foo", surname="Example", email="[email protected]", password="foo1234"), | ||
follow_redirects=True, | ||
) | ||
assert response.request.path == url_for("public.index"), "Signup was unsuccessful" | ||
|
||
|
||
def test_service_create_with_profie_success(clean_database): | ||
data = { | ||
"name": "Test", | ||
"surname": "Foo", | ||
"email": "[email protected]", | ||
"password": "test1234" | ||
} | ||
|
||
AuthenticationService().create_with_profile(**data) | ||
|
||
assert UserRepository().count() == 1 | ||
assert UserProfileRepository().count() == 1 | ||
|
||
|
||
def test_service_create_with_profile_fail_no_email(clean_database): | ||
data = { | ||
"name": "Test", | ||
"surname": "Foo", | ||
"email": "", | ||
"password": "1234" | ||
} | ||
|
||
with pytest.raises(ValueError, match="Email is required."): | ||
AuthenticationService().create_with_profile(**data) | ||
|
||
assert UserRepository().count() == 0 | ||
assert UserProfileRepository().count() == 0 | ||
|
||
|
||
def test_service_create_with_profile_fail_no_password(clean_database): | ||
data = { | ||
"name": "Test", | ||
"surname": "Foo", | ||
"email": "[email protected]", | ||
"password": "" | ||
} | ||
|
||
assert response.request.path == url_for('auth.login'), "Login was unsuccessful" | ||
with pytest.raises(ValueError, match="Password is required."): | ||
AuthenticationService().create_with_profile(**data) | ||
|
||
test_client.get('/logout', follow_redirects=True) | ||
assert UserRepository().count() == 0 | ||
assert UserProfileRepository().count() == 0 |
Oops, something went wrong.