giant massive dep upgrade and dialyxir-found error emporium (#371)
[akkoma] / test / pleroma / web / mastodon_api / controllers / app_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AppControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Repo
9 alias Pleroma.Web.OAuth.App
10 alias Pleroma.Web.Push
11
12 import Pleroma.Factory
13
14 test "apps/verify_credentials", %{conn: conn} do
15 user_bound_token = insert(:oauth_token)
16 app_bound_token = insert(:oauth_token, user: nil)
17 refute app_bound_token.user
18
19 for token <- [app_bound_token, user_bound_token] do
20 conn =
21 conn
22 |> put_req_header("authorization", "Bearer #{token.token}")
23 |> get("/api/v1/apps/verify_credentials")
24
25 app = Repo.preload(token, :app).app
26
27 expected = %{
28 "name" => app.client_name,
29 "website" => app.website,
30 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
31 }
32
33 assert expected == json_response_and_validate_schema(conn, 200)
34 end
35 end
36
37 test "creates an oauth app", %{conn: conn} do
38 app_attrs = build(:oauth_app)
39
40 conn =
41 conn
42 |> put_req_header("content-type", "application/json")
43 |> post("/api/v1/apps", %{
44 client_name: app_attrs.client_name,
45 redirect_uris: app_attrs.redirect_uris
46 })
47
48 [app] = Repo.all(App)
49
50 expected = %{
51 "name" => app.client_name,
52 "website" => app.website,
53 "client_id" => app.client_id,
54 "client_secret" => app.client_secret,
55 "id" => app.id |> to_string(),
56 "redirect_uri" => app.redirect_uris,
57 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
58 }
59
60 assert expected == json_response_and_validate_schema(conn, 200)
61 assert app.user_id == nil
62 end
63
64 test "creates an oauth app with a user", %{conn: conn} do
65 user = insert(:user)
66 app_attrs = build(:oauth_app)
67
68 conn =
69 conn
70 |> put_req_header("content-type", "application/json")
71 |> assign(:user, user)
72 |> post("/api/v1/apps", %{
73 client_name: app_attrs.client_name,
74 redirect_uris: app_attrs.redirect_uris
75 })
76
77 [app] = Repo.all(App)
78
79 expected = %{
80 "name" => app.client_name,
81 "website" => app.website,
82 "client_id" => app.client_id,
83 "client_secret" => app.client_secret,
84 "id" => app.id |> to_string(),
85 "redirect_uri" => app.redirect_uris,
86 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
87 }
88
89 assert expected == json_response_and_validate_schema(conn, 200)
90 assert app.user_id == user.id
91 end
92 end