Add failing test, which exposes a bug
[akkoma] / test / user_invite_token_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.UserInviteTokenTest do
6 use ExUnit.Case, async: true
7 use Pleroma.DataCase
8 alias Pleroma.UserInviteToken
9
10 describe "valid_invite?/1 one time invites" do
11 setup do
12 invite = %UserInviteToken{invite_type: "one_time"}
13
14 {:ok, invite: invite}
15 end
16
17 test "not used returns true", %{invite: invite} do
18 invite = %{invite | used: false}
19 assert UserInviteToken.valid_invite?(invite)
20 end
21
22 test "used returns false", %{invite: invite} do
23 invite = %{invite | used: true}
24 refute UserInviteToken.valid_invite?(invite)
25 end
26 end
27
28 describe "valid_invite?/1 reusable invites" do
29 setup do
30 invite = %UserInviteToken{
31 invite_type: "reusable",
32 max_use: 5
33 }
34
35 {:ok, invite: invite}
36 end
37
38 test "with less uses then max use returns true", %{invite: invite} do
39 invite = %{invite | uses: 4}
40 assert UserInviteToken.valid_invite?(invite)
41 end
42
43 test "with equal or more uses then max use returns false", %{invite: invite} do
44 invite = %{invite | uses: 5}
45
46 refute UserInviteToken.valid_invite?(invite)
47
48 invite = %{invite | uses: 6}
49
50 refute UserInviteToken.valid_invite?(invite)
51 end
52 end
53
54 describe "valid_token?/1 date limited invites" do
55 setup do
56 invite = %UserInviteToken{invite_type: "date_limited"}
57 {:ok, invite: invite}
58 end
59
60 test "expires today returns true", %{invite: invite} do
61 invite = %{invite | expires_at: Date.utc_today()}
62 assert UserInviteToken.valid_invite?(invite)
63 end
64
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)
69 end
70 end
71
72 describe "valid_token?/1 reusable date limited invites" do
73 setup do
74 invite = %UserInviteToken{invite_type: "reusable_date_limited", max_use: 5}
75 {:ok, invite: invite}
76 end
77
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)
81 end
82
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)
87 end
88
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)
92 end
93
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)
98 end
99 end
100 end