diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-14 09:41:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-14 09:41:54 +0800 |
commit | cb439a126aa6a2728e423bcfd0d5e948337b8ddb (patch) | |
tree | f7d09181fe5b96ea444f7544091673b3c668b9fe /internal/db/two_factors_test.go | |
parent | 659acd48b1a131476fd98a54604fa6416b1cef9d (diff) |
db: add tests for two factors (#6099)
* Rename to TwoFactors.Create
* Use GORM to execute queries
* TwoFactor.GetByUserID
* Add tests
* Fix failing tests
* Add MD5 tests
* Add tests for RandomChars
Diffstat (limited to 'internal/db/two_factors_test.go')
-rw-r--r-- | internal/db/two_factors_test.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/internal/db/two_factors_test.go b/internal/db/two_factors_test.go new file mode 100644 index 00000000..e4dea6ca --- /dev/null +++ b/internal/db/two_factors_test.go @@ -0,0 +1,100 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db + +import ( + "testing" + "time" + + "github.com/jinzhu/gorm" + "github.com/stretchr/testify/assert" + + "gogs.io/gogs/internal/errutil" +) + +func Test_twoFactors(t *testing.T) { + if testing.Short() { + t.Skip() + } + + t.Parallel() + + tables := []interface{}{new(TwoFactor), new(TwoFactorRecoveryCode)} + db := &twoFactors{ + DB: initTestDB(t, "twoFactors", tables...), + } + + for _, tc := range []struct { + name string + test func(*testing.T, *twoFactors) + }{ + {"Create", test_twoFactors_Create}, + {"GetByUserID", test_twoFactors_GetByUserID}, + {"IsUserEnabled", test_twoFactors_IsUserEnabled}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Cleanup(func() { + err := clearTables(db.DB, tables...) + if err != nil { + t.Fatal(err) + } + }) + tc.test(t, db) + }) + } +} + +func test_twoFactors_Create(t *testing.T, db *twoFactors) { + // Create a 2FA token + err := db.Create(1, "secure-key", "secure-secret") + if err != nil { + t.Fatal(err) + } + + // Get it back and check the Created field + tf, err := db.GetByUserID(1) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), tf.Created.Format(time.RFC3339)) + + // Verify there are 10 recover codes generated + var count int64 + err = db.Model(new(TwoFactorRecoveryCode)).Count(&count).Error + if err != nil { + t.Fatal(err) + } + assert.Equal(t, int64(10), count) +} + +func test_twoFactors_GetByUserID(t *testing.T, db *twoFactors) { + // Create a 2FA token for user 1 + err := db.Create(1, "secure-key", "secure-secret") + if err != nil { + t.Fatal(err) + } + + // We should be able to get it back + _, err = db.GetByUserID(1) + if err != nil { + t.Fatal(err) + } + + // Try to get a non-existent 2FA token + _, err = db.GetByUserID(2) + expErr := ErrTwoFactorNotFound{args: errutil.Args{"userID": int64(2)}} + assert.Equal(t, expErr, err) +} + +func test_twoFactors_IsUserEnabled(t *testing.T, db *twoFactors) { + // Create a 2FA token for user 1 + err := db.Create(1, "secure-key", "secure-secret") + if err != nil { + t.Fatal(err) + } + + assert.True(t, db.IsUserEnabled(1)) + assert.False(t, db.IsUserEnabled(2)) +} |