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
193
194
195
196
197
198
|
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Import::JiraController do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
context 'with anonymous user' do
before do
stub_feature_flags(jira_issue_import: true)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(new_user_session_path)
end
end
context 'post import' do
it 'redirects to issues page' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'with logged in user' do
before do
sign_in(user)
project.add_maintainer(user)
end
context 'when feature flag not enabled' do
before do
stub_feature_flags(jira_issue_import: false)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(project_issues_path(project))
end
end
context 'post import' do
it 'redirects to issues page' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
expect(response).to redirect_to(project_issues_path(project))
end
end
end
context 'when feature flag enabled' do
before do
stub_feature_flags(jira_issue_import: true)
stub_feature_flags(jira_issue_import_vue: false)
end
context 'when jira service is enabled for the project' do
let_it_be(:jira_service) { create(:jira_service, project: project) }
context 'when running jira import first time' do
context 'get show' do
before do
allow(JIRA::Resource::Project).to receive(:all).and_return(jira_projects)
expect(project.import_state).to be_nil
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
end
context 'when no projects have been retrieved from Jira' do
let(:jira_projects) { [] }
it 'render an error message' do
expect(flash[:alert]).to eq('No projects have been returned from Jira. Please check your Jira configuration.')
expect(response).to render_template(:show)
end
end
context 'when everything is ok' do
let(:jira_projects) { [double(name: 'FOO project', key: 'FOO')] }
it 'renders show template' do
expect(response).to render_template(:show)
end
end
end
context 'post import' do
context 'when jira project key is empty' do
it 'redirects back to show with an error' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: '' }
expect(response).to redirect_to(project_import_jira_path(project))
expect(flash[:alert]).to eq('No jira project key has been provided.')
end
end
context 'when everything is ok' do
it 'creates import state' do
expect(project.import_state).to be_nil
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }
project.reload
jira_project = project.import_data.data.dig('jira', 'projects').first
expect(project.import_type).to eq 'jira'
expect(project.import_state.status).to eq 'scheduled'
expect(jira_project['key']).to eq 'Test'
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
end
context 'when import state is scheduled' do
let_it_be(:import_state) { create(:import_state, project: project, status: :scheduled) }
context 'get show' do
it 'renders import status' do
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
expect(project.import_state.status).to eq 'scheduled'
expect(flash.now[:notice]).to eq 'Import scheduled'
end
end
context 'post import' do
before do
project.reload
project.create_import_data(
data: {
'jira': {
'projects': [{ 'key': 'Test', scheduled_at: 5.days.ago, scheduled_by: { user_id: user.id, name: user.name } }]
}
}
)
end
it 'uses the existing import data' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
expect(flash[:notice]).to eq('Jira import is already running.')
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
context 'when jira import ran before' do
let_it_be(:import_state) { create(:import_state, project: project, status: :finished) }
context 'get show' do
it 'renders import status' do
allow(JIRA::Resource::Project).to receive(:all).and_return([])
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
expect(project.import_state.status).to eq 'finished'
expect(flash.now[:notice]).to eq 'Import finished'
end
end
context 'post import' do
before do
project.reload
project.create_import_data(
data: {
'jira': {
'projects': [{ 'key': 'Test', scheduled_at: 5.days.ago, scheduled_by: { user_id: user.id, name: user.name } }]
}
}
)
end
it 'uses the existing import data' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
project.reload
expect(project.import_state.status).to eq 'scheduled'
jira_imported_projects = project.import_data.data.dig('jira', 'projects')
expect(jira_imported_projects.size).to eq 2
expect(jira_imported_projects.first['key']).to eq 'Test'
expect(jira_imported_projects.last['key']).to eq 'New Project'
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
end
end
end
end
|