File size: 1,560 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
defmodule Plausible.Workers.CleanInvitationsTest do
  use Plausible.DataCase
  alias Plausible.Workers.CleanInvitations

  test "cleans invitations and transfers that are more than 48h old" do
    now = NaiveDateTime.utc_now(:second)

    owner = new_user()
    site = new_site(owner: owner)

    invite_guest(site, new_user(),
      role: :viewer,
      inviter: owner,
      inserted_at: NaiveDateTime.shift(now, hour: -49),
      team_invitation: [inserted_at: NaiveDateTime.shift(now, hour: -49)]
    )

    invite_transfer(site, new_user(),
      inviter: owner,
      inserted_at: NaiveDateTime.shift(now, hour: -49)
    )

    CleanInvitations.perform(nil)

    refute Repo.exists?(Plausible.Teams.Invitation)
    refute Repo.exists?(Plausible.Teams.GuestInvitation)
    refute Repo.exists?(Plausible.Teams.SiteTransfer)
  end

  test "does not clean invitations and transfers that are less than 48h old" do
    now = NaiveDateTime.utc_now(:second)

    owner = new_user()
    site = new_site(owner: owner)

    invite_guest(site, new_user(),
      role: :viewer,
      inviter: owner,
      inserted_at: NaiveDateTime.shift(now, hour: -47),
      team_invitation: [inserted_at: NaiveDateTime.shift(now, hour: -47)]
    )

    invite_transfer(site, new_user(),
      inviter: owner,
      inserted_at: NaiveDateTime.shift(now, hour: -47)
    )

    CleanInvitations.perform(nil)

    assert Repo.exists?(Plausible.Teams.Invitation)
    assert Repo.exists?(Plausible.Teams.GuestInvitation)
    assert Repo.exists?(Plausible.Teams.SiteTransfer)
  end
end