4
4
"bytes"
5
5
"context"
6
6
"encoding/hex"
7
- "encoding/json "
7
+ "fmt "
8
8
"net/http"
9
9
"net/url"
10
10
"testing"
@@ -14,6 +14,7 @@ import (
14
14
"github.com/SkynetLabs/skynet-accounts/database"
15
15
"github.com/SkynetLabs/skynet-accounts/test"
16
16
"gitlab.com/NebulousLabs/fastrand"
17
+ "go.sia.tech/siad/build"
17
18
18
19
"github.com/julienschmidt/httprouter"
19
20
"github.com/sirupsen/logrus"
@@ -198,15 +199,9 @@ func TestUserTierCache(t *testing.T) {
198
199
if err != nil {
199
200
t .Fatal (err )
200
201
}
201
- at .Cookie = test .ExtractCookie (r )
202
- // Get the user's limit. Since they are on a Pro account but their
203
- // SubscribedUntil is set in the past, we expect to get TierFree.
204
- _ , b , err := at .Get ("/user/limits" , nil )
205
- if err != nil {
206
- t .Fatal (err )
207
- }
208
- var ul api.UserLimitsGET
209
- err = json .Unmarshal (b , & ul )
202
+ at .SetCookie (test .ExtractCookie (r ))
203
+ // Get the user's limit.
204
+ ul , _ , err := at .UserLimits ()
210
205
if err != nil {
211
206
t .Fatal (err )
212
207
}
@@ -216,12 +211,11 @@ func TestUserTierCache(t *testing.T) {
216
211
if ul .TierID != database .TierPremium20 {
217
212
t .Fatalf ("Expected tier id '%d', got '%d'" , database .TierPremium20 , ul .TierID )
218
213
}
219
- // Now set their SubscribedUntil in the future, so their subscription tier
220
- // is active.
221
- u .SubscribedUntil = time .Now ().UTC ().Add (365 * 24 * time .Hour )
222
- err = at .DB .UserSave (at .Ctx , u .User )
223
- if err != nil {
224
- t .Fatal (err )
214
+ if ul .TierName != database .UserLimits [database .TierPremium20 ].TierName {
215
+ t .Fatalf ("Expected tier name '%s', got '%s'" , database .UserLimits [database .TierPremium20 ].TierName , ul .TierName )
216
+ }
217
+ if ul .UploadBandwidth != database .UserLimits [database .TierPremium20 ].UploadBandwidth {
218
+ t .Fatalf ("Expected upload bandwidth '%d', got '%d'" , database .UserLimits [database .TierPremium20 ].UploadBandwidth , ul .UploadBandwidth )
225
219
}
226
220
// Register a test upload that exceeds the user's allowed storage, so their
227
221
// QuotaExceeded flag will get raised.
@@ -232,45 +226,55 @@ func TestUserTierCache(t *testing.T) {
232
226
// Make a specific call to trackUploadPOST in order to trigger the
233
227
// checkUserQuotas method. This wil register the upload a second time but
234
228
// that doesn't affect the test.
235
- _ , _ , err = at .Post ("/track/upload/" + sl .Skylink , nil , nil )
236
- if err != nil {
237
- t .Fatal (err )
238
- }
239
- // Sleep for a short time in order to make sure that the background
240
- // goroutine that updates user's quotas has had time to run.
241
- time .Sleep (2 * time .Second )
242
- // We expect to get TierAnonymous.
243
- _ , b , err = at .Get ("/user/limits" , nil )
229
+ _ , err = at .TrackUpload (sl .Skylink )
244
230
if err != nil {
245
231
t .Fatal (err )
246
232
}
247
- err = json .Unmarshal (b , & ul )
233
+ // We need to try this several times because we'll only get the right result
234
+ // after the background goroutine that updates user's quotas has had time to
235
+ // run.
236
+ err = build .Retry (10 , 200 * time .Millisecond , func () error {
237
+ // We expect to get tier with name and id matching TierPremium20 but with
238
+ // speeds matching TierAnonymous.
239
+ ul , _ , err = at .UserLimits ()
240
+ if err != nil {
241
+ t .Fatal (err )
242
+ }
243
+ if ul .TierID != database .TierPremium20 {
244
+ return fmt .Errorf ("Expected tier id '%d', got '%d'" , database .TierPremium20 , ul .TierID )
245
+ }
246
+ if ul .TierName != database .UserLimits [database .TierPremium20 ].TierName {
247
+ return fmt .Errorf ("Expected tier name '%s', got '%s'" , database .UserLimits [database .TierPremium20 ].TierName , ul .TierName )
248
+ }
249
+ if ul .UploadBandwidth != database .UserLimits [database .TierAnonymous ].UploadBandwidth {
250
+ return fmt .Errorf ("Expected upload bandwidth '%d', got '%d'" , database .UserLimits [database .TierAnonymous ].UploadBandwidth , ul .UploadBandwidth )
251
+ }
252
+ return nil
253
+ })
248
254
if err != nil {
249
255
t .Fatal (err )
250
256
}
251
- if ul .TierID != database .TierAnonymous {
252
- t .Fatalf ("Expected tier id '%d', got '%d'" , database .TierAnonymous , ul .TierID )
253
- }
254
- if ul .TierName != database .UserLimits [database .TierAnonymous ].TierName {
255
- t .Fatalf ("Expected tier name '%s', got '%s'" , database .UserLimits [database .TierAnonymous ].TierName , ul .TierName )
256
- }
257
257
// Delete the uploaded file, so the user's quota recovers.
258
258
// This call should invalidate the tier cache.
259
259
_ , _ , err = at .Delete ("/user/uploads/" + sl .Skylink , nil )
260
- time .Sleep (2 * time .Second )
261
- // We expect to get TierPremium20.
262
- _ , b , err = at .Get ("/user/limits" , nil )
263
260
if err != nil {
264
261
t .Fatal (err )
265
262
}
266
- err = json .Unmarshal (b , & ul )
263
+ err = build .Retry (10 , 200 * time .Millisecond , func () error {
264
+ // We expect to get TierPremium20.
265
+ ul , _ , err = at .UserLimits ()
266
+ if err != nil {
267
+ return errors .AddContext (err , "failed to call /user/limits" )
268
+ }
269
+ if ul .TierID != database .TierPremium20 {
270
+ return fmt .Errorf ("Expected tier id '%d', got '%d'" , database .TierPremium20 , ul .TierID )
271
+ }
272
+ if ul .TierName != database .UserLimits [database .TierPremium20 ].TierName {
273
+ return fmt .Errorf ("Expected tier name '%s', got '%s'" , database .UserLimits [database .TierPremium20 ].TierName , ul .TierName )
274
+ }
275
+ return nil
276
+ })
267
277
if err != nil {
268
278
t .Fatal (err )
269
279
}
270
- if ul .TierID != database .TierPremium20 {
271
- t .Fatalf ("Expected tier id '%d', got '%d'" , database .TierPremium20 , ul .TierID )
272
- }
273
- if ul .TierName != database .UserLimits [database .TierPremium20 ].TierName {
274
- t .Fatalf ("Expected tier name '%s', got '%s'" , database .UserLimits [database .TierPremium20 ].TierName , ul .TierName )
275
- }
276
280
}
0 commit comments