henriquesati
I want to test a endpoint that makes external api calls. What I want to do is to only do this external api call once (generate a payload) and only generate new ones when the tag ;value is provided. Here is my tring but on each test it generates a new payload anyway
defmodule TestesPayTest.BusinessRules do
use ExUnit.Case, async: true
use Plug.Test
require Logger
@create_qr_url "https://sandbox.asaas.com/api/v3/pix/qrCodes/static"
@pix_key "ab09d852-14bd-43ac-91eb-d38b717e6aea"
setup_all do
access_token = System.get_env("ACCESS_TOKEN")
base_fee = 0.0765
basic_ref_fee = 0.0165
payload =
case create_payload(100) do
{:ok, payload} -> payload
{:error, _reason} -> nil
end
:ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)
Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})
{:ok, %{access_token: access_token, base_fee: base_fee, basic_ref_fee: basic_ref_fee}}
end
setup context do
value = context[:value]
if value do
new_payload =
case create_payload(value) do
{:ok, new_payload} ->
Map.put(context, :payload, new_payload)
{:error, reason} ->
Logger.error("Falha ao criar novo payload: #{inspect(reason)}")
nil
end
end
end
tests
describe "chain invalida" do
@tag value: 630
test "POST /api/create_contract", %{payload: payload} do
Logger.debug("deve ser padrao #{inspect(payload)}")
body = Jason.encode!(%{
"chain" => "qlqr",
"coin" => "ETH",
"payload" => payload
})
conn = conn(:post, "/api/create_contract", body)
|> put_req_header("content-type", "application/json")
|> TestesPay.MyRouter.call(body)
IO.inspect(conn.resp_body)
expected_error = %{
"field" => "chain",
"msg" => "rede invalida"
}
assert conn.status == 400
assert Jason.decode!(conn.resp_body) == expected_error
end
end
describe "coin invalida" do
test "POST /api/create_contract", %{payload: payload} do
Logger.debug("deve ser padrao #{inspect(payload)}")
body = Jason.encode!(%{
"chain" => "eth",
"coin" => "xereb",
"payload" => payload
})
conn = conn(:post, "/api/create_contract", body)
|> put_req_header("content-type", "application/json")
|> TestesPay.MyRouter.call(body)
IO.inspect(conn.resp_body)
expected_error = %{
"field" => "coin",
"msg" => "moeda invalida"
}
assert conn.status == 400
assert Jason.decode!(conn.resp_body) == expected_error
end
end
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
henriquesati