Fix MRF policies to also work with Update
[akkoma] / test / pleroma / akkoma / frontend_setting_profile_test.exs
1 defmodule Pleroma.Akkoma.FrontendSettingsProfileTest do
2 use Pleroma.DataCase, async: true
3 use Oban.Testing, repo: Pleroma.Repo
4 alias Pleroma.Akkoma.FrontendSettingsProfile
5
6 import Pleroma.Factory
7
8 describe "changeset/2" do
9 test "valid" do
10 user = insert(:user)
11 frontend_name = "test"
12 profile_name = "test"
13 settings = %{"test" => "test"}
14 struct = %FrontendSettingsProfile{}
15
16 attrs = %{
17 user_id: user.id,
18 frontend_name: frontend_name,
19 profile_name: profile_name,
20 settings: settings,
21 version: 1
22 }
23
24 assert %{valid?: true} = FrontendSettingsProfile.changeset(struct, attrs)
25 end
26
27 test "when settings is too long" do
28 clear_config([:instance, :max_frontend_settings_json_chars], 10)
29 user = insert(:user)
30 frontend_name = "test"
31 profile_name = "test"
32 settings = %{"verylong" => "verylongoops"}
33 struct = %FrontendSettingsProfile{}
34
35 attrs = %{
36 user_id: user.id,
37 frontend_name: frontend_name,
38 profile_name: profile_name,
39 settings: settings,
40 version: 1
41 }
42
43 assert %{valid?: false, errors: [settings: {"is too long", _}]} =
44 FrontendSettingsProfile.changeset(struct, attrs)
45 end
46
47 test "when frontend name is too short" do
48 user = insert(:user)
49 frontend_name = ""
50 profile_name = "test"
51 settings = %{"test" => "test"}
52 struct = %FrontendSettingsProfile{}
53
54 attrs = %{
55 user_id: user.id,
56 frontend_name: frontend_name,
57 profile_name: profile_name,
58 settings: settings,
59 version: 1
60 }
61
62 assert %{valid?: false, errors: [frontend_name: {"can't be blank", _}]} =
63 FrontendSettingsProfile.changeset(struct, attrs)
64 end
65
66 test "when profile name is too short" do
67 user = insert(:user)
68 frontend_name = "test"
69 profile_name = ""
70 settings = %{"test" => "test"}
71 struct = %FrontendSettingsProfile{}
72
73 attrs = %{
74 user_id: user.id,
75 frontend_name: frontend_name,
76 profile_name: profile_name,
77 settings: settings,
78 version: 1
79 }
80
81 assert %{valid?: false, errors: [profile_name: {"can't be blank", _}]} =
82 FrontendSettingsProfile.changeset(struct, attrs)
83 end
84
85 test "when version is negative" do
86 user = insert(:user)
87 frontend_name = "test"
88 profile_name = "test"
89 settings = %{"test" => "test"}
90 struct = %FrontendSettingsProfile{}
91
92 attrs = %{
93 user_id: user.id,
94 frontend_name: frontend_name,
95 profile_name: profile_name,
96 settings: settings,
97 version: -1
98 }
99
100 assert %{valid?: false, errors: [version: {"must be greater than %{number}", _}]} =
101 FrontendSettingsProfile.changeset(struct, attrs)
102 end
103 end
104
105 describe "create_or_update/2" do
106 test "it should create a new record" do
107 user = insert(:user)
108 frontend_name = "test"
109 profile_name = "test"
110 settings = %{"test" => "test"}
111
112 assert {:ok, %FrontendSettingsProfile{}} =
113 FrontendSettingsProfile.create_or_update(
114 user,
115 frontend_name,
116 profile_name,
117 settings,
118 1
119 )
120 end
121
122 test "it should update a record" do
123 user = insert(:user)
124 frontend_name = "test"
125 profile_name = "test"
126
127 insert(:frontend_setting_profile,
128 user: user,
129 frontend_name: frontend_name,
130 profile_name: profile_name,
131 settings: %{"test" => "test"},
132 version: 1
133 )
134
135 settings = %{"test" => "test2"}
136
137 assert {:ok, %FrontendSettingsProfile{settings: ^settings}} =
138 FrontendSettingsProfile.create_or_update(
139 user,
140 frontend_name,
141 profile_name,
142 settings,
143 2
144 )
145 end
146 end
147
148 describe "get_all_by_user_and_frontend_name/2" do
149 test "it should return all records" do
150 user = insert(:user)
151 frontend_name = "test"
152
153 insert(:frontend_setting_profile,
154 user: user,
155 frontend_name: frontend_name,
156 profile_name: "profileA",
157 settings: %{"test" => "test"},
158 version: 1
159 )
160
161 insert(:frontend_setting_profile,
162 user: user,
163 frontend_name: frontend_name,
164 profile_name: "profileB",
165 settings: %{"test" => "test"},
166 version: 1
167 )
168
169 assert [%FrontendSettingsProfile{profile_name: "profileA"}, %{profile_name: "profileB"}] =
170 FrontendSettingsProfile.get_all_by_user_and_frontend_name(user, frontend_name)
171 end
172 end
173
174 describe "get_by_user_and_frontend_name_and_profile_name/3" do
175 test "it should return a record" do
176 user = insert(:user)
177 frontend_name = "test"
178 profile_name = "profileA"
179
180 insert(:frontend_setting_profile,
181 user: user,
182 frontend_name: frontend_name,
183 profile_name: profile_name,
184 settings: %{"test" => "test"},
185 version: 1
186 )
187
188 assert %FrontendSettingsProfile{profile_name: "profileA"} =
189 FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
190 user,
191 frontend_name,
192 profile_name
193 )
194 end
195 end
196 end