hassamali

hassamali

Mocking with s3 ex_aws_s3

I want to mock the test with s3 library just to make sure my module is working. This is what i have done so far but i don’t want to use aws keys and secret what else i need to do so my testing scenario don’t use the keys and just test the module

uploads_test.ex

defmodule SignatureWeb.UploadsTest do
  use ExUnit.Case, async: false
  alias Signature.Uploads
  import Mox

  setup :verify_on_exit!

  describe "upload/1" do
    test "write s3 bucket with file and path name and return ok with s3 path" do
      content = "signature"

     SignatureWeb.ExAwsHttpMock
      |> expect(:request, 1, fn _method, _url, _body, _headers, _opts ->
        {:ok, %{status_code: 200}}
      end)

      assert Uploads.upload(content) == {:ok, "signatures/signature"}
    end
  end
end

uploads.ex ( upload module which logic i want to test in test )

defmodule SignatureWeb.Uploads do
  def upload(content) do
    # path to bucket + filename
    s3_path = "signatures/#{content}"

    bucket_name =
      :email_messaging
      |> Application.fetch_env!(__MODULE__)
      |> Keyword.fetch!(:bucket_name)

    bucket_name
    |> ExAws.S3.put_object(s3_path, content)
    |> ExAws.request()
    |> handle_aws_response(s3_path)
  end

  defp handle_aws_response({:ok, _}, s3_path), do: {:ok, s3_path}
  defp handle_aws_response({:error, reason}, _), do: {:error, reason}
end

Most Liked

zgore

zgore

For those who are still wondering, yes it’s possible to use mock with ExAws without wrapping ExAws.request/2 in another module in order to mock it.

To do so, I read how they are testing their own module : ExAws Github

In fact, they are mocking the HTTP client underneath.
We can do exactly the same thing and mock the HTTP client in our tests.

In the business logic code:

...
    response =
      bucket_name
      |> S3.get_object(filename_path)
      |> ExAws.request(get_ex_aws_request_config_override())
...
  def get_ex_aws_request_config_override,
    do: Application.get_env(:ex_aws, :request_config_override)

config.exs:

...
config :ex_aws,
  ...
  # note: it's a custom config, ex_aws will not take it in account directly
  request_config_override: %{}
...

test.exs:

...
config :ex_aws,
  request_config_override: %{
    http_client: ExAws.Request.HttpMock,
    access_key_id: "AKIAIOSFODNN7EXAMPLE",
    secret_access_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  }
...

In the test code:

stub(ExAws.Request.HttpMock, :request, fn
      _method, _url, _body, _headers, _opts -> 
           # whatever you want to return, example:
           {:ok, %{status_code: 200, body: ""}}
    end)

I hope it will help !

Where Next?

Popular in Questions Top

New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement