2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe JiraConnectHelper do
|
|
|
|
describe '#jira_connect_app_data' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let_it_be(:installation) { create(:jira_connect_installation) }
|
2021-03-11 19:13:27 +05:30
|
|
|
let_it_be(:subscription) { create(:jira_connect_subscription) }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:user) { create(:user) }
|
2022-05-07 20:08:51 +05:30
|
|
|
let(:client_id) { '123' }
|
|
|
|
|
|
|
|
before do
|
2022-07-23 23:45:48 +05:30
|
|
|
stub_application_setting(jira_connect_application_key: client_id)
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
subject { helper.jira_connect_app_data([subscription], installation) }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
context 'user is not logged in' do
|
|
|
|
before do
|
|
|
|
allow(view).to receive(:current_user).and_return(nil)
|
2022-10-11 01:57:18 +05:30
|
|
|
allow(Gitlab).to receive_message_chain('config.gitlab.url') { 'http://test.host' }
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes Jira Connect app attributes' do
|
|
|
|
is_expected.to include(
|
|
|
|
:groups_path,
|
2022-07-16 23:28:13 +05:30
|
|
|
:add_subscriptions_path,
|
2021-03-11 19:13:27 +05:30
|
|
|
:subscriptions_path,
|
2022-01-26 12:08:38 +05:30
|
|
|
:users_path,
|
|
|
|
:subscriptions,
|
|
|
|
:gitlab_user_path
|
2021-03-11 19:13:27 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns users_path with value' do
|
|
|
|
expect(subject[:users_path]).to eq(jira_connect_users_path)
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
context 'with oauth_metadata' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:oauth_metadata) { helper.jira_connect_app_data([subscription], installation)[:oauth_metadata] }
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
subject(:parsed_oauth_metadata) { Gitlab::Json.parse(oauth_metadata).deep_symbolize_keys }
|
|
|
|
|
|
|
|
it 'assigns oauth_metadata' do
|
|
|
|
expect(parsed_oauth_metadata).to include(
|
|
|
|
oauth_authorize_url: start_with('http://test.host/oauth/authorize?'),
|
2022-10-11 01:57:18 +05:30
|
|
|
oauth_token_path: '/oauth/token',
|
2022-05-07 20:08:51 +05:30
|
|
|
state: %r/[a-z0-9.]{32}/,
|
|
|
|
oauth_token_payload: hash_including(
|
|
|
|
grant_type: 'authorization_code',
|
|
|
|
client_id: client_id,
|
|
|
|
redirect_uri: 'http://test.host/-/jira_connect/oauth_callbacks'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes oauth_authorize_url with all params' do
|
|
|
|
params = Rack::Utils.parse_nested_query(URI.parse(parsed_oauth_metadata[:oauth_authorize_url]).query)
|
|
|
|
|
|
|
|
expect(params).to include(
|
|
|
|
'client_id' => client_id,
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => 'api',
|
|
|
|
'redirect_uri' => 'http://test.host/-/jira_connect/oauth_callbacks',
|
|
|
|
'state' => parsed_oauth_metadata[:state]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'jira_connect_oauth feature is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(jira_connect_oauth: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not assign oauth_metadata' do
|
|
|
|
expect(oauth_metadata).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
context 'with self-managed instance' do
|
|
|
|
let_it_be(:installation) { create(:jira_connect_installation, instance_url: 'https://gitlab.example.com') }
|
|
|
|
|
|
|
|
it 'points urls to the self-managed instance' do
|
|
|
|
expect(parsed_oauth_metadata).to include(
|
|
|
|
oauth_authorize_url: start_with('https://gitlab.example.com/oauth/authorize?'),
|
|
|
|
oauth_token_path: '/oauth/token'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and jira_connect_oauth_self_managed feature is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(jira_connect_oauth_self_managed: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not point urls to the self-managed instance' do
|
|
|
|
expect(parsed_oauth_metadata).not_to include(
|
|
|
|
oauth_authorize_url: start_with('https://gitlab.example.com/oauth/authorize?'),
|
|
|
|
oauth_token_path: 'https://gitlab.example.com/oauth/token'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'passes group as "skip_groups" param' do
|
|
|
|
skip_groups_param = CGI.escape('skip_groups[]')
|
|
|
|
|
|
|
|
expect(subject[:groups_path]).to include("#{skip_groups_param}=#{subscription.namespace.id}")
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
it 'assigns gitlab_user_path to nil' do
|
|
|
|
expect(subject[:gitlab_user_path]).to be_nil
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is logged in' do
|
|
|
|
before do
|
|
|
|
allow(view).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns users_path to nil' do
|
|
|
|
expect(subject[:users_path]).to be_nil
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
it 'assigns gitlab_user_path correctly' do
|
|
|
|
expect(subject[:gitlab_user_path]).to eq(user_path(user))
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|