Module: GdsApi::TestHelpers::ContentApi

Includes:
BusinessSupportHelper, CommonResponses
Included in:
ArtefactStub
Defined in:
lib/gds_api/test_helpers/content_api.rb,
lib/gds_api/test_helpers/content_api/artefact_stub.rb

Defined Under Namespace

Classes: ArtefactStub

Constant Summary collapse

CONTENT_API_ENDPOINT =

Generally true. If you are initializing the client differently, you could redefine/override the constant or stub directly.

Plek.current.find('contentapi')

Instance Method Summary collapse

Methods included from BusinessSupportHelper

#api_has_business_support, #setup_business_support_stubs

Methods included from CommonResponses

#acronymize_slug, #plural_response_base, #response_base, #titleize_slug

Instance Method Details

#artefact_for_slug(slug, options = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/gds_api/test_helpers/content_api.rb', line 316

def artefact_for_slug(slug, options = {})
  singular_response_base.merge(
    "title" => titleize_slug(slug),
    "format" => options.fetch(:format, "guide"),
    "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape(slug)}.json",
    "web_url" => "http://frontend.test.gov.uk/#{slug}",
    "details" => {
      "need_ids" => ["100001"],
      "business_proposition" => false, # To be removed and replaced with proposition tags
      "format" => options.fetch(:format, "guide"),
      "alternative_title" => "",
      "overview" => "This is an overview",
      "video_summary" => "",
      "video_url" => "",
      "parts" => [
        {
          "id" => "overview",
          "order" => 1,
          "title" => "Overview",
          "body" => "<p>Some content</p>"
        },
        {
          "id" => "#{slug}-part-2",
          "order" => 2,
          "title" => "How to make a nomination",
          "body" => "<p>Some more content</p>"
        }
      ]
    },
    "tags" => [],
    "related" => []
  )
end

#artefact_for_slug_in_a_section(slug, section_slug) ⇒ Object



36
37
38
# File 'lib/gds_api/test_helpers/content_api.rb', line 36

def artefact_for_slug_in_a_section(slug, section_slug)
  artefact_for_slug_with_a_tag("section", slug, section_slug)
end

#artefact_for_slug_in_a_subsection(slug, subsection_slug) ⇒ Object



40
41
42
# File 'lib/gds_api/test_helpers/content_api.rb', line 40

def artefact_for_slug_in_a_subsection(slug, subsection_slug)
  artefact_for_slug_with_a_child_tag("section", slug, subsection_slug)
end

#artefact_for_slug_with_a_child_tag(tag_type, slug, child_tag_id) ⇒ Object



356
357
358
# File 'lib/gds_api/test_helpers/content_api.rb', line 356

def artefact_for_slug_with_a_child_tag(tag_type, slug, child_tag_id)
  artefact_for_slug_with_a_child_tags(tag_type, slug, [child_tag_id])
end

#artefact_for_slug_with_a_child_tags(tag_type, slug, child_tag_ids) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/gds_api/test_helpers/content_api.rb', line 360

def artefact_for_slug_with_a_child_tags(tag_type, slug, child_tag_ids)
  artefact = artefact_for_slug(slug)

  child_tag_ids.each do |child_tag_id|
    # for each "part" of the path, we want to reduce across the
    # list and build up a tree of nested tags.
    # This will turn "thing1/thing2" into:
    #   Tag{ thing2, parent: Tag{ thing1 } }

    tag_tree = nil
    child_tag_id.split('/').inject(nil) do |parent_tag, child_tag|
      child_tag = [parent_tag, child_tag].join('/') if parent_tag
      next_level_tag = tag_for_slug(child_tag, tag_type)
      if tag_tree
        # Because tags are nested within one another, this makes
        # the current part the top, and the rest we've seen the
        # ancestors
        tag_tree = next_level_tag.merge("parent" => tag_tree)
      else
        tag_tree = next_level_tag
      end

      # This becomes the parent tag in the next iteration of the block
      child_tag
    end
    artefact["tags"] << tag_tree
  end

  artefact
end

#artefact_for_slug_with_a_tag(tag_type, slug, tag_id) ⇒ Object



350
351
352
353
354
# File 'lib/gds_api/test_helpers/content_api.rb', line 350

def artefact_for_slug_with_a_tag(tag_type, slug, tag_id)
  artefact = artefact_for_slug(slug)
  artefact["tags"] << tag_for_slug(tag_id, tag_type)
  artefact
end


391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/gds_api/test_helpers/content_api.rb', line 391

def artefact_for_slug_with_related_artefacts(slug, related_artefact_slugs)
  artefact = artefact_for_slug(slug)
  artefact["related"] = related_artefact_slugs.map do |related_slug|
    {
      "title" => titleize_slug(related_slug),
      "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape(related_slug)}.json",
      "web_url" => "https://www.test.gov.uk/#{related_slug}",
      "details" => {}
    }
  end
  artefact
end

#content_api_does_not_have_an_artefact(slug) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/gds_api/test_helpers/content_api.rb', line 283

def content_api_does_not_have_an_artefact(slug)
  body = {
    "_response_info" => {
      "status" => "not found"
    }
  }
  ArtefactStub.new(slug)
      .with_response_body(body)
      .with_response_status(404)
      .stub
end

#content_api_does_not_have_tag(tag_type, slug) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gds_api/test_helpers/content_api.rb', line 74

def content_api_does_not_have_tag(tag_type, slug)
  body = {
    "_response_info" => {
      "status" => "not found"
    }
  }

  urls = ["#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag_type)}/#{CGI.escape(slug)}.json"]

  if tag_type == "section"
    urls << "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(slug)}.json"
  end

  urls.each do |url|
    stub_request(:get, url).to_return(status: 404, body: body.to_json, headers: {})
  end
end

#content_api_does_not_have_tags(tag_type, slugs) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gds_api/test_helpers/content_api.rb', line 108

def content_api_does_not_have_tags(tag_type, slugs)
  body = {
    "_response_info" => {
      "status" => "not found"
    }
  }

  stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
    .with(query: hash_including({"type" => tag_type}))
    .to_return(status: 404, body: body.to_json, headers: {})
end

#content_api_has_an_archived_artefact(slug) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/gds_api/test_helpers/content_api.rb', line 295

def content_api_has_an_archived_artefact(slug)
  body = {
    "_response_info" => {
      "status" => "gone",
      "status_message" => "This item is no longer available"
    }
  }
  ArtefactStub.new(slug)
      .with_response_body(body)
      .with_response_status(410)
      .stub
end

#content_api_has_an_artefact(slug, body = artefact_for_slug(slug)) ⇒ Object



265
266
267
# File 'lib/gds_api/test_helpers/content_api.rb', line 265

def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
  ArtefactStub.new(slug).with_response_body(body).stub
end

#content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug)) ⇒ Object



276
277
278
279
280
281
# File 'lib/gds_api/test_helpers/content_api.rb', line 276

def content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug))
  ArtefactStub.new(slug)
      .with_response_body(body)
      .with_query_parameters(snac: snac)
      .stub
end

#content_api_has_artefacts_for_need_id(need_id, artefacts) ⇒ Object



533
534
535
536
537
538
539
540
# File 'lib/gds_api/test_helpers/content_api.rb', line 533

def content_api_has_artefacts_for_need_id(need_id, artefacts)
  url = "#{CONTENT_API_ENDPOINT}/for_need/#{CGI.escape(need_id.to_s)}.json"
  body = plural_response_base.merge(
    'results' => artefacts
  )

  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: [])
end

#content_api_has_artefacts_in_a_section(slug, artefact_slugs = nil) ⇒ Object



32
33
34
# File 'lib/gds_api/test_helpers/content_api.rb', line 32

def content_api_has_artefacts_in_a_section(slug, artefact_slugs=nil)
  content_api_has_artefacts_with_a_tag("section", slug, artefact_slugs)
end

#content_api_has_artefacts_with_a_draft_tag(tag_type, slug, artefact_slugs = []) ⇒ Object



229
230
231
# File 'lib/gds_api/test_helpers/content_api.rb', line 229

def content_api_has_artefacts_with_a_draft_tag(tag_type, slug, artefact_slugs=[])
  content_api_has_artefacts_with_a_tag(tag_type, slug, artefact_slugs, draft: true)
end

#content_api_has_artefacts_with_a_tag(tag_type, slug, artefact_slugs = [], options = {tag: {}, artefact: {}}) ⇒ Object



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
# File 'lib/gds_api/test_helpers/content_api.rb', line 165

def content_api_has_artefacts_with_a_tag(tag_type, slug, artefact_slugs=[], options={tag: {}, artefact: {}})
  if options.has_key?(:draft)
    puts "Passing a key of :draft outside of the 'tag' options hash is being deprecated. Please use tag: { draft: bool }"
  end

  draft = options[:draft] || (options[:tag] && options[:tag][:draft])

  body = plural_response_base.merge(
    "results" => artefact_slugs.map do |artefact_slug|
      artefact_for_slug(artefact_slug, options[:artefact])
    end
  )

  endpoint = "#{CONTENT_API_ENDPOINT}/with_tag.json"
  resp = { status: 200, body: body.to_json, headers: {} }

  unless draft
    stub_request(:get, endpoint)
      .with(:query => { tag_type => slug })
      .to_return(resp)

    if tag_type == "section"
      stub_request(:get, endpoint)
        .with(:query => { "tag" => slug })
        .to_return(resp)
    end
  end

  stub_request(:get, endpoint)
    .with(:query => { tag_type => slug, "draft" => "true" })
    .to_return(resp)

  if tag_type == "section"
    stub_request(:get, endpoint)
      .with(:query => { "tag" => slug, "draft" => "true" })
      .to_return(resp)
  end

  sort_orders = ["alphabetical", "curated"]
  sort_orders.each do |order|
    unless draft
      stub_request(:get, endpoint)
        .with(:query => { tag_type => slug, "sort" => order })
        .to_return(resp)

      if tag_type == "section"
        stub_request(:get, endpoint)
          .with(:query => { "tag" => slug, "sort" => order })
          .to_return(resp)
      end
    end

    stub_request(:get, endpoint)
      .with(:query => { tag_type => slug, "sort" => order, "draft" => "true" })
      .to_return(resp)

    if tag_type == "section"
      stub_request(:get, endpoint)
        .with(:query => { "tag" => slug, "sort" => order, "draft" => "true" })
        .to_return(resp)
    end
  end
end

#content_api_has_business_support_scheme(scheme, facets) ⇒ Object



463
464
465
# File 'lib/gds_api/test_helpers/content_api.rb', line 463

def content_api_has_business_support_scheme(scheme, facets)
  api_has_business_support(scheme, facets)
end

#content_api_has_child_tags(tag_type, parent_slug_or_hash, child_tag_ids) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gds_api/test_helpers/content_api.rb', line 140

def content_api_has_child_tags(tag_type, parent_slug_or_hash, child_tag_ids)
  parent_tag = tag_hash(parent_slug_or_hash, tag_type)
  child_tags = child_tag_ids.map { |id|
    tag_hash(id, tag_type).merge(parent: parent_tag)
  }
  body = plural_response_base.merge(
    "results" => child_tags.map { |s| tag_result(s, tag_type) }
  )
  url = "#{CONTENT_API_ENDPOINT}/tags.json?type=#{tag_type}&parent_id=#{CGI.escape(parent_tag[:slug])}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_default_business_support_schemesObject



467
468
469
470
471
472
473
474
475
476
477
# File 'lib/gds_api/test_helpers/content_api.rb', line 467

def content_api_has_default_business_support_schemes
  empty_results = {
    "_response_info" => {"status" => "ok"},
    "description" => "Business Support Schemes!",
    "total" => 0, "startIndex" => 1, "pageSize" => 0, "currentPage" => 1, "pages" => 1,
    "results" => []
  }

  stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).
    to_return(:body => empty_results.to_json)
end

#content_api_has_draft_and_live_tags(options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gds_api/test_helpers/content_api.rb', line 92

def content_api_has_draft_and_live_tags(options = {})
  type = options.fetch(:type)
  live_tags = options.fetch(:live).map { |tag| tag_result(tag, type, state: 'live') }
  draft_tags = options.fetch(:draft).map { |tag| tag_result(tag, type, state: 'draft') }

  body = plural_response_base.merge("results" => live_tags)
  stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
    .with(query: hash_including({"type" => type}))
    .to_return(status: 200, body: body.to_json, headers: {})

  body = plural_response_base.merge("results" => (live_tags + draft_tags))
  stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
    .with(query: hash_including({"type" => type, "draft" => "true"}))
    .to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_grouped_artefacts_with_a_tag(tag_type, tag_id, group_by, grouped_artefact_slugs = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gds_api/test_helpers/content_api.rb', line 246

def content_api_has_grouped_artefacts_with_a_tag(tag_type, tag_id, group_by, grouped_artefact_slugs={})
  body = plural_response_base.merge(
    "grouped_results" => grouped_artefact_slugs.map {|name,artefact_slugs|
      {
        "name" => name,
        "formats" => [name.downcase],
        "items" => artefact_slugs.map {|artefact_slug|
          artefact_for_slug(artefact_slug)
        }
      }
    }
  )

  endpoint = "#{CONTENT_API_ENDPOINT}/with_tag.json"
  stub_request(:get, endpoint)
    .with(:query => { tag_type => tag_id, "group_by" => group_by })
    .to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_licence(details) ⇒ Object



528
529
530
531
# File 'lib/gds_api/test_helpers/content_api.rb', line 528

def content_api_has_licence(details)
  raise "Need a licence identifier" if details[:licence_identifier].nil?
  @stubbed_content_api_licences << details
end

#content_api_has_root_sections(slugs_or_sections) ⇒ Object

Legacy section test helpers

Use of these should be retired in favour of the other test helpers in this module which work with any tag type.



20
21
22
# File 'lib/gds_api/test_helpers/content_api.rb', line 20

def content_api_has_root_sections(slugs_or_sections)
  content_api_has_root_tags("section", slugs_or_sections)
end

#content_api_has_root_tags(tag_type, slugs_or_tags) ⇒ Object

Takes an array of slugs, or hashes with section details (including a slug). Will stub out content_api calls for tags of type section to return these sections



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gds_api/test_helpers/content_api.rb', line 47

def content_api_has_root_tags(tag_type, slugs_or_tags)
  body = plural_response_base.merge(
    "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
  )
  urls = ["type=#{tag_type}", "root_sections=true&type=#{tag_type}"].map { |q|
    "#{CONTENT_API_ENDPOINT}/tags.json?#{q}"
  }
  urls.each do |url|
    stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
  end
end

#content_api_has_section(slug_or_hash, parent_slug = nil) ⇒ Object



24
25
26
# File 'lib/gds_api/test_helpers/content_api.rb', line 24

def content_api_has_section(slug_or_hash, parent_slug=nil)
  content_api_has_tag("section", slug_or_hash, parent_slug)
end

#content_api_has_sorted_artefacts_with_a_tag(tag_type, slug, sort_order, artefact_slugs = []) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/gds_api/test_helpers/content_api.rb', line 233

def content_api_has_sorted_artefacts_with_a_tag(tag_type, slug, sort_order, artefact_slugs=[])
  body = plural_response_base.merge(
    "results" => artefact_slugs.map do |artefact_slug|
      artefact_for_slug(artefact_slug)
    end
  )

  endpoint = "#{CONTENT_API_ENDPOINT}/with_tag.json"
  stub_request(:get, endpoint)
    .with(:query => { tag_type => slug, "sort" => sort_order })
    .to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_sorted_child_tags(tag_type, parent_slug_or_hash, sort_order, child_tag_ids) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gds_api/test_helpers/content_api.rb', line 152

def content_api_has_sorted_child_tags(tag_type, parent_slug_or_hash, sort_order, child_tag_ids)
  parent_tag = tag_hash(parent_slug_or_hash, tag_type)
  child_tags = child_tag_ids.map { |id|
    tag_hash(id, tag_type).merge(parent: parent_tag)
  }
  body = plural_response_base.merge(
    "results" => child_tags.map { |s| tag_result(s, tag_type) }
  )

  url = "#{CONTENT_API_ENDPOINT}/tags.json?parent_id=#{CGI.escape(parent_tag[:slug])}&sort=#{sort_order}&type=#{tag_type}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_sorted_tags(tag_type, sort_order, slugs_or_tags) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/gds_api/test_helpers/content_api.rb', line 130

def content_api_has_sorted_tags(tag_type, sort_order, slugs_or_tags)
  body = plural_response_base.merge(
    "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
  )

  stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
    .with(query: hash_including({"type" => tag_type, "sort" => sort_order}))
    .to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_subsections(parent_slug_or_hash, subsection_slugs) ⇒ Object



28
29
30
# File 'lib/gds_api/test_helpers/content_api.rb', line 28

def content_api_has_subsections(parent_slug_or_hash, subsection_slugs)
  content_api_has_child_tags("section", parent_slug_or_hash, subsection_slugs)
end

#content_api_has_tag(tag_type, slug_or_hash, parent_tag_id = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gds_api/test_helpers/content_api.rb', line 59

def content_api_has_tag(tag_type, slug_or_hash, parent_tag_id=nil)
  tag = tag_hash(slug_or_hash, tag_type).merge(parent: parent_tag_id)
  body = tag_result(tag)

  urls = ["#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag_type)}/#{CGI.escape(tag[:slug])}.json"]

  if tag_type == "section"
    urls << "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag[:slug])}.json"
  end

  urls.each do |url|
    stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
  end
end

#content_api_has_tags(tag_type, slugs_or_tags) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/gds_api/test_helpers/content_api.rb', line 120

def content_api_has_tags(tag_type, slugs_or_tags)
  body = plural_response_base.merge(
    "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
  )

  stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
    .with(query: hash_including({"type" => tag_type}))
    .to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug)) ⇒ Object



269
270
271
272
273
274
# File 'lib/gds_api/test_helpers/content_api.rb', line 269

def content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug))
  ArtefactStub.new(slug)
      .with_response_body(body)
      .with_query_parameters(edition: edition)
      .stub
end

#content_api_licence_hash(licence_identifier, options = {}) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/gds_api/test_helpers/content_api.rb', line 479

def content_api_licence_hash(licence_identifier, options = {})
  details = {
    title: "Publisher title",
    slug: 'licence-slug',
    licence_short_description: "Short description of licence"
  }
  details.merge!(options)

  {
    "title" => details[:title],
    "id" => "http://example.org/#{details[:slug]}.json",
    "web_url" =>  "http://www.test.gov.uk/#{details[:slug]}",
    "format" => "licence",
    "details" => {
      "need_ids" => [],
      "business_proposition" => false,
      "alternative_title" => nil,
      "overview" => nil,
      "will_continue_on" => nil,
      "continuation_link" => nil,
      "licence_identifier" => licence_identifier,
      "licence_short_description" => details[:licence_short_description],
      "licence_overview" => nil,
      "updated_at" => "2012-10-06T12:00:05+01:00"
    },
    "tags" => [],
    "related" => []
  }
end

#setup_content_api_business_support_schemes_stubsObject



459
460
461
# File 'lib/gds_api/test_helpers/content_api.rb', line 459

def setup_content_api_business_support_schemes_stubs
  setup_business_support_stubs(CONTENT_API_ENDPOINT, 'business_support_schemes')
end

#setup_content_api_licences_stubsObject



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/gds_api/test_helpers/content_api.rb', line 509

def setup_content_api_licences_stubs
  @stubbed_content_api_licences = []
  stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/licences}).to_return do |request|
    if request.uri.query_values and request.uri.query_values["ids"]
      ids = request.uri.query_values["ids"].split(',')
      valid_licences = @stubbed_content_api_licences.select { |l| ids.include? l[:licence_identifier] }
      {
        :body => {
          'results' => valid_licences.map { |licence|
            content_api_licence_hash(licence[:licence_identifier], licence)
          }
        }.to_json
      }
    else
      {:body => {'results' => []}.to_json}
    end
  end
end

#simple_tag_type_pluralizer(s) ⇒ Object

This is a nasty hack to get around the pluralized slugs in tag paths without having to require ActiveSupport



449
450
451
452
453
454
455
456
457
# File 'lib/gds_api/test_helpers/content_api.rb', line 449

def simple_tag_type_pluralizer(s)
  case s
  when /o\Z/ then s.sub(/o\Z/, "es")
  when /y\Z/ then s.sub(/y\Z/, "ies")
  when /ss\Z/ then s.sub(/ss\Z/, "sses")
  else
    "#{s}s"
  end
end

#stub_content_api_default_artefactObject

Stub requests, and then dynamically generate a response based on the slug in the request



309
310
311
312
313
314
# File 'lib/gds_api/test_helpers/content_api.rb', line 309

def stub_content_api_default_artefact
  stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/[a-z0-9-]+\.json}).to_return { |request|
    slug = request.uri.path.split('/').last.chomp('.json')
    {:body => artefact_for_slug(slug).to_json}
  }
end

#tag_for_slug(slug, tag_type, parent_slug = nil) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/gds_api/test_helpers/content_api.rb', line 404

def tag_for_slug(slug, tag_type, parent_slug=nil)
  parent = if parent_slug
    tag_for_slug(parent_slug, tag_type)
  end

  tag_result(slug: slug, type: tag_type, parent: parent)
end

#tag_hash(slug_or_hash, tag_type = "section") ⇒ Object

Construct a tag hash suitable for passing into tag_result



413
414
415
416
417
418
419
# File 'lib/gds_api/test_helpers/content_api.rb', line 413

def tag_hash(slug_or_hash, tag_type = "section")
  if slug_or_hash.is_a?(Hash)
    slug_or_hash
  else
    { slug: slug_or_hash, type: tag_type }
  end
end

#tag_result(slug_or_hash, tag_type = nil, options = {}) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/gds_api/test_helpers/content_api.rb', line 421

def tag_result(slug_or_hash, tag_type = nil, options = {})
  tag = tag_hash(slug_or_hash, tag_type)

  parent = tag_result(tag[:parent]) if tag[:parent]
  pluralized_tag_type = simple_tag_type_pluralizer(tag[:type])

  {
    "id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(pluralized_tag_type)}/#{CGI.escape(tag[:slug])}.json",
    "slug" => tag[:slug],
    "web_url" => "http://www.test.gov.uk/browse/#{tag[:slug]}",
    "title" => tag[:title] || titleize_slug(tag[:slug].split("/").last),
    "details" => {
      "type" => tag[:type],
      "description" => tag[:description] || "#{tag[:slug]} description",
      "short_description" => tag[:short_description] || "#{tag[:slug]} short description"
    },
    "parent" => parent,
    "content_with_tag" => {
      "id" => "#{CONTENT_API_ENDPOINT}/with_tag.json?tag=#{CGI.escape(tag[:slug])}",
      "web_url" => "http://www.test.gov.uk/browse/#{tag[:slug]}"
    },
    "state" => options[:state]
  }
end