From cd90695a349f33b84f287794bae6070e9eec446a Mon Sep 17 00:00:00 2001 From: eugenijm Date: Thu, 28 Mar 2019 14:52:09 +0300 Subject: Add PUT /api/pleroma/notification_settings endpoint --- test/web/mastodon_api/account_view_test.exs | 14 ++++++++++++++ test/web/twitter_api/util_controller_test.exs | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'test/web') diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index 6dc60afe9..aa6a1e960 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -71,6 +71,20 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do assert expected == AccountView.render("account.json", %{user: user}) end + test "Represent the user account for the account owner" do + user = insert(:user) + + notification_settings = %{ + "remote" => true, + "local" => true, + "followers" => true, + "follows" => true + } + + assert %{pleroma: %{notification_settings: ^notification_settings}} = + AccountView.render("account.json", %{user: user, for: user}) + end + test "Represent a Service(bot) account" do user = insert(:user, %{ diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index 832fdc096..426508353 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -3,6 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do alias Pleroma.Notification alias Pleroma.Repo + alias Pleroma.User alias Pleroma.Web.CommonAPI import Pleroma.Factory @@ -74,6 +75,26 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do end end + describe "PUT /api/pleroma/notification_settings" do + test "it updates notification settings", %{conn: conn} do + user = insert(:user) + + conn + |> assign(:user, user) + |> put("/api/pleroma/notification_settings", %{ + "remote" => false, + "followers" => false, + "bar" => 1 + }) + |> json_response(:ok) + + user = Repo.get(User, user.id) + + assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} == + user.info.notification_settings + end + end + describe "GET /api/statusnet/config.json" do test "returns the state of safe_dm_mentions flag", %{conn: conn} do option = Pleroma.Config.get([:instance, :safe_dm_mentions]) -- cgit v1.2.3 From d56866c824d8023c9ca162ffef4c2eff150de399 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:21:33 +0100 Subject: Add subscribed status to user view Added in pleroma extensions, but can be moved whenever --- test/web/twitter_api/views/user_view_test.exs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'test/web') diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 0feaf4b64..f9ae5888c 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -105,7 +105,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } @@ -153,7 +154,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false, } } @@ -202,13 +204,22 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } assert represented == UserView.render("show.json", %{user: follower, for: user}) end + test "a user that you are subscribed to" do + user = insert(:user) + subscriber = insert(:user) + {:ok, subscriber} = User.subscribe(subscriber, user) + represented = UserView.render("show.json", %{user: user, for: subscriber}) + assert represented["pleroma"]["subscribed"] == true + end + test "a user that is a moderator" do user = insert(:user, %{info: %{is_moderator: true}}) represented = UserView.render("show.json", %{user: user, for: user}) -- cgit v1.2.3 From 06cd547fc6562b00d505fcc8e78ba7c734b1adf0 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:22:48 +0100 Subject: Format subscriptions related stuff --- test/web/twitter_api/views/user_view_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/web') diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index f9ae5888c..8986112cc 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -155,7 +155,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "pleroma" => %{ "confirmation_pending" => false, "tags" => [], - "subscribed" => false, + "subscribed" => false } } -- cgit v1.2.3 From 73afd13c2114dde0f9939d2f8c8516d2693b7e95 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:37:00 +0100 Subject: Add subscription key to one test I forgot about --- test/web/twitter_api/views/user_view_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test/web') diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 8986112cc..69d9c5da5 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -299,7 +299,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } -- cgit v1.2.3 From 9ca91cbb8764ef4f8fe5303705dd98984e4e90c0 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 16:51:45 +0100 Subject: Change relationship direction of subscriptions --- test/web/mastodon_api/account_view_test.exs | 2 ++ .../mastodon_api/mastodon_api_controller_test.exs | 19 +++++++++++++++++++ test/web/twitter_api/views/user_view_test.exs | 20 ++++---------------- 3 files changed, 25 insertions(+), 16 deletions(-) (limited to 'test/web') diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index 6dc60afe9..48580ff1e 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -142,6 +142,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do blocking: true, muting: false, muting_notifications: false, + subscribing: false, requested: false, domain_blocking: false, showing_reblogs: true, @@ -198,6 +199,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do following: false, followed_by: false, blocking: true, + subscribing: false, muting: false, muting_notifications: false, requested: false, diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 6060cc97f..811a2dd7b 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1555,6 +1555,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert %{"id" => _id, "muting" => false} = json_response(conn, 200) end + test "subscribing / unsubscribing to a user", %{conn: conn} do + user = insert(:user) + subscription_target = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe") + + assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200) + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe") + + assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200) + end + test "getting a list of mutes", %{conn: conn} do user = insert(:user) other_user = insert(:user) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 69d9c5da5..0feaf4b64 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -105,8 +105,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } @@ -154,8 +153,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } @@ -204,22 +202,13 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } assert represented == UserView.render("show.json", %{user: follower, for: user}) end - test "a user that you are subscribed to" do - user = insert(:user) - subscriber = insert(:user) - {:ok, subscriber} = User.subscribe(subscriber, user) - represented = UserView.render("show.json", %{user: user, for: subscriber}) - assert represented["pleroma"]["subscribed"] == true - end - test "a user that is a moderator" do user = insert(:user, %{info: %{is_moderator: true}}) represented = UserView.render("show.json", %{user: user, for: user}) @@ -299,8 +288,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } -- cgit v1.2.3 From be54e40890432d2cd8e592e6d4acfa9f1e98586c Mon Sep 17 00:00:00 2001 From: Alex S Date: Sat, 6 Apr 2019 17:18:59 +0700 Subject: twitter api registration fix for twitter api tests --- test/web/twitter_api/twitter_api_test.exs | 361 ++++++++++++++++++++++++++---- 1 file changed, 312 insertions(+), 49 deletions(-) (limited to 'test/web') diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 6c00244de..716fccfb2 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -16,6 +16,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do import Pleroma.Factory + setup_all do + Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) + :ok + end + test "create a status" do user = insert(:user) mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"}) @@ -299,7 +304,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do UserView.render("show.json", %{user: fetched_user}) end - @moduletag skip: "needs 'account_activation_required: true' in config" test "it sends confirmation email if :account_activation_required is specified in instance config" do setting = Pleroma.Config.get([:instance, :account_activation_required]) @@ -353,68 +357,327 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert user2.bio == expected_text end - @moduletag skip: "needs 'registrations_open: false' in config" - test "it registers a new user via invite token and returns the user." do - {:ok, token} = UserInviteToken.create_token() + describe "register with one time token" do + setup do + setting = Pleroma.Config.get([:instance, :registrations_open]) - data = %{ - "nickname" => "vinny", - "email" => "pasta@pizza.vs", - "fullname" => "Vinny Vinesauce", - "bio" => "streamer", - "password" => "hiptofbees", - "confirm" => "hiptofbees", - "token" => token.token - } + if setting do + Pleroma.Config.put([:instance, :registrations_open], false) + on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end) + end - {:ok, user} = TwitterAPI.register_user(data) + :ok + end - fetched_user = User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) + test "returns user on success" do + {:ok, token} = UserInviteToken.create_token() - assert token.used == true + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees", + "token" => token.token + } - assert UserView.render("show.json", %{user: user}) == - UserView.render("show.json", %{user: fetched_user}) + {:ok, user} = TwitterAPI.register_user(data) + + fetched_user = User.get_by_nickname("vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + + assert token.used == true + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) + end + + test "returns error on invalid token" do + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => "DudeLetMeInImAFairy" + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Invalid token" + refute User.get_by_nickname("GrimReaper") + end + + test "returns error on expired token" do + {:ok, token} = UserInviteToken.create_token() + UserInviteToken.mark_as_used(token.token) + + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute User.get_by_nickname("GrimReaper") + end end - @moduletag skip: "needs 'registrations_open: false' in config" - test "it returns an error if invalid token submitted" do - data = %{ - "nickname" => "GrimReaper", - "email" => "death@reapers.afterlife", - "fullname" => "Reaper Grim", - "bio" => "Your time has come", - "password" => "scythe", - "confirm" => "scythe", - "token" => "DudeLetMeInImAFairy" - } + describe "registers with date limited token" do + setup do + setting = Pleroma.Config.get([:instance, :registrations_open]) + + if setting do + Pleroma.Config.put([:instance, :registrations_open], false) + on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end) + end + + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees" + } + + check_fn = fn token -> + data = Map.put(data, "token", token.token) + {:ok, user} = TwitterAPI.register_user(data) + fetched_user = User.get_by_nickname("vinny") + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) + end + + {:ok, data: data, check_fn: check_fn} + end + + test "returns user on success", %{check_fn: check_fn} do + {:ok, token} = UserInviteToken.create_token(expire_at: Date.utc_today()) + + check_fn.(token) + + token = Repo.get_by(UserInviteToken, token: token.token) + + refute token.used + end + + test "returns user on token which expired tomorrow", %{check_fn: check_fn} do + {:ok, token} = UserInviteToken.create_token(expire_at: Date.add(Date.utc_today(), 1)) + + check_fn.(token) + + token = Repo.get_by(UserInviteToken, token: token.token) - {:error, msg} = TwitterAPI.register_user(data) + refute token.used + end + + test "returns an error on overdue date", %{data: data} do + {:ok, token} = UserInviteToken.create_token(expire_at: Date.add(Date.utc_today(), -1)) + + data = Map.put(data, "token", token.token) + + {:error, msg} = TwitterAPI.register_user(data) - assert msg == "Invalid token" - refute User.get_by_nickname("GrimReaper") + assert msg == "Expired token" + refute User.get_by_nickname("vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + + assert token.used == true + end end - @moduletag skip: "needs 'registrations_open: false' in config" - test "it returns an error if expired token submitted" do - {:ok, token} = UserInviteToken.create_token() - UserInviteToken.mark_as_used(token.token) + describe "registers with reusable token" do + setup do + setting = Pleroma.Config.get([:instance, :registrations_open]) - data = %{ - "nickname" => "GrimReaper", - "email" => "death@reapers.afterlife", - "fullname" => "Reaper Grim", - "bio" => "Your time has come", - "password" => "scythe", - "confirm" => "scythe", - "token" => token.token - } + if setting do + Pleroma.Config.put([:instance, :registrations_open], false) + on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end) + end + + :ok + end + + test "returns user on success, after him registration fails" do + {:ok, token} = UserInviteToken.create_token(max_use: 100) + + Ecto.Changeset.change(token, uses: 99) |> Repo.update!() + + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees", + "token" => token.token + } + + {:ok, user} = TwitterAPI.register_user(data) + fetched_user = User.get_by_nickname("vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + + assert token.used == true + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) - {:error, msg} = TwitterAPI.register_user(data) + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } - assert msg == "Expired token" - refute User.get_by_nickname("GrimReaper") + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute User.get_by_nickname("GrimReaper") + end + end + + describe "registers with reusable date limited token" do + setup do + setting = Pleroma.Config.get([:instance, :registrations_open]) + + if setting do + Pleroma.Config.put([:instance, :registrations_open], false) + on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end) + end + + :ok + end + + test "returns user on success" do + {:ok, token} = + UserInviteToken.create_token( + expire_at: Date.utc_today(), + max_use: 100 + ) + + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees", + "token" => token.token + } + + {:ok, user} = TwitterAPI.register_user(data) + fetched_user = User.get_by_nickname("vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + + refute token.used + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) + end + + test "error after max uses" do + {:ok, token} = + UserInviteToken.create_token( + expire_at: Date.utc_today(), + max_use: 100 + ) + + Ecto.Changeset.change(token, uses: 99) |> Repo.update!() + + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees", + "token" => token.token + } + + {:ok, user} = TwitterAPI.register_user(data) + fetched_user = User.get_by_nickname("vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + assert token.used == true + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) + + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute User.get_by_nickname("GrimReaper") + end + + test "returns error on overdue date" do + {:ok, token} = + UserInviteToken.create_token( + expire_at: Date.add(Date.utc_today(), -1), + max_use: 100 + ) + + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute User.get_by_nickname("GrimReaper") + end + + test "returns error on with overdue date and after max" do + {:ok, token} = + UserInviteToken.create_token( + expire_at: Date.add(Date.utc_today(), -1), + max_use: 100 + ) + + Ecto.Changeset.change(token, uses: 100) |> Repo.update!() + + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute User.get_by_nickname("GrimReaper") + end end test "it returns the error on registration problems" do -- cgit v1.2.3 From 47b07cec495528ce22f83ca56717cc74aa0096f3 Mon Sep 17 00:00:00 2001 From: Alex S Date: Sat, 6 Apr 2019 20:24:22 +0700 Subject: token -> invite renaming --- test/web/twitter_api/twitter_api_test.exs | 102 +++++++++++++----------------- 1 file changed, 44 insertions(+), 58 deletions(-) (limited to 'test/web') diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 716fccfb2..519141c96 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -370,7 +370,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success" do - {:ok, token} = UserInviteToken.create_token() + {:ok, invite} = UserInviteToken.create_invite() data = %{ "nickname" => "vinny", @@ -379,15 +379,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "streamer", "password" => "hiptofbees", "confirm" => "hiptofbees", - "token" => token.token + "token" => invite.token } {:ok, user} = TwitterAPI.register_user(data) fetched_user = User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - assert token.used == true + assert invite.used == true assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) @@ -411,8 +411,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns error on expired token" do - {:ok, token} = UserInviteToken.create_token() - UserInviteToken.mark_as_used(token.token) + {:ok, invite} = UserInviteToken.create_invite() + UserInviteToken.update_invite!(invite, used: true) data = %{ "nickname" => "GrimReaper", @@ -421,7 +421,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "Your time has come", "password" => "scythe", "confirm" => "scythe", - "token" => token.token + "token" => invite.token } {:error, msg} = TwitterAPI.register_user(data) @@ -449,8 +449,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "confirm" => "hiptofbees" } - check_fn = fn token -> - data = Map.put(data, "token", token.token) + check_fn = fn invite -> + data = Map.put(data, "token", invite.token) {:ok, user} = TwitterAPI.register_user(data) fetched_user = User.get_by_nickname("vinny") @@ -462,37 +462,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success", %{check_fn: check_fn} do - {:ok, token} = UserInviteToken.create_token(expire_at: Date.utc_today()) + {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today()}) - check_fn.(token) + check_fn.(invite) - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - refute token.used + refute invite.used end test "returns user on token which expired tomorrow", %{check_fn: check_fn} do - {:ok, token} = UserInviteToken.create_token(expire_at: Date.add(Date.utc_today(), 1)) + {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), 1)}) - check_fn.(token) + check_fn.(invite) - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - refute token.used + refute invite.used end test "returns an error on overdue date", %{data: data} do - {:ok, token} = UserInviteToken.create_token(expire_at: Date.add(Date.utc_today(), -1)) + {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1)}) - data = Map.put(data, "token", token.token) + data = Map.put(data, "token", invite.token) {:error, msg} = TwitterAPI.register_user(data) assert msg == "Expired token" refute User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - assert token.used == true + assert invite.used == true end end @@ -509,9 +509,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success, after him registration fails" do - {:ok, token} = UserInviteToken.create_token(max_use: 100) + {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100}) - Ecto.Changeset.change(token, uses: 99) |> Repo.update!() + UserInviteToken.update_invite!(invite, uses: 99) data = %{ "nickname" => "vinny", @@ -520,14 +520,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "streamer", "password" => "hiptofbees", "confirm" => "hiptofbees", - "token" => token.token + "token" => invite.token } {:ok, user} = TwitterAPI.register_user(data) fetched_user = User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - assert token.used == true + assert invite.used == true assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) @@ -539,7 +539,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "Your time has come", "password" => "scythe", "confirm" => "scythe", - "token" => token.token + "token" => invite.token } {:error, msg} = TwitterAPI.register_user(data) @@ -562,11 +562,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success" do - {:ok, token} = - UserInviteToken.create_token( - expire_at: Date.utc_today(), - max_use: 100 - ) + {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100}) data = %{ "nickname" => "vinny", @@ -575,27 +571,23 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "streamer", "password" => "hiptofbees", "confirm" => "hiptofbees", - "token" => token.token + "token" => invite.token } {:ok, user} = TwitterAPI.register_user(data) fetched_user = User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) + invite = Repo.get_by(UserInviteToken, token: invite.token) - refute token.used + refute invite.used assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) end test "error after max uses" do - {:ok, token} = - UserInviteToken.create_token( - expire_at: Date.utc_today(), - max_use: 100 - ) + {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100}) - Ecto.Changeset.change(token, uses: 99) |> Repo.update!() + UserInviteToken.update_invite!(invite, uses: 99) data = %{ "nickname" => "vinny", @@ -604,13 +596,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "streamer", "password" => "hiptofbees", "confirm" => "hiptofbees", - "token" => token.token + "token" => invite.token } {:ok, user} = TwitterAPI.register_user(data) fetched_user = User.get_by_nickname("vinny") - token = Repo.get_by(UserInviteToken, token: token.token) - assert token.used == true + invite = Repo.get_by(UserInviteToken, token: invite.token) + assert invite.used == true assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) @@ -622,7 +614,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "Your time has come", "password" => "scythe", "confirm" => "scythe", - "token" => token.token + "token" => invite.token } {:error, msg} = TwitterAPI.register_user(data) @@ -632,11 +624,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns error on overdue date" do - {:ok, token} = - UserInviteToken.create_token( - expire_at: Date.add(Date.utc_today(), -1), - max_use: 100 - ) + {:ok, invite} = + UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100}) data = %{ "nickname" => "GrimReaper", @@ -645,7 +634,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "Your time has come", "password" => "scythe", "confirm" => "scythe", - "token" => token.token + "token" => invite.token } {:error, msg} = TwitterAPI.register_user(data) @@ -655,13 +644,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns error on with overdue date and after max" do - {:ok, token} = - UserInviteToken.create_token( - expire_at: Date.add(Date.utc_today(), -1), - max_use: 100 - ) + {:ok, invite} = + UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100}) - Ecto.Changeset.change(token, uses: 100) |> Repo.update!() + UserInviteToken.update_invite!(invite, uses: 100) data = %{ "nickname" => "GrimReaper", @@ -670,7 +656,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do "bio" => "Your time has come", "password" => "scythe", "confirm" => "scythe", - "token" => token.token + "token" => invite.token } {:error, msg} = TwitterAPI.register_user(data) -- cgit v1.2.3 From dcc54f8cfab2c3d278ea3f3eb54d866c8436703a Mon Sep 17 00:00:00 2001 From: Alex S Date: Sat, 6 Apr 2019 20:25:19 +0700 Subject: admin api endpoints for invites --- test/web/admin_api/admin_api_controller_test.exs | 163 ++++++++++++++++++++--- 1 file changed, 147 insertions(+), 16 deletions(-) (limited to 'test/web') diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index dd2fbfb15..ae287a953 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do use Pleroma.Web.ConnCase alias Pleroma.User + alias Pleroma.UserInviteToken import Pleroma.Factory describe "/api/pleroma/admin/user" do @@ -80,14 +81,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do user = insert(:user) follower = insert(:user) - conn = - build_conn() - |> assign(:user, admin) - |> put_req_header("accept", "application/json") - |> post("/api/pleroma/admin/user/follow", %{ - "follower" => follower.nickname, - "followed" => user.nickname - }) + build_conn() + |> assign(:user, admin) + |> put_req_header("accept", "application/json") + |> post("/api/pleroma/admin/user/follow", %{ + "follower" => follower.nickname, + "followed" => user.nickname + }) user = User.get_by_id(user.id) follower = User.get_by_id(follower.id) @@ -104,14 +104,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do User.follow(follower, user) - conn = - build_conn() - |> assign(:user, admin) - |> put_req_header("accept", "application/json") - |> post("/api/pleroma/admin/user/unfollow", %{ - "follower" => follower.nickname, - "followed" => user.nickname - }) + build_conn() + |> assign(:user, admin) + |> put_req_header("accept", "application/json") + |> post("/api/pleroma/admin/user/unfollow", %{ + "follower" => follower.nickname, + "followed" => user.nickname + }) user = User.get_by_id(user.id) follower = User.get_by_id(follower.id) @@ -642,4 +641,136 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do "tags" => [] } end + + describe "GET /api/pleroma/admin/invite_token" do + test "without options" do + admin = insert(:user, info: %{is_admin: true}) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invite_token") + + token = json_response(conn, 200) + invite = UserInviteToken.find_by_token!(token) + refute invite.used + refute invite.expire_at + refute invite.max_use + assert invite.invite_type == "one_time" + end + + test "with expire_at" do + admin = insert(:user, info: %{is_admin: true}) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invite_token", %{ + "invite" => %{"expire_at" => Date.to_string(Date.utc_today())} + }) + + token = json_response(conn, 200) + invite = UserInviteToken.find_by_token!(token) + + refute invite.used + assert invite.expire_at == Date.utc_today() + refute invite.max_use + assert invite.invite_type == "date_limited" + end + + test "with max_use" do + admin = insert(:user, info: %{is_admin: true}) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invite_token", %{ + "invite" => %{"max_use" => 150} + }) + + token = json_response(conn, 200) + invite = UserInviteToken.find_by_token!(token) + refute invite.used + refute invite.expire_at + assert invite.max_use == 150 + assert invite.invite_type == "reusable" + end + + test "with max use and expire_at" do + admin = insert(:user, info: %{is_admin: true}) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invite_token", %{ + "invite" => %{"max_use" => 150, "expire_at" => Date.to_string(Date.utc_today())} + }) + + token = json_response(conn, 200) + invite = UserInviteToken.find_by_token!(token) + refute invite.used + assert invite.expire_at == Date.utc_today() + assert invite.max_use == 150 + assert invite.invite_type == "reusable_date_limited" + end + end + + describe "GET /api/pleroma/admin/invites_list" do + test "no invites" do + admin = insert(:user, info: %{is_admin: true}) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invites_list") + + assert json_response(conn, 200) == %{"invites" => []} + end + + test "with invite" do + admin = insert(:user, info: %{is_admin: true}) + {:ok, invite} = UserInviteToken.create_invite() + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/invites_list") + + assert json_response(conn, 200) == %{ + "invites" => [ + %{ + "expire_at" => nil, + "id" => invite.id, + "invite_type" => "one_time", + "max_use" => nil, + "token" => invite.token, + "used" => false, + "uses" => 0 + } + ] + } + end + end + + describe "POST /api/pleroma/admin/invite_revoke" do + test "with token" do + admin = insert(:user, info: %{is_admin: true}) + {:ok, invite} = UserInviteToken.create_invite() + + conn = + build_conn() + |> assign(:user, admin) + |> post("/api/pleroma/admin/invite_revoke", %{"token" => invite.token}) + + assert json_response(conn, 200) == %{ + "expire_at" => nil, + "id" => invite.id, + "invite_type" => "one_time", + "max_use" => nil, + "token" => invite.token, + "used" => true, + "uses" => 0 + } + end + end end -- cgit v1.2.3 From b810aac117563a941b50180f19bca2d96a329a0a Mon Sep 17 00:00:00 2001 From: Alex S Date: Sun, 7 Apr 2019 19:48:52 +0700 Subject: added docs to docs/api/admin_api.md code style and little renamings --- test/web/admin_api/admin_api_controller_test.exs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'test/web') diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index ae287a953..67ee0f3fa 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -715,14 +715,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end end - describe "GET /api/pleroma/admin/invites_list" do + describe "GET /api/pleroma/admin/invites" do test "no invites" do admin = insert(:user, info: %{is_admin: true}) conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/invites_list") + |> get("/api/pleroma/admin/invites") assert json_response(conn, 200) == %{"invites" => []} end @@ -734,7 +734,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/invites_list") + |> get("/api/pleroma/admin/invites") assert json_response(conn, 200) == %{ "invites" => [ @@ -752,7 +752,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end end - describe "POST /api/pleroma/admin/invite_revoke" do + describe "POST /api/pleroma/admin/revoke_invite" do test "with token" do admin = insert(:user, info: %{is_admin: true}) {:ok, invite} = UserInviteToken.create_invite() @@ -760,7 +760,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do conn = build_conn() |> assign(:user, admin) - |> post("/api/pleroma/admin/invite_revoke", %{"token" => invite.token}) + |> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token}) assert json_response(conn, 200) == %{ "expire_at" => nil, -- cgit v1.2.3 From 012bb5dcc9bfbf6f3ea210ec4e865f3adcea9dfd Mon Sep 17 00:00:00 2001 From: Alex S Date: Mon, 8 Apr 2019 16:01:28 +0700 Subject: renaming expire_at -> expires_at keyword style change --- test/web/admin_api/admin_api_controller_test.exs | 20 ++++++++++---------- test/web/twitter_api/twitter_api_test.exs | 14 +++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'test/web') diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 67ee0f3fa..d44392c9d 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -654,26 +654,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do token = json_response(conn, 200) invite = UserInviteToken.find_by_token!(token) refute invite.used - refute invite.expire_at + refute invite.expires_at refute invite.max_use assert invite.invite_type == "one_time" end - test "with expire_at" do + test "with expires_at" do admin = insert(:user, info: %{is_admin: true}) conn = build_conn() |> assign(:user, admin) |> get("/api/pleroma/admin/invite_token", %{ - "invite" => %{"expire_at" => Date.to_string(Date.utc_today())} + "invite" => %{"expires_at" => Date.to_string(Date.utc_today())} }) token = json_response(conn, 200) invite = UserInviteToken.find_by_token!(token) refute invite.used - assert invite.expire_at == Date.utc_today() + assert invite.expires_at == Date.utc_today() refute invite.max_use assert invite.invite_type == "date_limited" end @@ -691,25 +691,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do token = json_response(conn, 200) invite = UserInviteToken.find_by_token!(token) refute invite.used - refute invite.expire_at + refute invite.expires_at assert invite.max_use == 150 assert invite.invite_type == "reusable" end - test "with max use and expire_at" do + test "with max use and expires_at" do admin = insert(:user, info: %{is_admin: true}) conn = build_conn() |> assign(:user, admin) |> get("/api/pleroma/admin/invite_token", %{ - "invite" => %{"max_use" => 150, "expire_at" => Date.to_string(Date.utc_today())} + "invite" => %{"max_use" => 150, "expires_at" => Date.to_string(Date.utc_today())} }) token = json_response(conn, 200) invite = UserInviteToken.find_by_token!(token) refute invite.used - assert invite.expire_at == Date.utc_today() + assert invite.expires_at == Date.utc_today() assert invite.max_use == 150 assert invite.invite_type == "reusable_date_limited" end @@ -739,7 +739,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do assert json_response(conn, 200) == %{ "invites" => [ %{ - "expire_at" => nil, + "expires_at" => nil, "id" => invite.id, "invite_type" => "one_time", "max_use" => nil, @@ -763,7 +763,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do |> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token}) assert json_response(conn, 200) == %{ - "expire_at" => nil, + "expires_at" => nil, "id" => invite.id, "invite_type" => "one_time", "max_use" => nil, diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 519141c96..798a009fe 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -462,7 +462,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success", %{check_fn: check_fn} do - {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today()}) + {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()}) check_fn.(invite) @@ -472,7 +472,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on token which expired tomorrow", %{check_fn: check_fn} do - {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), 1)}) + {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)}) check_fn.(invite) @@ -482,7 +482,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns an error on overdue date", %{data: data} do - {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1)}) + {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)}) data = Map.put(data, "token", invite.token) @@ -562,7 +562,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns user on success" do - {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100}) + {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100}) data = %{ "nickname" => "vinny", @@ -585,7 +585,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "error after max uses" do - {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100}) + {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100}) UserInviteToken.update_invite!(invite, uses: 99) @@ -625,7 +625,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "returns error on overdue date" do {:ok, invite} = - UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100}) + UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100}) data = %{ "nickname" => "GrimReaper", @@ -645,7 +645,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "returns error on with overdue date and after max" do {:ok, invite} = - UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100}) + UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100}) UserInviteToken.update_invite!(invite, uses: 100) -- cgit v1.2.3 From fee50636d07c54328ececfe8805c658e3bb80cc6 Mon Sep 17 00:00:00 2001 From: Alex S Date: Mon, 8 Apr 2019 20:08:16 +0700 Subject: removing not needed invite update --- test/web/twitter_api/twitter_api_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/web') diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 798a009fe..a4540e651 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -492,7 +492,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do refute User.get_by_nickname("vinny") invite = Repo.get_by(UserInviteToken, token: invite.token) - assert invite.used == true + refute invite.used end end -- cgit v1.2.3 From b57b43027cf958d3a3a82b95f155ae27b235b543 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 9 Apr 2019 23:20:31 +0300 Subject: Change response format of /api/pleroma/emoji to the one that actually makes sense --- test/web/twitter_api/util_controller_test.exs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'test/web') diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index 410f20f87..a1a5e3964 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -172,22 +172,19 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do describe "/api/pleroma/emoji" do test "returns json with custom emoji with tags", %{conn: conn} do - [emoji | _body] = + emoji = conn |> get("/api/pleroma/emoji") |> json_response(200) - [key] = Map.keys(emoji) - - %{ - ^key => %{ - "image_url" => url, - "tags" => tags - } - } = emoji - - assert is_binary(url) - assert is_list(tags) + assert Enum.all?(emoji, fn + {_key, + %{ + "image_url" => url, + "tags" => tags + }} -> + is_binary(url) and is_list(tags) + end) end end -- cgit v1.2.3