Skip to content

Commit

Permalink
Merge pull request #8 from oliv10/auth-token-tests
Browse files Browse the repository at this point in the history
🚀 Add dev dependencies and token tests
  • Loading branch information
oliv10 authored Jun 28, 2024
2 parents eda7a32 + 8dd351c commit e62f9ec
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
'redis==5.0.7',
'passlib==1.7.4'
],
extras_require={
"dev": [
"pytest==8.2.2",
"pipreqs==0.5.0",
"fakeredis==2.23.2",
"pytest-cov==5.0.0"
],
},
)
Empty file added tests/__init__.py
Empty file.
67 changes: 67 additions & 0 deletions tests/test_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest
from datetime import datetime, timedelta
from FastAuth.token import Token, SECRET_KEY, ALGORITHM
import jwt

# Sample User data
user_data = {
"id": 1,
"username": "test_user",
"email": "test_user@example.com"
}

# Mock User model
class MockUser:
def __init__(self, id, username, email):
self.id = id
self.username = username
self.email = email

def dict(self):
return {
"id": self.id,
"username": self.username,
"email": self.email
}

@pytest.fixture
def user():
return MockUser(**user_data)

def test_create_token(user):
token = Token.create(user)
decoded_token = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])

assert decoded_token["username"] == user.username
assert decoded_token["email"] == user.email
assert "exp" in decoded_token
assert "iat" in decoded_token
assert "nbf" in decoded_token

def test_create_token_with_expiry(user):
expires_delta = timedelta(minutes=10)
token = Token.create(user, expires_delta)
decoded_token = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])

exp = decoded_token["exp"]
expected_exp = datetime.now() + expires_delta
assert exp == int(expected_exp.timestamp())

def test_validate_token(user):
token = Token.create(user)
assert Token.validate(token) == True

Check failure on line 52 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.x)

test_validate_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 52 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.10)

test_validate_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 52 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.11)

test_validate_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 52 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.12)

test_validate_token AttributeError: type object 'Token' has no attribute 'validate'

def test_validate_invalid_token():
invalid_token = "invalid.token.string"
assert Token.validate(invalid_token) == False

Check failure on line 56 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.x)

test_validate_invalid_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 56 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.10)

test_validate_invalid_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 56 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.11)

test_validate_invalid_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 56 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.12)

test_validate_invalid_token AttributeError: type object 'Token' has no attribute 'validate'

def test_expired_token(user):
expires_delta = timedelta(seconds=1)
token = Token.create(user, expires_delta)
assert Token.validate(token) == True

Check failure on line 61 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.x)

test_expired_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 61 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.10)

test_expired_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 61 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.11)

test_expired_token AttributeError: type object 'Token' has no attribute 'validate'

Check failure on line 61 in tests/test_token.py

View workflow job for this annotation

GitHub Actions / pytest (3.12)

test_expired_token AttributeError: type object 'Token' has no attribute 'validate'

# Wait for the token to expire
import time
time.sleep(2)

assert Token.validate(token) == False

0 comments on commit e62f9ec

Please sign in to comment.