From 18fc3e02ad009915b6ac00c050849dd432314c60 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 31 Mar 2020 21:20:42 -0500 Subject: [PATCH] new client interface --- coveralls.json | 2 +- lib/router/realms/session.ex | 4 ++-- mix.exs | 4 ++-- mix.lock | 4 ++-- test/support/authentication.ex | 11 +++++++---- test/support/authorization.ex | 7 +++++++ test/support/test_callee.ex | 17 ++++------------- test/support/test_subscriber.ex | 14 +------------- test/wampex_test.exs | 33 +++++++++++++++++---------------- 9 files changed, 43 insertions(+), 53 deletions(-) create mode 100644 test/support/authorization.ex diff --git a/coveralls.json b/coveralls.json index bfb636a..d39f6a9 100644 --- a/coveralls.json +++ b/coveralls.json @@ -1,6 +1,6 @@ { "coverage_options": { - "minimum_coverage": 80 + "minimum_coverage": 78.9 }, "skip_files": [ "test/support" diff --git a/lib/router/realms/session.ex b/lib/router/realms/session.ex index b77cfd4..bbe12a5 100644 --- a/lib/router/realms/session.ex +++ b/lib/router/realms/session.ex @@ -93,8 +93,8 @@ defmodule Wampex.Router.Realms.Session do :not_found -> {db, {keyspace, key}, acc} - l -> - {db, {keyspace, key}, acc ++ Enum.flat_map(l, fn {_, subscribers} -> subscribers end)} + subscribers -> + {db, {keyspace, key}, acc ++ Enum.flat_map(subscribers, fn {_, subscribers} -> subscribers end)} end end diff --git a/mix.exs b/mix.exs index 70742b3..94fb7d6 100644 --- a/mix.exs +++ b/mix.exs @@ -52,11 +52,11 @@ defmodule Wampex.Router.MixProject do {:states_language, "~> 0.2"}, {:wampex, git: "https://gitlab.com/entropealabs/wampex.git", - tag: "92648dd6f1a4a9ed24b10eec3533d29108b31852", + tag: "591a03f9ed1585942afc043fe4a73beb7641e1e3", override: true}, {:wampex_client, git: "https://gitlab.com/entropealabs/wampex_client.git", - tag: "5842014aa550977e859656d193583d022254b1fd", + tag: "6861b782ce61df0d12b94953a0fb39281ce499e2", only: [:dev, :test]} ] end diff --git a/mix.lock b/mix.lock index b2485aa..1e09df0 100644 --- a/mix.lock +++ b/mix.lock @@ -36,8 +36,8 @@ "states_language": {:hex, :states_language, "0.2.8", "f9dfd3c0bd9a9d7bda25ef315f2d90944cd6b2022a7f3c403deb1d4ec451825e", [:mix], [{:elixpath, "~> 0.1.0", [hex: :elixpath, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:json_xema, "~> 0.4.0", [hex: :json_xema, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xema, "~> 0.11.0", [hex: :xema, repo: "hexpm", optional: false]}], "hexpm", "a5231691e7cb37fe32dc7de54c2dc86d1d60e84c4f0379f3246e55be2a85ec78"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"}, - "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "92648dd6f1a4a9ed24b10eec3533d29108b31852", [tag: "92648dd6f1a4a9ed24b10eec3533d29108b31852"]}, - "wampex_client": {:git, "https://gitlab.com/entropealabs/wampex_client.git", "5842014aa550977e859656d193583d022254b1fd", [tag: "5842014aa550977e859656d193583d022254b1fd"]}, + "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "591a03f9ed1585942afc043fe4a73beb7641e1e3", [tag: "591a03f9ed1585942afc043fe4a73beb7641e1e3"]}, + "wampex_client": {:git, "https://gitlab.com/entropealabs/wampex_client.git", "6861b782ce61df0d12b94953a0fb39281ce499e2", [tag: "6861b782ce61df0d12b94953a0fb39281ce499e2"]}, "websockex": {:hex, :websockex, "0.4.2", "9a3b7dc25655517ecd3f8ff7109a77fce94956096b942836cdcfbc7c86603ecc", [:mix], [], "hexpm", "803cd76e91544b56f0e655e36790be797fa6436db9224f7c303db9b9df2a3df4"}, "xema": {:hex, :xema, "0.11.0", "7b5118418633cffc27092110d02d4faeea938149dd3f6c64299e41e747067e80", [:mix], [{:conv_case, "~> 0.2.2", [hex: :conv_case, repo: "hexpm", optional: false]}, {:decimal, "~> 1.7", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "51491c9a953d65069d4b30aa2f70bc45ff99fd1bc3345bc72ce4e644d01ea14e"}, } diff --git a/test/support/authentication.ex b/test/support/authentication.ex index 594ef04..ac048e8 100644 --- a/test/support/authentication.ex +++ b/test/support/authentication.ex @@ -54,10 +54,13 @@ defmodule Wampex.Router.AuthenticationImpl do def authenticate(signature, realm, authid, %{ challenge: challenge }) do - authid - |> get_secret(realm) - |> Crypto.hash_challenge(challenge) - |> :pbkdf2.compare_secure(signature) + authed = + authid + |> get_secret(realm) + |> Crypto.hash_challenge(challenge) + |> :pbkdf2.compare_secure(signature) + + {authed, %{}} end defp get_secret(_authid, _uri), do: Crypto.pbkdf2(@auth_password, @salt, @iterations, @keylen) diff --git a/test/support/authorization.ex b/test/support/authorization.ex new file mode 100644 index 0000000..f0a55d7 --- /dev/null +++ b/test/support/authorization.ex @@ -0,0 +1,7 @@ +defmodule Wampex.Router.AuthorizationImpl do + @moduledoc false + @behaviour Wampex.Router.Authorization + + @impl true + def authorized?(_, _, _), do: true +end diff --git a/test/support/test_callee.ex b/test/support/test_callee.ex index c57dc82..df81f8b 100644 --- a/test/support/test_callee.ex +++ b/test/support/test_callee.ex @@ -4,7 +4,7 @@ defmodule TestCallee do require Logger alias Wampex.Client alias Wampex.Roles.{Callee, Dealer} - alias Callee.{Register, Unregister, Yield} + alias Callee.{Register, Yield} alias Dealer.Invocation def start_link(test, name, device) do @@ -25,24 +25,15 @@ defmodule TestCallee do send(test, invocation) - Client.cast_send_request( + Client.yield( name, - Callee.yield(%Yield{ + %Yield{ request_id: id, arg_list: [:ok], arg_kw: %{color: Map.get(arg_kw, "color")} - }) + } ) {:noreply, state} end - - def terminate(_r, {_test, name, id}) do - Client.send_request( - name, - Callee.unregister(%Unregister{registration_id: id}) - ) - - :ok - end end diff --git a/test/support/test_subscriber.ex b/test/support/test_subscriber.ex index 77af14c..1055fbc 100644 --- a/test/support/test_subscriber.ex +++ b/test/support/test_subscriber.ex @@ -3,8 +3,7 @@ defmodule TestSubscriber do use GenServer require Logger alias Wampex.Client - alias Wampex.Roles.Subscriber - alias Subscriber.{Subscribe, Unsubscribe} + alias Wampex.Roles.Subscriber.Subscribe def start_link(test, name, topic) do GenServer.start_link(__MODULE__, {test, name, topic}) @@ -31,15 +30,4 @@ defmodule TestSubscriber do send(test, event) {:noreply, state} end - - def terminate(_r, {_test, name, subs}) do - Enum.each(subs, fn sub -> - Client.send_request( - name, - Subscriber.unsubscribe(%Unsubscribe{subscription_id: sub}) - ) - end) - - :ok - end end diff --git a/test/wampex_test.exs b/test/wampex_test.exs index 52ddf3a..6200b9e 100644 --- a/test/wampex_test.exs +++ b/test/wampex_test.exs @@ -8,10 +8,11 @@ defmodule WampexTest do alias Wampex.Roles.{Callee, Caller, Peer, Publisher, Subscriber} alias Wampex.Roles.Caller.Call alias Wampex.Roles.Dealer.{Invocation, Result} - alias Wampex.Roles.Peer.Hello + alias Wampex.Roles.Peer.{Error, Hello} alias Wampex.Roles.Publisher.Publish alias Wampex.Router alias Wampex.Router.AuthenticationImpl + alias Wampex.Router.AuthorizationImpl require Logger @url "ws://localhost:4000/ws" @@ -35,7 +36,7 @@ defmodule WampexTest do replicas: 1, quorum: 1, authentication_module: AuthenticationImpl, - authorization_module: nil + authorization_module: AuthorizationImpl ) ] end @@ -53,7 +54,7 @@ defmodule WampexTest do Process.flag(:trap_exit, true) callee_name = TestAbort {:ok, pid} = Client.start_link(name: callee_name, session: @session) - Client.cast_send_request(callee_name, Peer.hello(%Hello{realm: "test", roles: [Callee]})) + Client.cast(callee_name, Peer.hello(%Hello{realm: "test", roles: [Callee]})) assert_receive {:EXIT, ^pid, :shutdown}, 1000 end @@ -62,14 +63,14 @@ defmodule WampexTest do caller_name = TestExistCaller Client.start_link(name: caller_name, session: @session) - assert {:error, 48, "wamp.error.no_registration", %{"procedure" => "this.should.not.exist"}, [], %{}} = - Client.send_request( + assert %Error{error: "wamp.error.no_registration"} = + Client.call( caller_name, - Caller.call(%Call{ + %Call{ procedure: "this.should.not.exist", arg_list: [1], arg_kw: %{color: "#FFFFFF"} - }) + } ) end @@ -83,13 +84,13 @@ defmodule WampexTest do Client.start_link(name: caller_name, session: @session) %Result{arg_list: ["ok"], arg_kw: %{"color" => "#FFFFFF"}} = - Client.send_request( + Client.call( caller_name, - Caller.call(%Call{ + %Call{ procedure: "com.actuator.#{@device}.light", arg_list: [1], arg_kw: %{color: "#FFFFFF"} - }) + } ) assert_receive %Invocation{} @@ -112,22 +113,22 @@ defmodule WampexTest do Client.start_link(name: TestPublisher, session: @session) - Client.cast_send_request( + Client.publish( TestPublisher, - Publisher.publish(%Publish{ + %Publish{ topic: "no.subscribers", arg_list: [12.5, 45.6, 87.5], arg_kw: %{loc: "60645"} - }) + } ) - Client.cast_send_request( + Client.publish( TestPublisher, - Publisher.publish(%Publish{ + %Publish{ topic: "com.data.test.temp", arg_list: [12.5, 45.6, 87.5], arg_kw: %{loc: "60645"} - }) + } ) assert_receive %Event{}, 2000 -- 2.45.3