1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JiraConnect::SubscriptionsController do
let_it_be(:installation) { create(:jira_connect_installation) }
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 }
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 '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) }
before do
group.add_maintainer(user)
end
subject { post :create, params: { jwt: jwt, namespace_path: group.path, format: :json } }
context 'without JWT' do
let(:jwt) { nil }
it 'returns 403' do
sign_in(user)
subject
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'with valid JWT' do
let(:claims) { { iss: installation.client_key, sub: 1234 } }
let(:jwt) { Atlassian::Jwt.encode(claims, installation.shared_secret) }
let(:jira_user) { { 'groups' => { 'items' => [{ 'name' => jira_group_name }] } } }
let(:jira_group_name) { 'site-admins' }
context 'signed in to GitLab' do
before do
sign_in(user)
WebMock
.stub_request(:get, "#{installation.base_url}/rest/api/3/user?accountId=1234&expand=groups")
.to_return(body: jira_user.to_json, status: 200, headers: { 'Content-Type' => 'application/json' })
end
context 'dev panel integration is available' do
it 'creates a subscription' do
expect { subject }.to change { installation.subscriptions.count }.from(0).to(1)
end
it 'returns 200' do
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'when the Jira user is not a site-admin' do
let(:jira_group_name) { 'some-other-group' }
it 'returns forbidden' do
subject
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
context 'not signed in to GitLab' do
it 'returns 401' do
subject
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
end
describe '#destroy' do
let(:subscription) { create(:jira_connect_subscription, installation: installation) }
let(:jira_user) { { 'groups' => { 'items' => [{ 'name' => jira_group_name }] } } }
let(:jira_group_name) { 'site-admins' }
before do
WebMock
.stub_request(:get, "#{installation.base_url}/rest/api/3/user?accountId=1234&expand=groups")
.to_return(body: jira_user.to_json, status: 200, headers: { 'Content-Type' => 'application/json' })
delete :destroy, params: { jwt: jwt, id: subscription.id }
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(:claims) { { iss: installation.client_key, sub: 1234 } }
let(:jwt) { Atlassian::Jwt.encode(claims, installation.shared_secret) }
it 'deletes the subscription' do
expect { subscription.reload }.to raise_error ActiveRecord::RecordNotFound
expect(response).to have_gitlab_http_status(:ok)
end
context 'when the Jira user is not a site admin' do
let(:jira_group_name) { 'some-other-group' }
it 'does not delete the subscription' do
expect(response).to have_gitlab_http_status(:forbidden)
expect { subscription.reload }.not_to raise_error
end
end
end
end
end
|