3
0

Tidy up id.Next() and time.Now() use

This commit is contained in:
Denis Arh
2020-09-15 20:32:28 +02:00
parent 3a7cc105d0
commit 4cef8eecd4
23 changed files with 107 additions and 118 deletions

View File

@@ -106,7 +106,7 @@ func (svc *application) Create(ctx context.Context, new *types.Application) (app
// Set new values after beforeCreate events are emitted
new.ID = nextID()
new.CreatedAt = now()
new.CreatedAt = *now()
if new.Unify == nil {
new.Unify = &types.ApplicationUnify{}
@@ -152,7 +152,7 @@ func (svc *application) Update(ctx context.Context, upd *types.Application) (app
// Assign changed values after afterUpdate events are emitted
app.Name = upd.Name
app.Enabled = upd.Enabled
app.UpdatedAt = nowPtr()
app.UpdatedAt = now()
if upd.Unify != nil {
app.Unify = upd.Unify
@@ -194,7 +194,7 @@ func (svc *application) Delete(ctx context.Context, ID uint64) (err error) {
return
}
app.DeletedAt = nowPtr()
app.DeletedAt = now()
if err = store.UpdateApplication(ctx, svc.store, app); err != nil {
return
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
intAuth "github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/id"
files "github.com/cortezaproject/corteza-server/pkg/objstore"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
@@ -108,7 +107,7 @@ func (svc attachment) DeleteByID(ID uint64) (err error) {
return err
}
att.DeletedAt = nowPtr()
att.DeletedAt = now()
aaProps.setAttachment(att)
return store.UpdateAttachment(svc.ctx, svc.store, att)
@@ -158,7 +157,7 @@ func (svc attachment) CreateSettingsAttachment(name string, size int64, fh io.Re
}
att = &types.Attachment{
ID: id.Next(),
ID: nextID(),
OwnerID: currentUserID,
Name: strings.TrimSpace(name),
Kind: types.AttachmentKindSettings,

View File

@@ -8,7 +8,6 @@ import (
internalAuth "github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/pkg/handle"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/pkg/permissions"
"github.com/cortezaproject/corteza-server/pkg/rand"
"github.com/cortezaproject/corteza-server/store"
@@ -154,7 +153,7 @@ func (svc auth) External(ctx context.Context, profile goth.User) (u *types.User,
if u.Valid() {
// Valid user, Bingo!
c.LastUsedAt = nowPtr()
c.LastUsedAt = now()
if err = store.UpdateCredentials(ctx, svc.store, c); err != nil {
return err
}
@@ -214,8 +213,8 @@ func (svc auth) External(ctx context.Context, profile goth.User) (u *types.User,
createHandle(ctx, svc.store, u)
}
u.ID = id.Next()
u.CreatedAt = now()
u.ID = nextID()
u.CreatedAt = *now()
if err = store.CreateUser(ctx, svc.store, u); err != nil {
return err
}
@@ -255,12 +254,12 @@ func (svc auth) External(ctx context.Context, profile goth.User) (u *types.User,
// If we got to this point, assume that user is authenticated
// but credentials need to be stored
c := &types.Credentials{
ID: id.Next(),
CreatedAt: now(),
ID: nextID(),
CreatedAt: *now(),
Kind: profile.Provider,
OwnerID: u.ID,
Credentials: profile.UserID,
LastUsedAt: nowPtr(),
LastUsedAt: now(),
}
if err = store.CreateCredentials(ctx, svc.store, c); err != nil {
@@ -331,8 +330,8 @@ func (svc auth) InternalSignUp(ctx context.Context, input *types.User, password
return AuthErrInvalidCredentials(aam)
} else {
// Update last-used-by timestamp on matching credentials
c.LastUsedAt = nowPtr()
c.UpdatedAt = nowPtr()
c.LastUsedAt = now()
c.UpdatedAt = now()
aam.setCredentials(c)
if err = store.UpdateCredentials(ctx, svc.store, c); err != nil {
@@ -377,7 +376,7 @@ func (svc auth) InternalSignUp(ctx context.Context, input *types.User, password
}
var nUser = &types.User{
ID: id.Next(),
ID: nextID(),
Email: input.Email,
Name: input.Name,
Username: input.Username,
@@ -483,8 +482,8 @@ func (svc auth) InternalLogin(ctx context.Context, email string, password string
return AuthErrInvalidCredentials(aam)
} else {
// Update last-used-by timestamp on matching credentials
c.UpdatedAt = nowPtr()
c.LastUsedAt = nowPtr()
c.UpdatedAt = now()
c.LastUsedAt = now()
aam.setCredentials(c)
if err = store.UpdateCredentials(ctx, svc.store, c); err != nil {
@@ -664,7 +663,7 @@ func (svc auth) SetPasswordCredentials(ctx context.Context, userID uint64, passw
// Mark all credentials as deleted
_ = cc.Walk(func(c *types.Credentials) error {
c.DeletedAt = nowPtr()
c.DeletedAt = now()
return nil
})
@@ -675,8 +674,8 @@ func (svc auth) SetPasswordCredentials(ctx context.Context, userID uint64, passw
// Add new credentials with new password
c := &types.Credentials{
ID: id.Next(),
CreatedAt: now(),
ID: nextID(),
CreatedAt: *now(),
OwnerID: userID,
Kind: credentialsTypePassword,
Credentials: string(hash),
@@ -747,7 +746,7 @@ func (svc auth) loadFromTokenAndConfirmEmail(ctx context.Context, token, tokenTy
}
u.EmailConfirmed = true
u.UpdatedAt = nowPtr()
u.UpdatedAt = now()
if err = store.UpdateUser(ctx, svc.store, u); err != nil {
return err
}
@@ -979,15 +978,15 @@ func (svc auth) createUserToken(ctx context.Context, u *types.User, kind string)
switch kind {
case credentialsTypeAuthToken:
// 15 sec expiration for all tokens that are part of redirection
expiresAt = nowPtr().Add(time.Second * 15)
expiresAt = now().Add(time.Second * 15)
default:
// 1h expiration for all tokens send via email
expiresAt = nowPtr().Add(time.Minute * 60)
expiresAt = now().Add(time.Minute * 60)
}
c := &types.Credentials{
ID: id.Next(),
CreatedAt: now(),
ID: nextID(),
CreatedAt: *now(),
OwnerID: u.ID,
Kind: kind,
Credentials: string(rand.Bytes(credentialsTokenLength)),

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/store/sqlite3"
"github.com/cortezaproject/corteza-server/system/types"
@@ -55,15 +54,15 @@ func TestAuth_External(t *testing.T) {
ctx = context.Background()
// Create some virtual user and credentials
validUser = &types.User{Email: "valid@test.cortezaproject.org", ID: id.Next(), CreatedAt: now()}
suspendedUser = &types.User{Email: "suspended@test.cortezaproject.org", ID: id.Next(), CreatedAt: now(), SuspendedAt: nowPtr()}
validUser = &types.User{Email: "valid@test.cortezaproject.org", ID: nextID(), CreatedAt: *now()}
suspendedUser = &types.User{Email: "suspended@test.cortezaproject.org", ID: nextID(), CreatedAt: *now(), SuspendedAt: now()}
freshProfileID = func() string {
return fmt.Sprintf("fresh-profile-id-%d", id.Next())
return fmt.Sprintf("fresh-profile-id-%d", nextID())
}
fooCredentials = &types.Credentials{
ID: id.Next(),
ID: nextID(),
OwnerID: validUser.ID,
Label: "credentials for foo provider",
Kind: "foo",
@@ -72,7 +71,7 @@ func TestAuth_External(t *testing.T) {
}
barCredentials = &types.Credentials{
ID: id.Next(),
ID: nextID(),
OwnerID: validUser.ID,
Label: "credentials for bar provider",
Kind: "bar",
@@ -146,8 +145,8 @@ func TestAuth_InternalLogin(t *testing.T) {
ctx = context.Background()
validPass = "this is a valid password !! 42"
validUser = &types.User{Email: "valid@test.cortezaproject.org", ID: id.Next(), CreatedAt: now(), EmailConfirmed: true}
suspendedUser = &types.User{Email: "suspended@test.cortezaproject.org", ID: id.Next(), CreatedAt: now(), SuspendedAt: nowPtr()}
validUser = &types.User{Email: "valid@test.cortezaproject.org", ID: nextID(), CreatedAt: *now(), EmailConfirmed: true}
suspendedUser = &types.User{Email: "suspended@test.cortezaproject.org", ID: nextID(), CreatedAt: *now(), SuspendedAt: now()}
tests = []struct {
name string

View File

@@ -4,7 +4,6 @@ import (
"context"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
intAuth "github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
"time"
@@ -119,8 +118,8 @@ func (svc reminder) Create(ctx context.Context, new *types.Reminder) (r *types.R
return err
}
r.ID = id.Next()
r.CreatedAt = now()
r.ID = nextID()
r.CreatedAt = *now()
if err = store.CreateReminder(ctx, svc.store, new); err != nil {
return err
@@ -161,7 +160,7 @@ func (svc reminder) Update(ctx context.Context, upd *types.Reminder) (r *types.R
r.Payload = upd.Payload
r.RemindAt = upd.RemindAt
r.Resource = upd.Resource
r.UpdatedAt = nowPtr()
r.UpdatedAt = now()
if err = store.UpdateReminder(ctx, svc.store, r); err != nil {
return err
@@ -254,7 +253,7 @@ func (svc reminder) Delete(ctx context.Context, ID uint64) (err error) {
return ReminderErrNotFound()
}
r.DeletedAt = nowPtr()
r.DeletedAt = now()
raProps.setReminder(r)

View File

@@ -5,7 +5,6 @@ import (
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/pkg/handle"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/pkg/permissions"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/service/event"
@@ -212,8 +211,8 @@ func (svc role) Create(new *types.Role) (r *types.Role, err error) {
return
}
new.ID = id.Next()
new.CreatedAt = now()
new.ID = nextID()
new.CreatedAt = *now()
if err = store.CreateRole(svc.ctx, svc.store, new); err != nil {
return
@@ -263,7 +262,7 @@ func (svc role) Update(upd *types.Role) (r *types.Role, err error) {
r.Handle = upd.Handle
r.Name = upd.Name
r.UpdatedAt = nowPtr()
r.UpdatedAt = now()
// Assign changed values
if err = store.UpdateRole(svc.ctx, svc.store, r); err != nil {
@@ -321,7 +320,7 @@ func (svc role) Delete(roleID uint64) (err error) {
return
}
r.DeletedAt = nowPtr()
r.DeletedAt = now()
if err = store.UpdateRole(svc.ctx, svc.store, r); err != nil {
return
@@ -381,7 +380,7 @@ func (svc role) Archive(roleID uint64) (err error) {
return RoleErrNotAllowedToArchive()
}
r.ArchivedAt = nowPtr()
r.ArchivedAt = now()
if err = store.UpdateRole(svc.ctx, svc.store, r); err != nil {
return
}

View File

@@ -91,17 +91,12 @@ var (
DefaultStatistics *statistics
// wrapper around time.Now() that will aid service testing
now = func() time.Time {
return time.Now()
now = func() *time.Time {
c := time.Now()
return &c
}
// returns pointer to time.Time struct that is set to current time
nowPtr = func() *time.Time {
n := now()
return &n
}
// wrapper around id.Next() that will aid service testing
// wrapper around nextID that will aid service testing
nextID = func() uint64 {
return id.Next()
}

View File

@@ -8,7 +8,6 @@ import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/pkg/filter"
"github.com/cortezaproject/corteza-server/pkg/handle"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/service/event"
"github.com/cortezaproject/corteza-server/system/types"
@@ -333,8 +332,8 @@ func (svc user) Create(new *types.User) (u *types.User, err error) {
return
}
new.ID = id.Next()
new.CreatedAt = now()
new.ID = nextID()
new.CreatedAt = *now()
// consider email confirmed
// when creating user like this
@@ -392,7 +391,7 @@ func (svc user) Update(upd *types.User) (u *types.User, err error) {
u.Name = upd.Name
u.Handle = upd.Handle
u.Kind = upd.Kind
u.UpdatedAt = nowPtr()
u.UpdatedAt = now()
if err = svc.eventbus.WaitFor(svc.ctx, event.UserBeforeUpdate(upd, u)); err != nil {
return
@@ -498,7 +497,7 @@ func (svc user) Delete(userID uint64) (err error) {
return
}
u.DeletedAt = nowPtr()
u.DeletedAt = now()
if err = store.UpdateUser(svc.ctx, svc.store, u); err != nil {
return
}
@@ -568,7 +567,7 @@ func (svc user) Suspend(userID uint64) (err error) {
return UserErrNotAllowedToSuspend()
}
u.SuspendedAt = nowPtr()
u.SuspendedAt = now()
if err = store.UpdateUser(svc.ctx, svc.store, u); err != nil {
return
}