]> git.openstreetmap.org Git - rails.git/blob - test/lib/tasks/normalise_avatars_test.rb
Merge remote-tracking branch 'upstream/pull/7650'
[rails.git] / test / lib / tasks / normalise_avatars_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class NormaliseAvatarsTest < ActiveSupport::TestCase
6   include ActiveJob::TestHelper
7
8   def setup
9     Rails.application.load_tasks if Rake::Task.tasks.empty?
10     Rake::Task["db:normalise_avatars"].reenable
11   end
12
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")
16     user = create(:user)
17     original = attach_unnormalised(user, image.write_to_buffer(".jpg"), "IMG_1234.jpg", Time.utc(2026, 11, 19))
18
19     run_task("2026-11-20T00:00:00Z")
20
21     user.reload
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"
28   end
29
30   def test_skips_avatar_uploaded_after_cutoff
31     user = create(:user)
32     blob = attach_unnormalised(user, Vips::Image.black(10, 10).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 21))
33
34     run_task("2026-11-20T00:00:00Z")
35
36     assert_equal blob.id, user.reload.avatar.blob.id
37   end
38
39   def test_skips_unsupported_type
40     user = create(:user)
41     blob = attach_unnormalised(user, '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>', "photo.svg", Time.utc(2026, 11, 19))
42
43     output = run_task("2026-11-20T00:00:00Z")
44
45     assert_equal blob.id, user.reload.avatar.blob.id
46     assert_no_match "user #{user.id}", output, "should not try to normalise it"
47   end
48
49   def test_normalises_avatar_over_current_file_size_limit
50     user = create(:user)
51     original = attach_unnormalised(user, Vips::Image.black(1000, 1000).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 19))
52
53     with_settings(:max_avatar_size => original.byte_size - 1) do
54       run_task("2026-11-20T00:00:00Z")
55     end
56
57     assert_not_equal original.id, user.reload.avatar.blob.id
58   end
59
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")
63     user = create(:user)
64     original = attach_unnormalised(user, Vips::Image.black(10, 10).write_to_buffer(".png"), "photo.png", Time.utc(2026, 11, 19))
65
66     output = run_task("2026-11-20T00:00:00Z")
67
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
71   end
72
73   def test_requires_cutoff
74     assert_raises(SystemExit) { run_task(nil) }
75   end
76
77   private
78
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)
84     blob
85   end
86
87   def run_task(before)
88     with_env("BEFORE" => before) do
89       perform_enqueued_jobs do
90         capture_io { Rake::Task["db:normalise_avatars"].invoke }.first
91       end
92     end
93   end
94
95   def with_env(vars)
96     saved = vars.keys.index_with { |k| ENV.fetch(k, nil) }
97     vars.each { |k, v| ENV[k] = v }
98     yield
99   ensure
100     saved.each { |k, v| ENV[k] = v }
101   end
102 end