email_concern.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. require 'rails_helper'
  2. shared_examples_for EmailConcern do
  3. let(:valid_options) {
  4. {
  5. :subject => "hello!",
  6. :expected_receive_period_in_days => "2"
  7. }
  8. }
  9. let(:agent) do
  10. _agent = described_class.new(:name => "some email agent", :options => valid_options)
  11. _agent.user = users(:jane)
  12. _agent
  13. end
  14. describe "validations" do
  15. it "should be valid" do
  16. expect(agent).to be_valid
  17. end
  18. it "should validate the presence of 'subject'" do
  19. agent.options['subject'] = ''
  20. expect(agent).not_to be_valid
  21. agent.options['subject'] = nil
  22. expect(agent).not_to be_valid
  23. end
  24. it "should validate the presence of 'expected_receive_period_in_days'" do
  25. agent.options['expected_receive_period_in_days'] = ''
  26. expect(agent).not_to be_valid
  27. agent.options['expected_receive_period_in_days'] = nil
  28. expect(agent).not_to be_valid
  29. end
  30. it "should validate that recipients, when provided, is one or more valid email addresses or Liquid commands" do
  31. agent.options['recipients'] = ''
  32. expect(agent).to be_valid
  33. agent.options['recipients'] = nil
  34. expect(agent).to be_valid
  35. agent.options['recipients'] = 'bob@example.com'
  36. expect(agent).to be_valid
  37. agent.options['recipients'] = ['bob@example.com']
  38. expect(agent).to be_valid
  39. agent.options['recipients'] = '{{ email }}'
  40. expect(agent).to be_valid
  41. agent.options['recipients'] = '{% if x %}a@x{% else %}b@y{% endif %}'
  42. expect(agent).to be_valid
  43. agent.options['recipients'] = ['bob@example.com', 'jane@example.com']
  44. expect(agent).to be_valid
  45. agent.options['recipients'] = ['bob@example.com', 'example.com']
  46. expect(agent).not_to be_valid
  47. agent.options['recipients'] = ['hi!']
  48. expect(agent).not_to be_valid
  49. agent.options['recipients'] = { :foo => "bar" }
  50. expect(agent).not_to be_valid
  51. agent.options['recipients'] = "wut"
  52. expect(agent).not_to be_valid
  53. end
  54. end
  55. describe "#recipients" do
  56. it "defaults to the user's email address" do
  57. expect(agent.recipients).to eq([users(:jane).email])
  58. end
  59. it "wraps a string with an array" do
  60. agent.options['recipients'] = 'bob@bob.com'
  61. expect(agent.recipients).to eq(['bob@bob.com'])
  62. end
  63. it "handles an array" do
  64. agent.options['recipients'] = ['bob@bob.com', 'jane@jane.com']
  65. expect(agent.recipients).to eq(['bob@bob.com', 'jane@jane.com'])
  66. end
  67. it "interpolates" do
  68. agent.options['recipients'] = "{{ username }}@{{ domain }}"
  69. expect(agent.recipients('username' => 'bob', 'domain' => 'example.com')).to eq(["bob@example.com"])
  70. end
  71. end
  72. end