Skip to content

Commit c2346e4

Browse files
Morlinestlunny
authored andcommitted
Add repository search unit and integration tests (#2575)
* Add more repo search tests * Fix repo search tests * Always test returned repos length * Add test with lower pagesize limit (test more pages) * Add and fix /api/repo/search integration tests * Simplify unit tests code * Simplify and unify integration tests code * Improve test coverage * Temporary fix tests due to bugs in current repo search implementation * Revert removing not nil Searcher * Add more checks to tests * Simplify privacy checks in /api/repo tests * Temporary remove privacy check from repo search tests
1 parent 0cef8ce commit c2346e4

File tree

2 files changed

+174
-7
lines changed

2 files changed

+174
-7
lines changed

integrations/api_repo_test.go

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integrations
66

77
import (
8+
"fmt"
89
"net/http"
910
"testing"
1011

@@ -32,7 +33,7 @@ func TestAPIUserReposNotLogin(t *testing.T) {
3233
}
3334
}
3435

35-
func TestAPISearchRepoNotLogin(t *testing.T) {
36+
func TestAPISearchRepo(t *testing.T) {
3637
prepareTestEnv(t)
3738
const keyword = "test"
3839

@@ -46,6 +47,102 @@ func TestAPISearchRepoNotLogin(t *testing.T) {
4647
assert.Contains(t, repo.Name, keyword)
4748
assert.False(t, repo.Private)
4849
}
50+
51+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User)
52+
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User)
53+
orgUser := models.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User)
54+
55+
// Map of expected results, where key is user for login
56+
type expectedResults map[*models.User]struct {
57+
count int
58+
repoOwnerID int64
59+
repoName string
60+
includesPrivate bool
61+
}
62+
63+
testCases := []struct {
64+
name, requestURL string
65+
expectedResults
66+
}{
67+
{name: "RepositoriesMax50", requestURL: "/api/v1/repos/search?limit=50", expectedResults: expectedResults{
68+
nil: {count: 12},
69+
user: {count: 12},
70+
user2: {count: 12}},
71+
},
72+
{name: "RepositoriesMax10", requestURL: "/api/v1/repos/search?limit=10", expectedResults: expectedResults{
73+
nil: {count: 10},
74+
user: {count: 10},
75+
user2: {count: 10}},
76+
},
77+
{name: "RepositoriesDefaultMax10", requestURL: "/api/v1/repos/search", expectedResults: expectedResults{
78+
nil: {count: 10},
79+
user: {count: 10},
80+
user2: {count: 10}},
81+
},
82+
{name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/v1/repos/search?q=%s", "big_test_"), expectedResults: expectedResults{
83+
nil: {count: 4, repoName: "big_test_"},
84+
user: {count: 4, repoName: "big_test_"},
85+
user2: {count: 4, repoName: "big_test_"}},
86+
},
87+
{name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
88+
// FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user
89+
nil: {count: 2},
90+
user: {count: 8, includesPrivate: true},
91+
// FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user
92+
user2: {count: 2}},
93+
},
94+
{name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
95+
nil: {count: 1},
96+
user: {count: 1},
97+
user2: {count: 2, includesPrivate: true}},
98+
},
99+
{name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
100+
nil: {count: 1, repoOwnerID: orgUser.ID},
101+
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
102+
user2: {count: 1, repoOwnerID: orgUser.ID}},
103+
},
104+
}
105+
106+
for _, testCase := range testCases {
107+
t.Run(testCase.name, func(t *testing.T) {
108+
for userToLogin, expected := range testCase.expectedResults {
109+
var session *TestSession
110+
var testName string
111+
if userToLogin != nil && userToLogin.ID > 0 {
112+
testName = fmt.Sprintf("LoggedUser%d", userToLogin.ID)
113+
session = loginUser(t, userToLogin.Name)
114+
} else {
115+
testName = "AnonymousUser"
116+
session = emptyTestSession(t)
117+
}
118+
119+
t.Run(testName, func(t *testing.T) {
120+
request := NewRequest(t, "GET", testCase.requestURL)
121+
response := session.MakeRequest(t, request, http.StatusOK)
122+
123+
var body api.SearchResults
124+
DecodeJSON(t, response, &body)
125+
126+
assert.Len(t, body.Data, expected.count)
127+
for _, repo := range body.Data {
128+
assert.NotEmpty(t, repo.Name)
129+
130+
if len(expected.repoName) > 0 {
131+
assert.Contains(t, repo.Name, expected.repoName)
132+
}
133+
134+
if expected.repoOwnerID > 0 {
135+
assert.Equal(t, expected.repoOwnerID, repo.Owner.ID)
136+
}
137+
138+
if !expected.includesPrivate {
139+
assert.False(t, repo.Private)
140+
}
141+
}
142+
})
143+
}
144+
})
145+
}
49146
}
50147

51148
func TestAPIViewRepo(t *testing.T) {

models/repo_list_test.go

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ func TestSearchRepositoryByName(t *testing.T) {
1818
Keyword: "repo_12",
1919
Page: 1,
2020
PageSize: 10,
21-
Searcher: nil,
2221
})
2322

24-
assert.NotNil(t, repos)
2523
assert.NoError(t, err)
2624
if assert.Len(t, repos, 1) {
2725
assert.Equal(t, "test_repo_12", repos[0].Name)
@@ -32,12 +30,11 @@ func TestSearchRepositoryByName(t *testing.T) {
3230
Keyword: "test_repo",
3331
Page: 1,
3432
PageSize: 10,
35-
Searcher: nil,
3633
})
3734

38-
assert.NotNil(t, repos)
3935
assert.NoError(t, err)
4036
assert.Equal(t, int64(2), count)
37+
assert.Len(t, repos, 2)
4138

4239
// test search private repository on explore page
4340
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
@@ -48,7 +45,6 @@ func TestSearchRepositoryByName(t *testing.T) {
4845
Searcher: &User{ID: 14},
4946
})
5047

51-
assert.NotNil(t, repos)
5248
assert.NoError(t, err)
5349
if assert.Len(t, repos, 1) {
5450
assert.Equal(t, "test_repo_13", repos[0].Name)
@@ -63,7 +59,81 @@ func TestSearchRepositoryByName(t *testing.T) {
6359
Searcher: &User{ID: 14},
6460
})
6561

66-
assert.NotNil(t, repos)
6762
assert.NoError(t, err)
6863
assert.Equal(t, int64(3), count)
64+
assert.Len(t, repos, 3)
65+
66+
testCases := []struct {
67+
name string
68+
opts *SearchRepoOptions
69+
count int
70+
}{
71+
{name: "PublicRepositoriesByName",
72+
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10},
73+
count: 4},
74+
{name: "PublicAndPrivateRepositoriesByName",
75+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true},
76+
count: 8},
77+
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
78+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true},
79+
count: 8},
80+
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
81+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true},
82+
count: 8},
83+
{name: "PublicRepositoriesOfUser",
84+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
85+
count: 3}, // FIXME: Should return 2 (only directly owned repositories), now includes 1 public repository from owned organization
86+
{name: "PublicAndPrivateRepositoriesOfUser",
87+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
88+
count: 6}, // FIXME: Should return 4 (only directly owned repositories), now includes 2 repositories from owned organization
89+
{name: "PublicRepositoriesOfUserIncludingCollaborative",
90+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true},
91+
count: 4},
92+
{name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
93+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true},
94+
count: 8},
95+
{name: "PublicRepositoriesOfOrganization",
96+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17},
97+
count: 1},
98+
{name: "PublicAndPrivateRepositoriesOfOrganization",
99+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
100+
count: 2},
101+
}
102+
103+
for _, testCase := range testCases {
104+
t.Run(testCase.name, func(t *testing.T) {
105+
if testCase.opts.OwnerID > 0 {
106+
testCase.opts.Searcher = &User{ID: testCase.opts.OwnerID}
107+
}
108+
repos, count, err := SearchRepositoryByName(testCase.opts)
109+
110+
assert.NoError(t, err)
111+
assert.Equal(t, int64(testCase.count), count)
112+
113+
var expectedLen int
114+
if testCase.opts.PageSize*testCase.opts.Page > testCase.count {
115+
expectedLen = testCase.count % testCase.opts.PageSize
116+
} else {
117+
expectedLen = testCase.opts.PageSize
118+
}
119+
assert.Len(t, repos, expectedLen)
120+
121+
for _, repo := range repos {
122+
assert.NotEmpty(t, repo.Name)
123+
124+
if len(testCase.opts.Keyword) > 0 {
125+
assert.Contains(t, repo.Name, testCase.opts.Keyword)
126+
}
127+
128+
// FIXME: Can't check, need to fix current behaviour (see previous FIXME comments in test cases)
129+
/*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate {
130+
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
131+
}*/
132+
133+
if !testCase.opts.Private {
134+
assert.False(t, repo.IsPrivate)
135+
}
136+
}
137+
})
138+
}
69139
}

0 commit comments

Comments
 (0)