diff options
Diffstat (limited to 'vendor/gems/microsoft_graph_mailer/spec')
6 files changed, 358 insertions, 0 deletions
diff --git a/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab.txt b/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab.txt new file mode 100644 index 00000000000..1df29200042 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab.txt @@ -0,0 +1 @@ +GitLab diff --git a/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab_logo.png b/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab_logo.png Binary files differnew file mode 100644 index 00000000000..12525056939 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/fixtures/attachments/gitlab_logo.png 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 diff --git a/vendor/gems/microsoft_graph_mailer/spec/spec_helper.rb b/vendor/gems/microsoft_graph_mailer/spec/spec_helper.rb new file mode 100644 index 00000000000..ac5dd817904 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/spec/spec_helper.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require "rails" +require "action_mailer/railtie" + +require "microsoft_graph_mailer" + +require "mail" + +require "webmock/rspec" + +RSpec.configure do |config| +end + +def fixture_path(*path) + File.join(__dir__, "fixtures", path) +end + +def stub_token_request(microsoft_graph_settings:, access_token:, response_status:) + stub_request( + :post, + "#{microsoft_graph_settings[:azure_ad_endpoint]}/#{microsoft_graph_settings[:tenant]}/oauth2/v2.0/token" + ).with( + body: { + "grant_type" => "client_credentials", + "scope" => "#{microsoft_graph_settings[:graph_endpoint]}/.default" + } + ).to_return( + body: { + "token_type" => "Bearer", + "expires_in" => "3599", + "access_token" => access_token + }.to_json, + status: response_status, + headers: { "content-type" => "application/json; charset=utf-8" } + ) +end + +def stub_send_mail_request(microsoft_graph_settings:, access_token:, message:, response_status:) + if message[:bcc] + previous_message_bcc_include_in_headers = message[:bcc].include_in_headers + message[:bcc].include_in_headers = true + end + + stub_request( + :post, + "#{microsoft_graph_settings[:graph_endpoint]}/v1.0/users/#{microsoft_graph_settings[:user_id]}/sendMail" + ).with( + body: Base64.encode64(message.encoded), + headers: { + "Authorization" => "Bearer #{access_token}", + "Content-Type" => "text/plain" + } + ).to_return( + body: "", + status: response_status + ) +ensure + message[:bcc].include_in_headers = previous_message_bcc_include_in_headers if message[:bcc] +end |