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.

61 lines
1.7 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. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/setting"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. type SearchResults struct {
  15. OK bool `json:"ok"`
  16. Data []*api.User `json:"data"`
  17. }
  18. func TestAPIUserSearchLoggedIn(t *testing.T) {
  19. defer prepareTestEnv(t)()
  20. adminUsername := "user1"
  21. session := loginUser(t, adminUsername)
  22. token := getTokenForLoggedInUser(t, session)
  23. query := "user2"
  24. req := NewRequestf(t, "GET", "/api/v1/users/search?token=%s&q=%s", token, query)
  25. resp := session.MakeRequest(t, req, http.StatusOK)
  26. var results SearchResults
  27. DecodeJSON(t, resp, &results)
  28. assert.NotEmpty(t, results.Data)
  29. for _, user := range results.Data {
  30. assert.Contains(t, user.UserName, query)
  31. assert.NotEmpty(t, user.Email)
  32. }
  33. }
  34. func TestAPIUserSearchNotLoggedIn(t *testing.T) {
  35. defer prepareTestEnv(t)()
  36. query := "user2"
  37. req := NewRequestf(t, "GET", "/api/v1/users/search?q=%s", query)
  38. resp := MakeRequest(t, req, http.StatusOK)
  39. var results SearchResults
  40. DecodeJSON(t, resp, &results)
  41. assert.NotEmpty(t, results.Data)
  42. var modelUser *models.User
  43. for _, user := range results.Data {
  44. assert.Contains(t, user.UserName, query)
  45. modelUser = models.AssertExistsAndLoadBean(t, &models.User{ID: user.ID}).(*models.User)
  46. if modelUser.KeepEmailPrivate {
  47. assert.EqualValues(t, fmt.Sprintf("%s@%s", modelUser.LowerName, setting.Service.NoReplyAddress), user.Email)
  48. } else {
  49. assert.EqualValues(t, modelUser.Email, user.Email)
  50. }
  51. }
  52. }