Merge branch 'builder-note' into 'develop'
[akkoma] / test / pleroma / web / twitter_api / controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.OAuth.Token
11
12 import Pleroma.Factory
13
14 describe "GET /api/account/confirm_email/:id/:token" do
15 setup do
16 {:ok, user} =
17 insert(:user)
18 |> User.confirmation_changeset(set_confirmation: false)
19 |> Repo.update()
20
21 refute user.is_confirmed
22
23 [user: user]
24 end
25
26 test "it redirects to root url", %{conn: conn, user: user} do
27 conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
28
29 assert 302 == conn.status
30 end
31
32 test "it confirms the user account", %{conn: conn, user: user} do
33 get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
34
35 user = User.get_cached_by_id(user.id)
36
37 assert user.is_confirmed
38 refute user.confirmation_token
39 end
40
41 test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
42 conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
43
44 assert 500 == conn.status
45 end
46
47 test "it returns 500 if token is invalid", %{conn: conn, user: user} do
48 conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
49
50 assert 500 == conn.status
51 end
52 end
53
54 describe "GET /api/oauth_tokens" do
55 setup do
56 token = insert(:oauth_token) |> Repo.preload(:user)
57
58 %{token: token}
59 end
60
61 test "renders list", %{token: token} do
62 response =
63 build_conn()
64 |> assign(:user, token.user)
65 |> get("/api/oauth_tokens")
66
67 keys =
68 json_response(response, 200)
69 |> hd()
70 |> Map.keys()
71
72 assert keys -- ["id", "app_name", "valid_until"] == []
73 end
74
75 test "revoke token", %{token: token} do
76 response =
77 build_conn()
78 |> assign(:user, token.user)
79 |> delete("/api/oauth_tokens/#{token.id}")
80
81 tokens = Token.get_user_tokens(token.user)
82
83 assert tokens == []
84 assert response.status == 201
85 end
86 end
87 end