diff options
author | multi prise <korokoko.toi@gmail.com> | 2021-02-06 01:03:34 +0000 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2021-02-06 01:03:34 +0000 |
commit | 5e5da3051a3fc932759425c39774faee2329ae6a (patch) | |
tree | 38d89654b5b66c70c7e0aab5adcdf4b9508e284b | |
parent | Merge branch 'user_impersonation' into 'master' (diff) |
rename display_name_test and remove get_acount_test
corrected displayname
corrected format
12 files changed, 847 insertions, 0 deletions
diff --git a/lib/polyjuice/client/endpoint/get_profile.ex b/lib/polyjuice/client/endpoint/get_profile.ex new file mode 100644 index 0000000..9684c85 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile.ex @@ -0,0 +1,48 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfile do + @moduledoc """ + Get the combined profile information for this user + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(req) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(req.user_id)}", + headers: [ + {"Accept", "application/json"} + ] + ) + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end +end diff --git a/lib/polyjuice/client/endpoint/get_profile_avatar_url.ex b/lib/polyjuice/client/endpoint/get_profile_avatar_url.ex new file mode 100644 index 0000000..3242407 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile_avatar_url.ex @@ -0,0 +1,58 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfileAvatarUrl do + @moduledoc """ + Get avatar URL for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid-avatar-url + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: user_id + }) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(user_id)}/avatar_url", + headers: [ + {"Accept", "application/json"} + ], + auth_required: true + ) + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "avatar_url")} + end + end +end diff --git a/lib/polyjuice/client/endpoint/get_profile_displayname.ex b/lib/polyjuice/client/endpoint/get_profile_displayname.ex new file mode 100644 index 0000000..305f9fb --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile_displayname.ex @@ -0,0 +1,58 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfileDisplayname do + @moduledoc """ + Get display name for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid-displayname + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: user_id + }) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(user_id)}/displayname", + headers: [ + {"Accept", "application/json"} + ], + auth_required: true + ) + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "displayname")} + end + end +end diff --git a/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex b/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex new file mode 100644 index 0000000..2649b1e --- /dev/null +++ b/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex @@ -0,0 +1,67 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PutProfileAvatarUrl do + @moduledoc """ + Change avatar URL for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#put-matrix-client-r0-profile-userid-avatar-url + """ + + @type t :: %__MODULE__{ + user_id: String.t(), + avatar_url: String.t() + } + + @enforce_keys [:user_id, :avatar_url] + + defstruct [ + :user_id, + :avatar_url + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: user_id, + avatar_url: avatar_url + }) do + e = &URI.encode_www_form/1 + + body = + %{avatar_url: avatar_url} + |> Jason.encode!() + + Polyjuice.Client.Endpoint.HttpSpec.put( + :r0, + "profile/#{e.(user_id)}/avatar_url", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + body: body, + auth_required: true + ) + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + :ok + end + end +end diff --git a/lib/polyjuice/client/endpoint/put_profile_displayname.ex b/lib/polyjuice/client/endpoint/put_profile_displayname.ex new file mode 100644 index 0000000..a08ada3 --- /dev/null +++ b/lib/polyjuice/client/endpoint/put_profile_displayname.ex @@ -0,0 +1,67 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PutProfileDisplayname do + @moduledoc """ + Change display name for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#put-matrix-client-r0-profile-userid-displayname + """ + + @type t :: %__MODULE__{ + user_id: String.t(), + displayname: String.t() + } + + @enforce_keys [:user_id, :displayname] + + defstruct [ + :user_id, + :displayname + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: user_id, + displayname: displayname + }) do + e = &URI.encode_www_form/1 + + body = + %{displayname: displayname} + |> Jason.encode!() + + Polyjuice.Client.Endpoint.HttpSpec.put( + :r0, + "profile/#{e.(user_id)}/displayname", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + body: body, + auth_required: true + ) + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + :ok + end + end +end diff --git a/lib/polyjuice/client/profile.ex b/lib/polyjuice/client/profile.ex new file mode 100644 index 0000000..74612c3 --- /dev/null +++ b/lib/polyjuice/client/profile.ex @@ -0,0 +1,126 @@ +# Copyright 2020 Multi Prise <multiprisestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Profile do + @moduledoc """ + Profile-related functions. + """ + require Logger + + @doc """ + Get user profile. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_profile( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, map()} | any + def get_profile(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfile{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Get avatar url for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_avatar_url( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, String.t()} | any + def get_avatar_url(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Get display name for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_displayname( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, String.t()} | any + def get_displayname(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Modify avatar url for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + `avatar_url` is an url specifying the location of the new profile avatar + """ + @spec put_avatar_url( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil, + avatar_url :: String.t() + ) :: :ok | any + def put_avatar_url(client_api, user_id \\ nil, avatar_url) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), + avatar_url: avatar_url + } + ) + end + + @doc """ + Modify display name for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + `displayname` is a string representing the new displayname + """ + @spec put_displayname( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil, + displayname :: String.t() + ) :: :ok | any + def put_displayname(client_api, user_id \\ nil, displayname) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), + displayname: displayname + } + ) + end +end diff --git a/test/polyjuice/client/endpoint/get_profile_avatar_url_test.exs b/test/polyjuice/client/endpoint/get_profile_avatar_url_test.exs new file mode 100644 index 0000000..e0b756b --- /dev/null +++ b/test/polyjuice/client/endpoint/get_profile_avatar_url_test.exs @@ -0,0 +1,50 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfileAvatarUrlTest do + use ExUnit.Case + + test "GET /_matrix/client/r0/user/{userId}/avatar_url" do + with endpoint = %Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: "@alice:kazarma.local" + } do + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + headers: [ + {"Accept", "application/json"} + ], + method: :get, + path: "_matrix/client/r0/profile/%40alice%3Akazarma.local/avatar_url" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{\"avatar_url\": \"mxc://matrix.org/wefh34uihSDRGhw34\"}" + ) == {:ok, "mxc://matrix.org/wefh34uihSDRGhw34"} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, + %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end + end +end diff --git a/test/polyjuice/client/endpoint/get_profile_displayname_test.exs b/test/polyjuice/client/endpoint/get_profile_displayname_test.exs new file mode 100644 index 0000000..8c39a17 --- /dev/null +++ b/test/polyjuice/client/endpoint/get_profile_displayname_test.exs @@ -0,0 +1,50 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfileDisplaynameTest do + use ExUnit.Case + + test "GET /_matrix/client/r0/profile/{userId}/displayname" do + with endpoint = %Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: "@alice:kazarma.local" + } do + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + headers: [ + {"Accept", "application/json"} + ], + method: :get, + path: "_matrix/client/r0/profile/%40alice%3Akazarma.local/displayname" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{\"displayname\": \"alice\"}" + ) == {:ok, "alice"} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, + %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end + end +end diff --git a/test/polyjuice/client/endpoint/get_profile_test.exs b/test/polyjuice/client/endpoint/get_profile_test.exs new file mode 100644 index 0000000..4f2624b --- /dev/null +++ b/test/polyjuice/client/endpoint/get_profile_test.exs @@ -0,0 +1,50 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetProfileTest do + use ExUnit.Case + + test "GET profile/{user_id}" do + endpoint = %Polyjuice.Client.Endpoint.GetProfile{ + user_id: "@alice:kazarma.local" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "", + headers: [ + {"Accept", "application/json"} + ], + method: :get, + path: "_matrix/client/r0/profile/%40alice%3Akazarma.local" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "[{\"avatar_url\": \"yoyo\", \"displayname\": \"polo\"}]" + ) == {:ok, [%{"avatar_url" => "yoyo", "displayname" => "polo"}]} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/endpoint/put_profile_avatar_url_test.exs b/test/polyjuice/client/endpoint/put_profile_avatar_url_test.exs new file mode 100644 index 0000000..5caf235 --- /dev/null +++ b/test/polyjuice/client/endpoint/put_profile_avatar_url_test.exs @@ -0,0 +1,52 @@ +# Copyright 2020 Multi Prise <multipriseestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PutProfileAvatarUrlTest do + use ExUnit.Case + + test "PUT /_matrix/client/r0/profile/{userId}/avatar_url" do + endpoint = %Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: "@alice:homeserver.local", + avatar_url: "mxc://matrix.org/wefh34uihSDRGhw34" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "{\"avatar_url\":\"mxc://matrix.org/wefh34uihSDRGhw34\"}", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :put, + path: "_matrix/client/r0/profile/%40alice%3Ahomeserver.local/avatar_url" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{}" + ) == :ok + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/endpoint/put_profile_displayname_test.exs b/test/polyjuice/client/endpoint/put_profile_displayname_test.exs new file mode 100644 index 0000000..10f2ae1 --- /dev/null +++ b/test/polyjuice/client/endpoint/put_profile_displayname_test.exs @@ -0,0 +1,52 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PutProfileDisplaynameTest do + use ExUnit.Case + + test "PUT /_matrix/client/r0/profile/{userId}/displayname" do + endpoint = %Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: "@alice:kazarma.local", + displayname: "alicecapique" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "{\"displayname\":\"alicecapique\"}", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :put, + path: "_matrix/client/r0/profile/%40alice%3Akazarma.local/displayname" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{}" + ) == :ok + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/profile_test.exs b/test/polyjuice/client/profile_test.exs new file mode 100644 index 0000000..cf08a5b --- /dev/null +++ b/test/polyjuice/client/profile_test.exs @@ -0,0 +1,169 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.ProfileTest do + use ExUnit.Case + doctest Polyjuice.Client.Profile + + test "get_profile" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfile{ + user_id: "@amelie:example.org" + }, + {:ok, %{avatar_url: "mxc://matrix.org/SDGdghriugerRg", displayname: "poulain"}} + } + } do + {:ok, data} = Polyjuice.Client.Profile.get_profile(client, "@amelie:example.org") + assert data[:displayname] == "poulain" + assert data[:avatar_url] == "mxc://matrix.org/SDGdghriugerRg" + end + end + + test "get_profile without user_id" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfile{ + user_id: "@alice:example.org" + }, + {:ok, %{avatar_url: "mxc://matrix.org/SDGdghriugerRg", displayname: "poulain"}} + } + } do + {:ok, data} = Polyjuice.Client.Profile.get_profile(client) + assert data[:displayname] == "poulain" + assert data[:avatar_url] == "mxc://matrix.org/SDGdghriugerRg" + end + end + + test "get avatar url" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: "@alice:example.org" + }, + {:ok, "mxc://matrix.org/wefh34uihSDRGhw34"} + } + } do + {:ok, avatar_url} = Polyjuice.Client.Profile.get_avatar_url(client, "@alice:example.org") + assert avatar_url == "mxc://matrix.org/wefh34uihSDRGhw34" + end + end + + test "get avatar url without user_id" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: "@alice:example.org" + }, + {:ok, "mxc://matrix.org/wefh34uihSDRGhw34"} + } + } do + {:ok, avatar_url} = Polyjuice.Client.Profile.get_avatar_url(client) + assert avatar_url == "mxc://matrix.org/wefh34uihSDRGhw34" + end + end + + test "modify avatar url" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: "@alice:example.org", + avatar_url: "mxc://matrix.org/wefh34uihSDRGhw34" + }, + {:ok} + } + } do + {:ok} = + Polyjuice.Client.Profile.put_avatar_url( + client, + "@alice:example.org", + "mxc://matrix.org/wefh34uihSDRGhw34" + ) + end + end + + test "modify avatar url without user_id" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: "@alice:example.org", + avatar_url: "mxc://matrix.org/wefh34uihSDRGhw34" + }, + {:ok} + } + } do + {:ok} = + Polyjuice.Client.Profile.put_avatar_url( + client, + "mxc://matrix.org/wefh34uihSDRGhw34" + ) + end + end + + test "get displayname" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: "@alice:example.org" + }, + {:ok, "alice"} + } + } do + {:ok, displayname} = Polyjuice.Client.Profile.get_displayname(client, "@alice:example.org") + assert displayname == "alice" + end + end + + test "get displayname wihout user_id" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: "@alice:example.org" + }, + {:ok, "alice"} + } + } do + {:ok, displayname} = Polyjuice.Client.Profile.get_displayname(client) + assert displayname == "alice" + end + end + + test "modify displayname" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: "@alice:example.org", + displayname: "marie" + }, + {:ok} + } + } do + {:ok} = Polyjuice.Client.Profile.put_displayname(client, "@alice:example.org", "marie") + end + end + + test "modify displayname without user_id" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: "@alice:example.org", + displayname: "marie" + }, + {:ok} + } + } do + {:ok} = Polyjuice.Client.Profile.put_displayname(client, "marie") + end + end +end |