Compare commits

...
3 Commits
Author SHA1 Message Date
KKlochko 01efdc201d Update the docker image for the CI/CD pipeline.
continuous-integration/drone/tag Build is passing
2024-08-05 21:56:02 +03:00
KKlochko 8a423b721d Add ErrorHandler for API Auth.
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build was killed
2023-08-12 21:02:05 +03:00
KKlochko 06dfee5b57 Add tests for user login.
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build was killed
2023-08-11 15:38:00 +03:00
5 changed files with 47 additions and 9 deletions
+3 -3
View File
@@ -12,7 +12,7 @@ environment:
steps:
- name: install dependencies
image: elixir:1.14.0-slim
image: elixir:1.14.0
volumes:
- name: mix
path: /root/.mix
@@ -23,7 +23,7 @@ steps:
- mix compile
- name: run migrations
image: elixir:1.14.0-slim
image: elixir:1.14.0
volumes:
- name: mix
path: /root/.mix
@@ -31,7 +31,7 @@ steps:
- mix ecto.setup
- name: run tests
image: elixir:1.14.0-slim
image: elixir:1.14.0
volumes:
- name: mix
path: /root/.mix
+6 -1
View File
@@ -43,7 +43,6 @@
** 0.4.10 <2023-08-03 Thu>
Add the CI/CD configuration.
Add the test config.
** 0.5.1 <2023-08-04 Fri>
Add a generator for Link with random shorten.
Update Links.create_one to use a function.
@@ -61,3 +60,9 @@
Add the authentication for API sing in.
** 0.8.2 <2023-08-10 Thu>
Add the auth pipeline for the Link API path.
** 0.8.3 <2023-08-11 Fri>
Add tests for user login.
** 0.8.4 <2023-08-12 Sat>
Add ErrorHandler for API Auth.
** 0.8.5 <2024-08-05 Mon>
Update the docker image for the CI/CD pipeline.
@@ -0,0 +1,10 @@
defmodule LinkShortenerWeb.Auth.ErrorHandler do
import Plug.Conn
def auth_error(conn, {type, _reason}, _opts) do
body = Poison.encode!(%{error: to_string(type)})
conn
|> put_resp_content_type("application/json")
|> send_resp(401, body)
end
end
@@ -6,7 +6,7 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do
alias LinkShortener.Accounts.User
@create_attrs %{
email: "some_email@mail.com",
email: "user@mail.com",
password: "some password"
}
@@ -15,6 +15,11 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do
password: "some updated password"
}
@invalid_password_attrs %{
email: "user@mail.com",
password: ""
}
@invalid_attrs %{
email: nil,
encrypted_password: nil
@@ -24,11 +29,11 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "create user" do
describe "create user with sign up" do
test "renders user when data is valid", %{conn: conn} do
conn = post(conn, Routes.v1_user_path(conn, :create), user: @create_attrs)
assert %{
"email" => "some_email@mail.com",
"email" => "user@mail.com",
"token" => token
} = json_response(conn, 201)
end
@@ -39,8 +44,26 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do
end
end
describe "user sign in" do
setup [:create_user]
test "renders user when data is valid", %{conn: conn} do
conn = post(conn, Routes.v1_user_path(conn, :signin), @create_attrs)
assert %{
"email" => email,
"token" => token,
} = json_response(conn, 201)
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.v1_user_path(conn, :signin), @invalid_password_attrs)
assert "Unauthorized" == json_response(conn, 401)
end
end
defp create_user(_) do
user = user_fixture()
user = user_fixture(@create_attrs)
%{user: user}
end
end
+1 -1
View File
@@ -33,7 +33,7 @@ defmodule LinkShortener.AccountsFixtures do
def user_token_fixture(attrs \\ %{}) do
user_params = %{
email: "user@mail.com",
password: "hello world 12345"
password: "some password"
}
{:ok, %User{} = user} = Accounts.register_user(user_params)