code.gitea.io/gitea@v1.21.7/tests/integration/webfinger_test.go (about)

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