Skip to content

Commit a744b0f

Browse files
authored
Merge pull request #8493 from opsmill/pog-api-component-test-annotations
Fix type annotations
2 parents 42c651f + f8064ec commit a744b0f

File tree

12 files changed

+245
-132
lines changed

12 files changed

+245
-132
lines changed

backend/tests/component/api/conftest.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
from fastapi.testclient import TestClient
77

88
from infrahub import config
9+
from infrahub.core.branch import Branch
910
from infrahub.core.constants import InfrahubKind
1011
from infrahub.core.initialization import create_branch
1112
from infrahub.core.manager import NodeManager
1213
from infrahub.core.node import Node
14+
from infrahub.core.schema.schema_branch import SchemaBranch
1315
from infrahub.core.timestamp import Timestamp
1416
from infrahub.database import InfrahubDatabase
1517
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
1618
from infrahub.workers.dependencies import build_database, build_message_bus, build_workflow
1719
from infrahub.workflows.initialization import setup_task_manager
20+
from tests.conftest import TestHelper
1821

1922

2023
@pytest.fixture
21-
def client(dependency_provider: Provider, nats, redis) -> Generator[TestClient, None, None]:
24+
def client(
25+
dependency_provider: Provider, nats: dict[int, int] | None, redis: dict[int, int] | None
26+
) -> Generator[TestClient, None, None]:
2227
# In order to mock some methods later we can't load app by default because it will automatically load all import in main.py as well
2328
from infrahub.server import app
2429

@@ -40,7 +45,7 @@ def admin_headers() -> dict[str, str]:
4045

4146

4247
@pytest.fixture
43-
def rpc_bus(helper, dependency_provider) -> Generator[Any, None, None]:
48+
def rpc_bus(helper: TestHelper, dependency_provider: Provider) -> Generator[Any, None, None]:
4449
original = config.OVERRIDE.message_bus
4550
bus = helper.get_message_bus_rpc()
4651
config.OVERRIDE.message_bus = bus
@@ -50,7 +55,7 @@ def rpc_bus(helper, dependency_provider) -> Generator[Any, None, None]:
5055

5156

5257
@pytest.fixture
53-
async def workflow_local(dependency_provider) -> AsyncGenerator[WorkflowLocalExecution, None]:
58+
async def workflow_local(dependency_provider: Provider) -> AsyncGenerator[WorkflowLocalExecution, None]:
5459
original = config.OVERRIDE.workflow
5560
workflow = WorkflowLocalExecution()
5661
await setup_task_manager()
@@ -62,7 +67,10 @@ async def workflow_local(dependency_provider) -> AsyncGenerator[WorkflowLocalExe
6267

6368
@pytest.fixture
6469
async def car_person_data(
65-
db: InfrahubDatabase, register_core_models_schema, car_person_schema, first_account
70+
db: InfrahubDatabase,
71+
register_core_models_schema: SchemaBranch,
72+
car_person_schema: SchemaBranch,
73+
first_account: Node,
6674
) -> dict[str, Node]:
6775
p1 = await Node.init(db=db, schema="TestPerson")
6876
await p1.new(db=db, name="John", height=180)
@@ -148,7 +156,7 @@ async def car_person_data(
148156

149157
@pytest.fixture
150158
async def car_person_data_generic_diff(
151-
db: InfrahubDatabase, default_branch, car_person_data_generic, first_account
159+
db: InfrahubDatabase, default_branch: Branch, car_person_data_generic: dict[str, Node], first_account: Node
152160
) -> dict[str, Any]:
153161
branch2 = await create_branch(branch_name="branch2", db=db)
154162

@@ -239,7 +247,7 @@ async def car_person_data_generic_diff(
239247

240248
@pytest.fixture
241249
async def car_person_data_artifact_diff(
242-
db: InfrahubDatabase, default_branch, car_person_data_generic_diff
250+
db: InfrahubDatabase, default_branch: Branch, car_person_data_generic_diff: dict[str, Any]
243251
) -> dict[str, Any]:
244252
query = """
245253
query {
@@ -368,7 +376,7 @@ async def car_person_data_artifact_diff(
368376

369377
@pytest.fixture
370378
async def data_diff_attribute(
371-
db: InfrahubDatabase, default_branch, car_person_data_generic, first_account
379+
db: InfrahubDatabase, default_branch: Branch, car_person_data_generic: dict[str, Node], first_account: Node
372380
) -> dict[str, Any]:
373381
branch2 = await create_branch(branch_name="branch2", db=db)
374382

backend/tests/component/api/test_00_auth.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from infrahub import config
55
from infrahub.core.branch import Branch
6+
from infrahub.core.node import Node
67
from infrahub.database import InfrahubDatabase
78

89
EXPIRED_ACCESS_TOKEN = (
@@ -22,7 +23,9 @@
2223
)
2324

2425

25-
async def test_password_based_login(db: InfrahubDatabase, default_branch, client, first_account) -> None:
26+
async def test_password_based_login(
27+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
28+
) -> None:
2629
with client:
2730
response = client.post("/api/auth/login", json={"username": "First Account", "password": "FirstPassword123"})
2831

@@ -37,7 +40,9 @@ async def test_password_based_login(db: InfrahubDatabase, default_branch, client
3740
assert first_account.id == decoded["sub"]
3841

3942

40-
async def test_refresh_with_invalidated_token(db: InfrahubDatabase, default_branch, client, first_account) -> None:
43+
async def test_refresh_with_invalidated_token(
44+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
45+
) -> None:
4146
with client:
4247
response = client.post("/api/auth/login", json={"username": "First Account", "password": "FirstPassword123"})
4348

@@ -64,7 +69,9 @@ async def test_refresh_with_invalidated_token(db: InfrahubDatabase, default_bran
6469
}
6570

6671

67-
async def test_refresh_access_token(db: InfrahubDatabase, default_branch, client, first_account) -> None:
72+
async def test_refresh_access_token(
73+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
74+
) -> None:
6875
"""Validate that it's possible to refresh an access token using a refresh token"""
6976
with client:
7077
login_response = client.post(
@@ -88,7 +95,9 @@ async def test_refresh_access_token(db: InfrahubDatabase, default_branch, client
8895
assert decoded_access["session_id"] == decoded_refresh["session_id"]
8996

9097

91-
async def test_refresh_access_token_with_cookies(db: InfrahubDatabase, default_branch, client, first_account) -> None:
98+
async def test_refresh_access_token_with_cookies(
99+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
100+
) -> None:
92101
"""Validate that it's possible to refresh an access token using a refresh token stored in cookies"""
93102
with client:
94103
login_response = client.post(
@@ -119,7 +128,7 @@ async def test_refresh_access_token_with_cookies(db: InfrahubDatabase, default_b
119128

120129

121130
async def test_fail_to_refresh_access_token_with_access_token(
122-
db: InfrahubDatabase, default_branch, client, first_account
131+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
123132
) -> None:
124133
"""Validate that it's not possible to refresh an access token using an access token"""
125134
with client:
@@ -143,7 +152,9 @@ async def test_fail_to_refresh_access_token_with_access_token(
143152
}
144153

145154

146-
async def test_password_based_login_unknown_user(db: InfrahubDatabase, default_branch, client, first_account) -> None:
155+
async def test_password_based_login_unknown_user(
156+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
157+
) -> None:
147158
with client:
148159
response = client.post("/api/auth/login", json={"username": "i-do-not-exist", "password": "something"})
149160

@@ -155,7 +166,7 @@ async def test_password_based_login_unknown_user(db: InfrahubDatabase, default_b
155166

156167

157168
async def test_password_based_login_invalid_password(
158-
db: InfrahubDatabase, default_branch, client, first_account
169+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
159170
) -> None:
160171
with client:
161172
response = client.post("/api/auth/login", json={"username": "First Account", "password": "incorrect"})
@@ -190,7 +201,9 @@ async def test_refresh_access_token_with_expired_refresh_token(
190201
assert response.json() == {"data": None, "errors": [{"message": "Expired Signature", "extensions": {"code": 401}}]}
191202

192203

193-
async def test_access_resource_using_refresh_token(db: InfrahubDatabase, default_branch, client, first_account) -> None:
204+
async def test_access_resource_using_refresh_token(
205+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
206+
) -> None:
194207
"""It should not be possible to access a resource using a refresh token"""
195208
with client:
196209
login_response = client.post(
@@ -210,7 +223,9 @@ async def test_access_resource_using_refresh_token(db: InfrahubDatabase, default
210223
}
211224

212225

213-
async def test_generate_api_token(db: InfrahubDatabase, default_branch, client, create_test_admin) -> None:
226+
async def test_generate_api_token(
227+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, create_test_admin: Node
228+
) -> None:
214229
"""It should not be possible to generate an API token using a JWT token"""
215230
with client:
216231
login_response = client.post(

backend/tests/component/api/test_01_auth_cookies.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import jwt
2+
from fastapi.testclient import TestClient
23

34
from infrahub import config
5+
from infrahub.core.branch import Branch
6+
from infrahub.core.node import Node
47
from infrahub.database import InfrahubDatabase
58

69

7-
async def test_password_based_login(db: InfrahubDatabase, default_branch, client, first_account) -> None:
10+
async def test_password_based_login(
11+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
12+
) -> None:
813
with client:
914
response = client.post("/api/auth/login", json={"username": "First Account", "password": "FirstPassword123"})
1015

@@ -19,7 +24,9 @@ async def test_password_based_login(db: InfrahubDatabase, default_branch, client
1924
assert first_account.id == decoded["sub"]
2025

2126

22-
async def test_refresh_access_token(db: InfrahubDatabase, default_branch, client, first_account) -> None:
27+
async def test_refresh_access_token(
28+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
29+
) -> None:
2330
"""Validate that it's possible to refresh an access token using a refresh token"""
2431
with client:
2532
login_response = client.post(
@@ -43,7 +50,9 @@ async def test_refresh_access_token(db: InfrahubDatabase, default_branch, client
4350
assert decoded_access["session_id"] == decoded_refresh["session_id"]
4451

4552

46-
async def test_access_resource_using_refresh_token(db: InfrahubDatabase, default_branch, client, first_account) -> None:
53+
async def test_access_resource_using_refresh_token(
54+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, first_account: Node
55+
) -> None:
4756
"""It should not be possible to access a resource using a refresh token"""
4857
with client:
4958
login_response = client.post(
@@ -64,7 +73,9 @@ async def test_access_resource_using_refresh_token(db: InfrahubDatabase, default
6473
}
6574

6675

67-
async def test_generate_api_token(db: InfrahubDatabase, default_branch, client, create_test_admin) -> None:
76+
async def test_generate_api_token(
77+
db: InfrahubDatabase, default_branch: Branch, client: TestClient, create_test_admin: Node
78+
) -> None:
6879
"""It should not be possible to generate an API token using a JWT token"""
6980
with client:
7081
login_response = client.post(

backend/tests/component/api/test_03_menu.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
from fastapi.testclient import TestClient
2+
13
from infrahub.core.branch import Branch
24
from infrahub.core.initialization import create_default_menu
5+
from infrahub.core.node import Node
36
from infrahub.core.schema import SchemaRoot
47
from infrahub.database import InfrahubDatabase
58

69

710
async def test_get_menu_not_admin(
811
db: InfrahubDatabase,
9-
client,
10-
client_headers,
12+
client: TestClient,
13+
client_headers: dict[str, str],
1114
default_branch: Branch,
1215
car_person_schema_generics: SchemaRoot,
13-
car_person_data_generic,
16+
car_person_data_generic: dict[str, Node],
1417
) -> None:
1518
await create_default_menu(db=db)
1619

@@ -26,12 +29,12 @@ async def test_get_menu_not_admin(
2629

2730
async def test_get_menu_admin(
2831
db: InfrahubDatabase,
29-
client,
30-
admin_headers,
31-
authentication_base,
32+
client: TestClient,
33+
admin_headers: dict[str, str],
34+
authentication_base: Node,
3235
default_branch: Branch,
3336
car_person_schema_generics: SchemaRoot,
34-
car_person_data_generic,
37+
car_person_data_generic: dict[str, Node],
3538
) -> None:
3639
await create_default_menu(db=db)
3740

0 commit comments

Comments
 (0)