[#468] Merged `upstream/develop`, resolved conflicts.
[akkoma] / test / web / oauth / token_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.TokenTest do
6 use Pleroma.DataCase
7 alias Pleroma.Web.OAuth.App
8 alias Pleroma.Web.OAuth.Authorization
9 alias Pleroma.Web.OAuth.Token
10 alias Pleroma.Repo
11
12 import Pleroma.Factory
13
14 test "exchanges a auth token for an access token" do
15 {:ok, app} =
16 Repo.insert(
17 App.register_changeset(%App{}, %{
18 client_name: "client",
19 scopes: ["scope"],
20 redirect_uris: "url"
21 })
22 )
23
24 user = insert(:user)
25
26 {:ok, auth} = Authorization.create_authorization(app, user)
27
28 {:ok, token} = Token.exchange_token(app, auth)
29
30 assert token.app_id == app.id
31 assert token.user_id == user.id
32 assert String.length(token.token) > 10
33 assert String.length(token.refresh_token) > 10
34
35 auth = Repo.get(Authorization, auth.id)
36 {:error, "already used"} = Token.exchange_token(app, auth)
37 end
38
39 test "deletes all tokens of a user" do
40 {:ok, app1} =
41 Repo.insert(
42 App.register_changeset(%App{}, %{
43 client_name: "client1",
44 scopes: ["scope"],
45 redirect_uris: "url"
46 })
47 )
48
49 {:ok, app2} =
50 Repo.insert(
51 App.register_changeset(%App{}, %{
52 client_name: "client2",
53 scopes: ["scope"],
54 redirect_uris: "url"
55 })
56 )
57
58 user = insert(:user)
59
60 {:ok, auth1} = Authorization.create_authorization(app1, user)
61 {:ok, auth2} = Authorization.create_authorization(app2, user)
62
63 {:ok, _token1} = Token.exchange_token(app1, auth1)
64 {:ok, _token2} = Token.exchange_token(app2, auth2)
65
66 {tokens, _} = Token.delete_user_tokens(user)
67
68 assert tokens == 2
69 end
70 end