Merge branch 'fix/2132-emoji-packs' into 'develop'
[akkoma] / test / web / twitter_api / password_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.PasswordControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.PasswordResetToken
9 alias Pleroma.User
10 alias Pleroma.Web.OAuth.Token
11 import Pleroma.Factory
12
13 describe "GET /api/pleroma/password_reset/token" do
14 test "it returns error when token invalid", %{conn: conn} do
15 response =
16 conn
17 |> get("/api/pleroma/password_reset/token")
18 |> html_response(:ok)
19
20 assert response =~ "<h2>Invalid Token</h2>"
21 end
22
23 test "it shows password reset form", %{conn: conn} do
24 user = insert(:user)
25 {:ok, token} = PasswordResetToken.create_token(user)
26
27 response =
28 conn
29 |> get("/api/pleroma/password_reset/#{token.token}")
30 |> html_response(:ok)
31
32 assert response =~ "<h2>Password Reset for #{user.nickname}</h2>"
33 end
34 end
35
36 describe "POST /api/pleroma/password_reset" do
37 test "it returns HTTP 200", %{conn: conn} do
38 user = insert(:user)
39 {:ok, token} = PasswordResetToken.create_token(user)
40 {:ok, _access_token} = Token.create_token(insert(:oauth_app), user, %{})
41
42 params = %{
43 "password" => "test",
44 password_confirmation: "test",
45 token: token.token
46 }
47
48 response =
49 conn
50 |> assign(:user, user)
51 |> post("/api/pleroma/password_reset", %{data: params})
52 |> html_response(:ok)
53
54 assert response =~ "<h2>Password changed!</h2>"
55
56 user = refresh_record(user)
57 assert Pbkdf2.verify_pass("test", user.password_hash)
58 assert Enum.empty?(Token.get_user_tokens(user))
59 end
60
61 test "it sets password_reset_pending to false", %{conn: conn} do
62 user = insert(:user, password_reset_pending: true)
63
64 {:ok, token} = PasswordResetToken.create_token(user)
65 {:ok, _access_token} = Token.create_token(insert(:oauth_app), user, %{})
66
67 params = %{
68 "password" => "test",
69 password_confirmation: "test",
70 token: token.token
71 }
72
73 conn
74 |> assign(:user, user)
75 |> post("/api/pleroma/password_reset", %{data: params})
76 |> html_response(:ok)
77
78 assert User.get_by_id(user.id).password_reset_pending == false
79 end
80 end
81 end