This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
/
artefacts_controller_test.rb
310 lines (260 loc) · 11.8 KB
/
artefacts_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require 'test_helper'
class ArtefactsControllerTest < ActionController::TestCase
setup do
login_as_stub_user
end
context "accept HTML" do
context "GET show" do
should "redirect to publisher when publisher is the owning app" do
artefact = Artefact.create! :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :name => 'Whatever', :need_id => 1
get :show, id: artefact.to_param
assert_redirected_to Plek.current.find('publisher') + "/admin/publications/#{artefact.id}"
end
end
context "GET index" do
should "should filter by section" do
tag1 = FactoryGirl.create(:tag, tag_id: "crime", title: "Crime", tag_type: "section")
tag2 = FactoryGirl.create(:tag, tag_id: "education", title: "Education", tag_type: "section")
artefact1 = FactoryGirl.create(:artefact, sections: ["crime"])
artefact2 = FactoryGirl.create(:artefact, sections: ["education"])
get :index, section: "crime"
assert_select "tbody tr", count: 1
assert_select "tbody tr td", /crime/i
assert_select "tbody tr td", artefact1.name
end
end
context "PUT update" do
context "invalid artefact" do
should "be invalid with an empty title" do
artefact1 = FactoryGirl.create(:artefact)
artefact1.name = ""
put :update, id: artefact1.id, artefact: { name: "" }
assert_template :edit
end
end
end
end
context "accept JSON" do
context "POST /artefacts" do
should "create a new artefact" do
assert_difference "Artefact.count" do
post :create, format: "json", :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :rendering_app => "frontend", :name => 'Whatever', :need_id => 1
end
artefact = Artefact.order_by([[:id, :desc]]).last
assert_equal "whatever", artefact.slug
assert_equal "guide", artefact.kind
assert_equal "publisher", artefact.owning_app
assert_equal "frontend", artefact.rendering_app
assert_equal "Whatever", artefact.name
end
should "respond with JSON representing the new artefact" do
post :create, format: "json", :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :rendering_app => 'frontend', :name => 'Whatever', :need_id => 1
parsed = JSON.parse(response.body)
assert_equal "publisher", parsed["owning_app"]
assert_equal "frontend", parsed["rendering_app"]
assert_equal "guide", parsed["kind"]
assert_equal "whatever", parsed["slug"]
assert_equal "Whatever", parsed["name"]
assert parsed["id"].present?
end
should "record a create action on the artefact with the current user" do
post(
:create,
format: "json",
:slug => 'whatever',
:kind => 'guide',
:owning_app => 'publisher',
:rendering_app => 'frontend',
:name => 'Whatever',
:need_id => 1
)
parsed = JSON.parse(response.body)
artefact_id = parsed["id"]
artefact = Artefact.find(artefact_id)
assert_equal 1, artefact.actions.size
assert_equal "create", artefact.actions.first.action_type
assert_equal stub_user, artefact.actions.first.user
end
should "create an artefact of kind 'video' for the current user" do
post :create, format: "json", slug: "welcome-to-the-world-of-tomorrow", kind: "video", owning_app: "publisher", rendering_app: "frontend", name: "Welcome to the world of Tomorrow!", need_id: 1352344
parsed = JSON.parse(response.body)
assert_equal "publisher", parsed["owning_app"]
assert_equal "frontend", parsed["rendering_app"]
assert_equal "video", parsed["kind"]
assert_equal "welcome-to-the-world-of-tomorrow", parsed["slug"]
assert_equal "Welcome to the world of Tomorrow!", parsed["name"]
assert_equal true, parsed["id"].present?
artefact_id = parsed["id"]
artefact = Artefact.find(artefact_id)
assert_equal 1, artefact.actions.size
assert_equal "create", artefact.actions.first.action_type
assert_equal stub_user, artefact.actions.first.user
end
end
context "GET /artefacts/:id" do
should "Output json" do
artefact = Artefact.create! :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :name => 'Whatever', :need_id => 1
get :show, id: artefact.id, format: :json
parsed = JSON.parse(response.body)
assert_equal artefact.id.to_s, parsed['id']
assert_equal artefact.name, parsed['name']
assert_equal artefact.slug, parsed['slug']
assert_equal artefact.kind, parsed['kind']
assert_equal artefact.owning_app, parsed['owning_app']
end
should "Output json for slug with slash in slug" do
artefact = Artefact.create! :slug => 'done/whatever', :kind => 'answer', :owning_app => 'publisher', :name => 'Done Whatever', :need_id => 1
get :show, id: artefact.id, format: :json
parsed = JSON.parse(response.body)
assert_equal artefact.id.to_s, parsed['id']
assert_equal artefact.name, parsed['name']
assert_equal artefact.slug, parsed['slug']
end
should "Include section ID" do
FactoryGirl.create(:tag, :tag_id => 'crime', :tag_type => 'section', :title => 'Crime')
artefact = Artefact.create! :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :name => 'Whatever', :need_id => 1
artefact.sections = ['crime']
artefact.save!
get :show, id: artefact.id, format: :json
parsed = JSON.parse(response.body)
assert_equal artefact.section, parsed['section']
end
should "Include tag_ids" do
FactoryGirl.create(:tag, :tag_id => 'crime', :tag_type => 'section', :title => 'Crime')
FactoryGirl.create(:tag, :tag_id => 'businesslink', :tag_type => 'legacy_source', :title => 'Business Link')
artefact = Artefact.create! :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :name => 'Whatever', :need_id => 1
artefact.sections = ['crime']
artefact.legacy_sources = ['businesslink']
artefact.save!
get :show, id: artefact.id, format: :json
parsed = JSON.parse(response.body)
assert_equal %w(businesslink crime), parsed['tag_ids'].sort
end
should "return 404 if the artefact's not found" do
get :show, id: 'bad-slug', format: :json
assert_equal 404, response.code.to_i
end
context "GET /artefacts/:id/edit" do
should "Include history" do
# Create and update the artefact to set up some actions
artefact = Artefact.create!(
:slug => 'whatever',
:kind => 'guide',
:owning_app => 'publisher',
:name => 'Whatever',
:need_id => 1,
)
artefact.update_attributes_as stub_user, name: "Changed"
get :edit, id: artefact.id, format: :html
# Check the actions: note reverse order
actions = assigns["actions"]
assert_equal ["update", "create"], actions.map(&:action_type)
assert_equal [false, true], actions.map(&:initial?)
end
should "assign list of sections" do
FactoryGirl.create(:tag, :tag_type => 'section', :tag_id => 'kablooey', :title => 'Kablooey')
FactoryGirl.create(:tag, :tag_type => 'section', :tag_id => 'fooey', :title => 'Fooey')
FactoryGirl.create(:tag, :tag_type => 'section', :tag_id => 'gooey', :title => 'Gooey')
FactoryGirl.create(:tag, :tag_type => 'legacy_source', :tag_id => 'businesslink', :title => 'Business Link')
artefact = FactoryGirl.create(:artefact)
get :edit, id: artefact.id, format: :html
assert_equal ['fooey', 'gooey', 'kablooey'], assigns["tag_collection"].map(&:tag_id)
end
should "disable the name field as it's managed by the owning app" do
artefact = FactoryGirl.create(:artefact)
get :edit, id: artefact.id, format: :html
assert_select 'input[id=artefact_name][disabled=disabled]'
end
end
end
context "PUT /artefacts/:id" do
should "Update existing artefact" do
artefact = Artefact.create! :slug => 'whatever', :kind => 'guide', :owning_app => 'publisher', :rendering_app => 'frontend', :name => 'Whatever', :need_id => 1
assert_difference "Artefact.count", 0 do
put :update, id: artefact.id, format: :json, name: "Changed"
end
assert_response :success
assert_equal "Changed", artefact.reload.name
end
should "Record the action and responsible user" do
artefact = Artefact.create!(
:slug => 'whatever',
:kind => 'guide',
:owning_app => 'publisher',
:rendering_app => 'frontend',
:name => 'Whatever',
:need_id => 1
)
put :update, id: artefact.id, format: :json, name: "Changed"
assert_response :success
artefact.reload
assert_equal stub_user, artefact.actions.last.user
end
should "Not record the user for API requests" do
login_as GDS::SSO::ApiUser.new
artefact = Artefact.create!(
:slug => 'whatever',
:kind => 'guide',
:owning_app => 'publisher',
:rendering_app => 'frontend',
:name => 'Whatever',
:need_id => 1
)
put :update, id: artefact.id, format: :json, name: "Changed"
assert_response :success
artefact.reload
assert_equal nil, artefact.actions.last.user
end
should "Update our primary section and ensure it persists into sections" do
tag1 = FactoryGirl.create(:tag, tag_id: "crime", title: "Crime", tag_type: "section")
tag2 = FactoryGirl.create(:tag, tag_id: "education", title: "Education", tag_type: "section")
artefact = Artefact.create!(:slug => 'whatever', :kind => 'guide',
:owning_app => 'publisher', :name => 'Whatever', :need_id => 1)
artefact.sections = [tag1.tag_id]
artefact.save!
put :update, :id => artefact.id, :artefact => {:primary_section => tag2.tag_id}
artefact.reload
assert_equal tag2.tag_id, artefact.primary_section.tag_id
assert_equal [tag2.tag_id, tag1.tag_id], artefact.sections.map(&:tag_id)
end
should "Reject JSON requests to update an artefact's owning app" do
artefact = Artefact.create!(
slug: "whatever",
kind: "guide",
owning_app: "publisher",
name: "Whatever",
need_id: 1
)
put :update, id: artefact.id, "CONTENT_TYPE" => "application/json", owning_app: 'smart-answers'
assert_equal 409, response.status
assert response.body.include? "publisher"
end
end
context "DELETE /artefacts/:id" do
setup do
WebMock.stub_request(:delete, "http://router.cluster:8080/router/routes/whatever").
to_return(:status => 200)
WebMock.stub_request(:delete, "http://search.test.gov.uk/documents/%2Fwhatever").
to_return(:status => 200)
WebMock.stub_request(:post, "http://search.test.gov.uk/commit").
to_return(:status => 200)
end
should "mark an artefact as archived" do
artefact = Artefact.create!(
slug: "whatever",
kind: "guide",
owning_app: "publisher",
name: "Whatever",
need_id: 1
)
delete :destroy, id: artefact.id, format: "json"
assert_equal 200, response.status
assert_equal "archived", artefact.reload.state
end
should "return a 404" do
delete :destroy, id: "4567", format: "json"
assert_equal 404, response.status
end
end
end
end