Module: GdsApi::TestHelpers::AssetManager

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

Constant Summary collapse

ASSET_MANAGER_ENDPOINT =
Plek.current.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



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

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



103
104
105
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 103

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_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_a_whitehall_asset(legacy_url_path) ⇒ Object



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

def stub_asset_manager_does_not_have_a_whitehall_asset(legacy_url_path)
  response = {
    "_response_info" => { "status" => "not found" },
  }

  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/whitehall_assets/#{legacy_url_path}")
    .to_return(body: response.to_json, status: 404)
end

#stub_asset_manager_does_not_have_an_asset(id) ⇒ Object



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

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_asset(legacy_url_path, atts) ⇒ Object



31
32
33
34
35
36
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 31

def stub_asset_manager_has_a_whitehall_asset(legacy_url_path, atts)
  response = atts.merge("_response_info" => { "status" => "ok" })

  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/whitehall_assets/#{legacy_url_path}")
    .to_return(body: response.to_json, status: 200)
end

#stub_asset_manager_has_an_asset(id, atts) ⇒ Object



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

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

  stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/assets/#{id}")
    .to_return(body: response.to_json, 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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gds_api/test_helpers/asset_manager.rb', line 69

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: file_url }.to_json, status: 200 }
  end
end

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



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

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



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

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



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

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