Class: IFD_Email

Inherits:
Object
  • Object
show all
Defined in:
lib/helper/mail_helper.rb

Overview

include EmailSpec::Matcher

Class Method Summary collapse

Class Method Details

.send_email(raw_data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/helper/mail_helper.rb', line 7

def self.send_email(raw_data)
  include Mail::Matchers
  raw_data.strip!
  header, body = raw_data.split(/\n\n/, 2) # 2: maximum number of fields
  conditions = {}
  header.split("\n").each do |row|
    if row.lstrip.chop.match(/^[a-z\-]+:/i)
      key, value = row.split(":", 2)
      conditions[key.gsub(' ','').underscore.to_sym] = value.gsub(' ','')

    end
  end
  conditions[:body] = body if body
  filepath = ($test_data_dir + conditions[:attachments] if conditions[:attachments])
  # Mail::TestMailer.deliveries.clear
  Mail.deliver do
    from "[email protected]"
    to to_address if conditions[:to]
    subject subject if conditions[:subject]
    body body_email if conditions[:body]
    add_file attachments if filepath
  end
end

.verify_receive_email(raw_data) ⇒ Object



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
# File 'lib/helper/mail_helper.rb', line 31

def self.verify_receive_email(raw_data)
  raw_data.strip!
  header, body = raw_data.split(/\n\n/, 2) # 2: maximum number of fields
  conditions = {}
  header.split("\n").each do |row|
    if row.lstrip.chop.match(/^[a-z\-]+:/i)
      key, value = row.split(":", 2)
      conditions[key.gsub(' ','').underscore.to_sym] = value.gsub(' ','')

    end
  end
  conditions[:body] = body.squeeze(' ').strip if body
  sleep 5
  emails = Mail.find(:what => :last, :count => 1)

  if emails.instance_of? Mail::Message
    IFD_Assertion.assert_string_equal(conditions[:from], emails.from[0].to_s)
    IFD_Assertion.assert_string_equal(conditions[:subject], emails.subject)
    IFD_Assertion.assert_string_contain(conditions[:body], emails.body)
    emails.attachments.each do |attachment|
      IFD_Assertion.assert_string_equal(conditions[:attachments], attachment.filename)
    end if conditions[:attachments]
  else
    raise "WARNING: *** No new Email is found"
  end
end