Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sijmen Schoon <me@sijman.nl>
  • Loading branch information
vijfhoek committed Jan 10, 2023
1 parent 59a31ff commit ba0f797
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 22 deletions.
6 changes: 5 additions & 1 deletion clientapi/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func LoginFromJSONReader(
case authtypes.LoginTypeApplicationService:
token, err := ExtractAccessToken(req)
if err != nil {
token = ""
err := &util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.MissingToken(err.Error()),
}
return nil, nil, err
}

typ = &LoginTypeApplicationService{
Expand Down
141 changes: 121 additions & 20 deletions clientapi/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"net/http"
"reflect"
"regexp"
"strings"
"testing"

Expand All @@ -29,12 +30,49 @@ import (
"github.com/matrix-org/util"
)

var cfg = &config.ClientAPI{
Matrix: &config.Global{
SigningIdentity: gomatrixserverlib.SigningIdentity{
ServerName: serverName,
},
},
Derived: &config.Derived{
ApplicationServices: []config.ApplicationService{
{
ID: "anapplicationservice",
ASToken: "astoken",
NamespaceMap: map[string][]config.ApplicationServiceNamespace{
"users": {
{
Exclusive: true,
Regex: "@alice:example.com",
RegexpObject: regexp.MustCompile("@alice:example.com"),
},
},
},
},
},
},
}

func genHTTPRequest(body string, token string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, "POST", "", strings.NewReader(body))
if err != nil {
return nil, err
}
if token != "" {
req.Header.Add("Authorization", "Bearer "+token)
}
return req, err
}

func TestLoginFromJSONReader(t *testing.T) {
ctx := context.Background()

tsts := []struct {
Name string
Body string
Name string
Body string
Token string

WantUsername string
WantDeviceID string
Expand Down Expand Up @@ -62,21 +100,45 @@ func TestLoginFromJSONReader(t *testing.T) {
WantDeviceID: "adevice",
WantDeletedTokens: []string{"atoken"},
},
{
Name: "appServiceWorksUserID",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "@alice:example.com" },
"device_id": "adevice"
}`,
Token: "astoken",

WantUsername: "@alice:example.com",
WantDeviceID: "adevice",
},
{
Name: "appServiceWorksLocalpart",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "alice" },
"device_id": "adevice"
}`,
Token: "astoken",

WantUsername: "alice",
WantDeviceID: "adevice",
},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
var userAPI fakeUserInternalAPI
cfg := &config.ClientAPI{
Matrix: &config.Global{
SigningIdentity: gomatrixserverlib.SigningIdentity{
ServerName: serverName,
},
},
}
login, cleanup, err := LoginFromJSONReader(ctx, strings.NewReader(tst.Body), &userAPI, &userAPI, cfg)

req, err := genHTTPRequest(tst.Body, tst.Token)
if err != nil {
t.Fatalf("LoginFromJSONReader failed: %+v", err)
t.Fatalf("genHTTPRequest failed: %v", err)
}

login, cleanup, jsonErr := LoginFromJSONReader(req, &userAPI, &userAPI, cfg)
if jsonErr != nil {
t.Fatalf("LoginFromJSONReader failed: %+v", jsonErr)
}

cleanup(ctx, &util.JSONResponse{Code: http.StatusOK})

if login.Username() != tst.WantUsername {
Expand Down Expand Up @@ -104,8 +166,9 @@ func TestBadLoginFromJSONReader(t *testing.T) {
ctx := context.Background()

tsts := []struct {
Name string
Body string
Name string
Body string
Token string

WantErrCode string
}{
Expand Down Expand Up @@ -142,18 +205,56 @@ func TestBadLoginFromJSONReader(t *testing.T) {
}`,
WantErrCode: "M_INVALID_ARGUMENT_VALUE",
},
{
Name: "noASToken",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "@alice:example.com" },
"device_id": "adevice"
}`,
WantErrCode: "M_MISSING_TOKEN",
},
{
Name: "badASToken",
Token: "badastoken",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "@alice:example.com" },
"device_id": "adevice"
}`,
WantErrCode: "M_UNKNOWN_TOKEN",
},
{
Name: "badASNamespace",
Token: "astoken",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "@bob:example.com" },
"device_id": "adevice"
}`,
WantErrCode: "M_EXCLUSIVE",
},
{
Name: "badASUserID",
Token: "astoken",
Body: `{
"type": "m.login.application_service",
"identifier": { "type": "m.id.user", "user": "@alice:wrong.example.com" },
"device_id": "adevice"
}`,
WantErrCode: "M_INVALID_USERNAME",
},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
var userAPI fakeUserInternalAPI
cfg := &config.ClientAPI{
Matrix: &config.Global{
SigningIdentity: gomatrixserverlib.SigningIdentity{
ServerName: serverName,
},
},

req, err := genHTTPRequest(tst.Body, tst.Token)
if err != nil {
t.Fatalf("genHTTPRequest failed: %v", err)
}
_, cleanup, errRes := LoginFromJSONReader(ctx, strings.NewReader(tst.Body), &userAPI, &userAPI, cfg)

_, cleanup, errRes := LoginFromJSONReader(req, &userAPI, &userAPI, cfg)
if errRes == nil {
cleanup(ctx, nil)
t.Fatalf("LoginFromJSONReader err: got %+v, want code %q", errRes, tst.WantErrCode)
Expand Down
2 changes: 1 addition & 1 deletion clientapi/auth/user_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type LoginCleanupFunc func(context.Context, *util.JSONResponse)
// https://matrix.org/docs/spec/client_server/r0.6.1#identifier-types
type LoginIdentifier struct {
Type string `json:"type"`
// when type = m.id.user
// when type = m.id.user or m.id.application_service
User string `json:"user"`
// when type = m.id.thirdparty
Medium string `json:"medium"`
Expand Down

0 comments on commit ba0f797

Please sign in to comment.