-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_keys_cancel.go
More file actions
61 lines (51 loc) · 1.59 KB
/
test_keys_cancel.go
File metadata and controls
61 lines (51 loc) · 1.59 KB
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
56
57
58
59
60
61
package main
import (
"testing"
"time"
)
func TestCancelKey_DeferredUntilNextBilling(t *testing.T) {
// Minimal in-memory setup (no gin/router needed)
keys = nil
now := time.Now()
cancelAtMs := now.Add(2 * time.Hour).UnixMilli()
k := Key{
Key: "key1",
Creator: "owner",
Users: map[UserId]KeyUserData{},
Name: "sub key",
Price: 10,
Type: "subscription",
Webhook: nil,
Data: nil,
TotalIncome: 0,
Subscription: &Subscription{Active: true, Frequency: 1, Period: "month", NextBilling: cancelAtMs},
}
k.Users["alice"] = KeyUserData{Time: now.Unix(), Price: 10, NextBilling: cancelAtMs}
keys = []Key{k}
// Simulate what /keys/cancel does: set CancelAt = nextBilling
ud := keys[0].Users["alice"]
ud.CancelAt = ud.NextBilling
keys[0].Users["alice"] = ud
if !doesUserOwnKey("alice", "key1") {
t.Fatalf("expected user to still own key until cancel_at")
}
// Simulate the cleanup portion of checkSubscriptions: remove if cancel_at has passed
pastCancelAt := now.Add(-1 * time.Hour).UnixMilli()
ud = keys[0].Users["alice"]
ud.CancelAt = pastCancelAt
keys[0].Users["alice"] = ud
// run the relevant logic block: remove if time.Now >= cancelAt
usersToRemove := []UserId{}
if v, ok := keys[0].Users["alice"].CancelAt.(int64); ok {
if time.Now().UnixMilli() >= v {
usersToRemove = append(usersToRemove, "alice")
}
}
for _, u := range usersToRemove {
delete(keys[0].Users, u)
}
if doesUserOwnKey("alice", "key1") {
t.Fatalf("expected user to be removed after cancel_at")
}
}
func ptr(s string) *string { return &s }