2017-12-04 10:47:27 -05:00
|
|
|
from twisted.python import failure
|
|
|
|
|
2015-07-28 12:34:12 -04:00
|
|
|
from synapse.rest.client.v2_alpha.register import RegisterRestServlet
|
2017-12-04 10:47:27 -05:00
|
|
|
from synapse.api.errors import SynapseError, InteractiveAuthIncompleteError
|
2015-07-28 12:34:12 -04:00
|
|
|
from twisted.internet import defer
|
2016-02-19 10:34:38 -05:00
|
|
|
from mock import Mock
|
2015-07-28 12:34:12 -04:00
|
|
|
from tests import unittest
|
2016-09-12 05:46:02 -04:00
|
|
|
from tests.utils import mock_getRawHeaders
|
2015-07-28 12:34:12 -04:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterRestServletTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# do the dance to hook up request data to self.request_data
|
|
|
|
self.request_data = ""
|
|
|
|
self.request = Mock(
|
|
|
|
content=Mock(read=Mock(side_effect=lambda: self.request_data)),
|
2015-08-04 10:18:40 -04:00
|
|
|
path='/_matrix/api/v2_alpha/register'
|
2015-07-28 12:34:12 -04:00
|
|
|
)
|
|
|
|
self.request.args = {}
|
2016-09-12 05:46:02 -04:00
|
|
|
self.request.requestHeaders.getRawHeaders = mock_getRawHeaders()
|
2015-07-28 12:34:12 -04:00
|
|
|
|
|
|
|
self.appservice = None
|
|
|
|
self.auth = Mock(get_appservice_by_req=Mock(
|
2016-10-06 04:43:32 -04:00
|
|
|
side_effect=lambda x: self.appservice)
|
2015-07-28 12:34:12 -04:00
|
|
|
)
|
|
|
|
|
2017-12-04 10:47:27 -05:00
|
|
|
self.auth_result = failure.Failure(InteractiveAuthIncompleteError(None))
|
2015-07-28 12:34:12 -04:00
|
|
|
self.auth_handler = Mock(
|
2016-03-16 10:25:14 -04:00
|
|
|
check_auth=Mock(side_effect=lambda x, y, z: self.auth_result),
|
|
|
|
get_session_data=Mock(return_value=None)
|
2015-07-28 12:34:12 -04:00
|
|
|
)
|
|
|
|
self.registration_handler = Mock()
|
|
|
|
self.identity_handler = Mock()
|
|
|
|
self.login_handler = Mock()
|
2016-07-19 13:38:26 -04:00
|
|
|
self.device_handler = Mock()
|
2015-07-28 12:34:12 -04:00
|
|
|
|
|
|
|
# do the dance to hook it up to the hs global
|
|
|
|
self.handlers = Mock(
|
|
|
|
registration_handler=self.registration_handler,
|
|
|
|
identity_handler=self.identity_handler,
|
|
|
|
login_handler=self.login_handler
|
|
|
|
)
|
|
|
|
self.hs = Mock()
|
|
|
|
self.hs.hostname = "superbig~testing~thing.com"
|
|
|
|
self.hs.get_auth = Mock(return_value=self.auth)
|
|
|
|
self.hs.get_handlers = Mock(return_value=self.handlers)
|
2016-06-02 08:31:45 -04:00
|
|
|
self.hs.get_auth_handler = Mock(return_value=self.auth_handler)
|
2016-07-19 13:38:26 -04:00
|
|
|
self.hs.get_device_handler = Mock(return_value=self.device_handler)
|
2016-02-03 09:42:01 -05:00
|
|
|
self.hs.config.enable_registration = True
|
2018-01-18 19:28:08 -05:00
|
|
|
self.hs.config.registrations_require_3pid = []
|
2017-10-17 05:39:50 -04:00
|
|
|
self.hs.config.auto_join_rooms = []
|
2015-07-28 12:34:12 -04:00
|
|
|
|
|
|
|
# init the thing we're testing
|
|
|
|
self.servlet = RegisterRestServlet(self.hs)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_POST_appservice_registration_valid(self):
|
|
|
|
user_id = "@kermit:muppet"
|
|
|
|
token = "kermits_access_token"
|
|
|
|
self.request.args = {
|
|
|
|
"access_token": "i_am_an_app_service"
|
|
|
|
}
|
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": "kermit"
|
|
|
|
})
|
|
|
|
self.appservice = {
|
|
|
|
"id": "1234"
|
|
|
|
}
|
2015-07-29 05:00:54 -04:00
|
|
|
self.registration_handler.appservice_register = Mock(
|
2016-07-19 13:46:19 -04:00
|
|
|
return_value=user_id
|
2015-07-29 05:00:54 -04:00
|
|
|
)
|
2016-11-28 04:52:02 -05:00
|
|
|
self.auth_handler.get_access_token_for_user_id = Mock(
|
|
|
|
return_value=token
|
2016-07-22 09:52:53 -04:00
|
|
|
)
|
2016-07-19 13:46:19 -04:00
|
|
|
|
2016-03-09 18:08:37 -05:00
|
|
|
(code, result) = yield self.servlet.on_POST(self.request)
|
|
|
|
self.assertEquals(code, 200)
|
|
|
|
det_data = {
|
2016-03-10 04:13:35 -05:00
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
|
|
|
"home_server": self.hs.hostname
|
2016-03-09 18:08:37 -05:00
|
|
|
}
|
|
|
|
self.assertDictContainsSubset(det_data, result)
|
2015-07-28 12:34:12 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_POST_appservice_registration_invalid(self):
|
|
|
|
self.request.args = {
|
|
|
|
"access_token": "i_am_an_app_service"
|
|
|
|
}
|
2017-12-04 10:47:27 -05:00
|
|
|
|
2015-07-28 12:34:12 -04:00
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": "kermit"
|
|
|
|
})
|
|
|
|
self.appservice = None # no application service exists
|
|
|
|
result = yield self.servlet.on_POST(self.request)
|
|
|
|
self.assertEquals(result, (401, None))
|
|
|
|
|
|
|
|
def test_POST_bad_password(self):
|
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": "kermit",
|
|
|
|
"password": 666
|
|
|
|
})
|
|
|
|
d = self.servlet.on_POST(self.request)
|
|
|
|
return self.assertFailure(d, SynapseError)
|
|
|
|
|
|
|
|
def test_POST_bad_username(self):
|
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": 777,
|
|
|
|
"password": "monkey"
|
|
|
|
})
|
|
|
|
d = self.servlet.on_POST(self.request)
|
|
|
|
return self.assertFailure(d, SynapseError)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_POST_user_valid(self):
|
|
|
|
user_id = "@kermit:muppet"
|
|
|
|
token = "kermits_access_token"
|
2016-07-19 13:38:26 -04:00
|
|
|
device_id = "frogfone"
|
2015-07-28 12:34:12 -04:00
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": "kermit",
|
2016-07-19 13:38:26 -04:00
|
|
|
"password": "monkey",
|
|
|
|
"device_id": device_id,
|
2015-07-28 12:34:12 -04:00
|
|
|
})
|
|
|
|
self.registration_handler.check_username = Mock(return_value=True)
|
2017-12-04 10:47:27 -05:00
|
|
|
self.auth_result = (None, {
|
2015-07-28 12:34:12 -04:00
|
|
|
"username": "kermit",
|
|
|
|
"password": "monkey"
|
2016-03-16 10:25:14 -04:00
|
|
|
}, None)
|
2016-07-19 08:12:22 -04:00
|
|
|
self.registration_handler.register = Mock(return_value=(user_id, None))
|
2016-11-28 04:52:02 -05:00
|
|
|
self.auth_handler.get_access_token_for_user_id = Mock(
|
|
|
|
return_value=token
|
2016-07-22 09:52:53 -04:00
|
|
|
)
|
2016-07-19 13:38:26 -04:00
|
|
|
self.device_handler.check_device_registered = \
|
|
|
|
Mock(return_value=device_id)
|
2015-07-28 12:34:12 -04:00
|
|
|
|
2016-03-09 18:08:37 -05:00
|
|
|
(code, result) = yield self.servlet.on_POST(self.request)
|
|
|
|
self.assertEquals(code, 200)
|
|
|
|
det_data = {
|
2016-03-10 04:13:35 -05:00
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
2016-07-19 13:38:26 -04:00
|
|
|
"home_server": self.hs.hostname,
|
|
|
|
"device_id": device_id,
|
2016-03-09 18:08:37 -05:00
|
|
|
}
|
|
|
|
self.assertDictContainsSubset(det_data, result)
|
2016-07-22 09:52:53 -04:00
|
|
|
self.auth_handler.get_login_tuple_for_user_id(
|
|
|
|
user_id, device_id=device_id, initial_device_display_name=None)
|
2015-07-28 12:34:12 -04:00
|
|
|
|
|
|
|
def test_POST_disabled_registration(self):
|
2016-02-03 09:42:01 -05:00
|
|
|
self.hs.config.enable_registration = False
|
2015-07-28 12:34:12 -04:00
|
|
|
self.request_data = json.dumps({
|
|
|
|
"username": "kermit",
|
|
|
|
"password": "monkey"
|
|
|
|
})
|
|
|
|
self.registration_handler.check_username = Mock(return_value=True)
|
2017-12-04 10:47:27 -05:00
|
|
|
self.auth_result = (None, {
|
2015-07-28 12:34:12 -04:00
|
|
|
"username": "kermit",
|
|
|
|
"password": "monkey"
|
2016-03-16 10:25:14 -04:00
|
|
|
}, None)
|
2015-07-28 12:34:12 -04:00
|
|
|
self.registration_handler.register = Mock(return_value=("@user:id", "t"))
|
|
|
|
d = self.servlet.on_POST(self.request)
|
2015-08-04 10:18:40 -04:00
|
|
|
return self.assertFailure(d, SynapseError)
|