mirror of https://github.com/gogs/gogs.git
db: code tidy up for `AccessTokens` (#7006)
parent
38aff73251
commit
4455cc1244
|
@ -54,7 +54,7 @@ type AccessToken struct {
|
||||||
HasUsed bool `xorm:"-" gorm:"-" json:"-"`
|
HasUsed bool `xorm:"-" gorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This is a GORM create hook.
|
// BeforeCreate implements the GORM create hook.
|
||||||
func (t *AccessToken) BeforeCreate(tx *gorm.DB) error {
|
func (t *AccessToken) BeforeCreate(tx *gorm.DB) error {
|
||||||
if t.CreatedUnix == 0 {
|
if t.CreatedUnix == 0 {
|
||||||
t.CreatedUnix = tx.NowFunc().Unix()
|
t.CreatedUnix = tx.NowFunc().Unix()
|
||||||
|
@ -62,13 +62,13 @@ func (t *AccessToken) BeforeCreate(tx *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This is a GORM update hook.
|
// BeforeUpdate implements the GORM update hook.
|
||||||
func (t *AccessToken) BeforeUpdate(tx *gorm.DB) error {
|
func (t *AccessToken) BeforeUpdate(tx *gorm.DB) error {
|
||||||
t.UpdatedUnix = tx.NowFunc().Unix()
|
t.UpdatedUnix = tx.NowFunc().Unix()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This is a GORM query hook.
|
// AfterFind implements the GORM query hook.
|
||||||
func (t *AccessToken) AfterFind(tx *gorm.DB) error {
|
func (t *AccessToken) AfterFind(tx *gorm.DB) error {
|
||||||
t.Created = time.Unix(t.CreatedUnix, 0).Local()
|
t.Created = time.Unix(t.CreatedUnix, 0).Local()
|
||||||
t.Updated = time.Unix(t.UpdatedUnix, 0).Local()
|
t.Updated = time.Unix(t.UpdatedUnix, 0).Local()
|
||||||
|
|
|
@ -39,7 +39,7 @@ func TestAccessToken_BeforeCreate(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_accessTokens(t *testing.T) {
|
func TestAccessTokens(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
|
@ -55,11 +55,11 @@ func Test_accessTokens(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
test func(*testing.T, *accessTokens)
|
test func(*testing.T, *accessTokens)
|
||||||
}{
|
}{
|
||||||
{"Create", test_accessTokens_Create},
|
{"Create", accessTokensCreate},
|
||||||
{"DeleteByID", test_accessTokens_DeleteByID},
|
{"DeleteByID", accessTokensDeleteByID},
|
||||||
{"GetBySHA", test_accessTokens_GetBySHA},
|
{"GetBySHA", accessTokensGetBySHA},
|
||||||
{"List", test_accessTokens_List},
|
{"List", accessTokensList},
|
||||||
{"Save", test_accessTokens_Save},
|
{"Save", accessTokensSave},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
@ -70,10 +70,13 @@ func Test_accessTokens(t *testing.T) {
|
||||||
})
|
})
|
||||||
tc.test(t, db)
|
tc.test(t, db)
|
||||||
})
|
})
|
||||||
|
if t.Failed() {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_accessTokens_Create(t *testing.T, db *accessTokens) {
|
func accessTokensCreate(t *testing.T, db *accessTokens) {
|
||||||
// Create first access token with name "Test"
|
// Create first access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(1, "Test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -97,7 +100,7 @@ func test_accessTokens_Create(t *testing.T, db *accessTokens) {
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, expErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_accessTokens_DeleteByID(t *testing.T, db *accessTokens) {
|
func accessTokensDeleteByID(t *testing.T, db *accessTokens) {
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(1, "Test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -132,7 +135,7 @@ func test_accessTokens_DeleteByID(t *testing.T, db *accessTokens) {
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, expErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_accessTokens_GetBySHA(t *testing.T, db *accessTokens) {
|
func accessTokensGetBySHA(t *testing.T, db *accessTokens) {
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(1, "Test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -151,7 +154,7 @@ func test_accessTokens_GetBySHA(t *testing.T, db *accessTokens) {
|
||||||
assert.Equal(t, expErr, err)
|
assert.Equal(t, expErr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_accessTokens_List(t *testing.T, db *accessTokens) {
|
func accessTokensList(t *testing.T, db *accessTokens) {
|
||||||
// Create two access tokens for user 1
|
// Create two access tokens for user 1
|
||||||
_, err := db.Create(1, "user1_1")
|
_, err := db.Create(1, "user1_1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -182,7 +185,7 @@ func test_accessTokens_List(t *testing.T, db *accessTokens) {
|
||||||
assert.Equal(t, "user1_2", tokens[1].Name)
|
assert.Equal(t, "user1_2", tokens[1].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_accessTokens_Save(t *testing.T, db *accessTokens) {
|
func accessTokensSave(t *testing.T, db *accessTokens) {
|
||||||
// Create an access token with name "Test"
|
// Create an access token with name "Test"
|
||||||
token, err := db.Create(1, "Test")
|
token, err := db.Create(1, "Test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue