mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-11 08:53:11 -06:00
Improve ValidateApplicationService test coverage
This commit is contained in:
parent
9bef2876b4
commit
55bc0f6807
|
|
@ -174,24 +174,16 @@ func Test_validateUsername(t *testing.T) {
|
||||||
// This method tests validation of the provided Application Service token and
|
// This method tests validation of the provided Application Service token and
|
||||||
// username that they're registering
|
// username that they're registering
|
||||||
func TestValidationOfApplicationServices(t *testing.T) {
|
func TestValidationOfApplicationServices(t *testing.T) {
|
||||||
// Set up application service namespaces
|
// Create a fake application service
|
||||||
regex := "@_appservice_.*"
|
regex := "@_appservice_.*"
|
||||||
regexp, err := regexp.Compile(regex)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Error compiling regex: %s", regex)
|
|
||||||
}
|
|
||||||
|
|
||||||
fakeNamespace := config.ApplicationServiceNamespace{
|
fakeNamespace := config.ApplicationServiceNamespace{
|
||||||
Exclusive: true,
|
Exclusive: true,
|
||||||
Regex: regex,
|
Regex: regex,
|
||||||
RegexpObject: regexp,
|
RegexpObject: regexp.MustCompile(regex),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a fake application service
|
|
||||||
fakeID := "FakeAS"
|
|
||||||
fakeSenderLocalpart := "_appservice_bot"
|
fakeSenderLocalpart := "_appservice_bot"
|
||||||
fakeApplicationService := config.ApplicationService{
|
fakeApplicationService := config.ApplicationService{
|
||||||
ID: fakeID,
|
ID: "FakeAS",
|
||||||
URL: "null",
|
URL: "null",
|
||||||
ASToken: "1234",
|
ASToken: "1234",
|
||||||
HSToken: "4321",
|
HSToken: "4321",
|
||||||
|
|
@ -201,6 +193,25 @@ func TestValidationOfApplicationServices(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a second fake application service where userIDs ending in
|
||||||
|
// "_overlap" overlap with the first.
|
||||||
|
regex = "@.*_overlap"
|
||||||
|
fakeNamespace = config.ApplicationServiceNamespace{
|
||||||
|
Exclusive: true,
|
||||||
|
Regex: regex,
|
||||||
|
RegexpObject: regexp.MustCompile(regex),
|
||||||
|
}
|
||||||
|
fakeApplicationServiceOverlap := config.ApplicationService{
|
||||||
|
ID: "FakeASOverlap",
|
||||||
|
URL: fakeApplicationService.URL,
|
||||||
|
ASToken: fakeApplicationService.ASToken,
|
||||||
|
HSToken: fakeApplicationService.HSToken,
|
||||||
|
SenderLocalpart: "_appservice_bot_overlap",
|
||||||
|
NamespaceMap: map[string][]config.ApplicationServiceNamespace{
|
||||||
|
"users": {fakeNamespace},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Set up a config
|
// Set up a config
|
||||||
fakeConfig := &config.Dendrite{}
|
fakeConfig := &config.Dendrite{}
|
||||||
fakeConfig.Defaults(config.DefaultOpts{
|
fakeConfig.Defaults(config.DefaultOpts{
|
||||||
|
|
@ -208,29 +219,51 @@ func TestValidationOfApplicationServices(t *testing.T) {
|
||||||
Monolithic: true,
|
Monolithic: true,
|
||||||
})
|
})
|
||||||
fakeConfig.Global.ServerName = "localhost"
|
fakeConfig.Global.ServerName = "localhost"
|
||||||
fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService}
|
fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService, fakeApplicationServiceOverlap}
|
||||||
|
|
||||||
// Access token is correct, user_id omitted so we are acting as SenderLocalpart
|
// Access token is correct, user_id omitted so we are acting as SenderLocalpart
|
||||||
asID, resp := ValidateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, "1234")
|
asID, resp := ValidateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, fakeApplicationService.ASToken)
|
||||||
if resp != nil || asID != fakeID {
|
if resp != nil || asID != fakeApplicationService.ID {
|
||||||
t.Errorf("appservice should have validated and returned correct ID: %s", resp.JSON)
|
t.Errorf("appservice should have validated and returned correct ID: %s", resp.JSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access token is incorrect, user_id omitted so we are acting as SenderLocalpart
|
// Access token is incorrect, user_id omitted so we are acting as SenderLocalpart
|
||||||
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, "xxxx")
|
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, "xxxx")
|
||||||
if resp == nil || asID == fakeID {
|
if resp == nil || asID == fakeApplicationService.ID {
|
||||||
t.Errorf("access_token should have been marked as invalid")
|
t.Errorf("access_token should have been marked as invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access token is correct, acting as valid user_id
|
// Access token is correct, acting as valid user_id
|
||||||
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "_appservice_bob", "1234")
|
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "_appservice_bob", fakeApplicationService.ASToken)
|
||||||
if resp != nil || asID != fakeID {
|
if resp != nil || asID != fakeApplicationService.ID {
|
||||||
t.Errorf("access_token and user_id should've been valid: %s", resp.JSON)
|
t.Errorf("access_token and user_id should've been valid: %s", resp.JSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access token is correct, acting as invalid user_id
|
// Access token is correct, acting as invalid user_id
|
||||||
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "_something_else", "1234")
|
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "_something_else", fakeApplicationService.ASToken)
|
||||||
if resp == nil || asID == fakeID {
|
if resp == nil || asID == fakeApplicationService.ID {
|
||||||
t.Errorf("user_id should not have been valid: @_something_else:localhost")
|
t.Errorf("user_id should not have been valid: @_something_else:localhost")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Access token is correct, acting as user_id that matches two exclusive namespaces
|
||||||
|
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "_appservice_overlap", fakeApplicationService.ASToken)
|
||||||
|
if resp == nil || asID == fakeApplicationService.ID {
|
||||||
|
t.Errorf("user_id should not have been valid: @_appservice_overlap:localhost")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access token is correct, acting as matching user_id that is too long
|
||||||
|
asID, resp = ValidateApplicationService(
|
||||||
|
&fakeConfig.ClientAPI,
|
||||||
|
"_appservice_"+strings.Repeat("a", maxUsernameLength),
|
||||||
|
fakeApplicationService.ASToken,
|
||||||
|
)
|
||||||
|
if resp == nil || asID == fakeApplicationService.ID {
|
||||||
|
t.Errorf("user_id exceeding maxUsernameLength should not have been valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access token is correct, acting as user_id that matches but is invalid
|
||||||
|
asID, resp = ValidateApplicationService(&fakeConfig.ClientAPI, "@_appservice_bob::", fakeApplicationService.ASToken)
|
||||||
|
if resp == nil || asID == fakeApplicationService.ID {
|
||||||
|
t.Errorf("user_id should not have been valid: @_appservice_bob::")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue