diff options
-rw-r--r-- | internal/db/access_tokens_test.go | 56 | ||||
-rw-r--r-- | internal/db/actions_test.go | 22 | ||||
-rw-r--r-- | internal/db/login_sources.go | 6 | ||||
-rw-r--r-- | internal/db/login_sources_test.go | 82 | ||||
-rw-r--r-- | internal/db/repos_test.go | 63 | ||||
-rw-r--r-- | internal/db/two_factors_test.go | 22 | ||||
-rw-r--r-- | internal/db/users_test.go | 49 |
7 files changed, 291 insertions, 9 deletions
diff --git a/internal/db/access_tokens_test.go b/internal/db/access_tokens_test.go index 1a1d1c2d..a14f9a72 100644 --- a/internal/db/access_tokens_test.go +++ b/internal/db/access_tokens_test.go @@ -21,6 +21,7 @@ func TestAccessToken_BeforeCreate(t *testing.T) { now := time.Now() db := &gorm.DB{ Config: &gorm.Config{ + SkipDefaultTransaction: true, NowFunc: func() time.Time { return now }, @@ -28,17 +29,66 @@ func TestAccessToken_BeforeCreate(t *testing.T) { } t.Run("CreatedUnix has been set", func(t *testing.T) { - token := &AccessToken{CreatedUnix: 1} + token := &AccessToken{ + CreatedUnix: 1, + } _ = token.BeforeCreate(db) assert.Equal(t, int64(1), token.CreatedUnix) - assert.Equal(t, int64(0), token.UpdatedUnix) + assert.Equal(t, int64(0), token.UpdatedUnix) // Do not set UpdatedUnix until it is used. }) t.Run("CreatedUnix has not been set", func(t *testing.T) { token := &AccessToken{} _ = token.BeforeCreate(db) assert.Equal(t, db.NowFunc().Unix(), token.CreatedUnix) - assert.Equal(t, int64(0), token.UpdatedUnix) + assert.Equal(t, int64(0), token.UpdatedUnix) // Do not set UpdatedUnix until it is used. + }) +} + +func TestAccessToken_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + t.Run("UpdatedUnix has been set and within 7 days", func(t *testing.T) { + token := &AccessToken{ + CreatedUnix: now.Unix(), + UpdatedUnix: now.Add(time.Second).Unix(), + } + _ = token.AfterFind(db) + assert.Equal(t, token.CreatedUnix, token.Created.Unix()) + assert.Equal(t, token.UpdatedUnix, token.Updated.Unix()) + assert.True(t, token.HasUsed) + assert.True(t, token.HasRecentActivity) + }) + + t.Run("UpdatedUnix has been set and not within 7 days", func(t *testing.T) { + token := &AccessToken{ + CreatedUnix: now.Add(-1 * 9 * 24 * time.Hour).Unix(), + UpdatedUnix: now.Add(-1 * 8 * 24 * time.Hour).Unix(), + } + _ = token.AfterFind(db) + assert.Equal(t, token.CreatedUnix, token.Created.Unix()) + assert.Equal(t, token.UpdatedUnix, token.Updated.Unix()) + assert.True(t, token.HasUsed) + assert.False(t, token.HasRecentActivity) + }) + + t.Run("UpdatedUnix has not been set", func(t *testing.T) { + token := &AccessToken{ + CreatedUnix: now.Unix(), + } + _ = token.AfterFind(db) + assert.Equal(t, token.CreatedUnix, token.Created.Unix()) + assert.True(t, token.Updated.IsZero()) + assert.False(t, token.HasUsed) + assert.False(t, token.HasRecentActivity) }) } diff --git a/internal/db/actions_test.go b/internal/db/actions_test.go index ea50b9b4..9c8dae37 100644 --- a/internal/db/actions_test.go +++ b/internal/db/actions_test.go @@ -61,7 +61,9 @@ func TestAction_BeforeCreate(t *testing.T) { } t.Run("CreatedUnix has been set", func(t *testing.T) { - action := &Action{CreatedUnix: 1} + action := &Action{ + CreatedUnix: 1, + } _ = action.BeforeCreate(db) assert.Equal(t, int64(1), action.CreatedUnix) }) @@ -73,6 +75,24 @@ func TestAction_BeforeCreate(t *testing.T) { }) } +func TestAction_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + action := &Action{ + CreatedUnix: now.Unix(), + } + _ = action.AfterFind(db) + assert.Equal(t, action.CreatedUnix, action.Created.Unix()) +} + func TestActions(t *testing.T) { if testing.Short() { t.Skip() diff --git a/internal/db/login_sources.go b/internal/db/login_sources.go index cc7bae9e..1ce5caa2 100644 --- a/internal/db/login_sources.go +++ b/internal/db/login_sources.go @@ -52,11 +52,11 @@ var LoginSources LoginSourcesStore type LoginSource struct { ID int64 `gorm:"primaryKey"` Type auth.Type - Name string `xorm:"UNIQUE" gorm:"UNIQUE"` - IsActived bool `xorm:"NOT NULL DEFAULT false" gorm:"NOT NULL"` + Name string `xorm:"UNIQUE" gorm:"unique"` + IsActived bool `xorm:"NOT NULL DEFAULT false" gorm:"not null"` IsDefault bool `xorm:"DEFAULT false"` Provider auth.Provider `xorm:"-" gorm:"-"` - Config string `xorm:"TEXT cfg" gorm:"COLUMN:cfg;TYPE:TEXT" json:"RawConfig"` + Config string `xorm:"TEXT cfg" gorm:"column:cfg;type:TEXT" json:"RawConfig"` Created time.Time `xorm:"-" gorm:"-" json:"-"` CreatedUnix int64 diff --git a/internal/db/login_sources_test.go b/internal/db/login_sources_test.go index fc03c38b..f455761f 100644 --- a/internal/db/login_sources_test.go +++ b/internal/db/login_sources_test.go @@ -16,7 +16,9 @@ import ( "gogs.io/gogs/internal/auth" "gogs.io/gogs/internal/auth/github" + "gogs.io/gogs/internal/auth/ldap" "gogs.io/gogs/internal/auth/pam" + "gogs.io/gogs/internal/auth/smtp" "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) @@ -63,7 +65,9 @@ func TestLoginSource_BeforeCreate(t *testing.T) { } t.Run("CreatedUnix has been set", func(t *testing.T) { - s := &LoginSource{CreatedUnix: 1} + s := &LoginSource{ + CreatedUnix: 1, + } _ = s.BeforeCreate(db) assert.Equal(t, int64(1), s.CreatedUnix) assert.Equal(t, int64(0), s.UpdatedUnix) @@ -77,6 +81,82 @@ func TestLoginSource_BeforeCreate(t *testing.T) { }) } +func TestLoginSource_BeforeUpdate(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + s := &LoginSource{} + _ = s.BeforeUpdate(db) + assert.Equal(t, db.NowFunc().Unix(), s.UpdatedUnix) +} + +func TestLoginSource_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + tests := []struct { + name string + authType auth.Type + wantType any + }{ + { + name: "LDAP", + authType: auth.LDAP, + wantType: &ldap.Provider{}, + }, + { + name: "DLDAP", + authType: auth.DLDAP, + wantType: &ldap.Provider{}, + }, + { + name: "SMTP", + authType: auth.SMTP, + wantType: &smtp.Provider{}, + }, + { + name: "PAM", + authType: auth.PAM, + wantType: &pam.Provider{}, + }, + { + name: "GitHub", + authType: auth.GitHub, + wantType: &github.Provider{}, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + s := LoginSource{ + Type: test.authType, + Config: `{}`, + CreatedUnix: now.Unix(), + UpdatedUnix: now.Unix(), + } + err := s.AfterFind(db) + require.NoError(t, err) + + assert.Equal(t, s.CreatedUnix, s.Created.Unix()) + assert.Equal(t, s.UpdatedUnix, s.Updated.Unix()) + assert.IsType(t, test.wantType, s.Provider) + }) + } +} + func Test_loginSources(t *testing.T) { if testing.Short() { t.Skip() diff --git a/internal/db/repos_test.go b/internal/db/repos_test.go index 99c19505..2af42e8b 100644 --- a/internal/db/repos_test.go +++ b/internal/db/repos_test.go @@ -11,11 +11,74 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gorm.io/gorm" "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) +func TestRepository_BeforeCreate(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + t.Run("CreatedUnix has been set", func(t *testing.T) { + repo := &Repository{ + CreatedUnix: 1, + } + _ = repo.BeforeCreate(db) + assert.Equal(t, int64(1), repo.CreatedUnix) + }) + + t.Run("CreatedUnix has not been set", func(t *testing.T) { + repo := &Repository{} + _ = repo.BeforeCreate(db) + assert.Equal(t, db.NowFunc().Unix(), repo.CreatedUnix) + }) +} + +func TestRepository_BeforeUpdate(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + repo := &Repository{} + _ = repo.BeforeUpdate(db) + assert.Equal(t, db.NowFunc().Unix(), repo.UpdatedUnix) +} + +func TestRepository_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + repo := &Repository{ + CreatedUnix: now.Unix(), + UpdatedUnix: now.Unix(), + } + _ = repo.AfterFind(db) + assert.Equal(t, repo.CreatedUnix, repo.Created.Unix()) + assert.Equal(t, repo.UpdatedUnix, repo.Updated.Unix()) +} + func TestRepos(t *testing.T) { if testing.Short() { t.Skip() diff --git a/internal/db/two_factors_test.go b/internal/db/two_factors_test.go index 36e4eeca..ddff3fce 100644 --- a/internal/db/two_factors_test.go +++ b/internal/db/two_factors_test.go @@ -29,7 +29,9 @@ func TestTwoFactor_BeforeCreate(t *testing.T) { } t.Run("CreatedUnix has been set", func(t *testing.T) { - tf := &TwoFactor{CreatedUnix: 1} + tf := &TwoFactor{ + CreatedUnix: 1, + } _ = tf.BeforeCreate(db) assert.Equal(t, int64(1), tf.CreatedUnix) }) @@ -41,6 +43,24 @@ func TestTwoFactor_BeforeCreate(t *testing.T) { }) } +func TestTwoFactor_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + tf := &TwoFactor{ + CreatedUnix: now.Unix(), + } + _ = tf.AfterFind(db) + assert.Equal(t, tf.CreatedUnix, tf.Created.Unix()) +} + func TestTwoFactors(t *testing.T) { if testing.Short() { t.Skip() diff --git a/internal/db/users_test.go b/internal/db/users_test.go index efd5681c..5645ff7b 100644 --- a/internal/db/users_test.go +++ b/internal/db/users_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gorm.io/gorm" "gogs.io/gogs/internal/auth" "gogs.io/gogs/internal/dbtest" @@ -22,6 +23,54 @@ import ( "gogs.io/gogs/public" ) +func TestUser_BeforeCreate(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + t.Run("CreatedUnix has been set", func(t *testing.T) { + user := &User{ + CreatedUnix: 1, + } + _ = user.BeforeCreate(db) + assert.Equal(t, int64(1), user.CreatedUnix) + assert.Equal(t, int64(0), user.UpdatedUnix) + }) + + t.Run("CreatedUnix has not been set", func(t *testing.T) { + user := &User{} + _ = user.BeforeCreate(db) + assert.Equal(t, db.NowFunc().Unix(), user.CreatedUnix) + assert.Equal(t, db.NowFunc().Unix(), user.UpdatedUnix) + }) +} + +func TestUser_AfterFind(t *testing.T) { + now := time.Now() + db := &gorm.DB{ + Config: &gorm.Config{ + SkipDefaultTransaction: true, + NowFunc: func() time.Time { + return now + }, + }, + } + + user := &User{ + CreatedUnix: now.Unix(), + UpdatedUnix: now.Unix(), + } + _ = user.AfterFind(db) + assert.Equal(t, user.CreatedUnix, user.Created.Unix()) + assert.Equal(t, user.UpdatedUnix, user.Updated.Unix()) +} + func TestUsers(t *testing.T) { if testing.Short() { t.Skip() |