Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user creation time being zero. #196

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{},
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
SubscribedUntil: time.Time{},
SubscriptionStatus: "",
SubscriptionCancelAt: time.Time{},
SubscriptionCancelAtPeriodEnd: false,
StripeID: "",
QuotaExceeded: false,
PubKeys: make([]PubKey, 0),
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
}
// 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)) {
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
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