-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UT: Update and add tests for new TokenParser class
- Loading branch information
1 parent
67549c7
commit f9ad76b
Showing
3 changed files
with
69 additions
and
48 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
51 changes: 51 additions & 0 deletions
51
...ts/unit_tests/monkey_island/cc/services/authentication_service/token/test_token_parser.py
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,51 @@ | ||
import pytest | ||
from tests.unit_tests.monkey_island.cc.services.authentication_service.token.conftest import ( | ||
build_app, | ||
) | ||
|
||
from monkey_island.cc.services.authentication_service.token import TokenGenerator, TokenParser | ||
from monkey_island.cc.services.authentication_service.token.token_parser import TokenValidationError | ||
|
||
|
||
def test_parse(): | ||
token_expiration_timedelta = 1 * 60 # 1 minute | ||
payload = "fake_user_id" | ||
|
||
app, _ = build_app() | ||
token_generator = TokenGenerator(app.security) | ||
token_parser = TokenParser(app.security, token_expiration_timedelta) | ||
|
||
token = token_generator.generate_token(payload) | ||
parsed_token = token_parser.parse(token) | ||
|
||
assert parsed_token.token == token | ||
assert parsed_token.expiration_time == token_expiration_timedelta | ||
assert parsed_token.payload == payload | ||
|
||
|
||
def test_parse__expired_token(freezer): | ||
token_expiration = 1 * 60 # 1 minute | ||
generation_time = "2020-01-01 00:00:00" | ||
validation_time = "2020-01-01 00:03:30" | ||
payload = "fake_user_id" | ||
|
||
app, _ = build_app() | ||
token_generator = TokenGenerator(app.security) | ||
freezer.move_to(generation_time) | ||
token = token_generator.generate_token(payload) | ||
token_parser = TokenParser(app.security, token_expiration) | ||
freezer.move_to(validation_time) | ||
|
||
with pytest.raises(TokenValidationError): | ||
token_parser.parse(token) | ||
|
||
|
||
def test_parse__invalid_token(): | ||
token_expiration = 1 * 60 # 1 minute | ||
invalid_token = "invalid_token" | ||
|
||
app, _ = build_app() | ||
token_parser = TokenParser(app.security, token_expiration) | ||
|
||
with pytest.raises(TokenValidationError): | ||
token_parser.parse(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