Skip to content

Commit

Permalink
Merge pull request #196 from SkynetLabs/ivo/fix_user_creation_time
Browse files Browse the repository at this point in the history
Fix user creation time being zero.
  • Loading branch information
ro-tex authored Apr 20, 2022
2 parents df4e747 + b9b50e1 commit 3b6e97a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions api/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestUserTierCache(t *testing.T) {
u := &database.User{
Sub: t.Name(),
Tier: database.TierPremium5,
CreatedAt: time.Now().UTC(),
SubscribedUntil: time.Now().UTC().Add(100 * time.Hour),
QuotaExceeded: false,
}
Expand Down
2 changes: 1 addition & 1 deletion database/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (db *DB) APIKeyPatch(ctx context.Context, user User, akID primitive.ObjectI
}
filter := bson.M{
"_id": akID,
"public": &True, // you can only update public API keys
"public": true,
}
var update bson.M
// First, all new skylinks to the record.
Expand Down
29 changes: 22 additions & 7 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ var (
// AnonUser is a helper struct that we can use when we don't have a relevant
// user, e.g. when an upload is made by an anonymous user.
AnonUser = User{}
// True is a helper for when we need to pass a *bool to MongoDB.
True = true
// False is a helper for when we need to pass a *bool to MongoDB.
False = false
// UserLimits defines the speed limits for each tier.
// RegistryDelay delay is in ms.
UserLimits = map[int]TierLimits{
TierAnonymous: {
TierName: "anonymous",
UploadBandwidth: 5 * mbpsToBytesPerSecond,
// TODO: temporarily lowered the download bandwidth on the anon tier
// from 20mbps to 5mpbs
// from 20mbps to 5mbps
DownloadBandwidth: 5 * mbpsToBytesPerSecond,
MaxUploadSize: 1 * skynet.GiB,
MaxNumberUploads: 0,
Expand Down Expand Up @@ -334,8 +330,18 @@ func (db *DB) UserCreate(ctx context.Context, emailAddr, pass, sub string, tier
EmailConfirmationToken: emailConfToken,
EmailConfirmationTokenExpiration: time.Now().UTC().Add(EmailConfirmationTokenTTL).Truncate(time.Millisecond),
PasswordHash: string(passHash),
RecoveryToken: "",
Sub: sub,
Tier: tier,
CreatedAt: time.Now().UTC(),
MigratedAt: time.Time{},
SubscribedUntil: time.Time{},
SubscriptionStatus: "",
SubscriptionCancelAt: time.Time{},
SubscriptionCancelAtPeriodEnd: false,
StripeID: "",
QuotaExceeded: false,
PubKeys: make([]PubKey, 0),
}
// TODO This part can race and create multiple accounts with the same email, unless we add DB-level uniqueness restriction.
// Insert the user.
Expand Down Expand Up @@ -427,8 +433,17 @@ func (db *DB) UserCreatePK(ctx context.Context, emailAddr, pass, sub string, pk
EmailConfirmationToken: emailConfToken,
EmailConfirmationTokenExpiration: time.Now().UTC().Add(EmailConfirmationTokenTTL).Truncate(time.Millisecond),
PasswordHash: string(passHash),
RecoveryToken: "",
Sub: sub,
Tier: tier,
CreatedAt: time.Now().UTC(),
MigratedAt: time.Time{},
SubscribedUntil: time.Time{},
SubscriptionStatus: "",
SubscriptionCancelAt: time.Time{},
SubscriptionCancelAtPeriodEnd: false,
StripeID: "",
QuotaExceeded: false,
PubKeys: []PubKey{pk},
}
// Insert the user.
Expand Down Expand Up @@ -805,7 +820,7 @@ func (db *DB) userDownloadStats(ctx context.Context, id primitive.ObjectID, mont
}},
}
// This stage checks if the download has a non-zero `bytes` field and if so,
// it takes it as the download's size. Otherwise it reports the full
// it takes it as the download's size. Otherwise, it reports the full
// skylink's size as download's size.
projectStage := bson.D{{"$project", bson.D{
{"size", bson.D{
Expand Down Expand Up @@ -888,7 +903,7 @@ func (u User) HasKey(pk PubKey) bool {
// Users get their bandwidth quota reset at the start of the month.
//
// This function follows the behaviour of Stripe:
// If a month doesnt have the anchor day, the subscription will be billed on
// If a month doesn't have the anchor day, the subscription will be billed on
// the last day of the month. For example, a subscription starting on 31 January
// bills on 28 February (or 29 February in a leap year), then 31 March, 30
// April, and so on.
Expand Down
5 changes: 5 additions & 0 deletions test/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func testHandlerUserPOST(t *testing.T, at *test.AccountsTester) {
if err != nil {
t.Fatal("Error while fetching the user from the DB. Error ", err.Error())
}
// Make sure the creation timestamp is correct.
now := time.Now().UTC()
if u.CreatedAt.Before(now.Add(-1*time.Minute)) || u.CreatedAt.After(now.Add(time.Minute)) {
t.Fatal("Unexpected user creation time:", u.CreatedAt, "Current time:", now)
}
// Clean up the user after the test.
defer func(user *database.User) {
err = at.DB.UserDelete(at.Ctx, user)
Expand Down

0 comments on commit 3b6e97a

Please sign in to comment.