Merge branch 'develop' into improve-move-notificaions-api
[akkoma] / test / registration_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.RegistrationTest do
6 use Pleroma.DataCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Registration
11 alias Pleroma.Repo
12
13 describe "generic changeset" do
14 test "requires :provider, :uid" do
15 registration = build(:registration, provider: nil, uid: nil)
16
17 cs = Registration.changeset(registration, %{})
18 refute cs.valid?
19
20 assert [
21 provider: {"can't be blank", [validation: :required]},
22 uid: {"can't be blank", [validation: :required]}
23 ] == cs.errors
24 end
25
26 test "ensures uniqueness of [:provider, :uid]" do
27 registration = insert(:registration)
28 registration2 = build(:registration, provider: registration.provider, uid: registration.uid)
29
30 cs = Registration.changeset(registration2, %{})
31 assert cs.valid?
32
33 assert {:error,
34 %Ecto.Changeset{
35 errors: [
36 uid:
37 {"has already been taken",
38 [constraint: :unique, constraint_name: "registrations_provider_uid_index"]}
39 ]
40 }} = Repo.insert(cs)
41
42 # Note: multiple :uid values per [:user_id, :provider] are intentionally allowed
43 cs2 = Registration.changeset(registration2, %{uid: "available.uid"})
44 assert cs2.valid?
45 assert {:ok, _} = Repo.insert(cs2)
46
47 cs3 = Registration.changeset(registration2, %{provider: "provider2"})
48 assert cs3.valid?
49 assert {:ok, _} = Repo.insert(cs3)
50 end
51
52 test "allows `nil` :user_id (user-unbound registration)" do
53 registration = build(:registration, user_id: nil)
54 cs = Registration.changeset(registration, %{})
55 assert cs.valid?
56 assert {:ok, _} = Repo.insert(cs)
57 end
58 end
59 end