@@ -22,6 +22,13 @@ def mock_auth(mock_montagu_api_class, requests_mock):
22
22
mock_montagu_api .token = "test-montagu-token"
23
23
requests_mock .get (f"{ PACKIT_URL } /api/auth/login/montagu" , text = json .dumps ({"token" : "test-packit-token" } ))
24
24
25
+ def assert_expected_packit_api_request (requests_mock , index , method , url , text = None ):
26
+ req = requests_mock .request_history [index ]
27
+ assert req .headers ["Authorization" ] == "Bearer test-packit-token"
28
+ assert req .method == method
29
+ assert req .url == url
30
+ assert req .text == text
31
+
25
32
@patch ("montagu.MontaguAPI" )
26
33
def test_authenticates_on_init (MockMontaguAPI , requests_mock ):
27
34
mock_auth (MockMontaguAPI , requests_mock )
@@ -37,15 +44,40 @@ def test_authenticates_on_init(MockMontaguAPI, requests_mock):
37
44
assert sut .token == "test-packit-token"
38
45
39
46
40
- #@patch("montagu.MontaguAPI")
41
- #def test_run():
42
- # mock_auth(MockMontaguAPI, requests_mock)
47
+ @patch ("montagu.MontaguAPI" )
48
+ def test_run (MockMontaguAPI , requests_mock ):
49
+ mock_auth (MockMontaguAPI , requests_mock )
50
+ mock_branches_response = { "branches" : [
51
+ { "name" : "some_other_branch" , "commitHash" : "xyz987" },
52
+ { "name" : "main" , "commitHash" : "abc123" }
53
+ ] }
54
+ requests_mock .get (f"{ PACKIT_URL } /api/runner/git/branches" , text = json .dumps (mock_branches_response ))
55
+ requests_mock .post (f"{ PACKIT_URL } /api/runner/run" , text = json .dumps ({ "taskId" : "test-task-id" }))
43
56
44
- #def test_reauthenticates_on_401
57
+ sut = PackitClient (config )
58
+ task_id = sut .run ("test-packet-group" , {"a" : 1 , "b" : 2 })
45
59
46
- #def test_raises_exception_on_unexpected_status
60
+ assert task_id == "test-task-id"
61
+ assert_expected_packit_api_request (requests_mock , 1 , "GET" , f"{ PACKIT_URL } /api/runner/git/branches" )
62
+ expected_run_payload = json .dumps ({
63
+ "name" : "test-packet-group" ,
64
+ "parameters" : {"a" : 1 , "b" : 2 },
65
+ "branch" : "main" ,
66
+ "hash" : "abc123"
67
+ })
68
+ assert_expected_packit_api_request (requests_mock , 2 , "POST" , f"{ PACKIT_URL } /api/runner/run" , expected_run_payload )
69
+
70
+
71
+ @patch ("montagu.MontaguAPI" )
72
+ def test_poll_status (MockMontaguAPI , requests_mock ):
73
+ mock_auth (MockMontaguAPI , requests_mock )
74
+ mock_poll_response = { "status" : "RUNNING" }
75
+ requests_mock .get (f"{ PACKIT_URL } /api/runner/status/test-task-id" , text = json .dumps (mock_poll_response ))
76
+
77
+ sut = PackitClient (config )
78
+ resp = sut .poll ("test-task-id" )
47
79
48
- #def test_poll_status
80
+ assert resp == mock_poll_response
49
81
50
82
@patch ("montagu.MontaguAPI" )
51
83
def test_kill_task (MockMontaguAPI , requests_mock ):
@@ -57,8 +89,37 @@ def test_kill_task(MockMontaguAPI, requests_mock):
57
89
resp = sut .kill_task ("test-task-id" )
58
90
59
91
assert resp == mock_kill_response
60
- req = requests_mock .request_history [1 ]
61
- assert req .headers ["Authorization" ] == "Bearer test-packit-token"
92
+ assert_expected_packit_api_request (requests_mock , 1 , "POST" , f"{ PACKIT_URL } /api/runner/cancel/test-task-id" )
93
+
94
+
95
+ @patch ("montagu.MontaguAPI" )
96
+ def test_publish (MockMontaguAPI , requests_mock ):
97
+ mock_auth (MockMontaguAPI , requests_mock )
98
+ # publish polls for the packit - return 404 on first poll, 200 on second poll
99
+ requests_mock .get (f"{ PACKIT_URL } /api/packets/test-packet-id" , [
100
+ { "text" : json .dumps ({ "error" : { "detail" : "not found" } }), "status_code" : 404 },
101
+ { "text" : json .dumps ({ "id" : "test-packet-id" , "name" : "A packet" }), "status_code" : 200 }
102
+ ])
103
+ expected_permissions_payload = json .dumps ({
104
+ "addPermissions" : [{ "permission" : "packet.read" , "packetId" : "test-packet-id" }],
105
+ "removePermissions" : []
106
+ })
107
+ requests_mock .put (f"{ PACKIT_URL } /api/roles/test-role-1/permissions" , json = "null" )
108
+ requests_mock .put (f"{ PACKIT_URL } /api/roles/test-role-2/permissions" , text = "null" )
109
+
110
+ sut = PackitClient (config )
111
+ result = sut .publish ("A packet" , "test-packet-id" , ["test-role-1" , "test-role-2" ])
112
+ assert_expected_packit_api_request (requests_mock , 1 , "GET" , f"{ PACKIT_URL } /api/packets/test-packet-id" , None )
113
+ assert_expected_packit_api_request (requests_mock , 2 , "GET" , f"{ PACKIT_URL } /api/packets/test-packet-id" , None )
114
+
115
+ assert_expected_packit_api_request (requests_mock , 3 , "PUT" , f"{ PACKIT_URL } /api/roles/test-role-1/permissions" , expected_permissions_payload )
116
+ assert_expected_packit_api_request (requests_mock , 4 , "PUT" , f"{ PACKIT_URL } /api/roles/test-role-2/permissions" , expected_permissions_payload )
117
+ assert result
118
+
119
+ #def test_raises_exception_when_auth_fails
120
+
121
+ #def test_reauthenticates_on_401
122
+
123
+ #def test_raises_exception_on_unexpected_status
62
124
63
- #def test_publish
64
125
0 commit comments