-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Plausible.Workers.CleanEmailVerificationCodes do | ||
use Plausible.Repo | ||
use Oban.Worker, queue: :clean_email_verification_codes | ||
|
||
@impl Oban.Worker | ||
def perform(_args, _job) do | ||
Repo.update_all( | ||
from(c in "email_verification_codes", | ||
where: not is_nil(c.user_id), | ||
where: c.issued_at < fragment("now() - INTERVAL '4 hours'") | ||
), | ||
[set: [user_id: nil]] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Plausible.Workers.CleanEmailVerificationCodesTest do | ||
use Plausible.DataCase | ||
alias Plausible.Workers.CleanEmailVerificationCodes | ||
|
||
defp issue_code(user, issued_at) do | ||
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]]) | ||
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)) | ||
|
||
CleanEmailVerificationCodes.perform(nil, nil) | ||
|
||
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)) | ||
|
||
CleanEmailVerificationCodes.perform(nil, nil) | ||
|
||
assert Repo.exists?(from c in "email_verification_codes", where: c.user_id == ^user.id) | ||
end | ||
end |