Compare commits

...

2 Commits

Author SHA1 Message Date
Will Hunt b5477657ce
Update 8320.feature 2020-09-17 12:19:42 +01:00
Will Hunt b9b1d5bcb5 Add test 2020-09-17 12:17:26 +01:00
3 changed files with 74 additions and 3 deletions

View File

@ -1 +1 @@
Add `uk.half-shot.unstable.login.appservice` login type to allow appservices to login. Add `uk.half-shot.msc2778.login.application_service` login type to allow appservices to login.

View File

@ -16,7 +16,6 @@
import logging import logging
from typing import Awaitable, Callable, Dict, Optional from typing import Awaitable, Callable, Dict, Optional
from synapse.api.constants import LoginType
from synapse.api.errors import Codes, LoginError, SynapseError from synapse.api.errors import Codes, LoginError, SynapseError
from synapse.api.ratelimiting import Ratelimiter from synapse.api.ratelimiting import Ratelimiter
from synapse.appservice import ApplicationService from synapse.appservice import ApplicationService

View File

@ -7,8 +7,9 @@ from mock import Mock
import jwt import jwt
import synapse.rest.admin import synapse.rest.admin
from synapse.appservice import ApplicationService
from synapse.rest.client.v1 import login, logout from synapse.rest.client.v1 import login, logout
from synapse.rest.client.v2_alpha import devices from synapse.rest.client.v2_alpha import devices, register
from synapse.rest.client.v2_alpha.account import WhoamiRestServlet from synapse.rest.client.v2_alpha.account import WhoamiRestServlet
from tests import unittest from tests import unittest
@ -748,3 +749,74 @@ class JWTPubKeyTestCase(unittest.HomeserverTestCase):
channel.json_body["error"], channel.json_body["error"],
"JWT validation failed: Signature verification failed", "JWT validation failed: Signature verification failed",
) )
AS_USER = "as_user_alice"
class AppserviceLoginRestServletTestCase(unittest.HomeserverTestCase):
servlets = [
login.register_servlets,
register.register_servlets,
lambda hs, http_server: WhoamiRestServlet(hs).register(http_server),
]
def register_as_user(self, username):
request, channel = self.make_request(
b"POST",
f"/_matrix/client/r0/register?access_token={self.service.token}",
{"username": username},
)
self.render(request)
def make_homeserver(self, reactor, clock):
self.hs = self.setup_test_homeserver()
self.service = ApplicationService(
id="unique_identifier",
token="some_token",
hostname="example.com",
sender="@asbot:example.com",
namespaces={
ApplicationService.NS_USERS: [
{"regex": r"@as_user.*", "exclusive": False}
],
ApplicationService.NS_ROOMS: [],
ApplicationService.NS_ALIASES: [],
},
)
self.hs.get_datastore().services_cache.append(self.service)
return self.hs
def test_login_appservice_user(self):
"""Test that an appservice user can use /login
"""
self.register_as_user(AS_USER)
params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
"identifier": {"type": "m.id.user", "user": AS_USER},
}
request, channel = self.make_request(
b"POST", LOGIN_URL, params, access_token=self.service.token
)
self.render(request)
self.assertEquals(channel.result["code"], b"200", channel.result)
def test_login_appservice_user_bot(self):
"""Test that the appservice bot can use /login
"""
self.register_as_user(AS_USER)
params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
"identifier": {"type": "m.id.user", "user": self.service.sender},
}
request, channel = self.make_request(
b"POST", LOGIN_URL, params, access_token=self.service.token
)
self.render(request)
self.assertEquals(channel.result["code"], b"200", channel.result)