recurring_test.gno
4.56 Kb ยท 137 lines
1package recurring
2
3import (
4 "std"
5 "testing"
6 "time"
7
8 "gno.land/p/demo/testutils"
9 "gno.land/p/demo/uassert"
10)
11
12var (
13 alice = testutils.TestAddress("alice")
14 bob = testutils.TestAddress("bob")
15 charlie = testutils.TestAddress("charlie")
16)
17
18func TestRecurringSubscription(t *testing.T) {
19 testing.SetRealm(std.NewUserRealm(alice))
20 rs := NewRecurringSubscription(time.Hour*24, 1000)
21
22 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
23 err := rs.Subscribe()
24 uassert.NoError(t, err, "Expected ProcessPayment to succeed for Alice")
25
26 err = rs.HasValidSubscription(std.CurrentRealm().Address())
27 uassert.NoError(t, err, "Expected Alice to have access")
28
29 _, err = rs.GetExpiration(std.CurrentRealm().Address())
30 uassert.NoError(t, err, "Expected to get expiration for Alice")
31}
32
33func TestRecurringSubscriptionGift(t *testing.T) {
34 testing.SetRealm(std.NewUserRealm(alice))
35 rs := NewRecurringSubscription(time.Hour*24, 1000)
36
37 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
38 err := rs.GiftSubscription(bob)
39 uassert.NoError(t, err, "Expected ProcessPaymentGift to succeed for Bob")
40
41 err = rs.HasValidSubscription(bob)
42 uassert.NoError(t, err, "Expected Bob to have access")
43
44 err = rs.HasValidSubscription(charlie)
45 uassert.Error(t, err, "Expected Charlie to fail access check")
46}
47
48func TestRecurringSubscriptionExpiration(t *testing.T) {
49 testing.SetRealm(std.NewUserRealm(alice))
50 rs := NewRecurringSubscription(time.Hour, 1000)
51
52 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
53 err := rs.Subscribe()
54 uassert.NoError(t, err, "Expected ProcessPayment to succeed for Alice")
55
56 err = rs.HasValidSubscription(std.CurrentRealm().Address())
57 uassert.NoError(t, err, "Expected Alice to have access")
58
59 expiration := time.Now().Add(-time.Hour * 2)
60 rs.subs.Set(std.CurrentRealm().Address().String(), expiration)
61
62 err = rs.HasValidSubscription(std.CurrentRealm().Address())
63 uassert.Error(t, err, "Expected Alice's subscription to be expired")
64}
65
66func TestUpdateAmountAuthorization(t *testing.T) {
67 testing.SetRealm(std.NewUserRealm(alice))
68 rs := NewRecurringSubscription(time.Hour*24, 1000)
69
70 err := rs.UpdateAmount(2000)
71 uassert.NoError(t, err, "Expected Alice to succeed in updating amount")
72
73 testing.SetRealm(std.NewUserRealm(bob))
74 err = rs.UpdateAmount(3000)
75 uassert.Error(t, err, "Expected Bob to fail when updating amount")
76}
77
78func TestGetAmount(t *testing.T) {
79 testing.SetRealm(std.NewUserRealm(alice))
80 rs := NewRecurringSubscription(time.Hour*24, 1000)
81
82 amount := rs.GetAmount()
83 uassert.Equal(t, amount, int64(1000), "Expected the initial amount to be 1000 ugnot")
84
85 err := rs.UpdateAmount(2000)
86 uassert.NoError(t, err, "Expected Alice to succeed in updating amount")
87
88 amount = rs.GetAmount()
89 uassert.Equal(t, amount, int64(2000), "Expected the updated amount to be 2000 ugnot")
90}
91
92func TestIncorrectPaymentAmount(t *testing.T) {
93 testing.SetOriginCaller(alice)
94 testing.SetRealm(std.NewUserRealm(alice))
95 rs := NewRecurringSubscription(time.Hour*24, 1000)
96
97 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 500}})
98 err := rs.Subscribe()
99 uassert.Error(t, err, "Expected payment with incorrect amount to fail")
100}
101
102func TestMultiplePaymentsForSameUser(t *testing.T) {
103 testing.SetOriginCaller(alice)
104 testing.SetRealm(std.NewUserRealm(alice))
105 rs := NewRecurringSubscription(time.Hour*24, 1000)
106
107 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
108 err := rs.Subscribe()
109 uassert.NoError(t, err, "Expected first ProcessPayment to succeed for Alice")
110
111 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
112 err = rs.Subscribe()
113 uassert.Error(t, err, "Expected second ProcessPayment to fail for Alice due to existing subscription")
114}
115
116func TestRecurringSubscriptionWithMultiplePayments(t *testing.T) {
117 testing.SetOriginCaller(alice)
118 testing.SetRealm(std.NewUserRealm(alice))
119 rs := NewRecurringSubscription(time.Hour, 1000)
120
121 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
122 err := rs.Subscribe()
123 uassert.NoError(t, err, "Expected first ProcessPayment to succeed for Alice")
124
125 err = rs.HasValidSubscription(std.CurrentRealm().Address())
126 uassert.NoError(t, err, "Expected Alice to have access after first payment")
127
128 expiration := time.Now().Add(-time.Hour * 2)
129 rs.subs.Set(std.CurrentRealm().Address().String(), expiration)
130
131 testing.SetOriginSend([]std.Coin{{Denom: "ugnot", Amount: 1000}})
132 err = rs.Subscribe()
133 uassert.NoError(t, err, "Expected second ProcessPayment to succeed for Alice")
134
135 err = rs.HasValidSubscription(std.CurrentRealm().Address())
136 uassert.NoError(t, err, "Expected Alice to have access after second payment")
137}