From c2346e44696e57c31f9765c32ee60b365a15c8f0 Mon Sep 17 00:00:00 2001 From: Morlinest Date: Tue, 10 Oct 2017 03:23:29 +0200 Subject: 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 --- integrations/api_repo_test.go | 99 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) (limited to 'integrations/api_repo_test.go') diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index 8e383dbda2..c536b8d329 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -5,6 +5,7 @@ package integrations import ( + "fmt" "net/http" "testing" @@ -32,7 +33,7 @@ func TestAPIUserReposNotLogin(t *testing.T) { } } -func TestAPISearchRepoNotLogin(t *testing.T) { +func TestAPISearchRepo(t *testing.T) { prepareTestEnv(t) const keyword = "test" @@ -46,6 +47,102 @@ func TestAPISearchRepoNotLogin(t *testing.T) { assert.Contains(t, repo.Name, keyword) assert.False(t, repo.Private) } + + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User) + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User) + orgUser := models.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User) + + // Map of expected results, where key is user for login + type expectedResults map[*models.User]struct { + count int + repoOwnerID int64 + repoName string + includesPrivate bool + } + + testCases := []struct { + name, requestURL string + expectedResults + }{ + {name: "RepositoriesMax50", requestURL: "/api/v1/repos/search?limit=50", expectedResults: expectedResults{ + nil: {count: 12}, + user: {count: 12}, + user2: {count: 12}}, + }, + {name: "RepositoriesMax10", requestURL: "/api/v1/repos/search?limit=10", expectedResults: expectedResults{ + nil: {count: 10}, + user: {count: 10}, + user2: {count: 10}}, + }, + {name: "RepositoriesDefaultMax10", requestURL: "/api/v1/repos/search", expectedResults: expectedResults{ + nil: {count: 10}, + user: {count: 10}, + user2: {count: 10}}, + }, + {name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/v1/repos/search?q=%s", "big_test_"), expectedResults: expectedResults{ + nil: {count: 4, repoName: "big_test_"}, + user: {count: 4, repoName: "big_test_"}, + user2: {count: 4, repoName: "big_test_"}}, + }, + {name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user.ID), expectedResults: expectedResults{ + // FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user + nil: {count: 2}, + user: {count: 8, includesPrivate: true}, + // FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user + user2: {count: 2}}, + }, + {name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{ + nil: {count: 1}, + user: {count: 1}, + user2: {count: 2, includesPrivate: true}}, + }, + {name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{ + nil: {count: 1, repoOwnerID: orgUser.ID}, + user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true}, + user2: {count: 1, repoOwnerID: orgUser.ID}}, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + for userToLogin, expected := range testCase.expectedResults { + var session *TestSession + var testName string + if userToLogin != nil && userToLogin.ID > 0 { + testName = fmt.Sprintf("LoggedUser%d", userToLogin.ID) + session = loginUser(t, userToLogin.Name) + } else { + testName = "AnonymousUser" + session = emptyTestSession(t) + } + + t.Run(testName, func(t *testing.T) { + request := NewRequest(t, "GET", testCase.requestURL) + response := session.MakeRequest(t, request, http.StatusOK) + + var body api.SearchResults + DecodeJSON(t, response, &body) + + assert.Len(t, body.Data, expected.count) + for _, repo := range body.Data { + assert.NotEmpty(t, repo.Name) + + if len(expected.repoName) > 0 { + assert.Contains(t, repo.Name, expected.repoName) + } + + if expected.repoOwnerID > 0 { + assert.Equal(t, expected.repoOwnerID, repo.Owner.ID) + } + + if !expected.includesPrivate { + assert.False(t, repo.Private) + } + } + }) + } + }) + } } func TestAPIViewRepo(t *testing.T) { -- cgit v1.2.3