email_concern.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require 'spec_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. agent.should be_valid
  17. end
  18. it "should validate the presence of 'subject'" do
  19. agent.options['subject'] = ''
  20. agent.should_not be_valid
  21. agent.options['subject'] = nil
  22. agent.should_not 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. agent.should_not be_valid
  27. agent.options['expected_receive_period_in_days'] = nil
  28. agent.should_not be_valid
  29. end
  30. it "should validate that recipients, when provided, is one or more valid email addresses" do
  31. agent.options['recipients'] = ''
  32. agent.should be_valid
  33. agent.options['recipients'] = nil
  34. agent.should be_valid
  35. agent.options['recipients'] = 'bob@example.com'
  36. agent.should be_valid
  37. agent.options['recipients'] = ['bob@example.com']
  38. agent.should be_valid
  39. agent.options['recipients'] = ['bob@example.com', 'jane@example.com']
  40. agent.should be_valid
  41. agent.options['recipients'] = ['bob@example.com', 'example.com']
  42. agent.should_not be_valid
  43. agent.options['recipients'] = ['hi!']
  44. agent.should_not be_valid
  45. agent.options['recipients'] = { :foo => "bar" }
  46. agent.should_not be_valid
  47. agent.options['recipients'] = "wut"
  48. agent.should_not be_valid
  49. end
  50. end
  51. describe "#recipients" do
  52. it "defaults to the user's email address" do
  53. agent.recipients.should == [users(:jane).email]
  54. end
  55. it "wraps a string with an array" do
  56. agent.options['recipients'] = 'bob@bob.com'
  57. agent.recipients.should == ['bob@bob.com']
  58. end
  59. it "handles an array" do
  60. agent.options['recipients'] = ['bob@bob.com', 'jane@jane.com']
  61. agent.recipients.should == ['bob@bob.com', 'jane@jane.com']
  62. end
  63. it "interpolates" do
  64. agent.options['recipients'] = "{{ username }}@{{ domain }}"
  65. agent.recipients('username' => 'bob', 'domain' => 'example.com').should == ["bob@example.com"]
  66. end
  67. end
  68. end