1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.AppTest do
6 use Pleroma.DataCase, async: true
8 alias Pleroma.Web.OAuth.App
11 describe "get_or_make/2" do
12 test "gets exist app" do
13 attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
14 app = insert(:oauth_app, Map.merge(attrs, %{scopes: ["read", "write"]}))
15 {:ok, %App{} = exist_app} = App.get_or_make(attrs, [])
16 assert exist_app == app
20 attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
21 {:ok, %App{} = app} = App.get_or_make(attrs, ["write"])
22 assert app.scopes == ["write"]
25 test "gets exist app and updates scopes" do
26 attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
27 app = insert(:oauth_app, Map.merge(attrs, %{scopes: ["read", "write"]}))
28 {:ok, %App{} = exist_app} = App.get_or_make(attrs, ["read", "write", "follow", "push"])
29 assert exist_app.id == app.id
30 assert exist_app.scopes == ["read", "write", "follow", "push"]
33 test "has unique client_id" do
34 insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop")
37 catch_error(insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop"))
39 assert %Ecto.ConstraintError{} = error
40 assert error.constraint == "apps_client_id_index"
41 assert error.type == :unique
45 test "get_user_apps/1" do
49 insert(:oauth_app, user_id: user.id),
50 insert(:oauth_app, user_id: user.id),
51 insert(:oauth_app, user_id: user.id)
54 assert Enum.sort(App.get_user_apps(user)) == Enum.sort(apps)