Keine Beschreibung http://j1x-huginn.herokuapp.com

imap_folder_agent_spec.rb 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. require 'spec_helper'
  2. require 'time'
  3. describe Agents::ImapFolderAgent do
  4. describe 'checking IMAP' do
  5. before do
  6. @site = {
  7. 'expected_update_period_in_days' => 1,
  8. 'host' => 'mail.example.net',
  9. 'ssl' => true,
  10. 'username' => 'foo',
  11. 'password' => 'bar',
  12. 'folders' => ['INBOX'],
  13. 'conditions' => {
  14. }
  15. }
  16. @checker = Agents::ImapFolderAgent.new(:name => 'Example', :options => @site, :keep_events_for => 2)
  17. @checker.user = users(:bob)
  18. @checker.save!
  19. message_mixin = Module.new {
  20. def folder
  21. 'INBOX'
  22. end
  23. def uidvalidity
  24. '100'
  25. end
  26. def has_attachment?
  27. false
  28. end
  29. def body_parts(mime_types = %[text/plain text/enriched text/html])
  30. mime_types.map { |type|
  31. all_parts.find { |part|
  32. part.mime_type == type
  33. }
  34. }.compact
  35. end
  36. }
  37. @mails = [
  38. Mail.read(Rails.root.join('spec/data_fixtures/imap1.eml')).tap { |mail|
  39. mail.extend(message_mixin)
  40. stub(mail).uid.returns(1)
  41. },
  42. Mail.read(Rails.root.join('spec/data_fixtures/imap2.eml')).tap { |mail|
  43. mail.extend(message_mixin)
  44. stub(mail).uid.returns(2)
  45. stub(mail).has_attachment?.returns(true)
  46. },
  47. ]
  48. stub(@checker).each_unread_mail.returns { |yielder|
  49. @mails.each(&yielder)
  50. }
  51. @payloads = [
  52. {
  53. 'folder' => 'INBOX',
  54. 'from' => 'nanashi.gombeh@example.jp',
  55. 'to' => ['jane.doe@example.com', 'john.doe@example.com'],
  56. 'cc' => [],
  57. 'date' => '2014-05-09T16:00:00+09:00',
  58. 'subject' => 'some subject',
  59. 'body' => "Some plain text\nSome second line\n",
  60. 'has_attachment' => false,
  61. 'matches' => {},
  62. 'mime_type' => 'text/plain',
  63. },
  64. {
  65. 'folder' => 'INBOX',
  66. 'from' => 'john.doe@example.com',
  67. 'to' => ['jane.doe@example.com', 'nanashi.gombeh@example.jp'],
  68. 'cc' => [],
  69. 'subject' => 'Re: some subject',
  70. 'body' => "Some reply\n",
  71. 'date' => '2014-05-09T17:00:00+09:00',
  72. 'has_attachment' => true,
  73. 'matches' => {},
  74. 'mime_type' => 'text/plain',
  75. }
  76. ]
  77. end
  78. describe 'validations' do
  79. before do
  80. @checker.should be_valid
  81. end
  82. it 'should validate the integer fields' do
  83. @checker.options['expected_update_period_in_days'] = 'nonsense'
  84. @checker.should_not be_valid
  85. @checker.options['expected_update_period_in_days'] = '2'
  86. @checker.should be_valid
  87. @checker.options['port'] = -1
  88. @checker.should_not be_valid
  89. @checker.options['port'] = 'imap'
  90. @checker.should_not be_valid
  91. @checker.options['port'] = '143'
  92. @checker.should be_valid
  93. @checker.options['port'] = 993
  94. @checker.should be_valid
  95. end
  96. it 'should validate the boolean fields' do
  97. @checker.options['ssl'] = false
  98. @checker.should be_valid
  99. @checker.options['ssl'] = 'true'
  100. @checker.should_not be_valid
  101. end
  102. it 'should validate regexp conditions' do
  103. @checker.options['conditions'] = {
  104. 'subject' => '(foo'
  105. }
  106. @checker.should_not be_valid
  107. @checker.options['conditions'] = {
  108. 'body' => '***'
  109. }
  110. @checker.should_not be_valid
  111. @checker.options['conditions'] = {
  112. 'subject' => '\ARe:',
  113. 'body' => '(?<foo>http://\S+)'
  114. }
  115. @checker.should be_valid
  116. end
  117. end
  118. describe '#check' do
  119. it 'should check for mails and save memory' do
  120. lambda { @checker.check }.should change { Event.count }.by(2)
  121. @checker.memory['notified'].sort.should == @mails.map(&:message_id).sort
  122. @checker.memory['seen'].should == @mails.each_with_object({}) { |mail, seen|
  123. (seen[mail.uidvalidity] ||= []) << mail.uid
  124. }
  125. Event.last(2).map(&:payload) == @payloads
  126. lambda { @checker.check }.should_not change { Event.count }
  127. end
  128. it 'should narrow mails by To' do
  129. @checker.options['conditions']['to'] = 'John.Doe@*'
  130. lambda { @checker.check }.should change { Event.count }.by(1)
  131. @checker.memory['notified'].sort.should == [@mails.first.message_id]
  132. @checker.memory['seen'].should == @mails.each_with_object({}) { |mail, seen|
  133. (seen[mail.uidvalidity] ||= []) << mail.uid
  134. }
  135. Event.last.payload.should == @payloads.first
  136. lambda { @checker.check }.should_not change { Event.count }
  137. end
  138. it 'should perform regexp matching and save named captures' do
  139. @checker.options['conditions'].update(
  140. 'subject' => '\ARe: (?<a>.+)',
  141. 'body' => 'Some (?<b>.+) reply',
  142. )
  143. lambda { @checker.check }.should change { Event.count }.by(1)
  144. @checker.memory['notified'].sort.should == [@mails.last.message_id]
  145. @checker.memory['seen'].should == @mails.each_with_object({}) { |mail, seen|
  146. (seen[mail.uidvalidity] ||= []) << mail.uid
  147. }
  148. Event.last.payload.should == @payloads.last.update(
  149. 'body' => "<div dir=\"ltr\">Some HTML reply<br></div>\n",
  150. 'matches' => { 'a' => 'some subject', 'b' => 'HTML' },
  151. 'mime_type' => 'text/html',
  152. )
  153. lambda { @checker.check }.should_not change { Event.count }
  154. end
  155. it 'should narrow mails by has_attachment (true)' do
  156. @checker.options['conditions']['has_attachment'] = true
  157. lambda { @checker.check }.should change { Event.count }.by(1)
  158. Event.last.payload['subject'].should == 'Re: some subject'
  159. end
  160. it 'should narrow mails by has_attachment (false)' do
  161. @checker.options['conditions']['has_attachment'] = false
  162. lambda { @checker.check }.should change { Event.count }.by(1)
  163. Event.last.payload['subject'].should == 'some subject'
  164. end
  165. it 'should narrow mail parts by MIME types' do
  166. @checker.options['mime_types'] = %w[text/plain]
  167. @checker.options['conditions'].update(
  168. 'subject' => '\ARe: (?<a>.+)',
  169. 'body' => 'Some (?<b>.+) reply',
  170. )
  171. lambda { @checker.check }.should_not change { Event.count }
  172. @checker.memory['notified'].sort.should == []
  173. @checker.memory['seen'].should == @mails.each_with_object({}) { |mail, seen|
  174. (seen[mail.uidvalidity] ||= []) << mail.uid
  175. }
  176. end
  177. it 'should never mark mails as read unless mark_as_read is true' do
  178. @mails.each { |mail|
  179. stub(mail).mark_as_read.never
  180. }
  181. lambda { @checker.check }.should change { Event.count }.by(2)
  182. end
  183. it 'should mark mails as read if mark_as_read is true' do
  184. @checker.options['mark_as_read'] = true
  185. @mails.each { |mail|
  186. stub(mail).mark_as_read.once
  187. }
  188. lambda { @checker.check }.should change { Event.count }.by(2)
  189. end
  190. end
  191. end
  192. end