2020-09-01 08:11:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe JiraConnect::EventsController do
|
2021-10-11 05:09:08 -04:00
|
|
|
shared_examples 'verifies asymmetric JWT token' do
|
|
|
|
context 'when token is valid' do
|
|
|
|
include_context 'valid JWT token'
|
|
|
|
|
|
|
|
it 'renders successful' do
|
|
|
|
send_request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is invalid' do
|
|
|
|
include_context 'invalid JWT token'
|
|
|
|
|
|
|
|
it 'renders unauthorized' do
|
|
|
|
send_request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'valid JWT token' do
|
|
|
|
before do
|
2022-04-25 17:09:46 -04:00
|
|
|
allow_next_instance_of(Atlassian::JiraConnect::Jwt::Asymmetric) do |asymmetric_jwt|
|
2021-10-11 05:09:08 -04:00
|
|
|
allow(asymmetric_jwt).to receive(:valid?).and_return(true)
|
|
|
|
allow(asymmetric_jwt).to receive(:iss_claim).and_return(client_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'invalid JWT token' do
|
|
|
|
before do
|
2022-04-25 17:09:46 -04:00
|
|
|
allow_next_instance_of(Atlassian::JiraConnect::Jwt::Asymmetric) do |asymmetric_jwt|
|
2021-10-11 05:09:08 -04:00
|
|
|
allow(asymmetric_jwt).to receive(:valid?).and_return(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 08:11:01 -04:00
|
|
|
describe '#installed' do
|
2022-03-17 11:07:44 -04:00
|
|
|
let_it_be(:client_key) { '1234' }
|
|
|
|
let_it_be(:shared_secret) { 'secret' }
|
|
|
|
let_it_be(:base_url) { 'https://test.atlassian.net' }
|
2021-10-11 05:09:08 -04:00
|
|
|
|
2020-10-16 02:08:48 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
clientKey: client_key,
|
|
|
|
sharedSecret: shared_secret,
|
2022-03-17 11:07:44 -04:00
|
|
|
baseUrl: base_url
|
2020-09-01 08:11:01 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
include_context 'valid JWT token'
|
|
|
|
|
2020-10-16 02:08:48 -04:00
|
|
|
subject do
|
|
|
|
post :installed, params: params
|
|
|
|
end
|
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
it_behaves_like 'verifies asymmetric JWT token' do
|
|
|
|
let(:send_request) { subject }
|
|
|
|
end
|
|
|
|
|
2020-09-01 08:11:01 -04:00
|
|
|
it 'saves the jira installation data' do
|
|
|
|
expect { subject }.to change { JiraConnectInstallation.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the correct values' do
|
|
|
|
subject
|
|
|
|
|
2020-10-16 02:08:48 -04:00
|
|
|
installation = JiraConnectInstallation.find_by_client_key(client_key)
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2020-10-16 02:08:48 -04:00
|
|
|
expect(installation.shared_secret).to eq(shared_secret)
|
2020-09-01 08:11:01 -04:00
|
|
|
expect(installation.base_url).to eq('https://test.atlassian.net')
|
|
|
|
end
|
|
|
|
|
2022-03-17 11:07:44 -04:00
|
|
|
context 'when the shared_secret param is missing' do
|
2020-10-16 02:08:48 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
clientKey: client_key,
|
2022-03-17 11:07:44 -04:00
|
|
|
baseUrl: base_url
|
2020-10-16 02:08:48 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
it 'returns 422' do
|
|
|
|
subject
|
2020-10-16 02:08:48 -04:00
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
end
|
2022-03-17 11:07:44 -04:00
|
|
|
end
|
2021-10-11 05:09:08 -04:00
|
|
|
|
2022-03-17 11:07:44 -04:00
|
|
|
context 'when an installation already exists' do
|
|
|
|
let_it_be(:installation) { create(:jira_connect_installation, base_url: base_url, client_key: client_key, shared_secret: shared_secret) }
|
|
|
|
|
|
|
|
it 'validates the JWT token in authorization header and returns 200 without creating a new installation', :aggregate_failures do
|
|
|
|
expect { subject }.not_to change { JiraConnectInstallation.count }
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when parameters include a new shared secret and base_url' do
|
|
|
|
let(:shared_secret) { 'new_secret' }
|
|
|
|
let(:base_url) { 'https://new_test.atlassian.net' }
|
|
|
|
|
|
|
|
it 'updates the installation', :aggregate_failures do
|
|
|
|
subject
|
2021-10-11 05:09:08 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2022-03-17 11:07:44 -04:00
|
|
|
expect(installation.reload).to have_attributes(
|
|
|
|
shared_secret: shared_secret,
|
|
|
|
base_url: base_url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the new base_url is invalid' do
|
|
|
|
let(:base_url) { 'invalid' }
|
|
|
|
|
|
|
|
it 'renders 422', :aggregate_failures do
|
|
|
|
expect { subject }.not_to change { installation.reload.base_url }
|
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2021-10-11 05:09:08 -04:00
|
|
|
end
|
2020-10-16 02:08:48 -04:00
|
|
|
end
|
|
|
|
end
|
2021-09-27 14:12:38 -04:00
|
|
|
end
|
2020-10-16 02:08:48 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
describe '#uninstalled' do
|
2021-10-11 05:09:08 -04:00
|
|
|
let_it_be(:installation) { create(:jira_connect_installation) }
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
let(:client_key) { installation.client_key }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
clientKey: client_key,
|
|
|
|
baseUrl: 'https://test.atlassian.net'
|
|
|
|
}
|
2021-09-27 14:12:38 -04:00
|
|
|
end
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
it_behaves_like 'verifies asymmetric JWT token' do
|
|
|
|
let(:send_request) { post :uninstalled, params: params }
|
|
|
|
end
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
subject(:post_uninstalled) { post :uninstalled, params: params }
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
context 'when JWT is invalid' do
|
|
|
|
include_context 'invalid JWT token'
|
2020-09-01 08:11:01 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
it 'does not delete the installation' do
|
|
|
|
expect { post_uninstalled }.not_to change { JiraConnectInstallation.count }
|
|
|
|
end
|
|
|
|
end
|
2021-07-01 14:07:29 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
context 'when JWT is valid' do
|
2021-10-11 05:09:08 -04:00
|
|
|
include_context 'valid JWT token'
|
2021-07-01 14:07:29 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
let(:jira_base_path) { '/-/jira_connect' }
|
|
|
|
let(:jira_event_path) { '/-/jira_connect/events/uninstalled' }
|
2021-07-01 14:07:29 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
it 'calls the DestroyService and returns ok in case of success' do
|
|
|
|
expect_next_instance_of(JiraConnectInstallations::DestroyService, installation, jira_base_path, jira_event_path) do |destroy_service|
|
|
|
|
expect(destroy_service).to receive(:execute).and_return(true)
|
2021-07-01 14:07:29 -04:00
|
|
|
end
|
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
post_uninstalled
|
2021-07-01 14:07:29 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
2021-07-01 14:07:29 -04:00
|
|
|
|
2021-09-27 14:12:38 -04:00
|
|
|
it 'calls the DestroyService and returns unprocessable_entity in case of failure' do
|
|
|
|
expect_next_instance_of(JiraConnectInstallations::DestroyService, installation, jira_base_path, jira_event_path) do |destroy_service|
|
|
|
|
expect(destroy_service).to receive(:execute).and_return(false)
|
2020-09-01 08:11:01 -04:00
|
|
|
end
|
2021-09-27 14:12:38 -04:00
|
|
|
|
|
|
|
post_uninstalled
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2020-09-01 08:11:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|