Skip to content

Commit

Permalink
Added remaining unit tests for #2933 (#3020)
Browse files Browse the repository at this point in the history
* Added remaining unit tests for #2933

* Minor change

* Indentation changed

* Added tests and moved emails to fixtures

* Added tests and moved emails to fixtures
  • Loading branch information
namangupta01 authored and jywarren committed Jul 12, 2018
1 parent 828db88 commit 3c79545
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 8 deletions.
2 changes: 2 additions & 0 deletions test/fixtures/incoming_test_emails/gmail/filtered_comment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is another reply by email comment

File renamed without changes.
9 changes: 9 additions & 0 deletions test/fixtures/incoming_test_emails/gmail/trimmed_content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
On Tue, Jul 3, 2018 at 11:17 PM Naman Gupta \<[01namangupta@gmail.com](mailto:01namangupta@gmail.com)\> wrote:

> This is reply by comment
>
>
> On Tue, Jul 3, 2018 at 11:13 PM Rails Projects \<[railsprojects2018@gmail.com](mailto:railsprojects2018@gmail.com)\> wrote:
>
> > This is a comment sent to the user by publiclab.

File renamed without changes.
44 changes: 36 additions & 8 deletions test/unit/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,43 +225,45 @@ class CommentTest < ActiveSupport::TestCase

test 'should parse incoming mail from gmail service correctly and add comment' do
require 'mail'
mail = Mail.read('test/incoming_test_emails/gmail/incoming_gmail_email.eml')
mail = Mail.read('test/fixtures/incoming_test_emails/gmail/incoming_gmail_email.eml')
node = Node.last
mail.subject = "Re: #{node.title} (##{node.nid})"
Comment.receive_mail(mail)
f = File.open('test/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
comment = Comment.last
user_email = mail.from.first
assert_equal comment.comment, f.read
assert_equal comment.nid, node.id
assert_equal comment.message_id, mail.message_id
assert_equal comment.comment_via, 1
assert_equal User.find(comment.uid).email, user_email
f.close()
end

test 'should parse incoming mail from yahoo service correctly and add comment' do
require 'mail'
mail = Mail.read('test/incoming_test_emails/yahoo/incoming_yahoo_email.eml')
mail = Mail.read('test/fixtures/incoming_test_emails/yahoo/incoming_yahoo_email.eml')
node = Node.last
mail.subject = "Re: #{node.title} (##{node.nid})"
Comment.receive_mail(mail)
f = File.open('test/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
f = File.open('test/fixtures/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
comment = Comment.last
user_email = mail.from.first
assert_equal comment.comment, f.read
assert_equal comment.nid, node.id
assert_equal comment.message_id, mail.message_id
assert_equal comment.comment_via, 1
assert_equal User.find(comment.uid).email, user_email
f.close()
end

test 'should parse incoming mail from gmail service correctly and add answer comment' do
require 'mail'
mail = Mail.read('test/incoming_test_emails/gmail/incoming_gmail_email.eml')
mail = Mail.read('test/fixtures/incoming_test_emails/gmail/incoming_gmail_email.eml')
answer = Answer.last
mail.subject = "Re: (#a#{answer.id})"
Comment.receive_mail(mail)
f = File.open('test/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
comment = Comment.last
user_email = mail.from.first
assert_equal comment.comment, f.read
Expand All @@ -274,12 +276,12 @@ class CommentTest < ActiveSupport::TestCase

test 'should parse incoming mail from yahoo service correctly and add answer comment' do
require 'mail'
mail = Mail.read('test/incoming_test_emails/yahoo/incoming_yahoo_email.eml')
mail = Mail.read('test/fixtures/incoming_test_emails/yahoo/incoming_yahoo_email.eml')
# Mail contain ["01namangupta@gmail.com"] in from field.
answer = Answer.last
mail.subject = "Re: (#a#{answer.id})"
Comment.receive_mail(mail)
f = File.open('test/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
f = File.open('test/fixtures/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
comment = Comment.last
user_email = mail.from.first
assert_equal comment.comment, f.read
Expand All @@ -290,4 +292,30 @@ class CommentTest < ActiveSupport::TestCase
f.close()
end

test 'should give the domain of gmail correctly' do
domain = Comment.get_domain("01namangupta@gmail.com")
assert_equal domain, "gmail"
end

test 'should give the domain of yahoo mail correctly' do
domain = Comment.get_domain("naman18996@yahoo.com")
assert_equal domain, "yahoo"
end

test 'should be true when there is trimmed content in comment' do
comment = Comment.new
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
comment.comment = f.read
f.close()
comment.save
assert_equal true, comment.trimmed_content?
end

test 'should be false when there is no trimmed content in comment' do
comment = Comment.new
comment.comment = "This is a comment without trimmed content"
comment.save
assert_equal false, comment.trimmed_content?
end

end
35 changes: 35 additions & 0 deletions test/unit/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase

test 'should give filtered comment body' do
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
filtered_body = filtered_comment_body(f.read)
f.close()
f = File.open('test/fixtures/incoming_test_emails/gmail/filtered_comment.txt', 'r')
assert_equal filtered_body, f.read
f.close()
end

test 'should give trimmed content of comment' do
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
trimmed_content = trimmed_body(f.read)
f.close()
f = File.open('test/fixtures/incoming_test_emails/gmail/trimmed_content.txt', 'r')
assert_equal trimmed_content, f.read
f.close()
end

test 'should return true if contain trimmed content' do
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
contain_trimmed_body = contain_trimmed_body?(f.read)
assert_equal contain_trimmed_body, true
f.close()
end

test 'should return false if there is no trimmed content' do
contain_trimmed_body = contain_trimmed_body?("Without trimmed content")
assert_equal contain_trimmed_body, false
end

end

0 comments on commit 3c79545

Please sign in to comment.