Module: GdsApi::TestHelpers::PublishingApi

Includes:
ContentItemHelpers, IntentHelpers
Defined in:
lib/gds_api/test_helpers/publishing_api.rb

Constant Summary collapse

PUBLISHING_API_ENDPOINT =
Plek.current.find('publishing-api')

Instance Method Summary collapse

Methods included from IntentHelpers

#intent_for_base_path

Methods included from ContentItemHelpers

#content_item_for_base_path, #gone_content_item_for_base_path, #titleize_base_path

Instance Method Details

#assert_publishing_api_put(url, attributes_or_matcher = {}, times = 1) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 34

def assert_publishing_api_put(url, attributes_or_matcher = {}, times = 1)
  if attributes_or_matcher.is_a?(Hash)
    matcher = attributes_or_matcher.empty? ? nil : request_json_matching(attributes_or_matcher)
  else
    matcher = attributes_or_matcher
  end

  if matcher
    assert_requested(:put, url, times: times, &matcher)
  else
    assert_requested(:put, url, times: times)
  end
end

#assert_publishing_api_put_intent(base_path, attributes_or_matcher = {}, times = 1) ⇒ Object


29
30
31
32
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 29

def assert_publishing_api_put_intent(base_path, attributes_or_matcher = {}, times = 1)
  url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path
  assert_publishing_api_put(url, attributes_or_matcher, times)
end

#publishing_api_has_path_reservation_for(path, publishing_app) ⇒ Object


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

def publishing_api_has_path_reservation_for(path, publishing_app)
  data = publishing_api_path_data_for(path, "publishing_app" => publishing_app)
  error_data = data.merge("errors" => { "path" => ["is already reserved by the #{publishing_app} application"] })

  stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{path}").
            to_return(status: 422, body: error_data.to_json,
                      headers: { content_type: "application/json" })

  stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{path}").
    with(body: { "publishing_app" => publishing_app }).
    to_return(status: 200,
              headers: { content_type: "application/json" },
              body: data.to_json)
end

#publishing_api_isnt_availableObject


62
63
64
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 62

def publishing_api_isnt_available
  stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(status: 503)
end

#publishing_api_returns_path_reservation_validation_error_for(path, error_details = nil) ⇒ Object


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

def publishing_api_returns_path_reservation_validation_error_for(path, error_details = nil)
  error_details ||= { "base" => ["computer says no"] }
  error_data = publishing_api_path_data_for(path).merge("errors" => error_details)

  stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{path}").
    to_return(status: 422, body: error_data.to_json, headers: { content_type: "application/json" })
end

#request_json_including(required_attributes) ⇒ Object


55
56
57
58
59
60
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 55

def request_json_including(required_attributes)
  ->(request) do
    data = JSON.parse(request.body)
    values_match_recursively(required_attributes, data)
  end
end

#request_json_matching(required_attributes) ⇒ Object


48
49
50
51
52
53
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 48

def request_json_matching(required_attributes)
  ->(request) do
    data = JSON.parse(request.body)
    required_attributes.to_a.all? { |key, value| data[key.to_s] == value }
  end
end

#stub_default_publishing_api_path_reservationObject


66
67
68
69
70
71
72
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 66

def stub_default_publishing_api_path_reservation
  stub_request(:put, %r[\A#{PUBLISHING_API_ENDPOINT}/paths/]).to_return { |request|
    base_path = request.uri.path.sub(%r{\A/paths/}, "")
    { status: 200, headers: { content_type: "application/json" },
      body: publishing_api_path_data_for(base_path).to_json }
  }
end

#stub_default_publishing_api_put_intentObject


25
26
27
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 25

def stub_default_publishing_api_put_intent
  stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/publish-intent})
end

#stub_publishing_api_destroy_intent(base_path) ⇒ Object


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

def stub_publishing_api_destroy_intent(base_path)
  url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path
  stub_request(:delete, url).to_return(status: 200, body: '{}', headers: { "Content-Type" => "application/json; charset=utf-8" })
end

#stub_publishing_api_put_intent(base_path, body = intent_for_publishing_api(base_path)) ⇒ Object


14
15
16
17
18
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 14

def stub_publishing_api_put_intent(base_path, body = intent_for_publishing_api(base_path))
  url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path
  body = body.to_json unless body.is_a?(String)
  stub_request(:put, url).with(body: body).to_return(status: 200, body: '{}', headers: { "Content-Type" => "application/json; charset=utf-8" })
end