2020-12-15 12:30:45 +03:00
|
|
|
defmodule Plausible.Workers.CleanEmailVerificationCodesTest do
|
|
|
|
use Plausible.DataCase
|
|
|
|
alias Plausible.Workers.CleanEmailVerificationCodes
|
|
|
|
|
|
|
|
defp issue_code(user, issued_at) do
|
2020-12-29 16:17:27 +03:00
|
|
|
code =
|
|
|
|
Repo.one(
|
|
|
|
from(c in "email_verification_codes", where: is_nil(c.user_id), select: c.code, limit: 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
Repo.update_all(from(c in "email_verification_codes", where: c.code == ^code),
|
|
|
|
set: [user_id: user.id, issued_at: issued_at]
|
|
|
|
)
|
2020-12-15 12:30:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "cleans codes that are more than 4 hours old" do
|
|
|
|
user = insert(:user)
|
|
|
|
issue_code(user, Timex.now() |> Timex.shift(hours: -5))
|
|
|
|
issue_code(user, Timex.now() |> Timex.shift(days: -5))
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CleanEmailVerificationCodes.perform(nil)
|
2020-12-15 12:30:45 +03:00
|
|
|
|
|
|
|
refute Repo.exists?(from c in "email_verification_codes", where: c.user_id == ^user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not clean code from 2 hours ago" do
|
|
|
|
user = insert(:user)
|
|
|
|
issue_code(user, Timex.now() |> Timex.shift(hours: -2))
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CleanEmailVerificationCodes.perform(nil)
|
2020-12-15 12:30:45 +03:00
|
|
|
|
|
|
|
assert Repo.exists?(from c in "email_verification_codes", where: c.user_id == ^user.id)
|
|
|
|
end
|
|
|
|
end
|