1 # frozen_string_literal: true
5 class NormaliseAvatarsTest < ActiveSupport::TestCase
6 include ActiveJob::TestHelper
9 Rails.application.load_tasks if Rake::Task.tasks.empty?
10 Rake::Task["db:normalise_avatars"].reenable
13 def test_normalises_old_avatar
14 image = Vips::Image.black(1000, 600, :bands => 3).copy
15 image.set_type(GObject::GSTR_TYPE, "exif-ifd0-ImageDescription", "Somewhere secret")
17 original = attach_unnormalised(user, image.write_to_buffer(".jpg"), "IMG_1234.jpg", Time.utc(2026, 11, 19))
19 run_task("2026-11-20T00:00:00Z")
22 assert_not_equal original.id, user.avatar.blob.id
23 assert_equal "avatar.jpg", user.avatar.filename.to_s
24 stored = Vips::Image.new_from_buffer(user.avatar.blob.download, "")
25 assert_empty stored.get_fields.grep(/exif/)
26 assert_equal [512, 307], [stored.width, stored.height]
27 assert_not ActiveStorage::Blob.exists?(original.id), "should purge the original"
30 def test_skips_avatar_uploaded_after_cutoff
32 blob = attach_unnormalised(user, Vips::Image.black(10, 10).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 21))
34 run_task("2026-11-20T00:00:00Z")
36 assert_equal blob.id, user.reload.avatar.blob.id
39 def test_skips_unsupported_type
41 blob = attach_unnormalised(user, '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>', "photo.svg", Time.utc(2026, 11, 19))
43 output = run_task("2026-11-20T00:00:00Z")
45 assert_equal blob.id, user.reload.avatar.blob.id
46 assert_no_match "user #{user.id}", output, "should not try to normalise it"
49 def test_normalises_avatar_over_current_file_size_limit
51 original = attach_unnormalised(user, Vips::Image.black(1000, 1000).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 19))
53 with_settings(:max_avatar_size => original.byte_size - 1) do
54 run_task("2026-11-20T00:00:00Z")
57 assert_not_equal original.id, user.reload.avatar.blob.id
60 def test_continues_after_failure
61 broken_user = create(:user)
62 broken = attach_unnormalised(broken_user, "not an image", "broken.png", Time.utc(2026, 11, 19), :content_type => "image/png")
64 original = attach_unnormalised(user, Vips::Image.black(10, 10).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 19))
66 output = run_task("2026-11-20T00:00:00Z")
68 assert_match "Failed to normalise image/png avatar for user #{broken_user.id}", output
69 assert_equal broken.id, broken_user.reload.avatar.blob.id, "should keep the avatar that failed"
70 assert_not_equal original.id, user.reload.avatar.blob.id
73 def test_requires_cutoff
74 assert_raises(SystemExit) { run_task(nil) }
79 # Attach an avatar directly, bypassing the upload normalisation
80 def attach_unnormalised(user, data, filename, created_at, content_type: nil)
81 blob = ActiveStorage::Blob.create_and_upload!(:io => StringIO.new(data), :filename => filename, :content_type => content_type, :identify => content_type.nil?)
82 blob.update!(:created_at => created_at)
83 ActiveStorage::Attachment.create!(:name => "avatar", :record => user, :blob => blob)
88 with_env("BEFORE" => before) do
89 perform_enqueued_jobs do
90 capture_io { Rake::Task["db:normalise_avatars"].invoke }.first
96 saved = vars.keys.index_with { |k| ENV.fetch(k, nil) }
97 vars.each { |k, v| ENV[k] = v }
100 saved.each { |k, v| ENV[k] = v }