Commit 6ea0262a authored by Markus Koller's avatar Markus Koller

Merge branch 'add-json-response-for-jira_connect-subscriptions-index' into 'master'

JSON response to jira_connect/subscriptions#index

See merge request gitlab-org/gitlab!66603
parents 1a9bdace 0e4faf55
......@@ -22,6 +22,13 @@ class JiraConnect::SubscriptionsController < JiraConnect::ApplicationController
def index
@subscriptions = current_jira_installation.subscriptions.preload_namespace_route
respond_to do |format|
format.html
format.json do
render json: JiraConnect::AppDataSerializer.new(@subscriptions, !!current_user).as_json
end
end
end
def create
......
# frozen_string_literal: true
class JiraConnect::AppDataSerializer
include Gitlab::Routing
include ::API::Helpers::RelatedResourcesHelpers
def initialize(subscriptions, signed_in)
@subscriptions = subscriptions
@signed_in = signed_in
end
def as_json
skip_groups = @subscriptions.map(&:namespace_id)
{
groups_path: api_v4_groups_path(params: { min_access_level: Gitlab::Access::MAINTAINER, skip_groups: skip_groups }),
subscriptions: JiraConnect::SubscriptionEntity.represent(@subscriptions).as_json,
subscriptions_path: jira_connect_subscriptions_path,
login_path: signed_in? ? nil : jira_connect_users_path
}
end
private
def signed_in?
!!@signed_in
end
end
# frozen_string_literal: true
class JiraConnect::GroupEntity < Grape::Entity
expose :name
expose :avatar_url
expose :full_name
expose :description
end
# frozen_string_literal: true
class JiraConnect::SubscriptionEntity < Grape::Entity
include Gitlab::Routing
expose :created_at
expose :unlink_path do |subscription|
jira_connect_subscription_path(subscription)
end
expose :namespace, with: JiraConnect::GroupEntity, as: :group
end
......@@ -7,9 +7,13 @@ RSpec.describe JiraConnect::SubscriptionsController do
describe '#index' do
before do
request.headers['Accept'] = content_type
get :index, params: { jwt: jwt }
end
let(:content_type) { 'text/html' }
context 'without JWT' do
let(:jwt) { nil }
......@@ -29,13 +33,55 @@ RSpec.describe JiraConnect::SubscriptionsController do
it 'removes X-Frame-Options to allow rendering in iframe' do
expect(response.headers['X-Frame-Options']).to be_nil
end
context 'with JSON format' do
let_it_be(:subscription) { create(:jira_connect_subscription, installation: installation) }
let(:content_type) { 'application/json' }
it 'renders the relevant data as JSON', :aggregate_failures do
expect(json_response).to include('groups_path' => api_v4_groups_path(params: { min_access_level: Gitlab::Access::MAINTAINER, skip_groups: [subscription.namespace_id] }))
expect(json_response).to include(
'subscriptions' => [
'group' => {
'name' => subscription.namespace.name,
'avatar_url' => subscription.namespace.avatar_url,
'full_name' => subscription.namespace.full_name,
'description' => subscription.namespace.description
},
'created_at' => subscription.created_at.iso8601(3),
'unlink_path' => jira_connect_subscription_path(subscription)
]
)
expect(json_response).to include('subscriptions_path' => jira_connect_subscriptions_path)
end
context 'when not signed in to GitLab' do
it 'contains a login path' do
expect(json_response).to include('login_path' => jira_connect_users_path)
end
end
context 'when signed in to GitLab' do
let(:user) { create(:user) }
before do
sign_in(user)
get :index, params: { jwt: jwt }
end
it 'does not contain a login path' do
expect(json_response).to include('login_path' => nil)
end
end
end
end
end
describe '#create' do
let(:group) { create(:group) }
let(:user) { create(:user) }
let(:current_user) { user }
before do
group.add_maintainer(user)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JiraConnect::AppDataSerializer do
describe '#as_json' do
subject(:app_data_json) { described_class.new(subscriptions, signed_in).as_json }
let_it_be(:subscriptions) { create_list(:jira_connect_subscription, 2) }
let(:signed_in) { false }
it 'uses the subscription entity' do
expect(JiraConnect::SubscriptionEntity).to receive(:represent).with(subscriptions)
app_data_json
end
it 'includes a group path with already subscribed namespaces as skip_groups' do
expected_path = "/api/v4/groups?min_access_level=40&skip_groups%5B%5D=#{subscriptions.first.namespace_id}&skip_groups%5B%5D=#{subscriptions.last.namespace_id}"
expect(app_data_json).to include(groups_path: expected_path)
end
it { is_expected.to include(subscriptions_path: '/-/jira_connect/subscriptions') }
it { is_expected.to include(login_path: '/-/jira_connect/users') }
context 'when signed in' do
let(:signed_in) { true }
it { is_expected.to include(login_path: nil) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JiraConnect::GroupEntity do
subject do
described_class.new(subscription.namespace).as_json
end
let(:subscription) { create(:jira_connect_subscription) }
it 'contains all necessary elements of the group', :aggregate_failures do
expect(subject[:name]).to eq(subscription.namespace.name)
expect(subject[:avatar_url]).to eq(subscription.namespace.avatar_url)
expect(subject[:full_name]).to eq(subscription.namespace.full_name)
expect(subject[:description]).to eq(subscription.namespace.description)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JiraConnect::SubscriptionEntity do
subject do
described_class.new(subscription).as_json
end
let(:subscription) { create(:jira_connect_subscription) }
it 'contains all necessary elements of the subscription', :aggregate_failures do
expect(subject).to include(:created_at)
expect(subject[:unlink_path]).to eq("/-/jira_connect/subscriptions/#{subscription.id}")
expect(subject[:group]).to eq(
name: subscription.namespace.name,
avatar_url: subscription.namespace.avatar_url,
full_name: subscription.namespace.full_name,
description: subscription.namespace.description
)
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