Skip to content

Commit 112b734

Browse files
Change username default from empty string to None and add testing around infrahubctl repostory app.
1 parent 4df0d61 commit 112b734

File tree

2 files changed

+294
-6
lines changed

2 files changed

+294
-6
lines changed

infrahub_sdk/ctl/repository.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from pathlib import Path
2+
from typing import Optional
23

34
import typer
45
import yaml
56
from pydantic import ValidationError
67
from rich.console import Console
78

9+
from infrahub_sdk.ctl.client import initialize_client
10+
811
from ..async_typer import AsyncTyper
9-
from ..ctl.client import initialize_client
1012
from ..ctl.exceptions import FileNotValidError
1113
from ..ctl.utils import init_logging
1214
from ..graphql import Mutation
@@ -65,7 +67,7 @@ async def add(
6567
name: str,
6668
location: str,
6769
description: str = "",
68-
username: str = "",
70+
username: Optional[str] = None,
6971
password: str = "",
7072
commit: str = "",
7173
read_only: bool = False,
@@ -88,10 +90,9 @@ async def add(
8890

8991
client = initialize_client()
9092

91-
if username:
92-
credential = await client.create(kind="CorePasswordCredential", name=name, username=username, password=password)
93-
await credential.save()
94-
input_data["data"]["credential"] = {"id": credential.id}
93+
credential = await client.create(kind="CorePasswordCredential", name=name, username=username, password=password)
94+
await credential.save(allow_upsert=True)
95+
input_data["data"]["credential"] = {"id": credential.id}
9596

9697
query = Mutation(
9798
mutation="CoreReadOnlyRepositoryCreate" if read_only else "CoreRepositoryCreate",
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
"""Integration tests for infrahubctl commands."""
2+
3+
# import json
4+
import os
5+
6+
# import sys
7+
from pathlib import Path
8+
from unittest import mock
9+
10+
import pytest
11+
12+
# from pytest_httpx._httpx_mock import HTTPXMock
13+
from typer.testing import CliRunner
14+
15+
from infrahub_sdk.client import InfrahubClient
16+
from infrahub_sdk.ctl.cli_commands import app
17+
18+
# from tests.helpers.utils import change_directory, strip_color
19+
20+
runner = CliRunner()
21+
22+
23+
FIXTURE_BASE_DIR = Path(
24+
Path(os.path.abspath(__file__)).parent / ".." / ".." / "fixtures" / "integration" / "test_infrahubctl"
25+
)
26+
27+
28+
@pytest.fixture
29+
def mock_client() -> mock.Mock:
30+
"""Fixture for a mocked InfrahubClient."""
31+
client = mock.create_autospec(InfrahubClient)
32+
return client
33+
34+
35+
# ---------------------------------------------------------
36+
# infrahubctl repository command tests
37+
# ---------------------------------------------------------
38+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
39+
class TestInfrahubctlRepository:
40+
"""Groups the 'infrahubctl repository' test cases."""
41+
42+
def test_repo_no_username(self, mock_init_client, mock_client) -> None:
43+
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
44+
mock_cred = mock.AsyncMock()
45+
mock_cred.id = "1234"
46+
mock_client.create.return_value = mock_cred
47+
48+
mock_init_client.return_value = mock_client
49+
output = runner.invoke(
50+
app,
51+
[
52+
"repository",
53+
"add",
54+
"Gitlab",
55+
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
56+
"--password",
57+
"mySup3rSecureP@ssw0rd",
58+
],
59+
)
60+
assert output.exit_code == 0
61+
mock_client.create.assert_called_once()
62+
mock_client.create.assert_called_with(
63+
name="Gitlab",
64+
kind="CorePasswordCredential",
65+
password="mySup3rSecureP@ssw0rd",
66+
username=None,
67+
)
68+
mock_cred.save.assert_called_once()
69+
mock_cred.save.assert_called_with(allow_upsert=True)
70+
mock_client.execute_graphql.assert_called_once()
71+
mock_client.execute_graphql.assert_called_with(
72+
query="""
73+
mutation {
74+
CoreRepositoryCreate(
75+
data: {
76+
name: {
77+
value: "Gitlab"
78+
}
79+
location: {
80+
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
81+
}
82+
description: {
83+
value: ""
84+
}
85+
commit: {
86+
value: ""
87+
}
88+
credential: {
89+
id: "1234"
90+
}
91+
}
92+
){
93+
ok
94+
}
95+
}
96+
""",
97+
branch_name="main",
98+
tracker="mutation-repository-create",
99+
)
100+
101+
def test_repo_username(self, mock_init_client, mock_client) -> None:
102+
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
103+
mock_cred = mock.AsyncMock()
104+
mock_cred.id = "1234"
105+
mock_client.create.return_value = mock_cred
106+
107+
mock_init_client.return_value = mock_client
108+
output = runner.invoke(
109+
app,
110+
[
111+
"repository",
112+
"add",
113+
"Gitlab",
114+
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
115+
"--password",
116+
"mySup3rSecureP@ssw0rd",
117+
"--username",
118+
"opsmill",
119+
],
120+
)
121+
assert output.exit_code == 0
122+
mock_client.create.assert_called_once()
123+
mock_client.create.assert_called_with(
124+
name="Gitlab",
125+
kind="CorePasswordCredential",
126+
password="mySup3rSecureP@ssw0rd",
127+
username="opsmill",
128+
)
129+
mock_cred.save.assert_called_once()
130+
mock_cred.save.assert_called_with(allow_upsert=True)
131+
mock_client.execute_graphql.assert_called_once()
132+
mock_client.execute_graphql.assert_called_with(
133+
query="""
134+
mutation {
135+
CoreRepositoryCreate(
136+
data: {
137+
name: {
138+
value: "Gitlab"
139+
}
140+
location: {
141+
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
142+
}
143+
description: {
144+
value: ""
145+
}
146+
commit: {
147+
value: ""
148+
}
149+
credential: {
150+
id: "1234"
151+
}
152+
}
153+
){
154+
ok
155+
}
156+
}
157+
""",
158+
branch_name="main",
159+
tracker="mutation-repository-create",
160+
)
161+
162+
def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
163+
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
164+
mock_cred = mock.AsyncMock()
165+
mock_cred.id = "1234"
166+
mock_client.create.return_value = mock_cred
167+
168+
mock_init_client.return_value = mock_client
169+
output = runner.invoke(
170+
app,
171+
[
172+
"repository",
173+
"add",
174+
"Gitlab",
175+
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
176+
"--password",
177+
"mySup3rSecureP@ssw0rd",
178+
"--read-only",
179+
],
180+
)
181+
assert output.exit_code == 0
182+
mock_client.create.assert_called_once()
183+
mock_client.create.assert_called_with(
184+
name="Gitlab",
185+
kind="CorePasswordCredential",
186+
password="mySup3rSecureP@ssw0rd",
187+
username=None,
188+
)
189+
mock_cred.save.assert_called_once()
190+
mock_cred.save.assert_called_with(allow_upsert=True)
191+
mock_client.execute_graphql.assert_called_once()
192+
mock_client.execute_graphql.assert_called_with(
193+
query="""
194+
mutation {
195+
CoreReadOnlyRepositoryCreate(
196+
data: {
197+
name: {
198+
value: "Gitlab"
199+
}
200+
location: {
201+
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
202+
}
203+
description: {
204+
value: ""
205+
}
206+
commit: {
207+
value: ""
208+
}
209+
credential: {
210+
id: "1234"
211+
}
212+
}
213+
){
214+
ok
215+
}
216+
}
217+
""",
218+
branch_name="main",
219+
tracker="mutation-repository-create",
220+
)
221+
222+
def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> None:
223+
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
224+
mock_cred = mock.AsyncMock()
225+
mock_cred.id = "1234"
226+
mock_client.create.return_value = mock_cred
227+
228+
mock_init_client.return_value = mock_client
229+
output = runner.invoke(
230+
app,
231+
[
232+
"repository",
233+
"add",
234+
"Gitlab",
235+
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
236+
"--password",
237+
"mySup3rSecureP@ssw0rd",
238+
"--username",
239+
"opsmill",
240+
"--description",
241+
"This is a test description",
242+
"--commit",
243+
"myHashCommit",
244+
"--branch",
245+
"develop",
246+
],
247+
)
248+
assert output.exit_code == 0
249+
mock_client.create.assert_called_once()
250+
mock_client.create.assert_called_with(
251+
name="Gitlab",
252+
kind="CorePasswordCredential",
253+
password="mySup3rSecureP@ssw0rd",
254+
username="opsmill",
255+
)
256+
mock_cred.save.assert_called_once()
257+
mock_cred.save.assert_called_with(allow_upsert=True)
258+
mock_client.execute_graphql.assert_called_once()
259+
mock_client.execute_graphql.assert_called_with(
260+
query="""
261+
mutation {
262+
CoreRepositoryCreate(
263+
data: {
264+
name: {
265+
value: "Gitlab"
266+
}
267+
location: {
268+
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
269+
}
270+
description: {
271+
value: "This is a test description"
272+
}
273+
commit: {
274+
value: "myHashCommit"
275+
}
276+
credential: {
277+
id: "1234"
278+
}
279+
}
280+
){
281+
ok
282+
}
283+
}
284+
""",
285+
branch_name="develop",
286+
tracker="mutation-repository-create",
287+
)

0 commit comments

Comments
 (0)