1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserInviteTokenTest do
6 use ExUnit.Case, async: true
8 alias Pleroma.UserInviteToken
10 describe "valid_invite?/1 one time invites" do
12 invite = %UserInviteToken{invite_type: "one_time"}
17 test "not used returns true", %{invite: invite} do
18 invite = %{invite | used: false}
19 assert UserInviteToken.valid_invite?(invite)
22 test "used returns false", %{invite: invite} do
23 invite = %{invite | used: true}
24 refute UserInviteToken.valid_invite?(invite)
28 describe "valid_invite?/1 reusable invites" do
30 invite = %UserInviteToken{
31 invite_type: "reusable",
38 test "with less uses then max use returns true", %{invite: invite} do
39 invite = %{invite | uses: 4}
40 assert UserInviteToken.valid_invite?(invite)
43 test "with equal or more uses then max use returns false", %{invite: invite} do
44 invite = %{invite | uses: 5}
46 refute UserInviteToken.valid_invite?(invite)
48 invite = %{invite | uses: 6}
50 refute UserInviteToken.valid_invite?(invite)
54 describe "valid_token?/1 date limited invites" do
56 invite = %UserInviteToken{invite_type: "date_limited"}
60 test "expires today returns true", %{invite: invite} do
61 invite = %{invite | expires_at: Date.utc_today()}
62 assert UserInviteToken.valid_invite?(invite)
65 test "expires yesterday returns false", %{invite: invite} do
66 invite = %{invite | expires_at: Date.add(Date.utc_today(), -1)}
67 invite = Repo.insert!(invite)
68 refute UserInviteToken.valid_invite?(invite)
72 describe "valid_token?/1 reusable date limited invites" do
74 invite = %UserInviteToken{invite_type: "reusable_date_limited", max_use: 5}
78 test "not overdue date and less uses returns true", %{invite: invite} do
79 invite = %{invite | expires_at: Date.utc_today(), uses: 4}
80 assert UserInviteToken.valid_invite?(invite)
83 test "overdue date and less uses returns false", %{invite: invite} do
84 invite = %{invite | expires_at: Date.add(Date.utc_today(), -1)}
85 invite = Repo.insert!(invite)
86 refute UserInviteToken.valid_invite?(invite)
89 test "not overdue date with more uses returns false", %{invite: invite} do
90 invite = %{invite | expires_at: Date.utc_today(), uses: 5}
91 refute UserInviteToken.valid_invite?(invite)
94 test "overdue date with more uses returns false", %{invite: invite} do
95 invite = %{invite | expires_at: Date.add(Date.utc_today(), -1), uses: 5}
96 invite = Repo.insert!(invite)
97 refute UserInviteToken.valid_invite?(invite)