Module: GdsApi::TestHelpers::ContentApi

Defined in:
lib/gds_api/test_helpers/content_api.rb

Constant Summary collapse

CONTENT_API_ENDPOINT =
'https://contentapi.test.alphagov.co.uk'

Instance Method Summary collapse

Instance Method Details

#artefact_for_slug(slug) ⇒ Object



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

def artefact_for_slug(slug)
  singular_response_base.merge(
    "title" => titleize_slug(slug),
    "format" => "guide",
    "id" => "http://contentapi.test.gov.uk/#{slug}.json",
    "web_url" => "http://frontend.test.gov.uk/#{slug}",
    "details" => {
      "need_id" => "1234",
      "business_proposition" => false, # To be removed and replaced with proposition tags
      "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



139
140
141
142
143
# File 'lib/gds_api/test_helpers/content_api.rb', line 139

def artefact_for_slug_in_a_section(slug, section_slug)
  artefact = artefact_for_slug(slug)
  artefact["tags"] << tag_for_slug(section_slug, "section")
  artefact
end

#artefact_for_slug_in_a_subsection(slug, subsection_slug) ⇒ Object



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

def artefact_for_slug_in_a_subsection(slug, subsection_slug)
  artefact = artefact_for_slug(slug)

  # 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
  subsection_slug.split('/').inject(nil) do |last_section, subsection|
    subsection = [last_section, subsection].join('/') if last_section
    section = tag_for_slug(subsection, "section")
    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 = section.merge("parent" => tag_tree)
    else
      tag_tree = section
    end
    subsection
  end
  artefact["tags"] << tag_tree
  artefact
end


171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gds_api/test_helpers/content_api.rb', line 171

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" => "https://contentapi.test.alphagov.co.uk/#{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



88
89
90
91
92
93
94
95
96
# File 'lib/gds_api/test_helpers/content_api.rb', line 88

def content_api_does_not_have_an_artefact(slug)
  body = {
    "_response_info" => {
      "status" => "not found"
    }
  }
  url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
  stub_request(:get, url).to_return(status: 404, body: body.to_json, headers: {})
end

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



73
74
75
76
# File 'lib/gds_api/test_helpers/content_api.rb', line 73

def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
  url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

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



83
84
85
86
# File 'lib/gds_api/test_helpers/content_api.rb', line 83

def content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug))
  url = "#{CONTENT_API_ENDPOINT}/#{slug}.json?snac=#{snac}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_artefacts_in_a_section(slug, artefact_slugs = []) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/gds_api/test_helpers/content_api.rb', line 39

def content_api_has_artefacts_in_a_section(slug, artefact_slugs=[])
  body = plural_response_base.merge(
    "results" => artefact_slugs.map do |artefact_slug|
      artefact_for_slug(artefact_slug)
    end
  )
  url = "https://contentapi.test.alphagov.co.uk/with_tag.json?sort=alphabetical&tag=#{CGI.escape(slug)}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_business_support_scheme(scheme) ⇒ Object



215
216
217
218
# File 'lib/gds_api/test_helpers/content_api.rb', line 215

def content_api_has_business_support_scheme(scheme)
  raise "Need a licence identifier" if scheme["details"]["business_support_identifier"].nil?
  @stubbed_content_api_business_support_schemes << scheme
end

#content_api_has_root_sections(slugs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gds_api/test_helpers/content_api.rb', line 9

def content_api_has_root_sections(slugs)
  body = plural_response_base.merge(
    "results" => slugs.map do |slug|
      {
        "id" => "http://contentapi.test.gov.uk/tags/#{CGI.escape(slug)}.json",
        "web_url" => nil,
        "title" => titleize_slug(slug),
        "details" => {
          "type" => "section",
          "description" => "#{slug} description"
        },
        "parent" => nil,
        "content_with_tag" => {
          "id" => "http://contentapi.test.gov.uk/with_tag.json?tag=#{CGI.escape(slug)}",
          "web_url" => "http://www.test.gov.uk/browse/#{slug}"
        }
      }
    end
  )
  ["#{CONTENT_API_ENDPOINT}/tags.json?type=section", "#{CONTENT_API_ENDPOINT}/tags.json?root_sections=true&type=section"].each do |url|
    stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
  end
end

#content_api_has_section(slug, parent_slug = nil) ⇒ Object



33
34
35
36
37
# File 'lib/gds_api/test_helpers/content_api.rb', line 33

def content_api_has_section(slug, parent_slug=nil)
  body = tag_for_slug(slug, "section", parent_slug)
  url = "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(slug)}.json"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#content_api_has_subsections(parent_slug, subsection_slugs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gds_api/test_helpers/content_api.rb', line 49

def content_api_has_subsections(parent_slug, subsection_slugs)
  parent_section = tag_for_slug(parent_slug, "section")
  body = plural_response_base.merge(
    "results" => subsection_slugs.map do |slug|
      {
        "id" => "http://contentapi.test.gov.uk/tags/#{CGI.escape(slug)}.json",
        "web_url" => nil,
        "title" => titleize_slug(slug),
        "details" => {
          "type" => "section",
          "description" => "#{slug} description"
        },
        "parent" => parent_section,
        "content_with_tag" => {
          "id" => "http://contentapi.test.gov.uk/with_tag.json?tag=#{CGI.escape(slug)}",
          "web_url" => "http://www.test.gov.uk/browse/#{slug}"
        }
      }
    end
  )
  url = "#{CONTENT_API_ENDPOINT}/tags.json?type=section&parent_id=#{CGI.escape(parent_slug)}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

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



78
79
80
81
# File 'lib/gds_api/test_helpers/content_api.rb', line 78

def content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug))
  url = "#{CONTENT_API_ENDPOINT}/#{slug}.json?edition=#{edition}"
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
end

#setup_content_api_business_support_schemes_stubsObject



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gds_api/test_helpers/content_api.rb', line 202

def setup_content_api_business_support_schemes_stubs
  @stubbed_content_api_business_support_schemes = []
  stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).to_return do |request|
    if request.uri.query_values and request.uri.query_values["identifiers"]
      ids = request.uri.query_values["identifiers"].split(',')
      results = @stubbed_content_api_business_support_schemes.select {|bs| ids.include? bs["details"]["business_support_identifier"] }
    else
      results = []
    end
    {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json}
  end
end

#stub_content_api_default_artefactObject



98
99
100
101
102
103
# File 'lib/gds_api/test_helpers/content_api.rb', line 98

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gds_api/test_helpers/content_api.rb', line 184

def tag_for_slug(slug, tag_type, parent_slug=nil)
  parent = if parent_slug
    tag_for_slug(parent_slug, tag_type)
  end
  {
    "title" => titleize_slug(slug.split('/').last),
    "id" => "https://contentapi.test.alphagov.co.uk/tags/#{CGI.escape(slug)}.json",
    "details" => {
      "type" => tag_type
    },
    "content_with_tag" => {
      "id" => "https://contentapi.test.alphagov.co.uk/with_tag.json?tag=#{CGI.escape(slug)}",
      "web_url" => "https://www.test.gov.uk/browse/#{slug}",
    },
    "parent" => parent
  }
end