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
|
require 'spec_helper'
describe Projects::MilestonesController do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:milestone) { create(:milestone, project: project) }
let(:issue) { create(:issue, project: project, milestone: milestone) }
let!(:label) { create(:label, project: project, title: 'Issue Label', issues: [issue]) }
let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, milestone: milestone) }
let(:milestone_path) { namespace_project_milestone_path }
before do
sign_in(user)
project.add_maintainer(user)
controller.instance_variable_set(:@project, project)
end
it_behaves_like 'milestone tabs'
describe "#show" do
render_views
def view_milestone(options = {})
params = { namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid }
get :show, params.merge(options)
end
it 'shows milestone page' do
view_milestone
expect(response).to have_gitlab_http_status(200)
expect(response.content_type).to eq 'text/html'
end
it 'returns milestone json' do
view_milestone format: :json
expect(response).to have_http_status(404)
expect(response.content_type).to eq 'application/json'
end
end
describe "#index" do
context "as html" do
def render_index(project:, page:)
get :index, namespace_id: project.namespace.id,
project_id: project.id,
page: page
end
it "queries only projects milestones" do
render_index project: project, page: 1
milestones = assigns(:milestones)
expect(milestones.count).to eq(1)
expect(milestones.where(project_id: nil)).to be_empty
end
it 'renders paginated milestones without missing or duplicates' do
allow(Milestone).to receive(:default_per_page).and_return(2)
create_list(:milestone, 5, project: project)
render_index project: project, page: 1
page_1_milestones = assigns(:milestones)
expect(page_1_milestones.size).to eq(2)
render_index project: project, page: 2
page_2_milestones = assigns(:milestones)
expect(page_2_milestones.size).to eq(2)
render_index project: project, page: 3
page_3_milestones = assigns(:milestones)
expect(page_3_milestones.size).to eq(2)
rendered_milestone_ids =
page_1_milestones.pluck(:id) +
page_2_milestones.pluck(:id) +
page_3_milestones.pluck(:id)
expect(rendered_milestone_ids)
.to match_array(project.milestones.pluck(:id))
end
end
context "as json" do
let!(:group) { create(:group, :public) }
let!(:group_milestone) { create(:milestone, group: group) }
context 'with a single group ancestor' do
before do
project.update(namespace: group)
get :index, namespace_id: project.namespace.id, project_id: project.id, format: :json
end
it "queries projects milestones and groups milestones" do
milestones = assigns(:milestones)
expect(milestones.count).to eq(2)
expect(milestones).to match_array([milestone, group_milestone])
end
end
context 'with nested groups', :nested_groups do
let!(:subgroup) { create(:group, :public, parent: group) }
let!(:subgroup_milestone) { create(:milestone, group: subgroup) }
before do
project.update(namespace: subgroup)
get :index, namespace_id: project.namespace.id, project_id: project.id, format: :json
end
it "queries projects milestones and all ancestors milestones" do
milestones = assigns(:milestones)
expect(milestones.count).to eq(3)
expect(milestones).to match_array([milestone, group_milestone, subgroup_milestone])
end
end
end
end
describe "#destroy" do
it "removes milestone" do
expect(issue.milestone_id).to eq(milestone.id)
delete :destroy, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid, format: :js
expect(response).to be_success
expect(Event.recent.first.action).to eq(Event::DESTROYED)
expect { Milestone.find(milestone.id) }.to raise_exception(ActiveRecord::RecordNotFound)
issue.reload
expect(issue.milestone_id).to eq(nil)
merge_request.reload
expect(merge_request.milestone_id).to eq(nil)
# Check system note left for milestone removal
last_note = project.issues.find(issue.id).notes[-1].note
expect(last_note).to eq('removed milestone')
end
end
describe '#promote' do
context 'promotion succeeds' do
before do
group = create(:group)
group.add_developer(user)
milestone.project.update(namespace: group)
end
it 'shows group milestone' do
post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid
expect(flash[:notice]).to eq("#{milestone.title} promoted to <a href=\"#{group_milestone_path(project.group, milestone.iid)}\"><u>group milestone</u></a>.")
expect(response).to redirect_to(project_milestones_path(project))
end
it 'renders milestone name without parsing it as HTML' do
milestone.update!(name: 'CCC<img src=x onerror=alert(document.domain)>')
post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid
expect(flash[:notice]).to eq("CCC promoted to <a href=\"#{group_milestone_path(project.group, milestone.iid)}\"><u>group milestone</u></a>.")
end
end
context 'promotion fails' do
it 'shows project milestone' do
post :promote, namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid
expect(response).to redirect_to(project_milestone_path(project, milestone))
expect(flash[:alert]).to eq('Promotion failed - Project does not belong to a group.')
end
end
end
end
|