6cc08ee0e988229e66e0dafc4db37a82f52f7aef
[akkoma] / test / web / api_spec / account_operation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ApiSpec.AccountOperationTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Web.ApiSpec
9 alias Pleroma.Web.ApiSpec.Schemas.Account
10 alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
11 alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
12 alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse
13 alias Pleroma.Web.ApiSpec.Schemas.AccountUpdateCredentialsRequest
14
15 import OpenApiSpex.TestAssertions
16 import Pleroma.Factory
17
18 test "Account example matches schema" do
19 api_spec = ApiSpec.spec()
20 schema = Account.schema()
21 assert_schema(schema.example, "Account", api_spec)
22 end
23
24 test "AccountCreateRequest example matches schema" do
25 api_spec = ApiSpec.spec()
26 schema = AccountCreateRequest.schema()
27 assert_schema(schema.example, "AccountCreateRequest", api_spec)
28 end
29
30 test "AccountCreateResponse example matches schema" do
31 api_spec = ApiSpec.spec()
32 schema = AccountCreateResponse.schema()
33 assert_schema(schema.example, "AccountCreateResponse", api_spec)
34 end
35
36 test "AccountUpdateCredentialsRequest example matches schema" do
37 api_spec = ApiSpec.spec()
38 schema = AccountUpdateCredentialsRequest.schema()
39 assert_schema(schema.example, "AccountUpdateCredentialsRequest", api_spec)
40 end
41
42 test "AccountController produces a AccountCreateResponse", %{conn: conn} do
43 api_spec = ApiSpec.spec()
44 app_token = insert(:oauth_token, user: nil)
45
46 json =
47 conn
48 |> put_req_header("authorization", "Bearer " <> app_token.token)
49 |> put_req_header("content-type", "application/json")
50 |> post(
51 "/api/v1/accounts",
52 %{
53 username: "foo",
54 email: "bar@example.org",
55 password: "qwerty",
56 agreement: true
57 }
58 )
59 |> json_response(200)
60
61 assert_schema(json, "AccountCreateResponse", api_spec)
62 end
63
64 test "AccountUpdateCredentialsRequest produces an Account", %{conn: conn} do
65 api_spec = ApiSpec.spec()
66 token = insert(:oauth_token, scopes: ["read", "write"])
67
68 json =
69 conn
70 |> put_req_header("authorization", "Bearer " <> token.token)
71 |> put_req_header("content-type", "application/json")
72 |> patch(
73 "/api/v1/accounts/update_credentials",
74 %{
75 hide_followers_count: "true",
76 hide_follows_count: "true",
77 skip_thread_containment: "true",
78 hide_follows: "true",
79 pleroma_settings_store: %{"pleroma-fe" => %{"key" => "val"}},
80 note: "foobar",
81 fields_attributes: [%{name: "foo", value: "bar"}]
82 }
83 )
84 |> json_response(200)
85
86 assert_schema(json, "Account", api_spec)
87 end
88
89 test "AccountRelationshipsResponse example matches schema" do
90 api_spec = ApiSpec.spec()
91 schema = AccountRelationshipsResponse.schema()
92 assert_schema(schema.example, "AccountRelationshipsResponse", api_spec)
93 end
94
95 test "/api/v1/accounts/relationships produces AccountRelationshipsResponse", %{
96 conn: conn
97 } do
98 token = insert(:oauth_token, scopes: ["read", "write"])
99 other_user = insert(:user)
100 {:ok, _user} = Pleroma.User.follow(token.user, other_user)
101 api_spec = ApiSpec.spec()
102
103 assert [relationship] =
104 conn
105 |> put_req_header("authorization", "Bearer " <> token.token)
106 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
107 |> json_response(:ok)
108
109 assert_schema([relationship], "AccountRelationshipsResponse", api_spec)
110 end
111
112 test "/api/v1/accounts/:id produces Account", %{
113 conn: conn
114 } do
115 user = insert(:user)
116 api_spec = ApiSpec.spec()
117
118 assert resp =
119 conn
120 |> get("/api/v1/accounts/#{user.id}")
121 |> json_response(:ok)
122
123 assert_schema(resp, "Account", api_spec)
124 end
125 end