Skip to content

Commit

Permalink
Sytest: Add 50federation / 02server-names & 10query-profile rem…
Browse files Browse the repository at this point in the history
…aining tests (#508)

* add DoFederationRequest and 2 sytests

* revert removing 2xx to log line
  • Loading branch information
ShadowJonathan authored Oct 27, 2022
1 parent b8d0a7f commit 73415ae
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/federation/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ func (s *Server) SendFederationRequest(
return err
}

// DoFederationRequest signs and sends an arbitrary federation request from this server, and returns the response.
//
// The requests will be routed according to the deployment map in `deployment`.
func (s *Server) DoFederationRequest(
ctx context.Context,
t *testing.T,
deployment *docker.Deployment,
req gomatrixserverlib.FederationRequest) (*http.Response, error) {
if err := req.Sign(gomatrixserverlib.ServerName(s.serverName), s.KeyID, s.Priv); err != nil {
return nil, err
}

httpReq, err := req.HTTPRequest()
if err != nil {
return nil, err
}

httpClient := gomatrixserverlib.NewClient(gomatrixserverlib.WithTransport(&docker.RoundTripper{Deployment: deployment}))
start := time.Now()

var resp *http.Response
resp, err = httpClient.DoHTTPRequest(ctx, httpReq)

if httpError, ok := err.(gomatrix.HTTPError); ok {
t.Logf("[SSAPI] %s %s%s => error(%d): %s (%s)", req.Method(), req.Destination(), req.RequestURI(), httpError.Code, err, time.Since(start))
} else if err == nil {
t.Logf("[SSAPI] %s %s%s => %d (%s)", req.Method(), req.Destination(), req.RequestURI(), resp.StatusCode, time.Since(start))
}
return resp, err
}

// MustCreateEvent will create and sign a new latest event for the given room.
// It does not insert this event into the room however. See ServerRoom.AddEvent for that.
func (s *Server) MustCreateEvent(t *testing.T, room *ServerRoom, ev b.Event) *gomatrixserverlib.Event {
Expand Down
69 changes: 69 additions & 0 deletions tests/federation_query_profile_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package tests

import (
"context"
"encoding/json"
"net/http"
"testing"

"github.com/matrix-org/gomatrixserverlib"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/federation"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
Expand Down Expand Up @@ -63,3 +67,68 @@ func TestOutboundFederationProfile(t *testing.T) {
})
})
}

func TestInboundFederationProfile(t *testing.T) {
deployment := Deploy(t, b.BlueprintAlice)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")

srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
)
cancel := srv.Listen()
defer cancel()

// sytest: Non-numeric ports in server names are rejected
t.Run("Non-numeric ports in server names are rejected", func(t *testing.T) {
fedReq := gomatrixserverlib.NewFederationRequest(
"GET",
"hs1",
"/_matrix/federation/v1/query/profile"+
"?user_id=@user1:localhost:http"+
"&field=displayname",
)

resp, err := srv.DoFederationRequest(context.Background(), t, deployment, fedReq)

must.NotError(t, "failed to GET /profile", err)

must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 400,
})
})

// sytest: Inbound federation can query profile data
t.Run("Inbound federation can query profile data", func(t *testing.T) {
const alicePublicName = "Alice Cooper"

alice.MustDoFunc(
t,
"PUT",
[]string{"_matrix", "client", "v3", "profile", alice.UserID, "displayname"},
client.WithJSONBody(t, map[string]interface{}{
"displayname": alicePublicName,
}),
)

fedReq := gomatrixserverlib.NewFederationRequest(
"GET",
"hs1",
"/_matrix/federation/v1/query/profile"+
"?user_id=@alice:hs1"+
"&field=displayname",
)

resp, err := srv.DoFederationRequest(context.Background(), t, deployment, fedReq)

must.NotError(t, "failed to GET /profile", err)

must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
JSON: []match.JSON{
match.JSONKeyEqual("displayname", alicePublicName),
},
})
})
}

0 comments on commit 73415ae

Please sign in to comment.