-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_queue_status.py
More file actions
105 lines (86 loc) · 3.49 KB
/
Copy pathtest_queue_status.py
File metadata and controls
105 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
E2E tests validating that task submission is blocked when the queue is closed,
and that admin users can still submit when the queue is closed.
"""
from datetime import datetime, timedelta, timezone
import pytest
from sqlalchemy import select
from typer.testing import CliRunner
from divbase_api.models.queue_status import QueueStatusDB
from divbase_cli.cli_exceptions import DivBaseAPIError
from divbase_cli.divbase_cli import app
runner = CliRunner()
PROJECT_NAME = "project1"
TSV_FILTER = "Area:West of Ireland"
BCFTOOLS_CMD = "view -r 21:15000000-25000000"
COMMANDS = [
f"query vcf --tsv-filter '{TSV_FILTER}' --command '{BCFTOOLS_CMD}' --project {PROJECT_NAME}",
f"query tsv '{TSV_FILTER}' --project {PROJECT_NAME}",
f"dimensions update --project {PROJECT_NAME}",
]
@pytest.fixture
def queue_closed(db_session_sync):
"""
Set the queue to closed before the test, restore it to open after.
"""
result = db_session_sync.execute(select(QueueStatusDB).filter_by(id=1))
queue_status = result.scalar_one()
queue_status.is_closed = True
queue_status.scheduled_start = None
db_session_sync.commit()
yield
queue_status.is_closed = False
queue_status.scheduled_start = None
db_session_sync.commit()
@pytest.fixture
def queue_not_yet_closed(db_session_sync):
"""
Set the queue to be closed in the future, jobs should still be able to be submitted.
"""
result = db_session_sync.execute(select(QueueStatusDB).filter_by(id=1))
queue_status = result.scalar_one()
queue_status.is_closed = True
queue_status.scheduled_start = datetime.now(tz=timezone.utc) + timedelta(days=1)
db_session_sync.commit()
yield
queue_status.is_closed = False
queue_status.scheduled_start = None
db_session_sync.commit()
def test_regular_users_cannot_submit_tasks_when_queue_closed(
CONSTANTS,
logged_in_edit_user_with_existing_config,
queue_closed,
):
"""Test that when the queue is closed, regular users cannot submit tasks"""
for command in COMMANDS:
result = runner.invoke(app, command)
assert result.exit_code != 0
assert isinstance(result.exception, DivBaseAPIError)
assert "400" in str(result.exception)
assert "QueueClosedError" in str(result.exception)
def test_admin_users_can_submit_tasks_when_queue_closed(
CONSTANTS,
logged_in_admin_with_existing_config,
queue_closed,
):
"""Test that when the queue is closed, admin users can still submit tasks."""
for command in COMMANDS:
result = runner.invoke(app, command)
# It's okay for the job to actually fail,
# We just want the error to not be from the queue being closed
# This check happens early enough, that a badly formulated query does not matter
if result.exit_code != 0:
assert "QueueClosedError" not in str(result.exception)
def test_regular_users_can_submit_tasks_when_queue_not_yet_closed(
CONSTANTS,
logged_in_edit_user_with_existing_config,
queue_not_yet_closed,
):
"""Test that when the queue is not yet closed (just due to be closed), regular users can still submit tasks."""
for command in COMMANDS:
result = runner.invoke(app, command)
# It's okay for the job to actually fail,
# We just want the error to not be from the queue being closed
# This check happens early enough, that a badly formulated query does not matter
if result.exit_code != 0:
assert "QueueClosedError" not in str(result.exception)