diff options
Diffstat (limited to 'vendor/gems/microsoft_graph_mailer/spec/lib')
3 files changed, 297 insertions, 0 deletions
diff --git a/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/delivery_spec.rb b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/delivery_spec.rb new file mode 100644 index 00000000000..23096e75b76 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/delivery_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "securerandom" + +RSpec.describe MicrosoftGraphMailer::Delivery do + let(:microsoft_graph_settings) do + { + user_id: SecureRandom.hex, + tenant: SecureRandom.hex, + client_id: SecureRandom.hex, + client_secret: SecureRandom.hex, + azure_ad_endpoint: "https://test-azure_ad_endpoint", + graph_endpoint: "https://test-graph_endpoint" + } + end + + subject { described_class.new(microsoft_graph_settings) } + + describe ".new" do + it "sets #microsoft_graph_settings" do + expect(subject.microsoft_graph_settings).to eq(microsoft_graph_settings) + end + + [:user_id, :tenant, :client_id, :client_secret].each do |setting| + it "raises MicrosoftGraphMailer::ConfigurationError when '#{setting}' is missing" do + microsoft_graph_settings[setting] = nil + + expect { subject } + .to raise_error(MicrosoftGraphMailer::ConfigurationError, "'#{setting}' is missing") + end + end + + it "sets azure_ad_endpoint setting to 'https://login.microsoftonline.com' when it is missing" do + microsoft_graph_settings[:azure_ad_endpoint] = nil + + expect(subject.microsoft_graph_settings[:azure_ad_endpoint]).to eq("https://login.microsoftonline.com") + end + + it "sets graph_endpoint setting to 'https://graph.microsoft.com' when it is missing" do + microsoft_graph_settings[:graph_endpoint] = nil + + expect(subject.microsoft_graph_settings[:graph_endpoint]).to eq("https://graph.microsoft.com") + end + end + + describe "#deliver!" do + let(:access_token) { SecureRandom.hex } + + let(:message) do + Mail.new do + from "about@gitlab.com" + + to "to@example.com" + + cc "cc@example.com" + + subject "GitLab Mission" + + text_part do + body "It is GitLab's mission to make it so that everyone can contribute." + end + + html_part do + content_type "text/html; charset=UTF-8" + body "It is GitLab's mission to make it so that <strong>everyone can contribute</strong>." + end + + add_file fixture_path("attachments", "gitlab.txt") + + add_file fixture_path("attachments", "gitlab_logo.png") + end + end + + context "when token request is successful" do + before do + stub_token_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, response_status: 200) + end + + context "when send mail request returns response status 202" do + it "sends mail and returns an instance of OAuth2::Response" do + stub_send_mail_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + expect(subject.deliver!(message)).to be_an_instance_of(OAuth2::Response) + end + + it "sends mail including bcc field" do + message.bcc = "bcc@example.com" + + stub_send_mail_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + subject.deliver!(message) + end + + it "does not change message[:bcc].include_in_headers" do + message.bcc = "bcc@example.com" + expected_message_bcc_include_in_headers = "42" + message[:bcc].include_in_headers = expected_message_bcc_include_in_headers + + stub_send_mail_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + subject.deliver!(message) + + expect(message[:bcc].include_in_headers).to eq(expected_message_bcc_include_in_headers) + end + end + + context "when send mail request returns response status other than 202" do + it "raises MicrosoftGraphMailer::DeliveryError" do + stub_send_mail_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, message: message, response_status: 200) + + expect { subject.deliver!(message) }.to raise_error(MicrosoftGraphMailer::DeliveryError) + end + end + end + + context "when token request is not successful" do + before do + stub_token_request(microsoft_graph_settings: subject.microsoft_graph_settings, access_token: access_token, response_status: 503) + end + + it "raises OAuth2::Error" do + expect { subject.deliver!(message) }.to raise_error(OAuth2::Error) + end + end + end +end diff --git a/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/railtie_spec.rb b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/railtie_spec.rb new file mode 100644 index 00000000000..d1a60522cdf --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer/railtie_spec.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "securerandom" + +class TestMailer < ActionMailer::Base + def gitlab_mission(to:, cc: []) + mail(from: "about@gitlab.com", to: to, cc: cc, subject: "GitLab Mission") do |format| + format.text { render plain: "It is GitLab's mission to make it so that everyone can contribute." } + format.html { render html: "It is GitLab's mission to make it so that <strong>everyone can contribute</strong>.".html_safe } + end + + mail.attachments["gitlab.txt"] = File.read(fixture_path("attachments", "gitlab.txt")) + + mail.attachments["gitlab_logo.png"] = File.read(fixture_path("attachments", "gitlab_logo.png")) + end +end + +RSpec.describe MicrosoftGraphMailer::Railtie do + let(:microsoft_graph_settings) do + { + user_id: SecureRandom.hex, + tenant: SecureRandom.hex, + client_id: SecureRandom.hex, + client_secret: SecureRandom.hex, + azure_ad_endpoint: "https://test-azure_ad_endpoint", + graph_endpoint: "https://test-graph_endpoint" + } + end + + let(:message) { TestMailer.gitlab_mission(to: "to@example.com", cc: "cc@example.com") } + + before do + ActionMailer::Base.delivery_method = :microsoft_graph + ActionMailer::Base.microsoft_graph_settings = microsoft_graph_settings + end + + it "its superclass is Rails::Railtie" do + expect(MicrosoftGraphMailer::Railtie.superclass).to eq(Rails::Railtie) + end + + describe "settings" do + describe "ActionMailer::Base.delivery_methods[:microsoft_graph]" do + it "returns MicrosoftGraphMailer::Delivery" do + expect(ActionMailer::Base.delivery_methods[:microsoft_graph]).to eq(MicrosoftGraphMailer::Delivery) + end + end + + describe "ActionMailer::Base.microsoft_graph_settings" do + it "returns microsoft_graph_settings" do + expect(ActionMailer::Base.microsoft_graph_settings).to eq(microsoft_graph_settings) + end + end + + it "sets #microsoft_graph_settings" do + expect(message.delivery_method.microsoft_graph_settings).to eq(microsoft_graph_settings) + end + + [:user_id, :tenant, :client_id, :client_secret].each do |setting| + it "raises MicrosoftGraphMailer::ConfigurationError when '#{setting}' is missing" do + microsoft_graph_settings[setting] = nil + ActionMailer::Base.microsoft_graph_settings = microsoft_graph_settings + + expect { message.delivery_method } + .to raise_error(MicrosoftGraphMailer::ConfigurationError, "'#{setting}' is missing") + end + end + + it "sets azure_ad_endpoint setting to 'https://login.microsoftonline.com' when it is missing" do + microsoft_graph_settings[:azure_ad_endpoint] = nil + ActionMailer::Base.microsoft_graph_settings = microsoft_graph_settings + + expect(message.delivery_method.microsoft_graph_settings[:azure_ad_endpoint]).to eq("https://login.microsoftonline.com") + end + + it "sets graph_endpoint setting to 'https://graph.microsoft.com' when it is missing" do + microsoft_graph_settings[:graph_endpoint] = nil + ActionMailer::Base.microsoft_graph_settings = microsoft_graph_settings + + expect(message.delivery_method.microsoft_graph_settings[:graph_endpoint]).to eq("https://graph.microsoft.com") + end + end + + describe "ActionMailer::MessageDelivery#deliver_now" do + let(:access_token) { SecureRandom.hex } + + context "when token request is successful" do + before do + stub_token_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, response_status: 200) + end + + context "when send mail request returns response status 202" do + it "sends and returns mail" do + stub_send_mail_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + expect(message.deliver_now).to eq(message) + end + + it "sends mail including bcc field" do + message.bcc = "bcc@example.com" + + stub_send_mail_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + message.deliver_now + end + + it "does not change message[:bcc].include_in_headers" do + message.bcc = "bcc@example.com" + expected_message_bcc_include_in_headers = "42" + message[:bcc].include_in_headers = expected_message_bcc_include_in_headers + + stub_send_mail_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, message: message, response_status: 202) + + message.deliver_now + + expect(message[:bcc].include_in_headers).to eq(expected_message_bcc_include_in_headers) + end + end + + context "when send mail request returns response status other than 202" do + it "raises MicrosoftGraphMailer::DeliveryError" do + stub_send_mail_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, message: message, response_status: 200) + + expect { message.deliver_now }.to raise_error(MicrosoftGraphMailer::DeliveryError) + end + end + end + + context "when token request is not successful" do + before do + stub_token_request(microsoft_graph_settings: microsoft_graph_settings, access_token: access_token, response_status: 503) + end + + it "raises OAuth2::Error" do + expect { message.deliver_now }.to raise_error(OAuth2::Error) + end + end + end +end diff --git a/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer_spec.rb b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer_spec.rb new file mode 100644 index 00000000000..3306e91db38 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/lib/microsoft_graph_mailer_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe MicrosoftGraphMailer do + describe "::VERSION" do + it "returns a version number" do + expect(MicrosoftGraphMailer::VERSION).to eq("0.1.0") + end + end + + describe "::Error" do + it "its superclass is StandardError" do + expect(MicrosoftGraphMailer::Error.superclass).to eq(StandardError) + end + end + + describe "::ConfigurationError" do + it "its superclass is MicrosoftGraphMailer::Error" do + expect(MicrosoftGraphMailer::ConfigurationError.superclass).to eq(MicrosoftGraphMailer::Error) + end + end + + describe "::DeliveryError" do + it "its superclass is MicrosoftGraphMailer::Error" do + expect(MicrosoftGraphMailer::DeliveryError.superclass).to eq(MicrosoftGraphMailer::Error) + end + end +end |