Attempt to resolve merge conflict
[akkoma] / test / web / oauth / authorization_test.exs
1 defmodule Pleroma.Web.OAuth.AuthorizationTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.OAuth.{Authorization, App}
4 import Pleroma.Factory
5
6 test "create an authorization token for a valid app" do
7 {:ok, app} =
8 Repo.insert(
9 App.register_changeset(%App{}, %{
10 client_name: "client",
11 scopes: "scope",
12 redirect_uris: "url"
13 })
14 )
15
16 user = insert(:user)
17
18 {:ok, auth} = Authorization.create_authorization(app, user)
19
20 assert auth.user_id == user.id
21 assert auth.app_id == app.id
22 assert String.length(auth.token) > 10
23 assert auth.used == false
24 end
25
26 test "use up a token" do
27 {:ok, app} =
28 Repo.insert(
29 App.register_changeset(%App{}, %{
30 client_name: "client",
31 scopes: "scope",
32 redirect_uris: "url"
33 })
34 )
35
36 user = insert(:user)
37
38 {:ok, auth} = Authorization.create_authorization(app, user)
39
40 {:ok, auth} = Authorization.use_token(auth)
41
42 assert auth.used == true
43
44 assert {:error, "already used"} == Authorization.use_token(auth)
45
46 expired_auth = %Authorization{
47 user_id: user.id,
48 app_id: app.id,
49 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10),
50 token: "mytoken",
51 used: false
52 }
53
54 {:ok, expired_auth} = Repo.insert(expired_auth)
55
56 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
57 end
58
59 test "delete authorizations" do
60 {:ok, app} =
61 Repo.insert(
62 App.register_changeset(%App{}, %{
63 client_name: "client",
64 scopes: "scope",
65 redirect_uris: "url"
66 })
67 )
68
69 user = insert(:user)
70
71 {:ok, auth} = Authorization.create_authorization(app, user)
72 {:ok, auth} = Authorization.use_token(auth)
73
74 {auths, _} = Authorization.delete_user_authorizations(user)
75
76 {_, invalid} = Authorization.use_token(auth)
77
78 assert auth != invalid
79 end
80 end