2021-12-12 09:48:20 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 09:48:20 -06:00
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
package repo_test
|
2021-12-12 09:48:20 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 03:01:49 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-12-12 09:48:20 -06:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookupRedirect(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
repoID, err := repo_model.LookupRedirect(2, "oldrepo1")
|
2021-12-12 09:48:20 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, repoID)
|
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
_, err = repo_model.LookupRedirect(unittest.NonexistentID, "doesnotexist")
|
|
|
|
assert.True(t, repo_model.IsErrRedirectNotExist(err))
|
2021-12-12 09:48:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRedirect(t *testing.T) {
|
|
|
|
// redirect to a completely new name
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 03:01:49 -05:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "newreponame"))
|
2021-12-12 09:48:20 -06:00
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 09:48:20 -06:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
2022-06-06 03:01:49 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 09:48:20 -06:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: "oldrepo1",
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRedirect2(t *testing.T) {
|
|
|
|
// redirect to previously used name
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 03:01:49 -05:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "oldrepo1"))
|
2021-12-12 09:48:20 -06:00
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 09:48:20 -06:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
2022-06-06 03:01:49 -05:00
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.Redirect{
|
2021-12-12 09:48:20 -06:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: "oldrepo1",
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRedirect3(t *testing.T) {
|
|
|
|
// redirect for a previously-unredirected repo
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2022-06-06 03:01:49 -05:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "newreponame"))
|
2021-12-12 09:48:20 -06:00
|
|
|
|
2022-06-06 03:01:49 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 09:48:20 -06:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
|
|
|
}
|