-
Notifications
You must be signed in to change notification settings - Fork 142
/
stacks_controller_test.rb
279 lines (227 loc) · 8.66 KB
/
stacks_controller_test.rb
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# frozen_string_literal: true
require 'test_helper'
module Shipit
module Api
class StacksControllerTest < ApiControllerTestCase
setup do
authenticate!
@stack = shipit_stacks(:shipit)
end
test "#create fails with insufficient permissions" do
@client.permissions.delete('write:stack')
@client.save!
assert_no_difference 'Stack.count' do
post :create, params: { repo_name: 'rails', repo_owner: 'rails', environment: 'staging', branch: 'staging' }
end
assert_response :forbidden
assert_json 'message', 'This operation requires the `write:stack` permission'
end
test "#create fails with invalid stack" do
assert_no_difference "Stack.count" do
post :create, params: { repo_owner: 'some', repo_name: 'owner/path', branch: 'main' }
end
assert_response :unprocessable_entity
assert_json 'errors', 'repository' => ['is invalid']
end
test "#create creates a stack and renders it back" do
assert_difference -> { Stack.count } do
post :create, params: { repo_name: 'rails', repo_owner: 'rails', environment: 'staging', branch: 'staging' }
end
assert_response :ok
assert_json 'id', Stack.last.id
end
test "#create fails to create stack if it already exists" do
repository = shipit_repositories(:rails)
existing_stack = Stack.create!(
repository: repository,
environment: 'staging',
branch: 'staging',
)
assert_no_difference -> { Stack.count } do
post :create,
params: {
repo_name: existing_stack.repo_name,
repo_owner: existing_stack.repo_owner,
environment: existing_stack.environment,
branch: existing_stack.branch,
}
end
assert_response :unprocessable_entity
assert_json 'errors', 'repository' => [
'cannot be used more than once with this environment. Check archived stacks.',
]
end
test "#update updates a stack and renders it back" do
assert_equal true, @stack.merge_queue_enabled
assert_equal false, @stack.ignore_ci
assert_equal false, @stack.continuous_deployment
patch :update, params: {
id: @stack.to_param,
merge_queue_enabled: false,
ignore_ci: true,
continuous_deployment: true,
}
assert_response :ok
@stack.reload
assert_equal false, @stack.merge_queue_enabled
assert_equal true, @stack.ignore_ci
assert_equal true, @stack.continuous_deployment
end
test "#update allows changing the branch name" do
assert_equal 'master', @stack.branch
patch :update, params: {
id: @stack.to_param,
branch: 'test',
}
assert_response :ok
@stack.reload
assert_equal 'test', @stack.branch
end
test "#update updates the stack when nil deploy_url" do
@stack.update(deploy_url: nil)
@stack.update(continuous_deployment: true)
assert_nil @stack.deploy_url
assert @stack.continuous_deployment
patch :update, params: {
id: @stack.to_param,
continuous_deployment: false,
}
assert_response :ok
@stack.reload
assert_nil @stack.deploy_url
refute @stack.continuous_deployment
end
test "#update does not perform archive when key is not provided" do
refute_predicate @stack, :archived?
refute_predicate @stack, :locked?
patch :update, params: { id: @stack.to_param }
@stack.reload
refute_predicate @stack, :archived?
refute_predicate @stack, :locked?
end
test "#update does not perform unarchive when key is not provided" do
@stack.archive!(shipit_users(:walrus))
assert_predicate @stack, :locked?
assert_predicate @stack, :archived?
patch :update, params: { id: @stack.to_param }
@stack.reload
assert_predicate @stack, :locked?
assert_predicate @stack, :archived?
end
test "#update allows to archive the stack" do
refute_predicate @stack, :archived?
refute_predicate @stack, :locked?
patch :update, params: { id: @stack.to_param, archived: true }
@stack.reload
assert_predicate @stack, :locked?
assert_predicate @stack, :archived?
assert_instance_of AnonymousUser, @stack.lock_author
assert_equal "Archived", @stack.lock_reason
end
test "#update allows to unarchive the stack" do
@stack.archive!(shipit_users(:walrus))
assert_predicate @stack, :locked?
assert_predicate @stack, :archived?
patch :update, params: { id: @stack.to_param, archived: false }
@stack.reload
refute_predicate @stack, :archived?
refute_predicate @stack, :locked?
assert_nil @stack.locked_since
assert_nil @stack.lock_reason
assert_instance_of AnonymousUser, @stack.lock_author
end
test "#index returns a list of stacks" do
stack = Stack.last
get :index
assert_response :ok
assert_json '0.id', stack.id
assert_json do |stacks|
assert_equal Stack.count, stacks.size
end
end
test "#index returns a list of stacks filtered by repo if name and owner given" do
repo = shipit_repositories(:shipit)
get :index, params: { repo_owner: repo.owner, repo_name: repo.name }
assert_response :ok
assert_json do |stacks|
assert_equal stacks.size, repo.stacks.size
end
end
test "#index returns a list of stacks filtered by repo and api client" do
authenticate!(:here_come_the_walrus)
repo = shipit_repositories(:soc)
get :index, params: { repo_owner: repo.owner, repo_name: repo.name }
assert_response :ok
assert_json do |stacks|
assert_equal 0, stacks.size
end
end
test "#index is paginable" do
get :index, params: { page_size: 1 }
assert_json do |list|
assert_instance_of Array, list
assert_equal 1, list.size
stack_id = list.last['id']
assert_link 'next', api_stacks_url(since: stack_id, page_size: 1)
assert_link 'first', api_stacks_url(page_size: 1)
end
end
test "the `next` link is not provided when the last page is reached" do
get :index, params: { page_size: Stack.count }
assert_no_link 'next'
end
test "an api client scoped to a stack will only see that one stack" do
authenticate!(:here_come_the_walrus)
get :index
assert_json do |stacks|
assert_equal 1, stacks.size
end
end
test "a request with insufficient permissions will render a 403" do
@client.update!(permissions: [])
get :index
assert_response :forbidden
assert_json 'message', 'This operation requires the `read:stack` permission'
end
test "#show renders the stack" do
get :show, params: { id: @stack.to_param }
assert_response :ok
assert_json 'id', @stack.id
end
test "#show returns last_deployed_at column for stack" do
get :show, params: { id: @stack.to_param }
assert_response :ok
assert_json 'last_deployed_at', @stack.last_deployed_at
end
test "#destroy schedules stack deletion job" do
assert_enqueued_with(job: DestroyStackJob) do
delete :destroy, params: { id: @stack.to_param }
end
assert_response :accepted
end
test "#destroy fails with insufficient permissions" do
@client.permissions.delete('write:stack')
@client.save!
assert_no_difference 'Stack.count' do
delete :destroy, params: { id: @stack.to_param }
end
assert_response :forbidden
assert_json 'message', 'This operation requires the `write:stack` permission'
end
test "#refresh queues a GithubSyncJob" do
assert_enqueued_with(job: GithubSyncJob, args: [stack_id: @stack.id]) do
post :refresh, params: { id: @stack.to_param }
end
assert_response :accepted
end
test "#refresh queues a RefreshStatusesJob and RefreshCheckRunsJob" do
assert_enqueued_with(job: RefreshStatusesJob, args: [stack_id: @stack.id]) do
assert_enqueued_with(job: RefreshCheckRunsJob, args: [stack_id: @stack.id]) do
post :refresh, params: { id: @stack.to_param }
end
end
assert_response :accepted
end
end
end
end