logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

registration_test.exs (1909B)


  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. defmodule Pleroma.RegistrationTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Registration
  8. alias Pleroma.Repo
  9. describe "generic changeset" do
  10. test "requires :provider, :uid" do
  11. registration = build(:registration, provider: nil, uid: nil)
  12. cs = Registration.changeset(registration, %{})
  13. refute cs.valid?
  14. assert [
  15. provider: {"can't be blank", [validation: :required]},
  16. uid: {"can't be blank", [validation: :required]}
  17. ] == cs.errors
  18. end
  19. test "ensures uniqueness of [:provider, :uid]" do
  20. registration = insert(:registration)
  21. registration2 = build(:registration, provider: registration.provider, uid: registration.uid)
  22. cs = Registration.changeset(registration2, %{})
  23. assert cs.valid?
  24. assert {:error,
  25. %Ecto.Changeset{
  26. errors: [
  27. uid:
  28. {"has already been taken",
  29. [constraint: :unique, constraint_name: "registrations_provider_uid_index"]}
  30. ]
  31. }} = Repo.insert(cs)
  32. # Note: multiple :uid values per [:user_id, :provider] are intentionally allowed
  33. cs2 = Registration.changeset(registration2, %{uid: "available.uid"})
  34. assert cs2.valid?
  35. assert {:ok, _} = Repo.insert(cs2)
  36. cs3 = Registration.changeset(registration2, %{provider: "provider2"})
  37. assert cs3.valid?
  38. assert {:ok, _} = Repo.insert(cs3)
  39. end
  40. test "allows `nil` :user_id (user-unbound registration)" do
  41. registration = build(:registration, user_id: nil)
  42. cs = Registration.changeset(registration, %{})
  43. assert cs.valid?
  44. assert {:ok, _} = Repo.insert(cs)
  45. end
  46. end
  47. end