Add more register tests

This commit is contained in:
Pim Kunis 2021-07-13 19:35:02 +02:00
parent 096c99df92
commit 6b9c9333b8
6 changed files with 86 additions and 33 deletions

View file

@ -23,7 +23,7 @@ defmodule MatrixServerWeb.API.Register do
:username,
:inhibit_login
])
|> validate_required([:password, :username])
|> validate_required([:password])
end
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)

View file

@ -14,20 +14,14 @@ defmodule MatrixServerWeb.AuthController do
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
case Register.changeset(params) do
%Changeset{valid?: true} = cs ->
# TODO: refactor this
input =
apply_changes(cs)
|> Map.from_struct()
|> MatrixServer.maybe_update_map(:initial_device_display_name, :display_name)
|> MatrixServer.maybe_update_map(:username, :localpart)
|> MatrixServer.maybe_update_map(:password, :password_hash, &Bcrypt.hash_pwd_salt/1)
api = apply_changes(cs)
case Account.register(input) |> Repo.transaction() do
case Account.register(api) |> Repo.transaction() do
{:ok, %{device_with_access_token: device}} ->
data = %{user_id: MatrixServer.get_mxid(device.localpart)}
data =
if not input.inhibit_login do
if not api.inhibit_login do
data
|> Map.put(:device_id, device.device_id)
|> Map.put(:access_token, device.access_token)
@ -40,7 +34,6 @@ defmodule MatrixServerWeb.AuthController do
|> json(data)
{:error, _, cs, _} ->
IO.inspect(cs)
put_error(conn, Register.get_error(cs))
end

View file

@ -4,7 +4,7 @@ defmodule MatrixServerWeb.Plug.Error do
@error_code_and_message %{
bad_json: {400, "M_BAD_JSON", "Bad request."},
user_in_use: {400, "M_USE_IN_USE", "Username is already taken."},
user_in_use: {400, "M_USER_IN_USE", "Username is already taken."},
invalid_username: {400, "M_INVALID_USERNAME", "Invalid username."},
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
unrecognized: {400, "M_UNRECOGNIZED", "Unrecognized request."},