Skip to content

Commit

Permalink
Add support for verification codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Mar 8, 2022
1 parent f17f14d commit ea0ce4c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Lint/UnifiedInteger:

# Offense count: 16
Metrics/AbcSize:
Max: 49
Max: 100

# Offense count: 2
# Configuration parameters: CountComments, ExcludedMethods.
Expand Down
12 changes: 12 additions & 0 deletions lib/Mailosaur/models/code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Mailosaur
module Models
class Code < BaseModel
def initialize(data = {})
@value = data['value']
end

# @return [String]
attr_accessor :value
end
end
end
5 changes: 5 additions & 0 deletions lib/Mailosaur/models/message_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MessageContent < BaseModel
def initialize(data = {})
@links = []
(data['links'] || []).each do |i| @links << Mailosaur::Models::Link.new(i) end
@codes = []
(data['codes'] || []).each do |i| @codes << Mailosaur::Models::Code.new(i) end
@images = []
(data['images'] || []).each do |i| @images << Mailosaur::Models::Image.new(i) end
@body = data['body']
Expand All @@ -12,6 +14,9 @@ def initialize(data = {})
# @return [Array<Link>]
attr_accessor :links

# @return [Array<Code>]
attr_accessor :codes

# @return [Array<Image>]
attr_accessor :images

Expand Down
1 change: 1 addition & 0 deletions lib/mailosaur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Models
autoload :MessageContent, 'Mailosaur/models/message_content.rb'
autoload :Server, 'Mailosaur/models/server.rb'
autoload :Link, 'Mailosaur/models/link.rb'
autoload :Code, 'Mailosaur/models/code.rb'
autoload :ServerListResult, 'Mailosaur/models/server_list_result.rb'
autoload :SpamFilterResults, 'Mailosaur/models/spam_filter_results.rb'
autoload :ServerCreateOptions, 'Mailosaur/models/server_create_options.rb'
Expand Down
10 changes: 10 additions & 0 deletions test/emails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ def validate_html(email)
assert_equal('http://invalid/', email.html.links[2].href)
assert_equal('invalid', email.html.links[2].text)

# Html.Links
assert_equal(2, email.html.codes.length)
assert_equal('123456', email.html.codes[0].value)
assert_equal('G3H1Y2', email.html.codes[1].value)

# Html.Images
assert_true(email.html.images[1].src.start_with?('cid:'))
assert_equal('Inline image 1', email.html.images[1].alt)
Expand All @@ -401,6 +406,11 @@ def validate_text(email)
assert_equal(email.text.links[0].href, email.text.links[0].text)
assert_equal('https://mailosaur.com/', email.text.links[1].href)
assert_equal(email.text.links[1].href, email.text.links[1].text)

# Text.Links
assert_equal(2, email.text.codes.length)
assert_equal('654321', email.text.codes[0].value)
assert_equal('5H0Y2', email.text.codes[1].value)
end

def validate_headers(email)
Expand Down
3 changes: 1 addition & 2 deletions test/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'mail'
require 'securerandom'

Mail.defaults do
delivery_method :smtp, {
Expand All @@ -22,7 +21,7 @@ def self.send_emails(client, server, quantity)

def self.send_email(client, server, send_to_address = nil)
Mail.deliver do
random_string = SecureRandom.hex(5)
random_string = (0...10).map { rand(65..90).chr }.join

subject '%s subject' % [random_string]
random_to_address = send_to_address || client.servers.generate_email_address(server)
Expand Down
10 changes: 9 additions & 1 deletion test/resources/testEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<img src="cid:ii_1435fadb31d523f6" alt="Inline image 1">
</a>
</div>
<div>
Your verification code is 123456
<br>
</div>
<div>
Your special ID is G3H1Y2
<br>
</div>
<div>
<br>
</div>
Expand All @@ -25,4 +33,4 @@
</div>
</div>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion test/resources/testEmail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text

this is an image:[image: Inline image 1] <https://mailosaur.com/>

this is an invalid link: invalid
Your verification code is 654321

Your special ID is 5H0Y2

this is an invalid link: invalid

0 comments on commit ea0ce4c

Please sign in to comment.