add test coverage for original author conversion during migrations (#18506)

* add test coverage for original author conversion during migrations

And create a function to factorize a code snippet that is repeated
five times and would otherwise be more difficult to test and maintain
consistently.

Signed-off-by: Loïc Dachary <loic@dachary.org>

* fix variable scope and int64 formatting

* add missing calls to remapExternalUser and fix misplaced %d

Co-authored-by: Loïc Dachary <loic@dachary.org>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
singuliere 2022-02-01 19:20:28 +01:00 committed by GitHub
parent 6f6b8491da
commit 367894adc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 222 additions and 173 deletions

View file

@ -7,6 +7,7 @@ package migrations
import (
"context"
"strconv"
"testing"
"time"
@ -115,3 +116,50 @@ func TestGiteaUploadRepo(t *testing.T) {
assert.NoError(t, pulls[0].Issue.LoadDiscussComments())
assert.Len(t, pulls[0].Issue.Comments, 2)
}
func TestGiteaUploadRemapExternalUser(t *testing.T) {
unittest.PrepareTestEnv(t)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
repoName := "migrated"
uploader := NewGiteaLocalUploader(context.Background(), doer, doer.Name, repoName)
uploader.gitServiceType = structs.GiteaService
externalID := int64(1234567)
externalName := "url.or.something"
source := base.Release{
PublisherID: externalID,
PublisherName: externalName,
}
//
// When there is no user linked to the external ID, the migrated data is authored
// by the doer
//
target := models.Release{}
err := uploader.remapExternalUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, doer.ID, target.GetUserID())
//
// Link the external ID to an existing user
//
linkedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
externalLoginUser := &user_model.ExternalLoginUser{
ExternalID: strconv.FormatInt(externalID, 10),
UserID: linkedUser.ID,
LoginSourceID: 0,
Provider: structs.GiteaService.Name(),
}
err = user_model.LinkExternalToUser(linkedUser, externalLoginUser)
assert.NoError(t, err)
//
// When a user is linked to the external ID, it becomes the author of
// the migrated data
//
target = models.Release{}
err = uploader.remapExternalUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, target.GetUserID(), linkedUser.ID)
}