-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dependencies.py
45 lines (37 loc) · 1.41 KB
/
test_dependencies.py
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
import pytest
from fastapi import HTTPException
from sqlmodel import Session, SQLModel, create_engine
from sqlmodel.pool import StaticPool
from kharon.dependencies import get_cluster
from kharon.models import Cluster, User
from kharon.models.clusters import ClusterStatus
@pytest.fixture(name="session")
def session_fixture():
engine = create_engine(
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
def test_has_read_access(session):
one_user = User(email="[email protected]")
second_user = User(email="[email protected]")
session.add(one_user)
session.add(second_user)
session.commit()
one_cluster = Cluster(
creator=one_user.id,
name="App Cluster",
host="0.0.0.0",
user_read_allow=one_user.email,
status=ClusterStatus.healthy,
remote_host="localhost",
)
session.add(one_cluster)
session.commit()
session.refresh(one_cluster)
assert get_cluster(cluster_name="App Cluster", user=one_user, session=session) == one_cluster
with pytest.raises(HTTPException, match="Not Found"):
get_cluster(cluster_name="Not there", user=one_user, session=session)
with pytest.raises(HTTPException, match="Forbidden"):
get_cluster(cluster_name="App Cluster", user=second_user, session=session)