1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.AuthorizationTest do
7 alias Pleroma.Web.OAuth.Authorization
8 alias Pleroma.Web.OAuth.App
11 test "create an authorization token for a valid app" do
14 App.register_changeset(%App{}, %{
15 client_name: "client",
23 {:ok, auth} = Authorization.create_authorization(app, user)
25 assert auth.user_id == user.id
26 assert auth.app_id == app.id
27 assert String.length(auth.token) > 10
28 assert auth.used == false
31 test "use up a token" do
34 App.register_changeset(%App{}, %{
35 client_name: "client",
43 {:ok, auth} = Authorization.create_authorization(app, user)
45 {:ok, auth} = Authorization.use_token(auth)
47 assert auth.used == true
49 assert {:error, "already used"} == Authorization.use_token(auth)
51 expired_auth = %Authorization{
54 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10),
59 {:ok, expired_auth} = Repo.insert(expired_auth)
61 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
64 test "delete authorizations" do
67 App.register_changeset(%App{}, %{
68 client_name: "client",
76 {:ok, auth} = Authorization.create_authorization(app, user)
77 {:ok, auth} = Authorization.use_token(auth)
79 Authorization.delete_user_authorizations(user)
81 {_, invalid} = Authorization.use_token(auth)
83 assert auth != invalid