eva
[akkoma] / test / web / twitter_api / password_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.OAuth.Token
10 import Pleroma.Factory
11
12 describe "GET /api/pleroma/password_reset/token" do
13 test "it returns error when token invalid", %{conn: conn} do
14 response =
15 conn
16 |> get("/api/pleroma/password_reset/token")
17 |> html_response(:ok)
18
19 assert response =~ "<h2>Invalid Token</h2>"
20 end
21
22 test "it shows password reset form", %{conn: conn} do
23 user = insert(:user)
24 {:ok, token} = PasswordResetToken.create_token(user)
25
26 response =
27 conn
28 |> get("/api/pleroma/password_reset/#{token.token}")
29 |> html_response(:ok)
30
31 assert response =~ "<h2>Password Reset for #{user.nickname}</h2>"
32 end
33 end
34
35 describe "POST /api/pleroma/password_reset" do
36 test "it returns HTTP 200", %{conn: conn} do
37 user = insert(:user)
38 {:ok, token} = PasswordResetToken.create_token(user)
39 {:ok, _access_token} = Token.create_token(insert(:oauth_app), user, %{})
40
41 params = %{
42 "password" => "test",
43 password_confirmation: "test",
44 token: token.token
45 }
46
47 response =
48 conn
49 |> assign(:user, user)
50 |> post("/api/pleroma/password_reset", %{data: params})
51 |> html_response(:ok)
52
53 assert response =~ "<h2>Password changed!</h2>"
54
55 user = refresh_record(user)
56 assert Comeonin.Pbkdf2.checkpw("test", user.password_hash)
57 assert length(Token.get_user_tokens(user)) == 0
58 end
59 end
60 end