Module: GdsApi::TestHelpers::AssetManager

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

Constant Summary collapse

ASSET_MANAGER_ENDPOINT =
Plek.find("asset-manager")

Instance Method Summary collapse

Instance Method Details

#stub_any_asset_manager_callObject



6
7
8
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 6

def stub_any_asset_manager_call
  stub_request(:any, %r{\A#{ASSET_MANAGER_ENDPOINT}}).to_return(status: 200)
end

#stub_asset_manager_delete_asset(asset_id, body = {}) ⇒ Object



90
91
92
93
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 90

def stub_asset_manager_delete_asset(asset_id, body = {})
  stub_request(:delete, "#{ASSET_MANAGER_ENDPOINT}/assets/#{asset_id}")
    .to_return(body: body.to_json, status: 200)
end

#stub_asset_manager_delete_asset_failure(asset_id) ⇒ Object



100
101
102
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 100

def stub_asset_manager_delete_asset_failure(asset_id)
  stub_request(:delete, "#{ASSET_MANAGER_ENDPOINT}/assets/#{asset_id}").to_return(status: 500)
end

#stub_asset_manager_delete_asset_missing(asset_id) ⇒ Object



95
96
97
98
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 95

def stub_asset_manager_delete_asset_missing(asset_id)
  stub_request(:delete, "#{ASSET_MANAGER_ENDPOINT}/assets/#{asset_id}")
    .to_return(status: 404)
end

#stub_asset_manager_deletes_any_asset(body = {}) ⇒ Object



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

def stub_asset_manager_deletes_any_asset(body = {})
  stub_request(:delete, %r{\A#{ASSET_MANAGER_ENDPOINT}/assets})
    .to_return(body: body.to_json, status: 200)
end

#stub_asset_manager_does_not_have_an_asset(id) ⇒ Object



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

def stub_asset_manager_does_not_have_an_asset(id)
  response = {
    "_response_info" => { "status" => "not found" },
  }

  stub_request(:any, "#{ASSET_MANAGER_ENDPOINT}/assets/#{id}")
    .to_return(body: response.to_json, status: 404)
end

#stub_asset_manager_has_a_whitehall_media_asset(legacy_url_path, content) ⇒ Object



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

def stub_asset_manager_has_a_whitehall_media_asset(legacy_url_path, content)
  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/#{legacy_url_path}")
    .to_return(body: content, status: 200)
end

#stub_asset_manager_has_an_asset(id, atts, filename = "") ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 24

def stub_asset_manager_has_an_asset(id, atts, filename = "")
  response = atts.merge("_response_info" => { "status" => "ok" })

  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/assets/#{id}")
    .to_return(body: response.to_json, status: 200)

  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/media/#{id}/#{filename}")
    .to_return(body: "Some file content", status: 200)
end

#stub_asset_manager_isnt_availableObject



10
11
12
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 10

def stub_asset_manager_isnt_available
  stub_request(:any, %r{\A#{ASSET_MANAGER_ENDPOINT}}).to_return(status: 503)
end

#stub_asset_manager_receives_an_asset(response_url = {}) ⇒ Object

This can take a string of an exact url or a hash of options

with a string: ‘stub_asset_manager_receives_an_asset(“”)`

with a hash: ‘stub_asset_manager_receives_an_asset(id: “20d04259-e3ae-4f71-8157-e6c843096e96”, filename: “file.jpg”)` which would return a file url of “

with no argument

‘stub_asset_manager_receives_an_asset` which would return a file url of “asset-manager/media/0053adbf-0737-4923-9d8a-8180f2c723af/0d19136c4a94f07



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

def stub_asset_manager_receives_an_asset(response_url = {})
  stub_request(:post, "#{ASSET_MANAGER_ENDPOINT}/assets").to_return do
    if response_url.is_a?(String)
      file_url = response_url
    else
      options = {
        id: SecureRandom.uuid,
        filename: SecureRandom.hex(8),
      }.merge(response_url)

      file_url = "#{ASSET_MANAGER_ENDPOINT}/media/#{options[:id]}/#{options[:filename]}"
    end
    { body: { file_url: }.to_json, status: 200 }
  end
end

#stub_asset_manager_update_asset(asset_id, body = {}) ⇒ Object



81
82
83
84
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 81

def stub_asset_manager_update_asset(asset_id, body = {})
  stub_request(:put, "#{ASSET_MANAGER_ENDPOINT}/assets/#{asset_id}")
    .to_return(body: body.to_json, status: 200)
end

#stub_asset_manager_update_asset_failure(asset_id) ⇒ Object



86
87
88
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 86

def stub_asset_manager_update_asset_failure(asset_id)
  stub_request(:put, "#{ASSET_MANAGER_ENDPOINT}/assets/#{asset_id}").to_return(status: 500)
end

#stub_asset_manager_updates_any_asset(body = {}) ⇒ Object



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

def stub_asset_manager_updates_any_asset(body = {})
  stub_request(:put, %r{\A#{ASSET_MANAGER_ENDPOINT}/assets})
    .to_return(body: body.to_json, status: 200)
end

#stub_asset_manager_upload_failureObject



77
78
79
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 77

def stub_asset_manager_upload_failure
  stub_request(:post, "#{ASSET_MANAGER_ENDPOINT}/assets").to_return(status: 500)
end