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_TEMPLATE_CREATE = {"id": 1, "name": "test-template", "enrollment_key": "abc123"}

10

11 MOCK_TEMPLATE_TOKEN = "1234567890"

12

13

14@pytest.fixture

15 def mock_template_access_token(mocker):

16 return mocker.patch(

17 "meshadmin.cli.commands.template.get_access_token", return_value="fake-token"

18 )

19

20

21@pytest.fixture

22 def mock_template_context_config(mocker):

23 return mocker.patch(

24 "meshadmin.cli.commands.template.get_context_config",

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

26 )

27

28

29 def test_create_template_success(

30 mocker, mock_template_access_token, mock_template_context_config

31):

32 mock_response = httpx.Response(

33 status_code=200,

34 json=MOCK_TEMPLATE_CREATE,

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

36 )

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

38 result = runner.invoke(

39 app,

40 [

41 "template",

42 "create",

43 "test-network",

44 "test-template",

45 "true",

46 "false",

47 "false",

48 ],

49 )

50 assert result.exit_code == 0

51 mock_post.assert_called_once()

52 assert "test-template" in result.stdout

53 assert "abc123" in result.stdout

54

55

56 def test_create_template_auth_failure(

57 mock_template_access_token, mock_template_context_config

58):

59 mock_template_access_token.side_effect = Exception("Auth failed")

60 result = runner.invoke(

61 app,

62 [

63 "template",

64 "create",

65 "test-network",

66 "test-template",

67 "true",

68 "false",

69 "false",

70 ],

71 )

72 assert result.exit_code == 1

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

74

75

76 def test_get_template_token_success(

77 mocker, mock_template_access_token, mock_template_context_config

78):

79 mock_response = httpx.Response(

80 status_code=200,

81 text=MOCK_TEMPLATE_TOKEN,

82 request=httpx.Request(

83 "GET", "http://testserver/api/v1/templates/test-template/token"

84 ),

85 )

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

87 result = runner.invoke(app, ["template", "get-token", "test-template"])

88 assert result.exit_code == 0

89 mock_get.assert_called_once_with(

90 "http://testserver/api/v1/templates/test-template/token",

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

92 )

93 assert MOCK_TEMPLATE_TOKEN in result.stdout

94

95

96 def test_get_template_token_auth_failure(

97 mock_template_access_token, mock_template_context_config

98):

99 mock_template_access_token.side_effect = Exception("Auth failed")

100 result = runner.invoke(app, ["template", "get-token", "test-template"])

101 assert result.exit_code == 1

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

103

104

105 def test_delete_template_success(

106 mocker, mock_template_access_token, mock_template_context_config

107):

108 mock_response = httpx.Response(

109 status_code=200,

110 json={"message": "Template test-template deleted"},

111 request=httpx.Request(

112 "DELETE", "http://testserver/api/v1/templates/test-template"

113 ),

114 )

115 mock_delete = mocker.patch("httpx.delete", return_value=mock_response)

116 result = runner.invoke(app, ["template", "delete", "test-template"])

117 assert result.exit_code == 0

118 mock_delete.assert_called_once_with(

119 "http://testserver/api/v1/templates/test-template",

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

121 )

122 assert "deleted" in result.stdout.lower()

123

124

125 def test_delete_template_auth_failure(

126 mock_template_access_token, mock_template_context_config

127):

128 mock_template_access_token.side_effect = Exception("Auth failed")

129 result = runner.invoke(app, ["template", "delete", "test-template"])

130 assert result.exit_code == 1

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