require "test_helper" class MailOpensControllerTest < ActionDispatch::IntegrationTest setup do opportunities(:deal).update!(send_status: "to_send") @mailing = create_mailing(audience: "to_send") @mailing.rebuild_recipients! @recipient = @mailing.mailing_recipients.first @recipient.update!(status: "sent", sent_at: Time.current, rendered_subject: "Ciao ASD Test Calcio") end test "records the first open without login and returns a gif" do assert_difference -> { Activity.where(activity_type: "email_opened").count }, 1 do get mail_open_path(@recipient.tracking_token) end assert_response :success assert_equal "image/gif", response.media_type @recipient.reload assert @recipient.opened? assert_equal 1, @recipient.open_count assert_equal @recipient.opened_at, @recipient.last_opened_at end test "counts later loads without duplicating the timeline activity" do get mail_open_path(@recipient.tracking_token) assert_no_difference -> { Activity.where(activity_type: "email_opened").count } do get mail_open_path(@recipient.tracking_token) end @recipient.reload assert_equal 2, @recipient.open_count assert @recipient.last_opened_at >= @recipient.opened_at end test "unknown token still returns a gif" do get mail_open_path("missing-token-value-here") assert_response :success assert_equal "image/gif", response.media_type end test "does not mark pending emails as opened" do @recipient.update!(status: "pending", sent_at: nil) get mail_open_path(@recipient.tracking_token) assert_response :success @recipient.reload assert_not @recipient.opened? assert_equal 0, @recipient.open_count end end