From f544dc74f52be521d14e2b6d4eb41f275a9dad3e Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 7 Nov 2025 12:55:04 +0100 Subject: [PATCH] Add test --- app/helpers/avatars_helper.rb | 2 +- test/fixtures/files/avatar.png | Bin 0 -> 314 bytes .../notification/bundle_mailer_test.rb | 44 ++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/files/avatar.png create mode 100644 test/mailers/notification/bundle_mailer_test.rb diff --git a/app/helpers/avatars_helper.rb b/app/helpers/avatars_helper.rb index 14c771963..a745005a8 100644 --- a/app/helpers/avatars_helper.rb +++ b/app/helpers/avatars_helper.rb @@ -21,7 +21,7 @@ module AvatarsHelper def mail_avatar_tag(user, size: 48, **options) if user.avatar.attached? - image_tag user_avatar_url(notification.creator), alt: notification.creator.name, class: "avatar", **options + image_tag user_avatar_url(user), alt: user.name, class: "avatar", **options else tag.span class: "avatar", style: "background-color: #{avatar_background_color(user)};" do user.initials diff --git a/test/fixtures/files/avatar.png b/test/fixtures/files/avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..6491d2636a9b152a963c3f731775f75b7fcbaad3 GIT binary patch literal 314 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDH3?y^UWFG-i3dtTpz6=aiY77hwEes65fI54Gnb-%tH(ftxOH9j7+o*46F4ep=ij>PsvQH#I51qgu7Ni4H|G8 fN-}d(i%Sx73vlaM+4|=xP!EHrtDnm{r-UW|Bj8ko literal 0 HcmV?d00001 diff --git a/test/mailers/notification/bundle_mailer_test.rb b/test/mailers/notification/bundle_mailer_test.rb new file mode 100644 index 000000000..fbc4274c9 --- /dev/null +++ b/test/mailers/notification/bundle_mailer_test.rb @@ -0,0 +1,44 @@ +require "test_helper" + +class Notification::BundleMailerTest < ActionMailer::TestCase + setup do + @user = users(:david) + + @bundle = Notification::Bundle.create!( + user: @user, + starts_at: 1.hour.ago, + ends_at: 1.hour.from_now + ) + end + + test "renders avatar with initials in span when avatar is not attached" do + create_notification(@user) + + email = Notification::BundleMailer.notification(@bundle) + + assert_match /]*class="avatar"[^>]*>/, email.html_part.body.to_s + assert_match /#{@user.initials}/, email.html_part.body.to_s + assert_match /style="background-color: #[A-F0-9]{6};?"/, email.html_part.body.to_s + end + + test "renders avatar with external image URL when avatar is attached" do + @user.avatar.attach( + io: File.open(Rails.root.join("test", "fixtures", "files", "avatar.png")), + filename: "avatar.png", + content_type: "image/png" + ) + + create_notification(@user) + + email = Notification::BundleMailer.notification(@bundle) + + assert_match /]*class="avatar"[^>]*>/, email.html_part.body.to_s + assert_match /]*class="avatar"[^>]*src="[^"]*"/, email.html_part.body.to_s + assert_match /alt="#{@user.name}"/, email.html_part.body.to_s + end + + private + def create_notification(user) + Notification.create!(user: user, creator: user, source: events(:logo_published), created_at: 30.minutes.ago) + end +end