583c904b2e18ac1fb059dd4c2db4a24dff0fb197
[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.CommonAPI
11 alias Pleroma.Web.OAuth.Token
12
13 import Pleroma.Factory
14
15 describe "POST /api/qvitter/statuses/notifications/read" do
16 test "without valid credentials", %{conn: conn} do
17 conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567})
18 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
19 end
20
21 test "with credentials, without any params" do
22 %{conn: conn} = oauth_access(["write:notifications"])
23
24 conn = post(conn, "/api/qvitter/statuses/notifications/read")
25
26 assert json_response(conn, 400) == %{
27 "error" => "You need to specify latest_id",
28 "request" => "/api/qvitter/statuses/notifications/read"
29 }
30 end
31
32 test "with credentials, with params" do
33 %{user: current_user, conn: conn} =
34 oauth_access(["read:notifications", "write:notifications"])
35
36 other_user = insert(:user)
37
38 {:ok, _activity} =
39 CommonAPI.post(other_user, %{
40 status: "Hey @#{current_user.nickname}"
41 })
42
43 response_conn =
44 conn
45 |> get("/api/v1/notifications")
46
47 [notification] = json_response(response_conn, 200)
48
49 assert notification["pleroma"]["is_seen"] == false
50
51 response_conn =
52 conn
53 |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
54
55 [notification] = response = json_response(response_conn, 200)
56
57 assert length(response) == 1
58
59 assert notification["pleroma"]["is_seen"] == true
60 end
61 end
62
63 describe "GET /api/account/confirm_email/:id/:token" do
64 setup do
65 {:ok, user} =
66 insert(:user)
67 |> User.confirmation_changeset(set_confirmation: false)
68 |> Repo.update()
69
70 refute user.is_confirmed
71
72 [user: user]
73 end
74
75 test "it redirects to root url", %{conn: conn, user: user} do
76 conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
77
78 assert 302 == conn.status
79 end
80
81 test "it confirms the user account", %{conn: conn, user: user} do
82 get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
83
84 user = User.get_cached_by_id(user.id)
85
86 assert user.is_confirmed
87 refute user.confirmation_token
88 end
89
90 test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
91 conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
92
93 assert 500 == conn.status
94 end
95
96 test "it returns 500 if token is invalid", %{conn: conn, user: user} do
97 conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
98
99 assert 500 == conn.status
100 end
101 end
102
103 describe "GET /api/oauth_tokens" do
104 setup do
105 token = insert(:oauth_token) |> Repo.preload(:user)
106
107 %{token: token}
108 end
109
110 test "renders list", %{token: token} do
111 response =
112 build_conn()
113 |> assign(:user, token.user)
114 |> get("/api/oauth_tokens")
115
116 keys =
117 json_response(response, 200)
118 |> hd()
119 |> Map.keys()
120
121 assert keys -- ["id", "app_name", "valid_until"] == []
122 end
123
124 test "revoke token", %{token: token} do
125 response =
126 build_conn()
127 |> assign(:user, token.user)
128 |> delete("/api/oauth_tokens/#{token.id}")
129
130 tokens = Token.get_user_tokens(token.user)
131
132 assert tokens == []
133 assert response.status == 201
134 end
135 end
136 end