<commit_before>require 'test_helper'
require 'gds_api/asset_manager'
require 'gds_api/test_helpers/asset_manager'
require 'json'

describe GdsApi::AssetManager do
  include GdsApi::TestHelpers::AssetManager

  let(:base_api_url) { Plek.current.find('asset-manager') }
  let(:api) { GdsApi::AssetManager.new(base_api_url) }

  let(:file_fixture) { load_fixture_file("hello.txt") }

  let(:asset_url) { [base_api_url, "assets", asset_id].join("/") }
  let(:asset_id) { "new-asset-id" }

  let(:asset_manager_response) {
    {
      asset: {
        id: asset_url,
      }
    }
  }

  it "creates an asset with a file" do
    req = stub_request(:post, "#{base_api_url}/assets").
      with(:body => %r{Content\-Disposition: form\-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent\-Type: text/plain}).
      to_return(:body => JSON.dump(asset_manager_response), :status => 201)

    response = api.create_asset(:file => file_fixture)

    assert_equal asset_url, response.asset.id
    assert_requested(req)
  end

  it "returns nil when an asset does not exist" do
    asset_manager_does_not_have_an_asset("not-really-here")

    assert_nil api.asset("not-really-here")
  end

  describe "an asset exists" do
    before do
      asset_manager_has_an_asset(
        asset_id,
        "name" => "photo.jpg",
        "content_type" => "image/jpeg",
        "file_url" => "http://fooey.gov.uk/media/photo.jpg",
      )
    end

    let(:asset_id) { "test-id" }

    it "updates an asset with a file" do
      req = stub_request(:put, "#{base_api_url}/assets/test-id").
        to_return(:body => JSON.dump(asset_manager_response), :status => 200)

      response = api.update_asset(asset_id, :file => file_fixture)

      assert_equal "#{base_api_url}/assets/#{asset_id}", response.asset.id
      assert_requested(req)
    end

    it "retrieves an asset" do
      asset = api.asset(asset_id)

      assert_equal "photo.jpg", asset.name
      assert_equal "image/jpeg", asset.content_type<commit_msg>Fix deprecation warning in asset_manager_test<commit_after>require 'test_helper'
require 'gds_api/asset_manager'
require 'gds_api/test_helpers/asset_manager'
require 'json'

describe GdsApi::AssetManager do
  include GdsApi::TestHelpers::AssetManager

  let(:base_api_url) { Plek.current.find('asset-manager') }
  let(:api) { GdsApi::AssetManager.new(base_api_url) }

  let(:file_fixture) { load_fixture_file("hello.txt") }

  let(:asset_url) { [base_api_url, "assets", asset_id].join("/") }
  let(:asset_id) { "new-asset-id" }

  let(:asset_manager_response) {
    {
      asset: {
        id: asset_url,
      }
    }
  }

  before do
    GdsApi.config.always_raise_for_not_found = true
  end

  after do
    GdsApi.config.always_raise_for_not_found = false
  end

  it "creates an asset with a file" do
    req = stub_request(:post, "#{base_api_url}/assets").
      with(:body => %r{Content\-Disposition: form\-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent\-Type: text/plain}).
      to_return(:body => JSON.dump(asset_manager_response), :status => 201)

    response = api.create_asset(:file => file_fixture)

    assert_equal asset_url, response.asset.id
    assert_requested(req)
  end

  it "returns not found when an asset does not exist" do
    asset_manager_does_not_have_an_asset("not-really-here")

    assert_raises GdsApi::HTTPNotFound do
      api.asset("not-really-here")
    end
  end

  describe "an asset exists" do
    before do
      asset_manager_has_an_asset(
        asset_id,
        "name" => "photo.jpg",
        "content_type" => "image/jpeg",
        "file_url" => "http://fooey.gov.uk/media/photo.jpg",
      )
    end

    let(:asset_id) { "test-id" }

    it "updates an asset with a file" do
      req = stub_request(:put, "#{base_api_url}/assets/test-id").
        to_return(:body => JSON.dump(asset_manager_response), :status => 200)

      response = api.update_asset(asset_id, :file => file_fixture)

      assert_equal "#{base_api_url}/assets/#{asset_id}", response.asset.id
      assert_requested(req)
    end

    it "retrieves an asset" do
      asset = api.asset(asset_id)

      assert_equal "photo.jpg", asset.name
      assert_equal "image/jpeg", asset.content_type
{'fraction_non_alphanumeric': 0.1380941120832348, 'fraction_numerical': 0.0028375502482856467, 'mean_word_length': 3.466737064413939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '634494', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1443, 'n_words': 271}