test for valid_invite?

This commit is contained in:
Alex S 2019-04-08 17:06:04 +07:00
parent 012bb5dcc9
commit f0ca0696c8

View File

@ -0,0 +1,96 @@
defmodule Pleroma.UserInviteTokenTest do
use ExUnit.Case, async: true
use Pleroma.DataCase
alias Pleroma.UserInviteToken
describe "valid_invite?/1 one time invites" do
setup do
invite = %UserInviteToken{invite_type: "one_time"}
{:ok, invite: invite}
end
test "not used returns true", %{invite: invite} do
invite = %{invite | used: false}
assert UserInviteToken.valid_invite?(invite)
end
test "used returns false", %{invite: invite} do
invite = %{invite | used: true}
refute UserInviteToken.valid_invite?(invite)
end
end
describe "valid_invite?/1 reusable invites" do
setup do
invite = %UserInviteToken{
invite_type: "reusable",
max_use: 5
}
{:ok, invite: invite}
end
test "with less uses then max use returns true", %{invite: invite} do
invite = %{invite | uses: 4}
assert UserInviteToken.valid_invite?(invite)
end
test "with equal or more uses then max use returns false", %{invite: invite} do
invite = %{invite | uses: 5}
refute UserInviteToken.valid_invite?(invite)
invite = %{invite | uses: 6}
refute UserInviteToken.valid_invite?(invite)
end
end
describe "valid_token?/1 date limited invites" do
setup do
invite = %UserInviteToken{invite_type: "date_limited"}
{:ok, invite: invite}
end
test "expires today returns true", %{invite: invite} do
invite = %{invite | expires_at: Date.utc_today()}
assert UserInviteToken.valid_invite?(invite)
end
test "expires yesterday returns false", %{invite: invite} do
invite = %{invite | expires_at: Date.add(Date.utc_today(), -1)}
invite = Repo.insert!(invite)
refute UserInviteToken.valid_invite?(invite)
end
end
describe "valid_token?/1 reusable date limited invites" do
setup do
invite = %UserInviteToken{invite_type: "reusable_date_limited", max_use: 5}
{:ok, invite: invite}
end
test "not overdue date and less uses returns true", %{invite: invite} do
invite = %{invite | expires_at: Date.utc_today(), uses: 4}
assert UserInviteToken.valid_invite?(invite)
end
test "overdue date and less uses returns false", %{invite: invite} do
invite = %{invite | expires_at: Date.add(Date.utc_today(), -1)}
invite = Repo.insert!(invite)
refute UserInviteToken.valid_invite?(invite)
end
test "not overdue date with more uses returns false", %{invite: invite} do
invite = %{invite | expires_at: Date.utc_today(), uses: 5}
refute UserInviteToken.valid_invite?(invite)
end
test "overdue date with more uses returns false", %{invite: invite} do
invite = %{invite | expires_at: Date.add(Date.utc_today(), -1), uses: 5}
invite = Repo.insert!(invite)
refute UserInviteToken.valid_invite?(invite)
end
end
end