Merge remote-tracking branch 'remotes/origin/develop' into 1505-threads-federation
[akkoma] / test / web / twitter_api / 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 %{user: current_user, conn: conn} =
23 oauth_access(["read:notifications", "write:notifications"])
24
25 conn =
26 conn
27 |> assign(:user, current_user)
28 |> post("/api/qvitter/statuses/notifications/read")
29
30 assert json_response(conn, 400) == %{
31 "error" => "You need to specify latest_id",
32 "request" => "/api/qvitter/statuses/notifications/read"
33 }
34 end
35
36 test "with credentials, with params" do
37 %{user: current_user, conn: conn} =
38 oauth_access(["read:notifications", "write:notifications"])
39
40 other_user = insert(:user)
41
42 {:ok, _activity} =
43 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
44
45 response_conn =
46 conn
47 |> assign(:user, current_user)
48 |> get("/api/v1/notifications")
49
50 [notification] = response = json_response(response_conn, 200)
51
52 assert length(response) == 1
53
54 assert notification["pleroma"]["is_seen"] == false
55
56 response_conn =
57 conn
58 |> assign(:user, current_user)
59 |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
60
61 [notification] = response = json_response(response_conn, 200)
62
63 assert length(response) == 1
64
65 assert notification["pleroma"]["is_seen"] == true
66 end
67 end
68
69 describe "GET /api/account/confirm_email/:id/:token" do
70 setup do
71 {:ok, user} =
72 insert(:user)
73 |> User.confirmation_changeset(need_confirmation: true)
74 |> Repo.update()
75
76 assert user.confirmation_pending
77
78 [user: user]
79 end
80
81 test "it redirects to root url", %{conn: conn, user: user} do
82 conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
83
84 assert 302 == conn.status
85 end
86
87 test "it confirms the user account", %{conn: conn, user: user} do
88 get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
89
90 user = User.get_cached_by_id(user.id)
91
92 refute user.confirmation_pending
93 refute user.confirmation_token
94 end
95
96 test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
97 conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
98
99 assert 500 == conn.status
100 end
101
102 test "it returns 500 if token is invalid", %{conn: conn, user: user} do
103 conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
104
105 assert 500 == conn.status
106 end
107 end
108
109 describe "GET /api/oauth_tokens" do
110 setup do
111 token = insert(:oauth_token) |> Repo.preload(:user)
112
113 %{token: token}
114 end
115
116 test "renders list", %{token: token} do
117 response =
118 build_conn()
119 |> assign(:user, token.user)
120 |> get("/api/oauth_tokens")
121
122 keys =
123 json_response(response, 200)
124 |> hd()
125 |> Map.keys()
126
127 assert keys -- ["id", "app_name", "valid_until"] == []
128 end
129
130 test "revoke token", %{token: token} do
131 response =
132 build_conn()
133 |> assign(:user, token.user)
134 |> delete("/api/oauth_tokens/#{token.id}")
135
136 tokens = Token.get_user_tokens(token.user)
137
138 assert tokens == []
139 assert response.status == 201
140 end
141 end
142 end