analytics/test/plausible_web/plugins/api/errors_test.exs
hq1 117eef000d
Upgrade Erlang/Elixir stack (#3454)
* Bump deps

* Bump stack

* Fix deprecation warnings

* Fix VCR cassettes mismatch due to OTP-18414

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Format & fix flaky tests

* Handle raw IPv4 hostnames; test public suffix TLD

* Configure locus db cache_dir

So that maxmind unavailability doesn't affect
application startup. PERSISTENT_CACHE_DIR env var is used
to point locus at the GeoIP DB file.

* WIP: Remove ExVCR

* Fix test env config

* Fixup exvcr

* Remove exvcr from deps

* Add convert script

* Remove exvcr cassettes

* Remove convert script

* Rename test

* Update moduledoc

* Update dockerfile

* Bump CI cache

* Tag more slow tests, why not?

* Use charlist for locus cache option

* Pin nodejs

* Merge google tests, make them async

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
2023-10-24 10:33:48 +02:00

76 lines
1.9 KiB
Elixir

defmodule PlausibleWeb.Plugins.API.ErrorsTest do
use ExUnit.Case, async: true
import Phoenix.ConnTest, only: [json_response: 2]
alias PlausibleWeb.Plugins.API.Errors
describe "unauthorized/1" do
test "sends an 401 response with the `www-authenticate` header set" do
conn =
Plug.Test.conn(:get, "/")
|> Errors.unauthorized()
assert conn.halted
assert json_response(conn, 401) == %{
"errors" => [%{"detail" => "Plugins API: unauthorized"}]
}
assert Plug.Conn.get_resp_header(conn, "www-authenticate") == [
~s[Basic realm="Plugins API Access"]
]
end
end
describe "error/3" do
test "formats the given error message" do
message = "Some message"
conn =
Plug.Test.conn(:get, "/")
|> Errors.error(:forbidden, message)
assert conn.halted
assert json_response(conn, 403) == %{
"errors" => [%{"detail" => "Some message"}]
}
end
defmodule Example do
use Ecto.Schema
import Ecto.Changeset
schema "" do
field(:name)
field(:email)
field(:age, :integer)
end
def changeset(example, params \\ %{}) do
example
|> cast(params, [:name, :email, :age])
|> validate_required([:name, :email])
|> validate_format(:email, ~r/@/)
|> validate_inclusion(:age, 18..100)
end
end
test "formats changeset errors" do
changeset = Example.changeset(%Example{}, %{email: "foo", age: 101})
errors =
Plug.Test.conn(:get, "/")
|> Errors.error(:bad_request, changeset)
|> json_response(400)
|> Map.fetch!("errors")
assert Enum.count(errors) == 3
assert %{"detail" => "age: is invalid"} in errors
assert %{"detail" => "email: has invalid format"} in errors
assert %{"detail" => "name: can't be blank"} in errors
end
end
end