2021-01-24 23:23:05 +08:00
|
|
|
// Copyright 2020 The Gitea 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 models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-01-24 23:23:05 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookupUserRedirect(t *testing.T) {
|
2021-09-19 19:49:59 +08:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
2021-01-24 23:23:05 +08:00
|
|
|
|
|
|
|
userID, err := LookupUserRedirect("olduser1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, userID)
|
|
|
|
|
|
|
|
_, err = LookupUserRedirect("doesnotexist")
|
|
|
|
assert.True(t, IsErrUserRedirectNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewUserRedirect(t *testing.T) {
|
|
|
|
// redirect to a completely new name
|
2021-09-19 19:49:59 +08:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
2021-09-23 23:45:36 +08:00
|
|
|
assert.NoError(t, newUserRedirect(db.GetEngine(db.DefaultContext), user.ID, user.Name, "newusername"))
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
db.AssertExistsAndLoadBean(t, &UserRedirect{
|
2021-01-24 23:23:05 +08:00
|
|
|
LowerName: user.LowerName,
|
|
|
|
RedirectUserID: user.ID,
|
|
|
|
})
|
2021-09-19 19:49:59 +08:00
|
|
|
db.AssertExistsAndLoadBean(t, &UserRedirect{
|
2021-01-24 23:23:05 +08:00
|
|
|
LowerName: "olduser1",
|
|
|
|
RedirectUserID: user.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewUserRedirect2(t *testing.T) {
|
|
|
|
// redirect to previously used name
|
2021-09-19 19:49:59 +08:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
2021-09-23 23:45:36 +08:00
|
|
|
assert.NoError(t, newUserRedirect(db.GetEngine(db.DefaultContext), user.ID, user.Name, "olduser1"))
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
db.AssertExistsAndLoadBean(t, &UserRedirect{
|
2021-01-24 23:23:05 +08:00
|
|
|
LowerName: user.LowerName,
|
|
|
|
RedirectUserID: user.ID,
|
|
|
|
})
|
2021-09-19 19:49:59 +08:00
|
|
|
db.AssertNotExistsBean(t, &UserRedirect{
|
2021-01-24 23:23:05 +08:00
|
|
|
LowerName: "olduser1",
|
|
|
|
RedirectUserID: user.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewUserRedirect3(t *testing.T) {
|
|
|
|
// redirect for a previously-unredirected user
|
2021-09-19 19:49:59 +08:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
2021-09-23 23:45:36 +08:00
|
|
|
assert.NoError(t, newUserRedirect(db.GetEngine(db.DefaultContext), user.ID, user.Name, "newusername"))
|
2021-01-24 23:23:05 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
db.AssertExistsAndLoadBean(t, &UserRedirect{
|
2021-01-24 23:23:05 +08:00
|
|
|
LowerName: user.LowerName,
|
|
|
|
RedirectUserID: user.ID,
|
|
|
|
})
|
|
|
|
}
|