Skip to content

Commit c007ee1

Browse files
Magisusclaude
andcommitted
(CDPE-4754) Rewrite create_git_branch spec for V1 endpoint
Stubs the new V1 path with workspaceId query param. Asserts the V1 return shape: 204 maps to {'result' => 'success'}, 4xx maps the V1 error body's message to the function-result error shape, and 5xx raises Puppet::Error. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 944fdb1 commit c007ee1

1 file changed

Lines changed: 43 additions & 34 deletions

File tree

spec/functions/cd4pe/create_git_branch_spec.rb

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
require 'webmock/rspec'
44

55
describe 'cd4pe_deployments::create_git_branch' do
6-
let(:ajax_op) { 'CreateGitBranch' }
7-
86
context 'table steaks' do
97
include_context 'deployment'
108

@@ -21,61 +19,72 @@
2119
include_context 'deployment'
2220

2321
let(:repo_type) { 'CONTROL_REPO' }
24-
let(:git_branch) { 'development_b' }
22+
let(:branch_name) { 'development_b' }
2523
let(:commit_sha) { 'c090ea692e67405c5572af6b2a9dc5f11c9080c0' }
26-
let(:response) do
27-
{
28-
'result' => {
29-
'success' => true,
30-
},
31-
'error' => nil,
32-
}
24+
let(:full_path) do
25+
"#{api_v1_path}/deployments/#{deployment_id}:create-git-branch?workspaceId=#{deployment_domain}"
3326
end
3427

35-
it 'succeeds with parameters' do
36-
stub_request(:post, ajax_url)
28+
it 'returns success on 204' do
29+
stub_request(:post, full_path)
3730
.with(
3831
body: {
39-
op: ajax_op,
40-
content: {
41-
repoType: repo_type,
42-
deploymentId: deployment_id,
43-
branchName: git_branch,
44-
commitSha: commit_sha,
45-
cleanup: true,
46-
},
32+
repoType: repo_type,
33+
branchName: branch_name,
34+
commitSha: commit_sha,
35+
cleanup: true,
4736
},
4837
headers: {
4938
'authorization' => ENV['DEPLOYMENT_TOKEN'],
5039
},
5140
)
52-
.to_return(body: JSON.generate(response['result']))
41+
.to_return(status: 204, body: '')
5342
.times(1)
5443

55-
is_expected.to run.with_params(repo_type, git_branch, commit_sha).and_return(response)
44+
is_expected
45+
.to run
46+
.with_params(repo_type, branch_name, commit_sha)
47+
.and_return({'result' => 'success', 'error' => nil})
5648
end
5749

58-
it 'fails with non-200 response code' do
59-
stub_request(:post, ajax_url)
50+
it 'returns error result on 4xx with a V1 error body' do
51+
v1_error_body = {
52+
'message' => 'Some error message',
53+
'traceId' => 'abc',
54+
'uriPath' => full_path,
55+
}
56+
stub_request(:post, full_path)
6057
.with(
6158
body: {
62-
op: ajax_op,
63-
content: {
64-
repoType: repo_type,
65-
deploymentId: deployment_id,
66-
branchName: git_branch,
67-
commitSha: commit_sha,
68-
cleanup: true,
69-
},
59+
repoType: repo_type,
60+
branchName: branch_name,
61+
commitSha: commit_sha,
62+
cleanup: true,
7063
},
7164
headers: {
7265
'authorization' => ENV['DEPLOYMENT_TOKEN'],
7366
},
7467
)
75-
.to_return(body: JSON.generate(error_response), status: 404)
68+
.to_return(status: 400, body: JSON.generate(v1_error_body))
69+
.times(1)
70+
71+
is_expected
72+
.to run
73+
.with_params(repo_type, branch_name, commit_sha)
74+
.and_return(
75+
{'result' => nil, 'error' => {'message' => 'Some error message', 'code' => '400'}},
76+
)
77+
end
78+
79+
it 'raises on 5xx' do
80+
stub_request(:post, full_path)
81+
.to_return(status: 500, body: 'boom')
7682
.times(1)
7783

78-
is_expected.to run.with_params(repo_type, git_branch, commit_sha).and_return(error_response)
84+
is_expected
85+
.to run
86+
.with_params(repo_type, branch_name, commit_sha)
87+
.and_raise_error(Puppet::Error)
7988
end
8089
end
8190
end

0 commit comments

Comments
 (0)