1 defmodule Pleroma.Web.OAuth.AuthorizationTest do
3 alias Pleroma.Web.OAuth.{Authorization, App}
6 test "create an authorization token for a valid app" do
9 App.register_changeset(%App{}, %{
10 client_name: "client",
18 {:ok, auth} = Authorization.create_authorization(app, user)
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
26 test "use up a token" do
29 App.register_changeset(%App{}, %{
30 client_name: "client",
38 {:ok, auth} = Authorization.create_authorization(app, user)
40 {:ok, auth} = Authorization.use_token(auth)
42 assert auth.used == true
44 assert {:error, "already used"} == Authorization.use_token(auth)
46 expired_auth = %Authorization{
49 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10),
54 {:ok, expired_auth} = Repo.insert(expired_auth)
56 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
59 test "delete authorizations" do
62 App.register_changeset(%App{}, %{
63 client_name: "client",
71 {:ok, auth} = Authorization.create_authorization(app, user)
72 {:ok, auth} = Authorization.use_token(auth)
74 {auths, _} = Authorization.delete_user_authorizations(user)
76 {_, invalid} = Authorization.use_token(auth)
78 assert auth != invalid