First
[anni] / test / pleroma / web / o_auth / app_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.AppTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.Web.OAuth.App
9   import Pleroma.Factory
10
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
17     end
18
19     test "make app" do
20       attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
21       {:ok, %App{} = app} = App.get_or_make(attrs, ["write"])
22       assert app.scopes == ["write"]
23     end
24
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"]
31     end
32
33     test "has unique client_id" do
34       insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop")
35
36       error =
37         catch_error(insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop"))
38
39       assert %Ecto.ConstraintError{} = error
40       assert error.constraint == "apps_client_id_index"
41       assert error.type == :unique
42     end
43   end
44
45   test "get_user_apps/1" do
46     user = insert(:user)
47
48     apps = [
49       insert(:oauth_app, user_id: user.id),
50       insert(:oauth_app, user_id: user.id),
51       insert(:oauth_app, user_id: user.id)
52     ]
53
54     assert Enum.sort(App.get_user_apps(user)) == Enum.sort(apps)
55   end
56 end