Merge pull request #1554 from ywk253100/170309_bug_fix

bug fix in search API: do not return duplicate projects when user does not login
This commit is contained in:
Daniel Jiang 2017-03-09 20:20:19 +08:00 committed by GitHub
commit 6eb2912aad
3 changed files with 37 additions and 19 deletions

View File

@ -171,7 +171,7 @@ func SearchProjects(userID int) ([]models.Project, error) {
sql :=
`select distinct p.project_id, p.name, p.public,
p.owner_id, p.creation_time, p.update_time, pm.role role
p.owner_id, p.creation_time, p.update_time
from project p
left join project_member pm
on p.project_id = pm.project_id

View File

@ -73,15 +73,22 @@ func (s *SearchAPI) Get() {
sort.Sort(projectSorter)
projectResult := []models.Project{}
for _, p := range projects {
match := true
if len(keyword) > 0 && !strings.Contains(p.Name, keyword) {
match = false
continue
}
if match {
if userID != dao.NonExistUserID {
if isSysAdmin {
p.Role = models.PROJECTADMIN
} else {
roles, err := dao.GetUserProjectRoles(userID, p.ProjectID)
if err != nil {
log.Errorf("failed to get user's project role: %v", err)
s.CustomAbort(http.StatusInternalServerError, "")
}
p.Role = roles[0].RoleID
}
if p.Role == models.PROJECTADMIN {
p.Togglable = true
}
@ -97,7 +104,6 @@ func (s *SearchAPI) Get() {
projectResult = append(projectResult, p)
}
}
repositoryResult, err := filterRepositories(projects, keyword)
if err != nil {

View File

@ -28,7 +28,19 @@ func TestSearch(t *testing.T) {
}
//--------case 2 : Response Code = 200, sysAdmin and search repo--------//
httpStatusCode, result, err = apiTest.SearchGet("docker", *admin)
httpStatusCode, result, err = apiTest.SearchGet("library", *admin)
if err != nil {
t.Error("Error while search project or repository", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
assert.Equal("library", result.Repositories[0].ProjectName, "Project name should be library")
assert.Equal("library/docker", result.Repositories[0].RepositoryName, "Repository name should be library/docker")
assert.Equal(int32(1), result.Repositories[0].ProjectPublic, "Project public status should be 1 (true)")
}
//--------case 3 : Response Code = 200, normal user and search repo--------//
httpStatusCode, result, err = apiTest.SearchGet("library", *testUser)
if err != nil {
t.Error("Error while search project or repository", err.Error())
t.Log(err)