2020-06-15 08:54:11 +00:00
|
|
|
package userapi_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-08-10 13:18:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-06-15 08:54:11 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2020-06-15 15:57:59 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/test"
|
2020-06-15 08:54:11 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/inthttp"
|
2020-06-17 11:05:56 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/accounts"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/devices"
|
2020-06-15 08:54:11 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serverName = gomatrixserverlib.ServerName("example.com")
|
|
|
|
)
|
|
|
|
|
|
|
|
func MustMakeInternalAPI(t *testing.T) (api.UserInternalAPI, accounts.Database, devices.Database) {
|
2020-08-10 13:18:04 +00:00
|
|
|
accountDB, err := accounts.NewDatabase(&config.DatabaseOptions{
|
|
|
|
ConnectionString: "file::memory:",
|
|
|
|
}, serverName)
|
2020-06-15 08:54:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create account DB: %s", err)
|
|
|
|
}
|
2020-08-10 13:18:04 +00:00
|
|
|
deviceDB, err := devices.NewDatabase(&config.DatabaseOptions{
|
|
|
|
ConnectionString: "file::memory:",
|
|
|
|
}, serverName)
|
2020-06-15 08:54:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create device DB: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-07-30 17:00:56 +00:00
|
|
|
return userapi.NewInternalAPI(accountDB, deviceDB, serverName, nil, nil), accountDB, deviceDB
|
2020-06-15 08:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryProfile(t *testing.T) {
|
|
|
|
aliceAvatarURL := "mxc://example.com/alice"
|
|
|
|
aliceDisplayName := "Alice"
|
|
|
|
userAPI, accountDB, _ := MustMakeInternalAPI(t)
|
|
|
|
_, err := accountDB.CreateAccount(context.TODO(), "alice", "foobar", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to make account: %s", err)
|
|
|
|
}
|
|
|
|
if err := accountDB.SetAvatarURL(context.TODO(), "alice", aliceAvatarURL); err != nil {
|
|
|
|
t.Fatalf("failed to set avatar url: %s", err)
|
|
|
|
}
|
|
|
|
if err := accountDB.SetDisplayName(context.TODO(), "alice", aliceDisplayName); err != nil {
|
|
|
|
t.Fatalf("failed to set display name: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
req api.QueryProfileRequest
|
|
|
|
wantRes api.QueryProfileResponse
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
req: api.QueryProfileRequest{
|
|
|
|
UserID: fmt.Sprintf("@alice:%s", serverName),
|
|
|
|
},
|
|
|
|
wantRes: api.QueryProfileResponse{
|
|
|
|
UserExists: true,
|
|
|
|
AvatarURL: aliceAvatarURL,
|
|
|
|
DisplayName: aliceDisplayName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: api.QueryProfileRequest{
|
|
|
|
UserID: fmt.Sprintf("@bob:%s", serverName),
|
|
|
|
},
|
|
|
|
wantRes: api.QueryProfileResponse{
|
|
|
|
UserExists: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: api.QueryProfileRequest{
|
|
|
|
UserID: "@alice:wrongdomain.com",
|
|
|
|
},
|
|
|
|
wantErr: fmt.Errorf("wrong domain"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runCases := func(testAPI api.UserInternalAPI) {
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var gotRes api.QueryProfileResponse
|
|
|
|
gotErr := testAPI.QueryProfile(context.TODO(), &tc.req, &gotRes)
|
|
|
|
if tc.wantErr == nil && gotErr != nil || tc.wantErr != nil && gotErr == nil {
|
|
|
|
t.Errorf("QueryProfile error, got %s want %s", gotErr, tc.wantErr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tc.wantRes, gotRes) {
|
|
|
|
t.Errorf("QueryProfile response got %+v want %+v", gotRes, tc.wantRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("HTTP API", func(t *testing.T) {
|
|
|
|
router := mux.NewRouter().PathPrefix(httputil.InternalPathPrefix).Subrouter()
|
|
|
|
userapi.AddInternalRoutes(router, userAPI)
|
2020-06-15 15:57:59 +00:00
|
|
|
apiURL, cancel := test.ListenAndServe(t, router, false)
|
2020-06-15 08:54:11 +00:00
|
|
|
defer cancel()
|
|
|
|
httpAPI, err := inthttp.NewUserAPIClient(apiURL, &http.Client{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create HTTP client")
|
|
|
|
}
|
|
|
|
runCases(httpAPI)
|
|
|
|
})
|
|
|
|
t.Run("Monolith", func(t *testing.T) {
|
|
|
|
runCases(userAPI)
|
|
|
|
})
|
|
|
|
}
|