From ea0ce4cc39940b5872611a077d15627590f8b3b1 Mon Sep 17 00:00:00 2001 From: Jon M Date: Tue, 8 Mar 2022 14:20:37 +0000 Subject: [PATCH] Add support for verification codes --- .rubocop_todo.yml | 2 +- lib/Mailosaur/models/code.rb | 12 ++++++++++++ lib/Mailosaur/models/message_content.rb | 5 +++++ lib/mailosaur.rb | 1 + test/emails_test.rb | 10 ++++++++++ test/mailer.rb | 3 +-- test/resources/testEmail.html | 10 +++++++++- test/resources/testEmail.txt | 6 +++++- 8 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 lib/Mailosaur/models/code.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 39320f8..3b36328 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -44,7 +44,7 @@ Lint/UnifiedInteger: # Offense count: 16 Metrics/AbcSize: - Max: 49 + Max: 100 # Offense count: 2 # Configuration parameters: CountComments, ExcludedMethods. diff --git a/lib/Mailosaur/models/code.rb b/lib/Mailosaur/models/code.rb new file mode 100644 index 0000000..308d1f0 --- /dev/null +++ b/lib/Mailosaur/models/code.rb @@ -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 diff --git a/lib/Mailosaur/models/message_content.rb b/lib/Mailosaur/models/message_content.rb index d1f8f90..8bfd104 100644 --- a/lib/Mailosaur/models/message_content.rb +++ b/lib/Mailosaur/models/message_content.rb @@ -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'] @@ -12,6 +14,9 @@ def initialize(data = {}) # @return [Array] attr_accessor :links + # @return [Array] + attr_accessor :codes + # @return [Array] attr_accessor :images diff --git a/lib/mailosaur.rb b/lib/mailosaur.rb index eb74c85..91dc5f9 100644 --- a/lib/mailosaur.rb +++ b/lib/mailosaur.rb @@ -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' diff --git a/test/emails_test.rb b/test/emails_test.rb index 0ae9903..1fb363e 100644 --- a/test/emails_test.rb +++ b/test/emails_test.rb @@ -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) @@ -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) diff --git a/test/mailer.rb b/test/mailer.rb index 285c90f..704e762 100644 --- a/test/mailer.rb +++ b/test/mailer.rb @@ -1,5 +1,4 @@ require 'mail' -require 'securerandom' Mail.defaults do delivery_method :smtp, { @@ -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) diff --git a/test/resources/testEmail.html b/test/resources/testEmail.html index abce10b..60379d7 100644 --- a/test/resources/testEmail.html +++ b/test/resources/testEmail.html @@ -17,6 +17,14 @@ Inline image 1 +
+ Your verification code is 123456 +
+
+
+ Your special ID is G3H1Y2 +
+

@@ -25,4 +33,4 @@ - \ No newline at end of file + diff --git a/test/resources/testEmail.txt b/test/resources/testEmail.txt index 773df2e..b3de48a 100644 --- a/test/resources/testEmail.txt +++ b/test/resources/testEmail.txt @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text this is an image:[image: Inline image 1] -this is an invalid link: invalid \ No newline at end of file +Your verification code is 654321 + +Your special ID is 5H0Y2 + +this is an invalid link: invalid