-
Notifications
You must be signed in to change notification settings - Fork 0
Add unit tests #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add unit tests #32
Changes from 63 commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
33c4865
Add password validation tests and update devcontainer to run tests on…
fdd50e6
Add test to ensure tampered password fails verification
635350c
Nest unit tests and rename `utility.py` -> `utils.py`
3c3e498
Add initial routes module for organizing route handlers
6054967
Add GitHub Actions workflow for testing across multiple Python versio…
903a4c1
Refactor GitHub Actions workflow to combine dependency installation a…
f570a88
Remove redundant test command from GitHub Actions workflow
f9c4652
Update GitHub Actions workflow to run tests on multiple OS environments
01dddbb
Refactor GitHub Actions workflow to consolidate OS matrix for testing
9e1e6d9
Refactor GitHub Actions workflow to specify branch for push events an…
5ff6565
Rename workflow from "Python package" to "Pytest CI" and remove branc…
e7e463d
Add Python 3.x to the testing matrix in GitHub Actions workflow
c14917b
Simplify Python version specification in GitHub Actions workflow to u…
7d99134
Update Python version specification to use a list format in GitHub Ac…
4eb0780
Add descriptive comment for OS and Python versions in GitHub Actions …
3eb3ac2
Revert "Add descriptive comment for OS and Python versions in GitHub …
4170882
Add branch filter for main in GitHub Actions workflow
eb2926f
Merge branch 'main' into feture/add_unit_tests
0c874ce
Refactor imports in person.py and define __all__ in utils.py for bett…
a1b62ca
Add branch filter for main in Pytest CI workflow
2106c65
Merge remote-tracking branch 'refs/remotes/origin/feture/add_unit_tes…
e9cab74
Add RunOnSave extension and configure pytest command for test files
8090e3f
Update RunOnSave configuration to improve test file matching and outp…
70076e5
Add file exclusion settings for __pycache__ and .pytest_cache in VSCode
314b5ba
Refactor utility imports and reorganize test structure
a2c6af2
Remove obsolete test for index route in test_home.py
971b164
Refactor index route test to use a pytest fixture for the test client
11a6f48
Add tests for email encryption and hashing utilities; refactor passwo…
a9140e3
Refactor decrypt_email tests to separate type and value assertions
d2c6b15
Remove unused import of register_routes in test_routes initialization
0b04e7c
Refactor index route test to separate status code and JSON response a…
f9bc814
Update image upload folder path and ensure directory creation for new…
7c7e9c7
Update picture route to use Config.IMAGES_FOLDER for file handling
938a1e3
Rename upload folder from 'images' to 'pictures' in .gitignore
5f95bcd
Add tests for user registration with missing fields
0d6f324
Update postStartCommand in devcontainer.json for verbose pytest output
5bbe881
Add email validation to registration and update utility functions
01d525e
Add tests for user registration and login with missing fields
d2cad3c
Fix email validation regex and add comprehensive tests for valid and …
45d5b93
Add additional tests for email validation to cover edge cases
f2e5989
Add docstring to extract_error_message for clarity on functionality
6c3db4f
Update email validation regex and add git fetch command in VSCode set…
d34cc5c
Add email masking functionality and corresponding tests
9f01c88
Refactor authentication tests to use fixtures for sample data and imp…
1aad2ea
Remove git fetch command from VSCode settings to streamline run-on-sa…
5e63177
Remove verbosity from postStartCommand in devcontainer configuration
3aa80a5
Refactor JWT helper functions and add comprehensive tests for refresh…
ff46e79
Update GitHub Actions workflow to define permissions for test job
80a7388
Run Prettier
cfe5f79
Add utility tests for email and password handling, including hashing …
3645c19
Remove unused imports in refresh token test file
0a75e22
Start adding tests for access token generation and create test fixtures
9b4f642
Add tests for access token decoding and expiration validation
b669e2f
Add tests for extracting and generating JWT refresh tokens
a241c2e
Add fixtures and tests for access and refresh token handling
da1f16a
Remove redundant tests for token extraction and validation
2700328
Remove unused access token generation imports from test files
969cb84
Refactor imports in test_verify_token.py to remove unused functions
88f6194
Update routes/picture.py
Vianpyro b6fbf7e
Fix test for missing username in registration by updating test data
76b3400
Merge branch 'feture/add_unit_tests' of https://github.com/TheSmartCo…
9eb9963
Enhance GitHub Actions workflow by adding descriptive job name for Py…
2eb9163
Fix test comment
f6a42e7
Update tests/test_jwt/test_refresh_access_token.py
Vianpyro 340ca5d
Improve JWT token tests
2c24a10
Update tests/test_routes/test_authentication/test_login.py
Vianpyro 8310ae1
Fix test for missing password by changing username to email in test_l…
e917b98
Refactor refresh token tests: consolidate and enhance test coverage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,47 @@ | ||
| name: Pytest CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Pytest on ${{ matrix.os }} with Python ${{ matrix.python-version }} | ||
| runs-on: ${{ matrix.os }} | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: read | ||
| statuses: write | ||
|
|
||
| strategy: | ||
| matrix: | ||
| os: [ubuntu-latest, macos-latest, windows-latest] | ||
| python-version: ["3.x"] | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| cache: "pip" | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| pip install -r requirements.txt | ||
| pip install pytest pytest-cov | ||
|
|
||
| - name: Run Pytest (Linux/macOS) | ||
| if: runner.os != 'Windows' | ||
| run: python -m pytest --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html | ||
|
|
||
| - name: Run Pytest (Windows) | ||
| if: runner.os == 'Windows' | ||
| run: python -m pytest --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html |
This file contains hidden or 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,7 +2,7 @@ | |
| *.pem | ||
|
|
||
| # Upload folder | ||
| images/ | ||
| pictures/ | ||
|
|
||
| # Environment variables file | ||
| .env | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,4 +7,5 @@ flask-cors>=4.0.1 | |
| PyMySQL>=1.1.1 | ||
| requests>=2.32.3 | ||
| waitress>=3.0.0 | ||
| pytest>=8.3.5 | ||
| python-dotenv>=1.0.1 | ||
This file contains hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| import pytest | ||
| from flask import Flask | ||
|
|
||
| from jwt_helper import generate_access_token | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(app: Flask): | ||
| """Create a test client for the Flask application""" | ||
| with app.test_client() as client: | ||
| yield client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_person_id() -> int: | ||
| """Provide a sample person ID for testing""" | ||
| return 12345 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_token(): | ||
| """Provide a sample token for testing""" | ||
| return "mock_token_123" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_access_token(sample_person_id): | ||
| """Provide a sample access token for testing""" | ||
| return generate_access_token(sample_person_id) |
This file contains hidden or 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,42 @@ | ||
| import pytest | ||
| from flask import Flask | ||
|
|
||
| from jwt_helper import TokenError, extract_token_from_header | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| def test_extract_token_valid(sample_token): | ||
| """Test extracting a valid Bearer token.""" | ||
| with app.test_request_context(headers={"Authorization": f"Bearer {sample_token}"}): | ||
| assert extract_token_from_header() == sample_token | ||
|
|
||
|
|
||
| def test_extract_token_missing(): | ||
| """Test missing Authorization header.""" | ||
| with app.test_request_context(headers={}): | ||
| with pytest.raises( | ||
| TokenError, match="Token is missing or improperly formatted" | ||
| ) as excinfo: | ||
| extract_token_from_header() | ||
| assert excinfo.value.status_code == 401 | ||
|
|
||
|
|
||
| def test_extract_token_invalid_format(): | ||
| """Test an improperly formatted Authorization header.""" | ||
| with app.test_request_context(headers={"Authorization": "InvalidTokenFormat"}): | ||
| with pytest.raises( | ||
| TokenError, match="Token is missing or improperly formatted" | ||
| ) as excinfo: | ||
| extract_token_from_header() | ||
| assert excinfo.value.status_code == 401 | ||
|
|
||
|
|
||
| def test_extract_token_no_bearer(sample_token): | ||
| """Test Authorization header without 'Bearer ' prefix.""" | ||
| with app.test_request_context(headers={"Authorization": f"Basic {sample_token}"}): | ||
| with pytest.raises( | ||
| TokenError, match="Token is missing or improperly formatted" | ||
| ) as excinfo: | ||
| extract_token_from_header() | ||
| assert excinfo.value.status_code == 401 |
This file contains hidden or 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,40 @@ | ||
| import jwt | ||
|
|
||
| from jwt_helper import JWT_ACCESS_TOKEN_EXPIRY, JWT_SECRET_KEY | ||
|
|
||
|
|
||
| def test_access_token_type(sample_access_token): | ||
| """Ensure generate_access_token returns a string""" | ||
| assert isinstance(sample_access_token, str) | ||
|
|
||
|
|
||
| def test_decoded_access_token(sample_person_id, sample_access_token): | ||
| """ | ||
| Ensure the generated access token can be decoded and contains the correct payload | ||
| - Check if the payload contains the correct person ID | ||
| - Check if the token has an expiration time | ||
| - Check if the token type is 'access' | ||
| """ | ||
| decoded_payload = jwt.decode( | ||
| sample_access_token, JWT_SECRET_KEY, algorithms=["HS256"] | ||
| ) | ||
|
|
||
| assert decoded_payload["person_id"] == sample_person_id | ||
| assert "exp" in decoded_payload | ||
| assert decoded_payload["token_type"] == "access" | ||
|
|
||
|
|
||
| def test_access_token_expiration(sample_access_token): | ||
| """ | ||
| Ensure the generated access token has a valid expiration time | ||
| - Check if the expiration time is greater than 0 | ||
| - Check if the expiration time is greater than the issued at time | ||
| - Check if the token is not expired | ||
| """ | ||
| decoded_payload = jwt.decode( | ||
| sample_access_token, JWT_SECRET_KEY, algorithms=["HS256"] | ||
| ) | ||
|
|
||
| assert decoded_payload["exp"] > 0 | ||
| assert decoded_payload["exp"] > decoded_payload["iat"] | ||
| assert decoded_payload["exp"] > JWT_ACCESS_TOKEN_EXPIRY.total_seconds() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.