Add start of controller testing

This commit is contained in:
Pim Kunis 2021-07-13 17:08:07 +02:00
parent 598af7a884
commit 096c99df92
14 changed files with 106 additions and 87 deletions

View file

@ -38,9 +38,13 @@ defmodule MatrixServer.Account do
|> Multi.insert(:device, fn %{account: account} ->
device_id = Device.generate_device_id(account.localpart)
# TODO: fix device_id with UUID
params =
Map.update(params, :device_id, device_id, fn
nil -> device_id
x -> x
end)
Ecto.build_assoc(account, :devices)
|> Map.put(:device_id, device_id)
|> Device.changeset(params)
end)
|> Multi.run(:device_with_access_token, &Device.insert_new_access_token/2)

View file

@ -40,6 +40,7 @@ defmodule MatrixServer.Device do
end
def generate_device_id(localpart) do
# TODO: use random string instead
time_string =
DateTime.utc_now()
|> DateTime.to_unix()

View file

@ -1,44 +1,31 @@
# https://gist.github.com/char0n/6fca76e886a2cfbd3aaa05526f287728
defmodule MatrixServerWeb.API.Login do
use Ecto.Schema
import Ecto.Changeset
# TODO: Maybe use inline embedded schema here
# https://hexdocs.pm/ecto/Ecto.Schema.html#embeds_one/3
defmodule MatrixServerWeb.API.Login.Identifier do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field :type, :string
field :user, :string
end
def changeset(identifier, params) do
identifier
|> cast(params, [:type, :user])
|> validate_required([:type, :user])
end
end
alias MatrixServerWeb.API.Login.Identifier
@primary_key false
embedded_schema do
field :type, :string
field :password, :string
field :device_id, :string
field :initial_device_display_name, :string
embeds_one :identifier, Identifier
embeds_one :identifier, Identifier, primary_key: false do
field :type, :string
field :user, :string
end
end
def changeset(params) do
%__MODULE__{}
|> cast(params, [:type, :password, :device_id, :initial_device_display_name])
|> cast_embed(:identifier, with: &Identifier.changeset/2, required: true)
|> cast_embed(:identifier, with: &identifier_changeset/2, required: true)
|> validate_required([:type, :password])
end
def identifier_changeset(identifier, params) do
identifier
|> cast(params, [:type, :user])
|> validate_required([:type, :user])
end
end

View file

@ -14,6 +14,7 @@ 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()

View file

@ -15,7 +15,7 @@ defmodule MatrixServerWeb.Router do
scope "/_matrix", MatrixServerWeb do
pipe_through :public
scope "/client/r0", as: :client do
scope "/client/r0" do
post "/register", AuthController, :register
get "/register/available", AccountController, :available
get "/login", AuthController, :login_types
@ -28,7 +28,7 @@ defmodule MatrixServerWeb.Router do
scope "/_matrix", MatrixServerWeb do
pipe_through :authenticated
scope "/client/r0", as: :client do
scope "/client/r0" do
get "/account/whoami", AccountController, :whoami
post "/logout", AccountController, :logout
post "/logout/all", AccountController, :logout_all