Skip to content

Commit

Permalink
Specs updates for trigger_workflow_controller and workflow_controller
Browse files Browse the repository at this point in the history
Update specs to verify we don't create workflow_run entry when SCM event
is not supported

Co-authored-by: Dany Marcoux <dmarcoux@suse.com>
  • Loading branch information
rubhanazeem and Dany Marcoux committed Jan 18, 2022
1 parent f3f1aa9 commit b22c065
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
48 changes: 37 additions & 11 deletions src/api/spec/controllers/trigger_workflow_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,44 @@
end

it { expect(response).to have_http_status(:bad_request) }
it { expect(WorkflowRun.count).to eq(0) }
end

it { expect(WorkflowRun.count).to eq(1) }
it { expect(WorkflowRun.last.status).to eq('fail') }
it { expect(response.body).to include(WorkflowRun.last.response_body) }
context 'scm action is invalid' do
let(:octokit_client) { instance_double(Octokit::Client) }
let(:token_extractor_instance) { instance_double(::TriggerControllerService::TokenExtractor) }
let(:token) { build_stubbed(:workflow_token, user: build_stubbed(:confirmed_user, :in_beta)) }
let(:github_payload) do
{
action: 'assigned',
pull_request: {
head: {
repo: { full_name: 'username/test_repo' }
},
base: {
ref: 'main',
repo: { full_name: 'rubhanazeem/hello_world' }
}
},
number: 4,
sender: { url: 'https://api.github.com' }
}
end

before do
allow(::TriggerControllerService::TokenExtractor).to receive(:new).and_return(token_extractor_instance)
allow(token_extractor_instance).to receive(:call).and_return(token)
allow(Octokit::Client).to receive(:new).and_return(octokit_client)
allow(octokit_client).to receive(:content).and_return({ download_url: 'https://google.com' })
allow(Down).to receive(:download).and_raise(Down::Error, 'Beep Boop, something is wrong')
request.headers['ACCEPT'] = '*/*'
request.headers['CONTENT_TYPE'] = 'application/json'
request.headers['HTTP_X_GITHUB_EVENT'] = 'pull_request'
post :create, body: github_payload.to_json
end

it { expect(response).to have_http_status(:ok) }
it { expect(WorkflowRun.count).to eq(0) }
end

context 'scm payload is invalid' do
Expand All @@ -84,10 +118,6 @@
end

it { expect(response).to have_http_status(:bad_request) }

it { expect(WorkflowRun.count).to eq(1) }
it { expect(WorkflowRun.last.status).to eq('fail') }
it { expect(response.body).to include(WorkflowRun.last.response_body) }
end

context 'payload can not be parsed' do
Expand All @@ -96,10 +126,6 @@
end

it { expect(response).to have_http_status(:bad_request) }

it { expect(WorkflowRun.count).to eq(1) }
it { expect(WorkflowRun.last.status).to eq('fail') }
it { expect(response.body).to include(WorkflowRun.last.response_body) }
end
end

Expand Down
9 changes: 6 additions & 3 deletions src/api/spec/models/token/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

context 'without a payload' do
it do
expect { workflow_token.call({ workflow_run: workflow_run }) }.to raise_error(Token::Errors::MissingPayload, 'A payload is required').and(change(workflow_token, :triggered_at))
expect do
workflow_token.call({ workflow_run: workflow_run,
scm_webhook: ScmWebhook.new(payload: {}) })
end.to raise_error(Token::Errors::MissingPayload, 'A payload is required').and(change(workflow_token, :triggered_at))
end
end

Expand Down Expand Up @@ -70,7 +73,7 @@
allow(yaml_to_workflows_service).to receive(:call).and_return(workflows)
end

subject { workflow_token.call(scm: scm, event: event, payload: github_payload, workflow_run: workflow_run) }
subject { workflow_token.call(workflow_run: workflow_run, scm_webhook: scm_extractor.call) }

it 'returns no validation errors' do
expect(subject).to eq([])
Expand Down Expand Up @@ -123,7 +126,7 @@
allow(yaml_to_workflows_service).to receive(:call).and_return(workflows)
end

subject { workflow_token.call(scm: scm, event: event, payload: github_payload, workflow_run: workflow_run) }
subject { workflow_token.call(workflow_run: workflow_run, scm_webhook: scm_extractor.call) }

it 'returns the validation errors' do
expect(subject).to eq(['Event not supported.', 'Workflow steps are not present'])
Expand Down

0 comments on commit b22c065

Please sign in to comment.