Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into chat-federation...
[akkoma] / test / web / masto_fe_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MastoFEController do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9 alias Pleroma.User
10
11 import Pleroma.Factory
12
13 setup do: clear_config([:instance, :public])
14
15 test "put settings", %{conn: conn} do
16 user = insert(:user)
17
18 conn =
19 conn
20 |> assign(:user, user)
21 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:accounts"]))
22 |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
23
24 assert _result = json_response(conn, 200)
25
26 user = User.get_cached_by_ap_id(user.ap_id)
27 assert user.mastofe_settings == %{"programming" => "socks"}
28 end
29
30 describe "index/2 redirections" do
31 setup %{conn: conn} do
32 session_opts = [
33 store: :cookie,
34 key: "_test",
35 signing_salt: "cooldude"
36 ]
37
38 conn =
39 conn
40 |> Plug.Session.call(Plug.Session.init(session_opts))
41 |> fetch_session()
42
43 test_path = "/web/statuses/test"
44 %{conn: conn, path: test_path}
45 end
46
47 test "redirects not logged-in users to the login page", %{conn: conn, path: path} do
48 conn = get(conn, path)
49
50 assert conn.status == 302
51 assert redirected_to(conn) == "/web/login"
52 end
53
54 test "redirects not logged-in users to the login page on private instances", %{
55 conn: conn,
56 path: path
57 } do
58 Config.put([:instance, :public], false)
59
60 conn = get(conn, path)
61
62 assert conn.status == 302
63 assert redirected_to(conn) == "/web/login"
64 end
65
66 test "does not redirect logged in users to the login page", %{conn: conn, path: path} do
67 token = insert(:oauth_token, scopes: ["read"])
68
69 conn =
70 conn
71 |> assign(:user, token.user)
72 |> assign(:token, token)
73 |> get(path)
74
75 assert conn.status == 200
76 end
77
78 test "saves referer path to session", %{conn: conn, path: path} do
79 conn = get(conn, path)
80 return_to = Plug.Conn.get_session(conn, :return_to)
81
82 assert return_to == path
83 end
84 end
85 end