Enable system users search via the API (#28013)
Refs: https://codeberg.org/forgejo/forgejo/issues/1403 (cherry picked from commit dd4d17c159eaf8b642aa9e6105b0532e25972bb7)
This commit is contained in:
		
					parent
					
						
							
								f2ea31de36
							
						
					
				
			
			
				commit
				
					
						340055ab6c
					
				
			
		
					 2 changed files with 48 additions and 12 deletions
				
			
		| 
						 | 
					@ -54,19 +54,33 @@ func Search(ctx *context.APIContext) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	listOptions := utils.GetListOptions(ctx)
 | 
						listOptions := utils.GetListOptions(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
 | 
						uid := ctx.FormInt64("uid")
 | 
				
			||||||
		Actor:       ctx.Doer,
 | 
						var users []*user_model.User
 | 
				
			||||||
		Keyword:     ctx.FormTrim("q"),
 | 
						var maxResults int64
 | 
				
			||||||
		UID:         ctx.FormInt64("uid"),
 | 
						var err error
 | 
				
			||||||
		Type:        user_model.UserTypeIndividual,
 | 
					
 | 
				
			||||||
		ListOptions: listOptions,
 | 
						switch uid {
 | 
				
			||||||
	})
 | 
						case user_model.GhostUserID:
 | 
				
			||||||
	if err != nil {
 | 
							maxResults = 1
 | 
				
			||||||
		ctx.JSON(http.StatusInternalServerError, map[string]any{
 | 
							users = []*user_model.User{user_model.NewGhostUser()}
 | 
				
			||||||
			"ok":    false,
 | 
						case user_model.ActionsUserID:
 | 
				
			||||||
			"error": err.Error(),
 | 
							maxResults = 1
 | 
				
			||||||
 | 
							users = []*user_model.User{user_model.NewActionsUser()}
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							users, maxResults, err = user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
 | 
				
			||||||
 | 
								Actor:       ctx.Doer,
 | 
				
			||||||
 | 
								Keyword:     ctx.FormTrim("q"),
 | 
				
			||||||
 | 
								UID:         uid,
 | 
				
			||||||
 | 
								Type:        user_model.UserTypeIndividual,
 | 
				
			||||||
 | 
								ListOptions: listOptions,
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		return
 | 
							if err != nil {
 | 
				
			||||||
 | 
								ctx.JSON(http.StatusInternalServerError, map[string]any{
 | 
				
			||||||
 | 
									"ok":    false,
 | 
				
			||||||
 | 
									"error": err.Error(),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
 | 
						ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -56,6 +56,28 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAPIUserSearchSystemUsers(t *testing.T) {
 | 
				
			||||||
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
 | 
						for _, systemUser := range []*user_model.User{
 | 
				
			||||||
 | 
							user_model.NewGhostUser(),
 | 
				
			||||||
 | 
							user_model.NewActionsUser(),
 | 
				
			||||||
 | 
						} {
 | 
				
			||||||
 | 
							t.Run(systemUser.Name, func(t *testing.T) {
 | 
				
			||||||
 | 
								req := NewRequestf(t, "GET", "/api/v1/users/search?uid=%d", systemUser.ID)
 | 
				
			||||||
 | 
								resp := MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								var results SearchResults
 | 
				
			||||||
 | 
								DecodeJSON(t, resp, &results)
 | 
				
			||||||
 | 
								assert.NotEmpty(t, results.Data)
 | 
				
			||||||
 | 
								if assert.EqualValues(t, 1, len(results.Data)) {
 | 
				
			||||||
 | 
									user := results.Data[0]
 | 
				
			||||||
 | 
									assert.EqualValues(t, user.UserName, systemUser.Name)
 | 
				
			||||||
 | 
									assert.EqualValues(t, user.ID, systemUser.ID)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
 | 
					func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
 | 
				
			||||||
	defer tests.PrepareTestEnv(t)()
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
	adminUsername := "user1"
 | 
						adminUsername := "user1"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue