464d0ea2eed10825ed7210df2b4c323cc2ccb195
[akkoma] / test / pleroma / web / twitter_api / 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.TwitterAPI.ControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Builders.ActivityBuilder
9 alias Pleroma.Repo
10 alias Pleroma.User
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 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
40
41 response_conn =
42 conn
43 |> assign(:user, current_user)
44 |> get("/api/v1/notifications")
45
46 [notification] = response = json_response(response_conn, 200)
47
48 assert length(response) == 1
49
50 assert notification["pleroma"]["is_seen"] == false
51
52 response_conn =
53 conn
54 |> assign(:user, current_user)
55 |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
56
57 [notification] = response = json_response(response_conn, 200)
58
59 assert length(response) == 1
60
61 assert notification["pleroma"]["is_seen"] == true
62 end
63 end
64
65 describe "GET /api/account/confirm_email/:id/:token" do
66 setup do
67 {:ok, user} =
68 insert(:user)
69 |> User.confirmation_changeset(need_confirmation: true)
70 |> Repo.update()
71
72 assert user.confirmation_pending
73
74 [user: user]
75 end
76
77 test "it redirects to root url", %{conn: conn, user: user} do
78 conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
79
80 assert 302 == conn.status
81 end
82
83 test "it confirms the user account", %{conn: conn, user: user} do
84 get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
85
86 user = User.get_cached_by_id(user.id)
87
88 refute user.confirmation_pending
89 refute user.confirmation_token
90 end
91
92 test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
93 conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
94
95 assert 500 == conn.status
96 end
97
98 test "it returns 500 if token is invalid", %{conn: conn, user: user} do
99 conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
100
101 assert 500 == conn.status
102 end
103 end
104
105 describe "GET /api/oauth_tokens" do
106 setup do
107 token = insert(:oauth_token) |> Repo.preload(:user)
108
109 %{token: token}
110 end
111
112 test "renders list", %{token: token} do
113 response =
114 build_conn()
115 |> assign(:user, token.user)
116 |> get("/api/oauth_tokens")
117
118 keys =
119 json_response(response, 200)
120 |> hd()
121 |> Map.keys()
122
123 assert keys -- ["id", "app_name", "valid_until"] == []
124 end
125
126 test "revoke token", %{token: token} do
127 response =
128 build_conn()
129 |> assign(:user, token.user)
130 |> delete("/api/oauth_tokens/#{token.id}")
131
132 tokens = Token.get_user_tokens(token.user)
133
134 assert tokens == []
135 assert response.status == 201
136 end
137 end
138 end