Skip to content

Commit

Permalink
test(pytest): rename default_user to user0 (reanahub#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanRosendal committed Mar 20, 2024
1 parent bbe90f3 commit fba3ab4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 107 deletions.
75 changes: 37 additions & 38 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,34 @@
WorkspaceRetentionRuleStatus,
generate_uuid,
)

from reana_server.api_client import WorkflowSubmissionPublisher
from reana_server.reana_admin import reana_admin
from reana_server.reana_admin.check_workflows import check_workspaces
from reana_server.reana_admin.cli import RetentionRuleDeleter
from reana_server.reana_admin.consumer import MessageConsumer


def test_export_users(default_user):
def test_export_users(user0):
"""Test exporting all users as csv."""
runner = CliRunner()
expected_csv_file = io.StringIO()
csv_writer = csv.writer(expected_csv_file, dialect="unix")
csv_writer.writerow(
[
default_user.id_,
default_user.email,
default_user.access_token,
default_user.username,
default_user.full_name,
user0.id_,
user0.email,
user0.access_token,
user0.username,
user0.full_name,
]
)
result = runner.invoke(
reana_admin, ["user-export", "--admin-access-token", default_user.access_token]
reana_admin, ["user-export", "--admin-access-token", user0.access_token]
)
assert result.output == expected_csv_file.getvalue()


def test_import_users(app, session, default_user):
def test_import_users(app, session, user0):
"""Test importing users from CSV file."""
runner = CliRunner()
expected_output = "Users successfully imported."
Expand All @@ -81,7 +80,7 @@ def test_import_users(app, session, default_user):
[
"user-import",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--file",
users_csv_file_name,
],
Expand All @@ -95,7 +94,7 @@ def test_import_users(app, session, default_user):
assert user.full_name == user_full_name


def test_grant_token(default_user, session):
def test_grant_token(user0, session):
"""Test grant access token."""
runner = CliRunner()

Expand All @@ -105,7 +104,7 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
"[email protected]",
],
Expand All @@ -118,7 +117,7 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--id",
"fake_id",
],
Expand All @@ -134,7 +133,7 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
user.email,
],
Expand All @@ -147,7 +146,7 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
user.email,
],
Expand All @@ -161,15 +160,15 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
user.email,
],
input="y\n",
)
assert f"Token for user {user.id_} ({user.email}) granted" in result.output
assert user.access_token
assert default_user.audit_logs[-1].action is AuditLogAction.grant_token
assert user0.audit_logs[-1].action is AuditLogAction.grant_token

# user with active token
active_user = User(email="[email protected]", access_token="valid_token")
Expand All @@ -180,7 +179,7 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--id",
str(active_user.id_),
],
Expand All @@ -199,17 +198,17 @@ def test_grant_token(default_user, session):
[
"token-grant",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--id",
str(ui_user.id_),
],
)
assert ui_user.access_token_status is UserTokenStatus.active.name
assert ui_user.access_token
assert default_user.audit_logs[-1].action is AuditLogAction.grant_token
assert user0.audit_logs[-1].action is AuditLogAction.grant_token


def test_revoke_token(default_user, session):
def test_revoke_token(user0, session):
"""Test revoke access token."""
runner = CliRunner()

Expand All @@ -222,7 +221,7 @@ def test_revoke_token(default_user, session):
[
"token-revoke",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
user.email,
],
Expand All @@ -237,7 +236,7 @@ def test_revoke_token(default_user, session):
[
"token-revoke",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"-e",
user.email,
],
Expand All @@ -253,22 +252,22 @@ def test_revoke_token(default_user, session):
[
"token-revoke",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--id",
str(user.id_),
],
)
assert "was successfully revoked" in result.output
assert user.access_token_status == UserTokenStatus.revoked.name
assert default_user.audit_logs[-1].action is AuditLogAction.revoke_token
assert user0.audit_logs[-1].action is AuditLogAction.revoke_token

# try to revoke again
result = runner.invoke(
reana_admin,
[
"token-revoke",
"--admin-access-token",
default_user.access_token,
user0.access_token,
"--id",
str(user.id_),
],
Expand Down Expand Up @@ -398,7 +397,7 @@ def test_is_input_or_output(file_or_dir, expected_result):
],
)
def test_retention_rules_apply(
default_user,
user0,
workflow_with_retention_rules,
session,
time_delta,
Expand Down Expand Up @@ -437,7 +436,7 @@ def init_workspace(workspace, files):
command = [
"retention-rules-apply",
"--admin-access-token",
default_user.access_token,
user0.access_token,
]
if time_delta is not None:
forced_date = datetime.datetime.now() + time_delta
Expand Down Expand Up @@ -472,7 +471,7 @@ def init_workspace(workspace, files):

@patch("reana_server.reana_admin.cli.RetentionRuleDeleter.apply_rule")
def test_retention_rules_apply_error(
apply_rule_mock: Mock, workflow_with_retention_rules, default_user
apply_rule_mock: Mock, workflow_with_retention_rules, user0
):
"""Test that rules are reset to `active` if there are errors."""
workflow = workflow_with_retention_rules
Expand All @@ -484,7 +483,7 @@ def test_retention_rules_apply_error(
[
"retention-rules-apply",
"--admin-access-token",
default_user.access_token,
user0.access_token,
],
)

Expand All @@ -495,7 +494,7 @@ def test_retention_rules_apply_error(
assert rule.status == WorkspaceRetentionRuleStatus.active


def test_retention_rules_extend(workflow_with_retention_rules, default_user):
def test_retention_rules_extend(workflow_with_retention_rules, user0):
"""Test extending of retention rules."""
workflow = workflow_with_retention_rules
runner = CliRunner()
Expand All @@ -509,7 +508,7 @@ def test_retention_rules_extend(workflow_with_retention_rules, default_user):
"-d",
extend_days,
"--admin-access-token",
default_user.access_token,
user0.access_token,
],
)
assert result.output == "Invalid workflow UUID.\n"
Expand All @@ -524,7 +523,7 @@ def test_retention_rules_extend(workflow_with_retention_rules, default_user):
"-d",
extend_days,
"--admin-access-token",
default_user.access_token,
user0.access_token,
],
)
assert "Extending rule" in result.output
Expand Down Expand Up @@ -559,7 +558,7 @@ def test_retention_rule_deleter_file_outside_workspace(tmp_path):
)
@patch("reana_server.reana_admin.cli.requests.get")
def test_interactive_session_cleanup(
mock_requests, sample_serial_workflow_in_db, days, output, default_user
mock_requests, sample_serial_workflow_in_db, days, output, user0
):
"""Test closure of long running interactive sessions."""
runner = CliRunner()
Expand Down Expand Up @@ -602,7 +601,7 @@ def test_interactive_session_cleanup(
"-d",
days,
"--admin-access-token",
default_user.access_token,
user0.access_token,
],
)
assert output in result.output
Expand Down Expand Up @@ -972,7 +971,7 @@ def test_check_workspaces(
assert any(workflow.workspace_path in str(error) for error in result.errors)


def test_quota_set_default_limits_for_user_with_custom_limits(default_user, session):
def test_quota_set_default_limits_for_user_with_custom_limits(user0, session):
"""Test setting default quota when there are is one user with custom quota limits."""
runner = CliRunner()

Expand All @@ -981,7 +980,7 @@ def test_quota_set_default_limits_for_user_with_custom_limits(default_user, sess
for resource in resources:
user_resource = (
session.query(UserResource)
.filter_by(user_id=default_user.id_, resource_id=resource.id_)
.filter_by(user_id=user0.id_, resource_id=resource.id_)
.first()
)

Expand All @@ -995,7 +994,7 @@ def test_quota_set_default_limits_for_user_with_custom_limits(default_user, sess
[
"quota-set-default-limits",
"--admin-access-token",
default_user.access_token,
user0.access_token,
],
)

Expand Down
9 changes: 4 additions & 5 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA-Server decorators tests."""

from flask import jsonify
import json
from unittest.mock import Mock, patch

from flask import jsonify
from reana_db.models import User, UserToken

from reana_server.decorators import signin_required


def test_signing_required_with_token(default_user: User):
def test_signing_required_with_token(user0: User):
"""Test `signin_required` when user does not have a valid token."""
# Delete user tokens
UserToken.query.filter(UserToken.user_id == default_user.id_).delete()
UserToken.query.filter(UserToken.user_id == user0.id_).delete()

mock_endpoint = Mock(return_value=(jsonify(message="Ok"), 200))
mock_current_user = Mock()
mock_current_user.is_authenticated = True
mock_current_user.email = default_user.email
mock_current_user.email = user0.email
with patch("reana_server.decorators.current_user", mock_current_user):
decorated_endpoint = signin_required()(mock_endpoint)
response, code = decorated_endpoint()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_deleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA-Server deleter tests."""
import pytest
import pathlib
import uuid

import pytest
from reana_commons.workspace import iterdir, walk
from reana_server.deleter import Deleter, InOrOut
from reana_commons.workspace import iterdir, is_directory, walk


@pytest.mark.parametrize(
Expand Down Expand Up @@ -63,7 +63,7 @@
],
)
def test_file_deletion(
initial_list, which_to_keep, final_list, default_user, sample_serial_workflow_in_db
initial_list, which_to_keep, final_list, user0, sample_serial_workflow_in_db
):
"""Test delete files preserving inputs/outputs/none"""

Expand Down
Loading

0 comments on commit fba3ab4

Please sign in to comment.