You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.package models
  4. package integrations
  5. import (
  6. "net/http"
  7. "testing"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type SearchResults struct {
  12. OK bool `json:"ok"`
  13. Data []*api.User `json:"data"`
  14. }
  15. func TestAPIUserSearchLoggedIn(t *testing.T) {
  16. prepareTestEnv(t)
  17. adminUsername := "user1"
  18. session := loginUser(t, adminUsername)
  19. token := getTokenForLoggedInUser(t, session)
  20. query := "user2"
  21. req := NewRequestf(t, "GET", "/api/v1/users/search?token=%s&q=%s", token, query)
  22. resp := session.MakeRequest(t, req, http.StatusOK)
  23. var results SearchResults
  24. DecodeJSON(t, resp, &results)
  25. assert.NotEmpty(t, results.Data)
  26. for _, user := range results.Data {
  27. assert.Contains(t, user.UserName, query)
  28. assert.NotEmpty(t, user.Email)
  29. }
  30. }
  31. func TestAPIUserSearchNotLoggedIn(t *testing.T) {
  32. prepareTestEnv(t)
  33. query := "user2"
  34. req := NewRequestf(t, "GET", "/api/v1/users/search?q=%s", query)
  35. resp := MakeRequest(t, req, http.StatusOK)
  36. var results SearchResults
  37. DecodeJSON(t, resp, &results)
  38. assert.NotEmpty(t, results.Data)
  39. for _, user := range results.Data {
  40. assert.Contains(t, user.UserName, query)
  41. assert.Empty(t, user.Email)
  42. }
  43. }