aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb
blob: 33282d36f764d28aa657590f3bb0259669cda3b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require "test_helper"

class Rails::Conductor::ActionMailbox::InboundEmailsControllerTest < ActionDispatch::IntegrationTest
  test "create inbound email" do
    with_rails_env("development") do
      assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
        post rails_conductor_inbound_emails_path, params: {
          mail: {
            from: "Jason Fried <jason@37signals.com>",
            to: "Replies <replies@example.com>",
            cc: "CC <cc@example.com>",
            bcc: "Bcc <bcc@example.com>",
            in_reply_to: "<4e6e35f5a38b4_479f13bb90078178@small-app-01.mail>",
            subject: "Hey there",
            body: "How's it going?"
          }
        }
      end

      mail = ActionMailbox::InboundEmail.last.mail
      assert_equal %w[ jason@37signals.com ], mail.from
      assert_equal %w[ replies@example.com ], mail.to
      assert_equal %w[ cc@example.com ], mail.cc
      assert_equal %w[ bcc@example.com ], mail.bcc
      assert_equal "4e6e35f5a38b4_479f13bb90078178@small-app-01.mail", mail.in_reply_to
      assert_equal "Hey there", mail.subject
      assert_equal "How's it going?", mail.body.decoded
    end
  end

  test "create inbound email with bcc" do
    with_rails_env("development") do
      assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
        post rails_conductor_inbound_emails_path, params: {
          mail: {
            from: "Jason Fried <jason@37signals.com>",
            bcc: "Replies <replies@example.com>",
            subject: "Hey there",
            body: "How's it going?"
          }
        }
      end

      mail = ActionMailbox::InboundEmail.last.mail
      assert_equal %w[ jason@37signals.com ], mail.from
      assert_equal %w[ replies@example.com ], mail.bcc
      assert_equal "Hey there", mail.subject
      assert_equal "How's it going?", mail.body.decoded
    end
  end

  test "create inbound email with attachments" do
    with_rails_env("development") do
      assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
        post rails_conductor_inbound_emails_path, params: {
          mail: {
            from: "Jason Fried <jason@37signals.com>",
            to: "Replies <replies@example.com>",
            subject: "Let's debate some attachments",
            body: "Let's talk about these images:",
            attachments: [ fixture_file_upload("files/avatar1.jpeg"), fixture_file_upload("files/avatar2.jpeg") ]
          }
        }
      end

      mail = ActionMailbox::InboundEmail.last.mail
      assert_equal "Let's talk about these images:", mail.text_part.decoded
      assert_equal 2, mail.attachments.count
      assert_equal %w[ avatar1.jpeg avatar2.jpeg ], mail.attachments.collect(&:filename)
    end
  end

  private
    def with_rails_env(env)
      old_rails_env = Rails.env
      Rails.env = env
      yield
    ensure
      Rails.env = old_rails_env
    end
end