storj/satellite/console/resetpasswordtoken_test.go
Egon Elbre 2268cc1df3 all: fix linter complaints
Change-Id: Ia01404dbb6bdd19a146fa10ff7302e08f87a8c95
2020-10-13 15:59:01 +03:00

87 lines
2.4 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package console_test
import (
"testing"
"github.com/stretchr/testify/assert"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func TestNewRegistrationSecret(t *testing.T) {
const (
shortName = "lastName"
email = "email@mail.test"
pass = "123456"
userFullName = "name"
)
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
users := db.Console().Users()
rptokens := db.Console().ResetPasswordTokens()
var owner *console.User
var rptoken *console.ResetPasswordToken
t.Run("Insert reset password token successfully", func(t *testing.T) {
var err error
owner, err = users.Insert(ctx, &console.User{
ID: testrand.UUID(),
FullName: userFullName,
ShortName: shortName,
Email: email,
PasswordHash: []byte(pass),
})
assert.NoError(t, err)
assert.NotNil(t, owner)
rptoken, err = rptokens.Create(ctx, owner.ID)
assert.NotNil(t, rptoken)
assert.NoError(t, err)
})
t.Run("Get reset password token successfully", func(t *testing.T) {
tokenBySecret, err := rptokens.GetBySecret(ctx, rptoken.Secret)
assert.NoError(t, err)
assert.Equal(t, rptoken.Secret, tokenBySecret.Secret)
assert.Equal(t, rptoken.CreatedAt, tokenBySecret.CreatedAt)
assert.Equal(t, rptoken.OwnerID, tokenBySecret.OwnerID)
})
t.Run("Get reset password token by UUID and Secret equal", func(t *testing.T) {
tokenBySecret, err := rptokens.GetBySecret(ctx, rptoken.Secret)
assert.NoError(t, err)
tokenByUUID, err := rptokens.GetByOwnerID(ctx, *rptoken.OwnerID)
assert.NoError(t, err)
assert.Equal(t, tokenByUUID.Secret, tokenBySecret.Secret)
assert.Equal(t, tokenByUUID.CreatedAt, tokenBySecret.CreatedAt)
assert.Equal(t, tokenByUUID.OwnerID, tokenBySecret.OwnerID)
})
t.Run("Successful base64 encoding", func(t *testing.T) {
base64token := rptoken.Secret.String()
assert.NotEmpty(t, base64token)
})
t.Run("Successful base64 decoding", func(t *testing.T) {
base64token := rptoken.Secret.String()
assert.NotEmpty(t, base64token)
secretFromString, err := console.ResetPasswordSecretFromBase64(base64token)
assert.NoError(t, err)
assert.Equal(t, rptoken.Secret, secretFromString)
})
})
}