mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 05:36:19 +01:00
a666829a37
* Move user related model into models/user * Fix lint for windows * Fix windows lint * Fix windows lint * Move some tests in models * Merge
255 lines
7.2 KiB
Go
255 lines
7.2 KiB
Go
// Copyright 2017 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 (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"fmt"
|
|
"image"
|
|
"image/png"
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unit"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/markup"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMetas(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := &Repository{Name: "testRepo"}
|
|
repo.Owner = &user_model.User{Name: "testOwner"}
|
|
repo.OwnerName = repo.Owner.Name
|
|
|
|
repo.Units = nil
|
|
|
|
metas := repo.ComposeMetas()
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
|
|
externalTracker := RepoUnit{
|
|
Type: unit.TypeExternalTracker,
|
|
Config: &ExternalTrackerConfig{
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
},
|
|
}
|
|
|
|
testSuccess := func(expectedStyle string) {
|
|
repo.Units = []*RepoUnit{&externalTracker}
|
|
repo.RenderingMetas = nil
|
|
metas := repo.ComposeMetas()
|
|
assert.Equal(t, expectedStyle, metas["style"])
|
|
assert.Equal(t, "testRepo", metas["repo"])
|
|
assert.Equal(t, "testOwner", metas["user"])
|
|
assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
|
|
}
|
|
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
|
|
testSuccess(markup.IssueNameStyleAlphanumeric)
|
|
|
|
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
|
testSuccess(markup.IssueNameStyleNumeric)
|
|
|
|
repo, err := GetRepositoryByID(3)
|
|
assert.NoError(t, err)
|
|
|
|
metas = repo.ComposeMetas()
|
|
assert.Contains(t, metas, "org")
|
|
assert.Contains(t, metas, "teams")
|
|
assert.Equal(t, "user3", metas["org"])
|
|
assert.Equal(t, ",owners,team1,", metas["teams"])
|
|
}
|
|
|
|
func TestGetRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
count, err1 := GetRepositoryCount(db.DefaultContext, 10)
|
|
privateCount, err2 := GetPrivateRepositoryCount(&user_model.User{ID: int64(10)})
|
|
publicCount, err3 := GetPublicRepositoryCount(&user_model.User{ID: int64(10)})
|
|
assert.NoError(t, err1)
|
|
assert.NoError(t, err2)
|
|
assert.NoError(t, err3)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Equal(t, privateCount+publicCount, count)
|
|
}
|
|
|
|
func TestGetPublicRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
count, err := GetPublicRepositoryCount(&user_model.User{ID: int64(10)})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(1), count)
|
|
}
|
|
|
|
func TestGetPrivateRepositoryCount(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
count, err := GetPrivateRepositoryCount(&user_model.User{ID: int64(10)})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
}
|
|
|
|
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// Get sample repo and change visibility
|
|
repo, err := GetRepositoryByID(9)
|
|
assert.NoError(t, err)
|
|
repo.IsPrivate = true
|
|
|
|
// Update it
|
|
err = UpdateRepository(repo, true)
|
|
assert.NoError(t, err)
|
|
|
|
// Check visibility of action has become private
|
|
act := Action{}
|
|
_, err = db.GetEngine(db.DefaultContext).ID(3).Get(&act)
|
|
|
|
assert.NoError(t, err)
|
|
assert.True(t, act.IsPrivate)
|
|
}
|
|
|
|
func TestGetUserFork(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// User13 has repo 11 forked from repo10
|
|
repo, err := GetRepositoryByID(10)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, repo)
|
|
repo, err = repo.GetUserFork(13)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, repo)
|
|
|
|
repo, err = GetRepositoryByID(9)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, repo)
|
|
repo, err = repo.GetUserFork(13)
|
|
assert.NoError(t, err)
|
|
assert.Nil(t, repo)
|
|
}
|
|
|
|
func TestRepoAPIURL(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
|
|
|
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
|
|
}
|
|
|
|
func TestUploadAvatar(t *testing.T) {
|
|
// Generate image
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
var buff bytes.Buffer
|
|
png.Encode(&buff, myImage)
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
|
|
}
|
|
|
|
func TestUploadBigAvatar(t *testing.T) {
|
|
// Generate BIG image
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 5000, 1))
|
|
var buff bytes.Buffer
|
|
png.Encode(&buff, myImage)
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestDeleteAvatar(t *testing.T) {
|
|
// Generate image
|
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
var buff bytes.Buffer
|
|
png.Encode(&buff, myImage)
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
|
|
|
|
err := repo.UploadAvatar(buff.Bytes())
|
|
assert.NoError(t, err)
|
|
|
|
err = repo.DeleteAvatar()
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", repo.Avatar)
|
|
}
|
|
|
|
func TestDoctorUserStarNum(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
assert.NoError(t, DoctorUserStarNum())
|
|
}
|
|
|
|
func TestRepoGetReviewers(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// test public repo
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
reviewers, err := repo1.GetReviewers(2, 2)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, reviewers, 4)
|
|
|
|
// test private repo
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
|
reviewers, err = repo2.GetReviewers(2, 2)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, reviewers)
|
|
}
|
|
|
|
func TestRepoGetReviewerTeams(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
|
teams, err := repo2.GetReviewerTeams()
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, teams)
|
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
|
teams, err = repo3.GetReviewerTeams()
|
|
assert.NoError(t, err)
|
|
assert.Len(t, teams, 2)
|
|
}
|
|
|
|
func TestLinkedRepository(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
testCases := []struct {
|
|
name string
|
|
attachID int64
|
|
expectedRepo *Repository
|
|
expectedUnitType unit.Type
|
|
}{
|
|
{"LinkedIssue", 1, &Repository{ID: 1}, unit.TypeIssues},
|
|
{"LinkedComment", 3, &Repository{ID: 1}, unit.TypePullRequests},
|
|
{"LinkedRelease", 9, &Repository{ID: 1}, unit.TypeReleases},
|
|
{"Notlinked", 10, nil, -1},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
attach, err := repo_model.GetAttachmentByID(tc.attachID)
|
|
assert.NoError(t, err)
|
|
repo, unitType, err := LinkedRepository(attach)
|
|
assert.NoError(t, err)
|
|
if tc.expectedRepo != nil {
|
|
assert.Equal(t, tc.expectedRepo.ID, repo.ID)
|
|
}
|
|
assert.Equal(t, tc.expectedUnitType, unitType)
|
|
})
|
|
}
|
|
}
|