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, #titleize_base_path

Instance Method Details

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



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

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_draft_item(base_path, attributes_or_matcher = {}, times = 1) ⇒ Object



51
52
53
54
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 51

def assert_publishing_api_put_draft_item(base_path, attributes_or_matcher = {}, times = 1)
  url = PUBLISHING_API_ENDPOINT + "/draft-content" + base_path
  assert_publishing_api_put(url, attributes_or_matcher, times)
end

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



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

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

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



46
47
48
49
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 46

def assert_publishing_api_put_item(base_path, attributes_or_matcher = {}, times = 1)
  url = PUBLISHING_API_ENDPOINT + "/content" + base_path
  assert_publishing_api_put(url, attributes_or_matcher, times)
end

#publishing_api_has_path_reservation_for(path, publishing_app) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 101

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



89
90
91
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 89

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



118
119
120
121
122
123
124
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 118

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



82
83
84
85
86
87
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 82

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



75
76
77
78
79
80
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 75

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



93
94
95
96
97
98
99
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 93

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_putObject



34
35
36
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 34

def stub_default_publishing_api_put()
  stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/content})
end

#stub_default_publishing_api_put_draftObject



38
39
40
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 38

def stub_default_publishing_api_put_draft()
  stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/draft-content})
end

#stub_default_publishing_api_put_intentObject



42
43
44
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 42

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



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

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_draft_item(base_path, body = content_item_for_publishing_api(base_path)) ⇒ Object



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

def stub_publishing_api_put_draft_item(base_path, body = content_item_for_publishing_api(base_path))
  stub_publishing_api_put_item(base_path, body, '/draft-content')
end

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



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

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

#stub_publishing_api_put_item(base_path, body = content_item_for_publishing_api(base_path), resource_path = '/content') ⇒ Object



18
19
20
21
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 18

def stub_publishing_api_put_item(base_path, body = content_item_for_publishing_api(base_path), resource_path = '/content')
  url = PUBLISHING_API_ENDPOINT + resource_path + base_path
  stub_request(:put, url).with(body: body).to_return(status: 200, body: '{}', headers: {"Content-Type" => "application/json; charset=utf-8"})
end