2021-12-12 16:48:20 +01:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
package repo_test
|
2021-12-12 16:48:20 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 10:01:49 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-12-12 16:48:20 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookupRedirect(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2023-10-14 10:37:24 +02:00
|
|
|
repoID, err := repo_model.LookupRedirect(db.DefaultContext, 2, "oldrepo1")
|
2021-12-12 16:48:20 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, repoID)
|
|
|
|
|
2023-10-14 10:37:24 +02:00
|
|
|
_, err = repo_model.LookupRedirect(db.DefaultContext, unittest.NonexistentID, "doesnotexist")
|
2022-06-06 10:01:49 +02:00
|
|
|
assert.True(t, repo_model.IsErrRedirectNotExist(err))
|
2021-12-12 16:48:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRedirect(t *testing.T) {
|
|
|
|
// redirect to a completely new name
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 10:01:49 +02:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "newreponame"))
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 16:48:20 +01:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
2022-06-06 10:01:49 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 16:48:20 +01: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-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 10:01:49 +02:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "oldrepo1"))
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 16:48:20 +01:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
2022-06-06 10:01:49 +02:00
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.Redirect{
|
2021-12-12 16:48:20 +01: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-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2022-06-06 10:01:49 +02:00
|
|
|
assert.NoError(t, repo_model.NewRedirect(db.DefaultContext, repo.OwnerID, repo.ID, repo.Name, "newreponame"))
|
2021-12-12 16:48:20 +01:00
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Redirect{
|
2021-12-12 16:48:20 +01:00
|
|
|
OwnerID: repo.OwnerID,
|
|
|
|
LowerName: repo.LowerName,
|
|
|
|
RedirectRepoID: repo.ID,
|
|
|
|
})
|
|
|
|
}
|