"""Test api token.""" import random import uuid from datetime import datetime import pytest from grass.logic import api_token def random_id(): """Create a random id. Returns: int: the random id. """ return int(1000 + 4000 * random.random()) @pytest.fixture def random_token(): """Create a random token. Returns: Response: the token information. """ user_id = random_id() client_id = str(random_id()) oauth_token = str(uuid.uuid4()) response = api_token._create_token( token=oauth_token, client_id=client_id, user_id=user_id, user_type='oa', expires=int(datetime.utcnow().timestamp()), ) return response def test_creation_of_a_random_token(random_token): """Test the creation of a random token. The random token is generated by using the fixture. If no exceptions are raised, it means that the system has worked as expected. """ pass def test_token_validation(random_token): """Test validate token.""" assert api_token.is_token_valid( random_token.oauth_token, random_token.client_id, random_token.user_id ) def test_non_existent_token_validation(random_token): """Test validating a missing token.""" # Fake client id. assert not api_token.is_token_valid( random_token.oauth_token, 20, random_token.user_id ) # Fake user id. assert not api_token.is_token_valid( random_token.oauth_token, random_token.client_id, 20 ) # Fake token id. assert not api_token.is_token_valid( 20, random_token.client_id, random_token.user_id ) def test_exception_raised_on_missing_value(): """Test an exception is raised when a param value is missing.""" # If any of the value is missing – an exception is expected. for a in [None, True]: for b in [None, True]: for c in [None, True]: if a and b and c: continue assert pytest.raises(Exception, api_token.is_token_valid, a, b, c)