Merge branch 'oauth2' into 'develop'
[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} = Repo.insert(App.register_changeset(%App{}, %{client_name: "client", scopes: "scope", redirect_uris: "url"}))
8 user = insert(:user)
9
10 {:ok, auth} = Authorization.create_authorization(app, user)
11
12 assert auth.user_id == user.id
13 assert auth.app_id == app.id
14 assert String.length(auth.token) > 10
15 assert auth.used == false
16 end
17
18 test "use up a token" do
19 {:ok, app} = Repo.insert(App.register_changeset(%App{}, %{client_name: "client", scopes: "scope", redirect_uris: "url"}))
20 user = insert(:user)
21
22 {:ok, auth} = Authorization.create_authorization(app, user)
23
24 {:ok, auth} = Authorization.use_token(auth)
25
26 assert auth.used == true
27
28 assert {:error, "already used"} == Authorization.use_token(auth)
29
30 expired_auth = %Authorization{
31 user_id: user.id,
32 app_id: app.id,
33 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now, -10),
34 token: "mytoken",
35 used: false
36 }
37
38 {:ok, expired_auth} = Repo.insert(expired_auth)
39
40 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
41 end
42 end