two_factors_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "gorm.io/gorm"
  12. "gogs.io/gogs/internal/dbtest"
  13. "gogs.io/gogs/internal/errutil"
  14. )
  15. func TestTwoFactor_BeforeCreate(t *testing.T) {
  16. now := time.Now()
  17. db := &gorm.DB{
  18. Config: &gorm.Config{
  19. SkipDefaultTransaction: true,
  20. NowFunc: func() time.Time {
  21. return now
  22. },
  23. },
  24. }
  25. t.Run("CreatedUnix has been set", func(t *testing.T) {
  26. tf := &TwoFactor{
  27. CreatedUnix: 1,
  28. }
  29. _ = tf.BeforeCreate(db)
  30. assert.Equal(t, int64(1), tf.CreatedUnix)
  31. })
  32. t.Run("CreatedUnix has not been set", func(t *testing.T) {
  33. tf := &TwoFactor{}
  34. _ = tf.BeforeCreate(db)
  35. assert.Equal(t, db.NowFunc().Unix(), tf.CreatedUnix)
  36. })
  37. }
  38. func TestTwoFactor_AfterFind(t *testing.T) {
  39. now := time.Now()
  40. db := &gorm.DB{
  41. Config: &gorm.Config{
  42. SkipDefaultTransaction: true,
  43. NowFunc: func() time.Time {
  44. return now
  45. },
  46. },
  47. }
  48. tf := &TwoFactor{
  49. CreatedUnix: now.Unix(),
  50. }
  51. _ = tf.AfterFind(db)
  52. assert.Equal(t, tf.CreatedUnix, tf.Created.Unix())
  53. }
  54. func TestTwoFactors(t *testing.T) {
  55. if testing.Short() {
  56. t.Skip()
  57. }
  58. t.Parallel()
  59. ctx := context.Background()
  60. tables := []any{new(TwoFactor), new(TwoFactorRecoveryCode)}
  61. db := &twoFactors{
  62. DB: dbtest.NewDB(t, "twoFactors", tables...),
  63. }
  64. for _, tc := range []struct {
  65. name string
  66. test func(t *testing.T, ctx context.Context, db *twoFactors)
  67. }{
  68. {"Create", twoFactorsCreate},
  69. {"GetByUserID", twoFactorsGetByUserID},
  70. {"IsEnabled", twoFactorsIsEnabled},
  71. } {
  72. t.Run(tc.name, func(t *testing.T) {
  73. t.Cleanup(func() {
  74. err := clearTables(t, db.DB, tables...)
  75. require.NoError(t, err)
  76. })
  77. tc.test(t, ctx, db)
  78. })
  79. if t.Failed() {
  80. break
  81. }
  82. }
  83. }
  84. func twoFactorsCreate(t *testing.T, ctx context.Context, db *twoFactors) {
  85. // Create a 2FA token
  86. err := db.Create(ctx, 1, "secure-key", "secure-secret")
  87. require.NoError(t, err)
  88. // Get it back and check the Created field
  89. tf, err := db.GetByUserID(ctx, 1)
  90. require.NoError(t, err)
  91. assert.Equal(t, db.NowFunc().Format(time.RFC3339), tf.Created.UTC().Format(time.RFC3339))
  92. // Verify there are 10 recover codes generated
  93. var count int64
  94. err = db.Model(new(TwoFactorRecoveryCode)).Count(&count).Error
  95. require.NoError(t, err)
  96. assert.Equal(t, int64(10), count)
  97. }
  98. func twoFactorsGetByUserID(t *testing.T, ctx context.Context, db *twoFactors) {
  99. // Create a 2FA token for user 1
  100. err := db.Create(ctx, 1, "secure-key", "secure-secret")
  101. require.NoError(t, err)
  102. // We should be able to get it back
  103. _, err = db.GetByUserID(ctx, 1)
  104. require.NoError(t, err)
  105. // Try to get a non-existent 2FA token
  106. _, err = db.GetByUserID(ctx, 2)
  107. wantErr := ErrTwoFactorNotFound{args: errutil.Args{"userID": int64(2)}}
  108. assert.Equal(t, wantErr, err)
  109. }
  110. func twoFactorsIsEnabled(t *testing.T, ctx context.Context, db *twoFactors) {
  111. // Create a 2FA token for user 1
  112. err := db.Create(ctx, 1, "secure-key", "secure-secret")
  113. require.NoError(t, err)
  114. assert.True(t, db.IsEnabled(ctx, 1))
  115. assert.False(t, db.IsEnabled(ctx, 2))
  116. }