-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed #127
- Loading branch information
Showing
9 changed files
with
147 additions
and
14 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
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,41 @@ | ||
""" Custom exceptions for the ptmd package """ | ||
from __future__ import annotations | ||
from abc import ABC | ||
|
||
|
||
class APIError(Exception, ABC): | ||
""" Exception raised when an API error occurs. This is an abstract class, do not use directly. """ | ||
|
||
def __init__(self) -> None: | ||
""" Constructor, do not use """ | ||
self.message: str | None = None | ||
raise SyntaxError("Cannot instantiate abstract class APIError") | ||
|
||
def __str__(self) -> str: | ||
""" String representation of the exception """ | ||
return self.message or "" | ||
|
||
|
||
class PasswordPolicyError(APIError): | ||
""" Exception raised when a password does not meet the password policy """ | ||
|
||
def __init__(self) -> None: | ||
""" Constructor """ | ||
self.message: str = "Password must be between 8 and 20 characters long, contain at least one uppercase " \ | ||
"letter, one lowercase letter, one number and one special character." | ||
|
||
|
||
class TokenExpiredError(APIError): | ||
""" Exception raised when a token is expired """ | ||
|
||
def __init__(self) -> None: | ||
""" Constructor """ | ||
self.message: str = "Token expired" | ||
|
||
|
||
class TokenInvalidError(APIError): | ||
""" Exception raised when a token is invalid """ | ||
|
||
def __init__(self) -> None: | ||
""" Constructor """ | ||
self.message: str = "Invalid token" |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from unittest.mock import patch, mock_open | ||
|
||
from ptmd.database import User, Organisation, File | ||
from ptmd.exceptions import PasswordPolicyError | ||
|
||
|
||
@patch("builtins.open", mock_open(read_data="{'save_credentials_file': 'test'}")) | ||
|
@@ -10,14 +11,14 @@ class TestUser(TestCase): | |
@patch('ptmd.database.models.token.send_confirmation_mail', return_value=True) | ||
def test_user(self, mock_send_confirmation_mail): | ||
expected_user = {'files': [], 'id': None, 'organisation': None, 'username': 'test', 'role': 'disabled'} | ||
user = User(username='test', password='test', email='[email protected]') | ||
user = User(username='test', password='A!Str0ngPwd', email='[email protected]') | ||
self.assertEqual(dict(user), expected_user) | ||
self.assertTrue(user.validate_password('test')) | ||
self.assertTrue(user.validate_password('A!Str0ngPwd')) | ||
|
||
with patch('ptmd.database.models.user.session') as mock_session: | ||
changed = user.change_password(old_password='test', new_password='test2') | ||
changed = user.change_password(old_password='A!Str0ngPwd', new_password='A!Str0ngPwd2') | ||
self.assertTrue(changed) | ||
changed = user.change_password(old_password='test', new_password='test2') | ||
changed = user.change_password(old_password='test', new_password='A!Str0ngPwd') | ||
self.assertFalse(changed) | ||
|
||
with patch('ptmd.database.models.user.send_validation_mail') as mock_email: | ||
|
@@ -94,3 +95,12 @@ def test_user_serialisation_with_organisation(self, mock_organisation, mock_orga | |
files = dict(user)['files'] | ||
self.assertIn(dict(file_1), files) | ||
self.assertIn(dict(file_2), files) | ||
|
||
@patch('ptmd.database.models.user.session') | ||
def test_set_password_policy_failure(self, mock_session): | ||
user = User(username='test', password='test', email='[email protected]', role='admin') | ||
with self.assertRaises(PasswordPolicyError) as context: | ||
user.set_password('test') | ||
self.assertEqual(str(context.exception), | ||
"Password must be between 8 and 20 characters long, contain at least one uppercase letter, one " | ||
"lowercase letter, one number and one special character.") |
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 @@ | ||
from unittest import TestCase | ||
|
||
from ptmd.exceptions import APIError | ||
|
||
|
||
class TestExceptions(TestCase): | ||
|
||
def test_api_error(self): | ||
with self.assertRaises(SyntaxError) as context: | ||
APIError() | ||
self.assertEqual(str(context.exception), 'Cannot instantiate abstract class APIError') |