forked from electricdusk/rushlink
db: Do not warn during paste key generation
This commit is contained in:
parent
0048004252
commit
1ecb11c65f
@ -32,10 +32,10 @@ type Database = gorm.DB
|
|||||||
var (
|
var (
|
||||||
gormLogger logger.Interface = logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
|
gormLogger logger.Interface = logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
|
||||||
SlowThreshold: 50 * time.Millisecond,
|
SlowThreshold: 50 * time.Millisecond,
|
||||||
LogLevel: logger.Error,
|
LogLevel: logger.Warn,
|
||||||
Colorful: true,
|
Colorful: true,
|
||||||
})
|
})
|
||||||
gormConfig = gorm.Config{Logger: gormLogger}
|
gormConfig = gorm.Config{Logger: gormLogger, PrepareStmt: true}
|
||||||
)
|
)
|
||||||
|
|
||||||
// OpenDBFromEnvironment tries to open an SQL database, described by
|
// OpenDBFromEnvironment tries to open an SQL database, described by
|
||||||
|
@ -232,15 +232,12 @@ func GeneratePasteKey(db *gorm.DB, minimumEntropy int) (string, error) {
|
|||||||
return "", errors.Wrap(err, "url-key generation failed")
|
return "", errors.Wrap(err, "url-key generation failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
alreadyInUse := true
|
var count int64
|
||||||
var p Paste
|
db.Unscoped().Model(&Paste{}).Where("key = ?", []byte(key)).Count(&count)
|
||||||
err = db.Unscoped().Where("key = ?", []byte(key)).First(&p).Error
|
if err != nil {
|
||||||
if err != nil && err == gorm.ErrRecordNotFound {
|
|
||||||
alreadyInUse = false
|
|
||||||
err = nil
|
|
||||||
} else if err != nil {
|
|
||||||
return "", errors.Wrap(err, "failed to check if key already exists")
|
return "", errors.Wrap(err, "failed to check if key already exists")
|
||||||
}
|
}
|
||||||
|
alreadyInUse := count != 0
|
||||||
|
|
||||||
isReserved := false
|
isReserved := false
|
||||||
for _, reservedKey := range ReservedPasteKeys {
|
for _, reservedKey := range ReservedPasteKeys {
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
"testing/quick"
|
||||||
|
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func OpenTemporaryDB() (*Database, error) {
|
||||||
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gormConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidatePasteKey(t *testing.T) {
|
func TestValidatePasteKey(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -36,3 +50,24 @@ func TestValidatePasteKey(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGeneratedKeysAreValid(t *testing.T) {
|
||||||
|
db, err := OpenTemporaryDB()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
var minimumEntropy int
|
||||||
|
checkGeneratedKeyValid := func() bool {
|
||||||
|
key, err := GeneratePasteKey(db, minimumEntropy)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return ValidatePasteKey(key) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for minimumEntropy = 0; minimumEntropy <= 80; minimumEntropy++ {
|
||||||
|
if err = quick.Check(checkGeneratedKeyValid, nil); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user