1 import httpx

2 import pytest

3 from typer.testing import CliRunner

4

5 from meshadmin.cli.main import app

6

7 runner = CliRunner()

8

9 MOCK_NETWORK_LIST = [

10 {"id": 1, "name": "test-network", "cidr": "10.0.0.0/24"},

11 {"id": 2, "name": "prod-network", "cidr": "10.1.0.0/24"},

12]

13

14 MOCK_NETWORK_CREATE = {"id": 1, "name": "new-network", "cidr": "10.2.0.0/24"}

15

16

17@pytest.fixture

18 def mock_access_token(mocker):

19 return mocker.patch(

20 "meshadmin.cli.commands.network.get_access_token", return_value="fake-token"

21 )

22

23

24@pytest.fixture

25 def mock_context_config(mocker):

26 return mocker.patch(

27 "meshadmin.cli.commands.network.get_context_config",

28 return_value={"endpoint": "http://testserver"},

29 )

30

31

32 def test_list_networks_success(mocker, mock_access_token, mock_context_config):

33 mock_response = httpx.Response(

34 status_code=200,

35 json=MOCK_NETWORK_LIST,

36 request=httpx.Request("GET", "http://testserver/api/v1/networks"),

37 )

38 mock_get = mocker.patch("httpx.get", return_value=mock_response)

39 result = runner.invoke(app, ["network", "list"])

40 assert result.exit_code == 0

41 mock_get.assert_called_once_with(

42 "http://testserver/api/v1/networks",

43 headers={"Authorization": "Bearer fake-token"},

44 )

45 assert "test-network" in result.stdout

46 assert "prod-network" in result.stdout

47

48

49 def test_list_networks_auth_failure(mock_access_token, mock_context_config):

50 mock_access_token.side_effect = Exception("Auth failed")

51 result = runner.invoke(app, ["network", "list"])

52 assert result.exit_code == 1

53 assert "failed to get access token" in result.stdout

54

55

56 def test_create_network_success(mocker, mock_access_token, mock_context_config):

57 mock_response = httpx.Response(

58 status_code=200,

59 json=MOCK_NETWORK_CREATE,

60 request=httpx.Request("POST", "http://testserver/api/v1/networks"),

61 )

62 mock_post = mocker.patch("httpx.post", return_value=mock_response)

63 result = runner.invoke(app, ["network", "create", "new-network", "10.2.0.0/24"])

64 assert result.exit_code == 0

65 mock_post.assert_called_once()

66 call_args = mock_post.call_args

67 assert call_args[0][0] == "http://testserver/api/v1/networks"

68 assert call_args[1]["headers"] == {"Authorization": "Bearer fake-token"}

69 assert "new-network" in result.stdout

70

71

72 def test_create_network_server_error(mocker, mock_access_token, mock_context_config):

73 mock_response = httpx.Response(

74 status_code=400,

75 request=httpx.Request("POST", "http://testserver/api/v1/networks"),

76 )

77 mocker.patch("httpx.post", return_value=mock_response)

78 result = runner.invoke(app, ["network", "create", "new-network", "invalid-cidr"])

79 assert result.exit_code == 1

80 assert "could not create network" in result.stdout

81

82

83 def test_create_network_auth_failure(mock_access_token, mock_context_config):

84 mock_access_token.side_effect = Exception("Auth failed")

85 result = runner.invoke(app, ["network", "create", "new-network", "10.2.0.0/24"])

86 assert result.exit_code == 1

87 assert "failed to get access token" in result.stdout