First
[anni] / test / pleroma / web / pleroma_api / controllers / app_controller_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.PleromaAPI.AppControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Web.OAuth.App
9   alias Pleroma.Web.Push
10
11   import Pleroma.Factory
12
13   test "apps", %{conn: conn} do
14     user = insert(:user)
15     app_attrs = build(:oauth_app)
16
17     creation =
18       conn
19       |> put_req_header("content-type", "application/json")
20       |> assign(:user, user)
21       |> post("/api/v1/apps", %{
22         client_name: app_attrs.client_name,
23         redirect_uris: app_attrs.redirect_uris
24       })
25
26     [app] = App.get_user_apps(user)
27
28     expected = %{
29       "name" => app.client_name,
30       "website" => app.website,
31       "client_id" => app.client_id,
32       "client_secret" => app.client_secret,
33       "id" => app.id |> to_string(),
34       "redirect_uri" => app.redirect_uris,
35       "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
36     }
37
38     assert expected == json_response_and_validate_schema(creation, 200)
39
40     response =
41       conn
42       |> put_req_header("content-type", "application/json")
43       |> assign(:user, user)
44       |> assign(:token, insert(:oauth_token, user: user, scopes: ["read", "follow"]))
45       |> get("/api/v1/pleroma/apps")
46       |> json_response_and_validate_schema(200)
47
48     [apps] = response
49
50     assert length(response) == 1
51     assert apps["client_id"] == app.client_id
52   end
53 end