mirror of https://github.com/gogs/gogs.git
test(db): add missing tests (#7208)
parent
3265abfbc2
commit
f6acc4763e
|
@ -21,6 +21,7 @@ func TestAccessToken_BeforeCreate(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
db := &gorm.DB{
|
db := &gorm.DB{
|
||||||
Config: &gorm.Config{
|
Config: &gorm.Config{
|
||||||
|
SkipDefaultTransaction: true,
|
||||||
NowFunc: func() time.Time {
|
NowFunc: func() time.Time {
|
||||||
return now
|
return now
|
||||||
},
|
},
|
||||||
|
@ -28,17 +29,66 @@ func TestAccessToken_BeforeCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
||||||
token := &AccessToken{CreatedUnix: 1}
|
token := &AccessToken{
|
||||||
|
CreatedUnix: 1,
|
||||||
|
}
|
||||||
_ = token.BeforeCreate(db)
|
_ = token.BeforeCreate(db)
|
||||||
assert.Equal(t, int64(1), token.CreatedUnix)
|
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) {
|
t.Run("CreatedUnix has not been set", func(t *testing.T) {
|
||||||
token := &AccessToken{}
|
token := &AccessToken{}
|
||||||
_ = token.BeforeCreate(db)
|
_ = token.BeforeCreate(db)
|
||||||
assert.Equal(t, db.NowFunc().Unix(), token.CreatedUnix)
|
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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,9 @@ func TestAction_BeforeCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
||||||
action := &Action{CreatedUnix: 1}
|
action := &Action{
|
||||||
|
CreatedUnix: 1,
|
||||||
|
}
|
||||||
_ = action.BeforeCreate(db)
|
_ = action.BeforeCreate(db)
|
||||||
assert.Equal(t, int64(1), action.CreatedUnix)
|
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) {
|
func TestActions(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
|
|
|
@ -52,11 +52,11 @@ var LoginSources LoginSourcesStore
|
||||||
type LoginSource struct {
|
type LoginSource struct {
|
||||||
ID int64 `gorm:"primaryKey"`
|
ID int64 `gorm:"primaryKey"`
|
||||||
Type auth.Type
|
Type auth.Type
|
||||||
Name string `xorm:"UNIQUE" gorm:"UNIQUE"`
|
Name string `xorm:"UNIQUE" gorm:"unique"`
|
||||||
IsActived bool `xorm:"NOT NULL DEFAULT false" gorm:"NOT NULL"`
|
IsActived bool `xorm:"NOT NULL DEFAULT false" gorm:"not null"`
|
||||||
IsDefault bool `xorm:"DEFAULT false"`
|
IsDefault bool `xorm:"DEFAULT false"`
|
||||||
Provider auth.Provider `xorm:"-" gorm:"-"`
|
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:"-"`
|
Created time.Time `xorm:"-" gorm:"-" json:"-"`
|
||||||
CreatedUnix int64
|
CreatedUnix int64
|
||||||
|
|
|
@ -16,7 +16,9 @@ import (
|
||||||
|
|
||||||
"gogs.io/gogs/internal/auth"
|
"gogs.io/gogs/internal/auth"
|
||||||
"gogs.io/gogs/internal/auth/github"
|
"gogs.io/gogs/internal/auth/github"
|
||||||
|
"gogs.io/gogs/internal/auth/ldap"
|
||||||
"gogs.io/gogs/internal/auth/pam"
|
"gogs.io/gogs/internal/auth/pam"
|
||||||
|
"gogs.io/gogs/internal/auth/smtp"
|
||||||
"gogs.io/gogs/internal/dbtest"
|
"gogs.io/gogs/internal/dbtest"
|
||||||
"gogs.io/gogs/internal/errutil"
|
"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) {
|
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
||||||
s := &LoginSource{CreatedUnix: 1}
|
s := &LoginSource{
|
||||||
|
CreatedUnix: 1,
|
||||||
|
}
|
||||||
_ = s.BeforeCreate(db)
|
_ = s.BeforeCreate(db)
|
||||||
assert.Equal(t, int64(1), s.CreatedUnix)
|
assert.Equal(t, int64(1), s.CreatedUnix)
|
||||||
assert.Equal(t, int64(0), s.UpdatedUnix)
|
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) {
|
func Test_loginSources(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
|
|
|
@ -11,11 +11,74 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"gogs.io/gogs/internal/dbtest"
|
"gogs.io/gogs/internal/dbtest"
|
||||||
"gogs.io/gogs/internal/errutil"
|
"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) {
|
func TestRepos(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
|
|
|
@ -29,7 +29,9 @@ func TestTwoFactor_BeforeCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
t.Run("CreatedUnix has been set", func(t *testing.T) {
|
||||||
tf := &TwoFactor{CreatedUnix: 1}
|
tf := &TwoFactor{
|
||||||
|
CreatedUnix: 1,
|
||||||
|
}
|
||||||
_ = tf.BeforeCreate(db)
|
_ = tf.BeforeCreate(db)
|
||||||
assert.Equal(t, int64(1), tf.CreatedUnix)
|
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) {
|
func TestTwoFactors(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"gogs.io/gogs/internal/auth"
|
"gogs.io/gogs/internal/auth"
|
||||||
"gogs.io/gogs/internal/dbtest"
|
"gogs.io/gogs/internal/dbtest"
|
||||||
|
@ -22,6 +23,54 @@ import (
|
||||||
"gogs.io/gogs/public"
|
"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) {
|
func TestUsers(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
|
|
Loading…
Reference in New Issue