API - minor tests refacto

This commit is contained in:
Sam 2022-06-12 18:34:26 +02:00
parent ea4b6ed236
commit b678b6e76d

View File

@ -5,31 +5,15 @@ import pytest
from flask import Flask
from fittrackee.oauth2.models import OAuth2Client, OAuth2Token
from fittrackee.users.models import User
from ..mixins import RandomMixin
from ..mixins import OAuth2Mixin
class OAuth2ModelTestCase(RandomMixin):
def create_oauth2_client(self) -> OAuth2Client:
oauth_client = OAuth2Client(
id=self.random_int(),
client_id=self.random_string(),
client_id_issued_at=1653738796,
)
oauth_client.set_client_metadata(
{
'client_name': self.random_string(),
'client_description': self.random_string(),
'redirect_uris': [self.random_string()],
'client_uri': self.random_domain(),
}
)
return oauth_client
class TestOAuth2ClientSerialize(OAuth2ModelTestCase):
def test_it_returns_oauth_client(self, app: Flask) -> None:
oauth_client = self.create_oauth2_client()
class TestOAuth2ClientSerialize(OAuth2Mixin):
def test_it_returns_oauth_client(self, app: Flask, user_1: User) -> None:
oauth_client = self.create_oauth_client(user_1)
oauth_client.client_id_issued_at = 1653738796
serialized_oauth_client = oauth_client.serialize()
@ -76,14 +60,18 @@ class TestOAuth2ClientSerialize(OAuth2ModelTestCase):
)
class TestOAuth2Token(OAuth2ModelTestCase):
class TestOAuth2Token(OAuth2Mixin):
@pytest.mark.parametrize(
'input_expiration,expected_status', [(1000, True), (0, False)]
)
def test_it_returns_refresh_token_status(
self, app: Flask, input_expiration: int, expected_status: bool
self,
app: Flask,
user_1: User,
input_expiration: int,
expected_status: bool,
) -> None:
oauth_client = self.create_oauth2_client()
oauth_client = self.create_oauth_client(user_1)
token = OAuth2Token(
client_id=oauth_client.client_id,
access_token=self.random_string(),
@ -95,9 +83,9 @@ class TestOAuth2Token(OAuth2ModelTestCase):
assert token.is_refresh_token_active() is expected_status
def test_it_returns_refresh_token_active_when_below_twice_expiration(
self, app: Flask
self, app: Flask, user_1: User
) -> None:
oauth_client = self.create_oauth2_client()
oauth_client = self.create_oauth_client(user_1)
issued_at = int(time.time())
expires_in = self.random_int()
token = OAuth2Token(
@ -115,9 +103,9 @@ class TestOAuth2Token(OAuth2ModelTestCase):
assert token.is_refresh_token_active() is True
def test_it_returns_refresh_token_inactive_when_token_revoked(
self, app: Flask
self, app: Flask, user_1: User
) -> None:
oauth_client = self.create_oauth2_client()
oauth_client = self.create_oauth_client(user_1)
token = OAuth2Token(
client_id=oauth_client.client_id,
access_token=self.random_string(),