Remove support for outdated Create format
[akkoma] / test / pleroma / web / o_auth / token_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, async: true
7 alias Pleroma.Repo
8 alias Pleroma.Web.OAuth.App
9 alias Pleroma.Web.OAuth.Authorization
10 alias Pleroma.Web.OAuth.Token
11
12 import Pleroma.Factory
13
14 test "exchanges a auth token for an access token, preserving `scopes`" do
15 {:ok, app} =
16 Repo.insert(
17 App.register_changeset(%App{}, %{
18 client_name: "client",
19 scopes: ["read", "write"],
20 redirect_uris: "url"
21 })
22 )
23
24 user = insert(:user)
25
26 {:ok, auth} = Authorization.create_authorization(app, user, ["read"])
27 assert auth.scopes == ["read"]
28
29 {:ok, token} = Token.exchange_token(app, auth)
30
31 assert token.app_id == app.id
32 assert token.user_id == user.id
33 assert token.scopes == auth.scopes
34 assert String.length(token.token) > 10
35 assert String.length(token.refresh_token) > 10
36
37 auth = Repo.get(Authorization, auth.id)
38 {:error, "already used"} = Token.exchange_token(app, auth)
39 end
40
41 test "deletes all tokens of a user" do
42 {:ok, app1} =
43 Repo.insert(
44 App.register_changeset(%App{}, %{
45 client_name: "client1",
46 scopes: ["scope"],
47 redirect_uris: "url"
48 })
49 )
50
51 {:ok, app2} =
52 Repo.insert(
53 App.register_changeset(%App{}, %{
54 client_name: "client2",
55 scopes: ["scope"],
56 redirect_uris: "url"
57 })
58 )
59
60 user = insert(:user)
61
62 {:ok, auth1} = Authorization.create_authorization(app1, user)
63 {:ok, auth2} = Authorization.create_authorization(app2, user)
64
65 {:ok, _token1} = Token.exchange_token(app1, auth1)
66 {:ok, _token2} = Token.exchange_token(app2, auth2)
67
68 {tokens, _} = Token.delete_user_tokens(user)
69
70 assert tokens == 2
71 end
72 end