1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.App
8 alias Pleroma.Web.OAuth.Authorization
14 App.register_changeset(%App{}, %{
15 client_name: "client",
16 scopes: ["read", "write"],
24 test "create an authorization token for a valid app", %{app: app} do
27 {:ok, auth1} = Authorization.create_authorization(app, user)
28 assert auth1.scopes == app.scopes
30 {:ok, auth2} = Authorization.create_authorization(app, user, ["read"])
31 assert auth2.scopes == ["read"]
33 for auth <- [auth1, auth2] do
34 assert auth.user_id == user.id
35 assert auth.app_id == app.id
36 assert String.length(auth.token) > 10
37 assert auth.used == false
41 test "use up a token", %{app: app} do
44 {:ok, auth} = Authorization.create_authorization(app, user)
46 {:ok, auth} = Authorization.use_token(auth)
48 assert auth.used == true
50 assert {:error, "already used"} == Authorization.use_token(auth)
52 expired_auth = %Authorization{
55 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10),
60 {:ok, expired_auth} = Repo.insert(expired_auth)
62 assert {:error, "token expired"} == Authorization.use_token(expired_auth)
65 test "delete authorizations", %{app: app} do
68 {:ok, auth} = Authorization.create_authorization(app, user)
69 {:ok, auth} = Authorization.use_token(auth)
71 Authorization.delete_user_authorizations(user)
73 {_, invalid} = Authorization.use_token(auth)
75 assert auth != invalid