<commit_before>require 'test_helper'

class ImageUploaderTest < ActiveSupport::TestCase
  include ActionDispatch::TestProcess

  setup do
    ImageUploader.enable_processing = true
  end

  teardown do
    ImageUploader.enable_processing = false
  end

  test 'uses the asset manager and quarantined file storage engine' do
    assert_equal Whitehall::AssetManagerAndQuarantinedFileStorage, ImageUploader.storage
  end

  test "should only allow JPG, GIF, PNG or SVG images" do
    uploader = ImageUploader.new
    assert_equal %w(jpg jpeg gif png svg), uploader.extension_whitelist
  end

  test "should store uploads in a directory that persists across deploys" do
    model = stub("AR Model", id: 1)
    uploader = ImageUploader.new(model, "mounted-as")
    assert_match %r[^system], uploader.store_dir
  end

  test "should create resized versions of a bitmap image" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    @uploader.store!(fixture_file_upload('minister-of-funk.960x640.jpg', 'image/jpg'))

    [[712, 480], [630, 420], [465, 310], [300, 195], [216, 140]].each do |(width, height)|
      assert_image_size [width, height], @uploader.send(:"s#{width}")
    end
  end

  test "stores the original svg only" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    @uploader.store!(fixture_file_upload('images/test-svg.svg', 'image/svg+xml'))

    [[712, 480], [630, 420], [465, 310], [300, 195], [216, 140]].each do |(width, _height)|
      assert_nil @uploader.send(:url, :"s#{width}")
    end
  end

private

  def assert_image_size(dimensions, uploader_version)
    width, height = dimensions
    image = MiniMagick::Image.open(uploader_version.path)
    assert_equal width, image[:width], "#{dimensions.join('x')} image version should be #{width}px wide, but was #{image[:width]}"<commit_msg>Test that ImageUploader stores files in Asset Manager<commit_after>require 'test_helper'

class ImageUploaderTest < ActiveSupport::TestCase
  include ActionDispatch::TestProcess

  setup do
    ImageUploader.enable_processing = true
  end

  teardown do
    ImageUploader.enable_processing = false
  end

  test 'uses the asset manager and quarantined file storage engine' do
    assert_equal Whitehall::AssetManagerAndQuarantinedFileStorage, ImageUploader.storage
  end

  test "should only allow JPG, GIF, PNG or SVG images" do
    uploader = ImageUploader.new
    assert_equal %w(jpg jpeg gif png svg), uploader.extension_whitelist
  end

  test "should store uploads in a directory that persists across deploys" do
    model = stub("AR Model", id: 1)
    uploader = ImageUploader.new(model, "mounted-as")
    assert_match %r[^system], uploader.store_dir
  end

  test "should create resized versions of a bitmap image" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    @uploader.store!(fixture_file_upload('minister-of-funk.960x640.jpg', 'image/jpg'))

    [[712, 480], [630, 420], [465, 310], [300, 195], [216, 140]].each do |(width, height)|
      assert_image_size [width, height], @uploader.send(:"s#{width}")
    end
  end

  test "should store all the versions of a bitmap image in asset manager" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    Services.asset_manager.stubs(:create_whitehall_asset)
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s960_minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s712_minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s630_minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s465_minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s300_minister-of-funk.960x640.jpg/))
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/s216_minister-of-funk.960x640.jpg/))

    @uploader.store!(fixture_file_upload('minister-of-funk.960x640.jpg', 'image/jpg'))
  end

  test "stores the original svg only" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    @uploader.store!(fixture_file_upload('images/test-svg.svg', 'image/svg+xml'))

    [[712, 480], [630, 420], [465, 310], [300, 195], [216, 140]].each do |(width, _height)|
      assert_nil @uploader.send(:url, :"s#{width}")
    end
  end

  test "should store the original version only of a svg image in asset manager" do
    model = stub("AR Model", id: 1)
    @uploader = ImageUploader.new(model, "mounted-as")

    Services.asset_manager.stubs(:create_whitehall_asset)
    Services.asset_manager.expects(:create_whitehall_asset).with(file_and_legacy_url_path_matching(/test-svg.svg/))

    @uploader.store!(fixture_file_upload('images/test-svg.svg', 'image/svg+xml'))
  end

private

  def assert_image_size(dimensions, uploader_version)
    width, height = dimensions
    image = MiniMagick::Image.open(uploader_version.path)
    assert_equal width, image[:width], "#{dimensions.join('x')} image version should be #{width}px wide, but was #{image[:width]}"
{'fraction_non_alphanumeric': 0.12389699261660364, 'fraction_numerical': 0.03709706464973888, 'mean_word_length': 4.834033613445378, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '342799', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1833, 'n_words': 413}