nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: swoosh/swoosh
Pull request #145 (2017-07-25 05:48:45)

Allow dynamic adapter
@stevedomin @xadhoom

This should finish no.2 of https://github.com/swoosh/swoosh/issues/84#issuecomment-317634468

stevedomin: 👍 

Patch:
diff --git a/lib/swoosh/mailer.ex b/lib/swoosh/mailer.ex
index c1960e0..b520b0e 100644
--- a/lib/swoosh/mailer.ex
+++ b/lib/swoosh/mailer.ex
@@ -65,11 +65,10 @@ defmodule Swoosh.Mailer do
       @adapter adapter
       @config config
 
-      def __adapter__, do: @adapter
-
       def deliver(email, config \\ [])
       def deliver(email, config) do
-        Swoosh.Mailer.deliver(@adapter, email, Keyword.merge(@config, config))
+        config = Keyword.merge(@config, config)
+        Swoosh.Mailer.deliver(Keyword.get(config, :adapter, @adapter), email, config)
       end
 
       def deliver!(email, config \\ [])


Test patch:
diff --git a/test/swoosh/mailer_test.exs b/test/swoosh/mailer_test.exs
index af9b920..7775832 100644
--- a/test/swoosh/mailer_test.exs
+++ b/test/swoosh/mailer_test.exs
@@ -3,9 +3,12 @@ defmodule Swoosh.MailerTest do
 
   alias Swoosh.DeliveryError
 
-  Application.put_env(:swoosh, Swoosh.MailerTest.FakeMailer,
+  Application.put_env(
+    :swoosh,
+    Swoosh.MailerTest.FakeMailer,
     api_key: "api-key",
-    domain: "avengers.com")
+    domain: "avengers.com"
+  )
 
   defmodule FakeAdapter do
     use Swoosh.Adapter
@@ -18,11 +21,13 @@ defmodule Swoosh.MailerTest do
   end
 
   setup_all do
-    valid_email = Swoosh.Email.new(from: "tony.stark@example.com",
-                                   to: "steve.rogers@example.com",
-                                   subject: "Hello, Avengers!",
-                                   html_body: "<h1>Hello</h1>",
-                                   text_body: "Hello")
+    valid_email = Swoosh.Email.new(
+      from: "tony.stark@example.com",
+      to: "steve.rogers@example.com",
+      subject: "Hello, Avengers!",
+      html_body: "<h1>Hello</h1>",
+      text_body: "Hello"
+    )
     {:ok, valid_email: valid_email}
   end
 
@@ -34,6 +39,14 @@ defmodule Swoosh.MailerTest do
     end
   end
 
+  test "dynamic adapter", %{valid_email: email} do
+    defmodule OtherAdapterMailer do
+      use Swoosh.Mailer, otp_app: :swoosh, adapter: NotExistAdapter
+    end
+
+    assert {:ok, _} = OtherAdapterMailer.deliver(email, adapter: FakeAdapter)
+  end
+
   test "should raise if deliver!/2 is called with invalid from", %{valid_email: valid_email} do
     assert_raise DeliveryError, "delivery error: expected \"from\" to be set", fn ->
       Map.put(valid_email, :from, nil) |> FakeMailer.deliver!()
Repository: swoosh/swoosh
Pull request #914 (2024-05-29 22:17:49)

Fix ex_aws region override
Fixes issue #913 allowing to override the region set in ex_aws config.


Patch:
diff --git a/lib/swoosh/adapters/ex_aws_amazon_ses.ex b/lib/swoosh/adapters/ex_aws_amazon_ses.ex
index e67ed81..c31ab43 100644
--- a/lib/swoosh/adapters/ex_aws_amazon_ses.ex
+++ b/lib/swoosh/adapters/ex_aws_amazon_ses.ex
@@ -59,8 +59,8 @@ defmodule Swoosh.Adapters.ExAwsAmazonSES do
   alias Swoosh.Adapters.AmazonSES
 
   def deliver(%Email{} = email, config \\ []) do
-    credentials = ExAws.Config.new(:ses)
-
+    region_override = Keyword.get(config, :region)
+    credentials = get_credentials(region_override)
     config = add_credentials(config, credentials)
 
     email
@@ -68,6 +68,9 @@ defmodule Swoosh.Adapters.ExAwsAmazonSES do
     |> AmazonSES.deliver(config)
   end
 
+  defp get_credentials(nil), do: ExAws.Config.new(:ses)
+  defp get_credentials(region), do: ExAws.Config.new(:ses, region: region)
+
   defp add_credentials(config, credentials) do
     Keyword.merge(
       [


Test patch:
diff --git a/test/swoosh/adapters/ex_aws_amazonses_test.exs b/test/swoosh/adapters/ex_aws_amazonses_test.exs
index 8f19e63..7dfa829 100644
--- a/test/swoosh/adapters/ex_aws_amazonses_test.exs
+++ b/test/swoosh/adapters/ex_aws_amazonses_test.exs
@@ -63,6 +63,37 @@ defmodule Swoosh.Adapters.ExAwsAmazonSESTest do
              {:ok, %{id: "messageId", request_id: "requestId"}}
   end
 
+  test "unsupported region can be overridden with a supported region", %{
+    bypass: bypass,
+    config: config,
+    valid_email: email
+  } do
+    Application.put_env(:ex_aws, :access_key_id, "FAKE")
+    Application.put_env(:ex_aws, :secret_access_key, "FAKE")
+    Application.put_env(:ex_aws, :region, "ap-east-1")
+
+    on_exit(fn ->
+      Application.put_env(:ex_aws, :access_key_id, nil)
+      Application.put_env(:ex_aws, :secret_access_key, nil)
+      Application.put_env(:ex_aws, :region, nil)
+    end)
+
+    Bypass.expect(bypass, fn conn ->
+      import Plug.Conn
+
+      [authorization_header] = get_req_header(conn, "authorization")
+      assert "AWS4-HMAC-SHA256 Credential=FAKE/" <> _ = authorization_header
+      assert authorization_header =~ "us-east-1"
+
+      resp(conn, 200, @success_response)
+    end)
+
+    config = Keyword.put(config, :region, "us-east-1")
+
+    assert ExAwsAmazonSES.deliver(email, config) ==
+             {:ok, %{id: "messageId", request_id: "requestId"}}
+  end
+
   test "validate_config/1 with valid config", %{config: config} do
     assert ExAwsAmazonSES.validate_config(config) == :ok
   end