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, App}
10 test "create an authorization token for a valid app" do
13 App.register_changeset(%App{}, %{
14 client_name: "client",
22 {:ok, auth} = Authorization.create_authorization(app, user)
24 assert auth.user_id == user.id
25 assert auth.app_id == app.id
26 assert String.length(auth.token) > 10
27 assert auth.used == false
30 test "use up a token" do
33 App.register_changeset(%App{}, %{
34 client_name: "client",
42 {:ok, auth} = Authorization.create_authorization(app, user)
44 {:ok, auth} = Authorization.use_token(auth)
46 assert auth.used == true
48 assert {:error, "already used"} == Authorization.use_token(auth)
50 expired_auth = %Authorization{
53 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10),
58 {:ok, expired_auth} = Repo.insert(expired_auth)
60 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
63 test "delete authorizations" do
66 App.register_changeset(%App{}, %{
67 client_name: "client",
75 {:ok, auth} = Authorization.create_authorization(app, user)
76 {:ok, auth} = Authorization.use_token(auth)
78 Authorization.delete_user_authorizations(user)
80 {_, invalid} = Authorization.use_token(auth)
82 assert auth != invalid