Skip to content

Commit

Permalink
tests: user access token
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Apr 27, 2020
1 parent b013929 commit b4ed255
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion reana_db/models.py
Expand Up @@ -69,7 +69,7 @@ def access_token(self, value):
"""REANA access token setter."""
from .database import Session
if self.tokens.count() and self.active_token:
raise Exception(f'User {self} has already a valid access token.')
raise Exception(f'User {self} has already an active access token.')
else:
user_token = UserToken(user_=self, token=value, status='active',
type_='reana')
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Expand Up @@ -12,6 +12,7 @@
from uuid import uuid4

import pytest
from mock import patch
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy_utils import create_database, database_exists, drop_database
Expand Down Expand Up @@ -61,7 +62,9 @@ def test_create_workflow(session):
@pytest.fixture
def new_user(session):
"""Create new user."""
user = User(email=f'{uuid4()}@reana.io', access_token='secretkey')
with patch('reana_db.database.Session', return_value=session):
user = User(email=f'{uuid4()}@reana.io',
access_token=f'secretkey-{uuid4()}')
session.add(user)
session.commit()
return user
33 changes: 32 additions & 1 deletion tests/test_models.py
Expand Up @@ -12,6 +12,7 @@

import pytest
import sqlalchemy
from mock import patch

from reana_db.models import (ALLOWED_WORKFLOW_STATUS_TRANSITIONS, AuditLog,
AuditLogAction, User, Workflow, WorkflowStatus)
Expand Down Expand Up @@ -123,7 +124,7 @@ def test_workflow_can_transition_to(db, session, from_status, to_status,
]
)
def test_audit_action(session, new_user, action, can_do):
"""."""
"""Test audit log actions creation."""
details = {'reason': 'Use REANA.'}

def _audit_action():
Expand All @@ -144,3 +145,33 @@ def _audit_action():
else:
with pytest.raises(sqlalchemy.exc.IntegrityError):
_audit_action()


def test_access_token(session, new_user):
"""Test user access token use cases."""
assert new_user.access_token
assert new_user.access_token_status == 'active'
assert new_user.tokens.count() == 1
assert new_user.active_token.type_ == 'reana'

# Assign second valid access token
with pytest.raises(Exception) as e:
new_user.access_token = 'new_token'
assert 'has already an active access token' in e.value.args[0]

# Revoke token
new_user.active_token.status = 'revoked'
session.commit()
assert not new_user.access_token
assert not new_user.active_token
assert new_user.access_token_status == 'revoked'

# Grant new token
with patch('reana_db.database.Session', return_value=session):
new_user.access_token = 'new_token'
session.commit()
assert new_user.access_token
assert new_user.tokens.count() == 2

# Status of latest reana token
assert new_user.access_token_status == 'active'

0 comments on commit b4ed255

Please sign in to comment.