Commit f72f1fb8 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '325281_add-jira-connect-installations-controller' into 'master'

Add jira installations controller

See merge request gitlab-org/gitlab!68306
parents 95bc8490 1fadb26c
# frozen_string_literal: true
class JiraConnect::InstallationsController < JiraConnect::ApplicationController
def index
render json: installation_json(current_jira_installation)
end
def update
if current_jira_installation.update(installation_params)
render json: installation_json(current_jira_installation)
else
render(
json: { errors: current_jira_installation.errors },
status: :unprocessable_entity
)
end
end
private
def installation_json(installation)
{
gitlab_com: installation.instance_url.blank?,
instance_url: installation.instance_url
}
end
def installation_params
params.require(:installation).permit(:instance_url)
end
end
......@@ -14,4 +14,10 @@ namespace :jira_connect do
resources :subscriptions, only: [:index, :create, :destroy]
resources :branches, only: [:new]
resources :installations, only: [:index] do
collection do
put :update
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JiraConnect::InstallationsController do
let_it_be(:installation) { create(:jira_connect_installation) }
describe 'GET /-/jira_connect/installations' do
before do
get '/-/jira_connect/installations', params: { jwt: jwt }
end
context 'without JWT' do
let(:jwt) { nil }
it 'returns 403' do
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'with valid JWT' do
let(:qsh) { Atlassian::Jwt.create_query_string_hash('https://gitlab.test/installations', 'GET', 'https://gitlab.test') }
let(:jwt) { Atlassian::Jwt.encode({ iss: installation.client_key, qsh: qsh }, installation.shared_secret) }
it 'returns status ok' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns the installation as json' do
expect(json_response).to eq({
'gitlab_com' => true,
'instance_url' => nil
})
end
context 'with instance_url' do
let_it_be(:installation) { create(:jira_connect_installation, instance_url: 'https://example.com') }
it 'returns the installation as json' do
expect(json_response).to eq({
'gitlab_com' => false,
'instance_url' => 'https://example.com'
})
end
end
end
end
describe 'PUT /-/jira_connect/installations' do
before do
put '/-/jira_connect/installations', params: { jwt: jwt, installation: { instance_url: update_instance_url } }
end
let(:update_instance_url) { 'https://example.com' }
context 'without JWT' do
let(:jwt) { nil }
it 'returns 403' do
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'with valid JWT' do
let(:qsh) { Atlassian::Jwt.create_query_string_hash('https://gitlab.test/subscriptions', 'GET', 'https://gitlab.test') }
let(:jwt) { Atlassian::Jwt.encode({ iss: installation.client_key, qsh: qsh }, installation.shared_secret) }
it 'returns 200' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'updates the instance_url' do
expect(json_response).to eq({
'gitlab_com' => false,
'instance_url' => 'https://example.com'
})
end
context 'invalid URL' do
let(:update_instance_url) { 'invalid url' }
it 'returns 422 and errors', :aggregate_failures do
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response).to eq({
'errors' => {
'instance_url' => [
'is blocked: Only allowed schemes are http, https'
]
}
})
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment