-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_api.py
More file actions
111 lines (83 loc) · 3.96 KB
/
test_api.py
File metadata and controls
111 lines (83 loc) · 3.96 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
106
107
108
109
110
111
import os
import pytest
from pathlib import Path
from mock import mock, MagicMock, patch
from fastapi.testclient import TestClient
from app.db import DB
from app.api.utils import next_cron_occurrences
from app.api.config import Settings
from app.api.main import app
from app.api.authorize import get_settings
client = TestClient(app)
def get_settings_override() -> Settings:
return Settings(
HTTP_REST_API_USERNAME="new-username",
HTTP_REST_API_PASSWORD="new-password",
)
def get_settings_disable() -> Settings:
return Settings(
HTTP_REST_API_USERNAME="",
HTTP_REST_API_PASSWORD="",
)
def reset_settings_override() -> Settings:
return Settings(
HTTP_REST_API_USERNAME="admin",
HTTP_REST_API_PASSWORD="password",
)
class TestAPI:
@classmethod
def setup_class(cls):
"""Runs 1 time before all tests in this class"""
app.dependency_overrides[get_settings] = reset_settings_override
def test_root(self):
response = client.get("/")
assert response.status_code == 200
def test_login_on_default_settings(self):
response = client.get("/auth", auth=("admin", "password"))
assert response.status_code == 200
assert response.json() == {"username": "admin"}
response = client.get("/auth", auth=("admin", "BAD"))
assert response.status_code == 401
response = client.get("/auth")
assert response.status_code == 401
def test_login_disable(self, monkeypatch: pytest.MonkeyPatch):
# Apply the environment variable override
app.dependency_overrides[get_settings] = get_settings_disable
response = client.get("/auth", auth=("", ""))
assert response.status_code == 200
app.dependency_overrides[get_settings] = reset_settings_override
def test_login_on_with_env(self, monkeypatch: pytest.MonkeyPatch):
# Apply the environment variable override
app.dependency_overrides[get_settings] = get_settings_override
response = client.get("/auth", auth=("admin", "password"))
assert response.status_code == 401
response = client.get("/auth", auth=("new-username", "new-password"))
assert response.status_code == 200
assert response.json() == {"username": "new-username"}
# Reset the override
app.dependency_overrides[get_settings] = reset_settings_override
response = client.get("/auth", auth=("admin", "password"))
assert response.status_code == 200
assert response.json() == {"username": "admin"}
def test_dashboard(self):
db = DB()
response = client.get("/api/v1/nautical/dashboard", auth=("admin", "password"))
assert response.status_code == 200
next_crons = next_cron_occurrences(5)
assert response.json()["backup_running"] == db.get("backup_running", False)
assert response.json()["errors"] == db.get("errors", 0)
assert response.json()["skipped"] == db.get("containers_skipped", 0)
assert response.json()["completed"] == db.get("containers_completed", 0)
assert response.json()["number_of_containers"] == db.get("number_of_containers", 0)
assert response.json()["last_cron"] == db.get("last_cron", "None")
assert response.json()["last_backup_seconds_taken"] == db.get("last_backup_seconds_taken", 0)
assert response.json()["next_run"] == next_crons.get("1", [None, None])[1] if next_crons else None
assert len(response.json()["next_cron"]) == 7
assert set(response.json()["next_cron"]) == set(next_crons) if next_crons else None
@patch("subprocess.run")
def test_start_backup(self, patched_subprocess_run):
response = client.post("/api/v1/nautical/start_backup", auth=("admin", "password"))
assert response.status_code == 200
def test_next_cron(self):
response = client.get("/api/v1/nautical/next_cron/1", auth=("admin", "password"))
assert response.status_code == 200