code.gitea.io/gitea@v1.21.7/services/repository/adopt_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"os"
     8  	"path"
     9  	"testing"
    10  
    11  	"code.gitea.io/gitea/models/db"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	"code.gitea.io/gitea/modules/setting"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestCheckUnadoptedRepositories_Add(t *testing.T) {
    19  	start := 10
    20  	end := 20
    21  	unadopted := &unadoptedRepositories{
    22  		start: start,
    23  		end:   end,
    24  		index: 0,
    25  	}
    26  
    27  	total := 30
    28  	for i := 0; i < total; i++ {
    29  		unadopted.add("something")
    30  	}
    31  
    32  	assert.Equal(t, total, unadopted.index)
    33  	assert.Len(t, unadopted.repositories, end-start)
    34  }
    35  
    36  func TestCheckUnadoptedRepositories(t *testing.T) {
    37  	assert.NoError(t, unittest.PrepareTestDatabase())
    38  	//
    39  	// Non existent user
    40  	//
    41  	unadopted := &unadoptedRepositories{start: 0, end: 100}
    42  	err := checkUnadoptedRepositories(db.DefaultContext, "notauser", []string{"repo"}, unadopted)
    43  	assert.NoError(t, err)
    44  	assert.Empty(t, unadopted.repositories)
    45  	//
    46  	// Unadopted repository is returned
    47  	// Existing (adopted) repository is not returned
    48  	//
    49  	userName := "user2"
    50  	repoName := "repo2"
    51  	unadoptedRepoName := "unadopted"
    52  	unadopted = &unadoptedRepositories{start: 0, end: 100}
    53  	err = checkUnadoptedRepositories(db.DefaultContext, userName, []string{repoName, unadoptedRepoName}, unadopted)
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, []string{path.Join(userName, unadoptedRepoName)}, unadopted.repositories)
    56  	//
    57  	// Existing (adopted) repository is not returned
    58  	//
    59  	unadopted = &unadoptedRepositories{start: 0, end: 100}
    60  	err = checkUnadoptedRepositories(db.DefaultContext, userName, []string{repoName}, unadopted)
    61  	assert.NoError(t, err)
    62  	assert.Empty(t, unadopted.repositories)
    63  	assert.Equal(t, 0, unadopted.index)
    64  }
    65  
    66  func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
    67  	assert.NoError(t, unittest.PrepareTestDatabase())
    68  	username := "user2"
    69  	unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")}
    70  	for _, unadopted := range unadoptedList {
    71  		_ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
    72  	}
    73  
    74  	opts := db.ListOptions{Page: 1, PageSize: 1}
    75  	repoNames, count, err := ListUnadoptedRepositories(db.DefaultContext, "", &opts)
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, 2, count)
    78  	assert.Equal(t, unadoptedList[0], repoNames[0])
    79  
    80  	opts = db.ListOptions{Page: 2, PageSize: 1}
    81  	repoNames, count, err = ListUnadoptedRepositories(db.DefaultContext, "", &opts)
    82  	assert.NoError(t, err)
    83  	assert.Equal(t, 2, count)
    84  	assert.Equal(t, unadoptedList[1], repoNames[0])
    85  }