gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "testing"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/test"
  14. "code.gitea.io/gitea/tests"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestWebfinger(t *testing.T) {
  18. defer tests.PrepareTestEnv(t)()
  19. defer test.MockVariableValue(&setting.Federation.Enabled, true)()
  20. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  21. appURL, _ := url.Parse(setting.AppURL)
  22. type webfingerLink struct {
  23. Rel string `json:"rel,omitempty"`
  24. Type string `json:"type,omitempty"`
  25. Href string `json:"href,omitempty"`
  26. Titles map[string]string `json:"titles,omitempty"`
  27. Properties map[string]any `json:"properties,omitempty"`
  28. }
  29. type webfingerJRD struct {
  30. Subject string `json:"subject,omitempty"`
  31. Aliases []string `json:"aliases,omitempty"`
  32. Properties map[string]any `json:"properties,omitempty"`
  33. Links []*webfingerLink `json:"links,omitempty"`
  34. }
  35. session := loginUser(t, "user1")
  36. req := NewRequest(t, "GET", fmt.Sprintf("/.well-known/webfinger?resource=acct:%s@%s", user.LowerName, appURL.Host))
  37. resp := MakeRequest(t, req, http.StatusOK)
  38. var jrd webfingerJRD
  39. DecodeJSON(t, resp, &jrd)
  40. assert.Equal(t, "acct:user2@"+appURL.Host, jrd.Subject)
  41. assert.ElementsMatch(t, []string{user.HTMLURL(t.Context()), appURL.String() + "api/v1/activitypub/user-id/" + strconv.FormatInt(user.ID, 10)}, jrd.Aliases)
  42. req = NewRequest(t, "GET", fmt.Sprintf("/.well-known/webfinger?resource=acct:%s@%s", user.LowerName, "unknown.host"))
  43. MakeRequest(t, req, http.StatusBadRequest)
  44. req = NewRequest(t, "GET", fmt.Sprintf("/.well-known/webfinger?resource=acct:%s@%s", "user31", appURL.Host))
  45. MakeRequest(t, req, http.StatusNotFound)
  46. req = NewRequest(t, "GET", fmt.Sprintf("/.well-known/webfinger?resource=acct:%s@%s", "user31", appURL.Host))
  47. session.MakeRequest(t, req, http.StatusOK)
  48. req = NewRequest(t, "GET", "/.well-known/webfinger?resource=mailto:"+user.Email)
  49. MakeRequest(t, req, http.StatusNotFound)
  50. }