module GdsApi::TestHelpers::PublishingApi

def assert_publishing_api_put(url, attributes = {}, times = 1)

def assert_publishing_api_put(url, attributes = {}, times = 1)
  if attributes.empty?
    assert_requested(:put, url, times: times)
  else
    assert_requested(:put, url, times: times) do |req|
      data = JSON.parse(req.body)
      attributes.to_a.all? do |key, value|
        data[key.to_s] == value
      end
    end
  end
end

def assert_publishing_api_put_intent(base_path, attributes = {}, times = 1)

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

def assert_publishing_api_put_item(base_path, attributes = {}, times = 1)

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

def publishing_api_isnt_available

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

def stub_default_publishing_api_put()

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

def stub_default_publishing_api_put_intent()

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

def stub_publishing_api_destroy_intent(base_path)

def stub_publishing_api_destroy_intent(base_path)
  url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path
  response_body = {base_path: base_path}.to_json
  stub_request(:delete, url).to_return(status: 201, body: response_body)
end

def stub_publishing_api_put_intent(base_path, body = intent_for_base_path(base_path))

def stub_publishing_api_put_intent(base_path, body = intent_for_base_path(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: 201, body: body, headers: {})
end

def stub_publishing_api_put_item(base_path, body = content_item_for_base_path(base_path))

def stub_publishing_api_put_item(base_path, body = content_item_for_base_path(base_path))
  url = PUBLISHING_API_ENDPOINT + "/content" + base_path
  body = body.to_json unless body.is_a?(String)
  stub_request(:put, url).with(body: body).to_return(status: 201, body: body, headers: {})
end