1

We have Griddler Model tests working fine. e.g. we can instantiate the lib/email_processor.rb and it processes. We want to create a controller test that does an end to end post to the standard /email_processor.

The problem is the params are not coming through the post. our basic code is:

  @postattr= {to: "hello@hello.com", subject: "a subject", attachments: [
      ActionDispatch::Http::UploadedFile.new({
         filename: 'example_virgin_onetransaction.pdf',
         type: 'application/pdf',
         tempfile: File.new('testfiles/examplefile.pdf")})
  ]}
  post :create, @postattr
  expect(response).to be_success

it works as it posts to the correct route and gets processed except the email.attachments object is nil.

we've tried

  • @postattr.to_json # gives invalid byte sequence in UTF-8
  • @postattr.to_s.to_json # works, but no params passed through
  • uri encoding the json string

nothing seems to get processed correctly. what have we missed?

Ben
  • 1,292
  • 1
  • 13
  • 21

2 Answers2

1

Your params seems right for using only griddler. But incorrect when you use griddler-postmark. Griddle Postmark adapter accept params like your answer, then griddler-postmark pre-process params for griddler. The right format for passing params for incoming email in rails app is as following with griddler-postmark

 attributes = {Subject: "a subject", TextBody: "Hello!",
            ToFull: [{Email: 'to_email@email.com', Name: 'to email'}],
            FromFull: {Email: "from_email@email.com", Name: "from email"},
            Attachments: [{Name: 'filename.pdf',
                           Content: Base64.encode64(fixture_file.read),
                           ContentType: 'application/pdf',
                           ContentLength: fixture_file.size
                          }]}

post :create, attributes

You may fetch issues with handling incoming email with attachment. Thus I am adding hear an example EmailProcessor class as following

class EmailProcessor

  def initialize(email)
      @email = email
  end

  def process
    if @email.attachments.present?
      attachment = @email.attachments.first
      file = File.new(attachment.original_filename, 'wb')
      file.write attachment.read
      file.flush
      attached_document = AttachedDocument.new(paper: file)
      attached_document.save!
    end
  end
end

Wish this will help you :)

Rokibul Hasan
  • 4,078
  • 2
  • 19
  • 30
0

Right it seems like the formatting of email params is a not as obvious. to and from addresses are actually lists.

  @post_attr = {Subject: "a subject", TextBody: "Hello!",
                ToFull: [{Email: 'to_email@email.com', Name: 'to email'}],
                FromFull: {Email: "from_email@email.com", Name: "from email"},
                Attachments: [{Name: 'filename.pdf',
                               Content: Base64.encode64(fixture_file.read),
                               ContentType: 'application/pdf',
                               ContentLength: fixture_file.size
                              }]}

hope it helps someone

Ben
  • 1,292
  • 1
  • 13
  • 21