MCPcopy Create free account
hub / github.com/daodst/chat / TestValidationOfApplicationServices

Function TestValidationOfApplicationServices

clientapi/routing/register_test.go:154–211  ·  view source on GitHub ↗

This method tests validation of the provided Application Service token and username that they're registering

(t *testing.T)

Source from the content-addressed store, hash-verified

152// This method tests validation of the provided Application Service token and
153// username that they're registering
154func TestValidationOfApplicationServices(t *testing.T) {
155 // Set up application service namespaces
156 regex := "@_appservice_.*"
157 regexp, err := regexp.Compile(regex)
158 if err != nil {
159 t.Errorf("Error compiling regex: %s", regex)
160 }
161
162 fakeNamespace := config.ApplicationServiceNamespace{
163 Exclusive: true,
164 Regex: regex,
165 RegexpObject: regexp,
166 }
167
168 // Create a fake application service
169 fakeID := "FakeAS"
170 fakeSenderLocalpart := "_appservice_bot"
171 fakeApplicationService := config.ApplicationService{
172 ID: fakeID,
173 URL: "null",
174 ASToken: "1234",
175 HSToken: "4321",
176 SenderLocalpart: fakeSenderLocalpart,
177 NamespaceMap: map[string][]config.ApplicationServiceNamespace{
178 "users": {fakeNamespace},
179 },
180 }
181
182 // Set up a config
183 fakeConfig := &config.Dendrite{}
184 fakeConfig.Defaults(true)
185 fakeConfig.Global.ServerName = "localhost"
186 fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService}
187
188 // Access token is correct, user_id omitted so we are acting as SenderLocalpart
189 asID, resp := validateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, "1234")
190 if resp != nil || asID != fakeID {
191 t.Errorf("appservice should have validated and returned correct ID: %s", resp.JSON)
192 }
193
194 // Access token is incorrect, user_id omitted so we are acting as SenderLocalpart
195 asID, resp = validateApplicationService(&fakeConfig.ClientAPI, fakeSenderLocalpart, "xxxx")
196 if resp == nil || asID == fakeID {
197 t.Errorf("access_token should have been marked as invalid")
198 }
199
200 // Access token is correct, acting as valid user_id
201 asID, resp = validateApplicationService(&fakeConfig.ClientAPI, "_appservice_bob", "1234")
202 if resp != nil || asID != fakeID {
203 t.Errorf("access_token and user_id should've been valid: %s", resp.JSON)
204 }
205
206 // Access token is correct, acting as invalid user_id
207 asID, resp = validateApplicationService(&fakeConfig.ClientAPI, "_something_else", "1234")
208 if resp == nil || asID == fakeID {
209 t.Errorf("user_id should not have been valid: @_something_else:localhost")
210 }
211}

Callers

nothing calls this directly

Calls 2

DefaultsMethod · 0.95

Tested by

no test coverage detected