summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHadrien <26697460+ketsapiwiq@users.noreply.github.com>2020-11-11 16:45:04 +0100
committerPierre de Lacroix <pierre@pdelacroix.com>2021-03-03 18:13:42 +0100
commit52744181524f196a01d4013d6d918feba31ea192 (patch)
tree6272eaebdebf9937e2bf2561951754c937fb4841
parentregister interface for client and lowlevel (diff)
register interface tests for client and lowlevel
-rw-r--r--test/polyjuice/client/low_level_test.exs72
-rw-r--r--test/polyjuice/client_test.exs167
2 files changed, 239 insertions, 0 deletions
diff --git a/test/polyjuice/client/low_level_test.exs b/test/polyjuice/client/low_level_test.exs
index 7840e83..2c4b942 100644
--- a/test/polyjuice/client/low_level_test.exs
+++ b/test/polyjuice/client/low_level_test.exs
@@ -171,4 +171,76 @@ defmodule Polyjuice.Client.LowLevelTest do
File.rm_rf(tmpdir)
end
end
+
+ test "register" do
+ {:ok, tmpdir} = TestUtil.mktmpdir("register-")
+
+ try do
+ tmpdir_charlist = to_charlist(tmpdir)
+
+ :inets.start()
+
+ {:ok, httpd_pid} =
+ :inets.start(
+ :httpd,
+ port: 0,
+ server_name: 'sync.test',
+ server_root: tmpdir_charlist,
+ document_root: tmpdir_charlist,
+ bind_address: {127, 0, 0, 1},
+ modules: [:mod_esi],
+ erl_script_alias: {'', [Polyjuice.ClientTest.Httpd]}
+ )
+
+ port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port)
+
+ client =
+ Polyjuice.Client.LowLevel.create(
+ "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/",
+ test: true
+ )
+
+ opts = [username: "alice", password: "password", device_id: "foo"]
+
+ assert Polyjuice.Client.LowLevel.register(
+ client,
+ opts
+ ) ==
+ {:ok,
+ %{
+ "access_token" => "m.id.user_login",
+ "device_id" => "foo",
+ "user_id" => "@alice:example.org"
+ }}
+
+ # Test error when registering if username is taken
+
+ {:ok, httpd_pid} =
+ :inets.start(
+ :httpd,
+ port: 0,
+ server_name: 'sync.test',
+ server_root: tmpdir_charlist,
+ document_root: tmpdir_charlist,
+ bind_address: {127, 0, 0, 1},
+ modules: [:mod_esi],
+ erl_script_alias: {'', [Polyjuice.ClientTest.Httpd.UserAlreadyTaken]}
+ )
+
+ port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port)
+
+ client =
+ Polyjuice.Client.LowLevel.create(
+ "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd.UserAlreadyTaken/",
+ test: true
+ )
+
+ assert {:error, 400, %{"errcode" => "M_USER_IN_USE", "error" => _}} =
+ Polyjuice.Client.LowLevel.register(client, opts)
+
+ :inets.stop(:httpd, httpd_pid)
+ after
+ File.rm_rf(tmpdir)
+ end
+ end
end
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs
index aad44a3..a87fa4a 100644
--- a/test/polyjuice/client_test.exs
+++ b/test/polyjuice/client_test.exs
@@ -44,6 +44,9 @@ defmodule Polyjuice.ClientTest do
"client/r0/logout" ->
handle_logout(session_id, env, input)
+ "client/r0/register" ->
+ handle_register(session_id, env, input)
+
_ ->
:mod_esi.deliver(
session_id,
@@ -70,6 +73,43 @@ defmodule Polyjuice.ClientTest do
'Content-Type: application/json\r\n\r\n{}'
)
end
+
+ defp handle_register(session_id, _env, {_, input}) do
+ inhibit_login = to_string(input) |> Jason.decode!() |> Map.get("inhibit_login")
+ username = to_string(input) |> Jason.decode!() |> Map.get("username")
+
+ response =
+ if inhibit_login == "true" do
+ 'Content-Type: application/json\r\n\r\n{"user_id":"@#{username}:example.org"}'
+ else
+ 'Content-Type: application/json\r\n\r\n{"user_id":"@#{username}:example.org","access_token":"m.id.user_login","device_id":"foo"}'
+ end
+
+ :mod_esi.deliver(
+ session_id,
+ response
+ )
+ end
+ end
+
+ defmodule Httpd.LoggedOut do
+ # just tells the user that they were logged out, no matter what
+ def _matrix(session_id, _env, _input) do
+ :mod_esi.deliver(
+ session_id,
+ 'Status: 401 Unauthorized\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN_TOKEN","error":"Unknown token"}'
+ )
+ end
+ end
+
+ defmodule Httpd.UserAlreadyTaken do
+ # just tells the user that the user_id is already taken, no matter what
+ def _matrix(session_id, _env, _input) do
+ :mod_esi.deliver(
+ session_id,
+ 'Status: 400 Unauthorized\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_USER_IN_USE","error":"User ID already taken."}'
+ )
+ end
end
test "transaction_id is unique" do
@@ -430,4 +470,131 @@ defmodule Polyjuice.ClientTest do
assert Polyjuice.Client.API.get_user_and_device(client) == {"@alice:example.org", "DEVICEID"}
end
+
+ test "register" do
+ {:ok, tmpdir} = TestUtil.mktmpdir("register-")
+
+ storage = Polyjuice.Client.Storage.Ets.open()
+
+ try do
+ tmpdir_charlist = to_charlist(tmpdir)
+
+ :inets.start()
+
+ {:ok, httpd_pid} =
+ :inets.start(
+ :httpd,
+ port: 0,
+ server_name: 'sync.test',
+ server_root: tmpdir_charlist,
+ document_root: tmpdir_charlist,
+ bind_address: {127, 0, 0, 1},
+ modules: [:mod_esi],
+ erl_script_alias: {'', [Polyjuice.ClientTest.Httpd]}
+ )
+
+ port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port)
+
+ {:ok, client_pid} =
+ Polyjuice.Client.start_link(
+ "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/",
+ access_token: nil,
+ sync: false,
+ storage: storage,
+ handler: self(),
+ test: true
+ )
+
+ client = Polyjuice.Client.get_client(client_pid)
+
+ opts = [username: "alice", password: "password", device_id: "foo"]
+
+ assert Polyjuice.Client.register(client, opts) ==
+ {:ok,
+ %{
+ "access_token" => "m.id.user_login",
+ "device_id" => "foo",
+ "user_id" => "@alice:example.org"
+ }}
+
+ assert GenServer.call(client.pid, :get_state) == %{
+ access_token: "m.id.user_login",
+ user_id: "@alice:example.org",
+ device_id: "foo"
+ }
+
+ assert_receive({:polyjuice_client, :logged_in, {"@alice:example.org", "foo", _}})
+
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") ==
+ "m.id.user_login"
+
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") ==
+ "@alice:example.org"
+
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id") == "foo"
+
+ Polyjuice.Client.log_out(client)
+
+ # Test inhibit_login: :true
+ opts = [
+ username: "bob",
+ password: "password",
+ device_id: "foo",
+ inhibit_login: true
+ ]
+
+ assert Polyjuice.Client.register(client, opts) ==
+ {:ok,
+ %{
+ "user_id" => "@bob:example.org"
+ }}
+
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") ==
+ nil
+
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") == nil
+ assert Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id") == nil
+
+ assert GenServer.call(client.pid, :get_state) |> Map.get(:access_token) == nil
+
+ # Test error when registering if username is taken
+ {:ok, httpd_pid} =
+ :inets.start(
+ :httpd,
+ port: 0,
+ server_name: 'sync.test',
+ server_root: tmpdir_charlist,
+ document_root: tmpdir_charlist,
+ bind_address: {127, 0, 0, 1},
+ modules: [:mod_esi],
+ erl_script_alias: {'', [Polyjuice.ClientTest.Httpd.UserAlreadyTaken]}
+ )
+
+ port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port)
+
+ {:ok, client_pid} =
+ Polyjuice.Client.start_link(
+ "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd.UserAlreadyTaken/",
+ access_token: nil,
+ sync: false,
+ storage: storage,
+ handler: self(),
+ test: true
+ )
+
+ client = Polyjuice.Client.get_client(client_pid)
+
+ opts = [username: "alice", password: "password", device_id: "foo"]
+
+ assert {:error, 400, %{"errcode" => "M_USER_IN_USE", "error" => _}} =
+ Polyjuice.Client.register(client, opts)
+
+ Polyjuice.Client.API.stop(client)
+
+ :inets.stop(:httpd, httpd_pid)
+ after
+ Polyjuice.Client.Storage.close(storage)
+ File.rm_rf(tmpdir)
+ end
+ end
end