mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 17:44:43 +03:00
0007c0c108
* Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
74 lines
1.9 KiB
Elixir
74 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})
|
|
|
|
assert Plug.Test.conn(:get, "/")
|
|
|> Errors.error(:bad_request, changeset)
|
|
|> json_response(400)
|
|
|> Map.fetch!("errors") == [
|
|
%{"detail" => "age: is invalid"},
|
|
%{"detail" => "email: has invalid format"},
|
|
%{"detail" => "name: can't be blank"}
|
|
]
|
|
end
|
|
end
|
|
end
|