Exemplo n.º 1
0
class CreateUserServletTestCase(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)),
            path='/_matrix/client/api/v1/createUser')
        self.request.args = {}

        self.appservice = None
        self.auth = Mock(get_appservice_by_req=Mock(
            side_effect=lambda x: defer.succeed(self.appservice)))

        self.auth_result = (False, None, None, None)
        self.auth_handler = Mock(
            check_auth=Mock(side_effect=lambda x, y, z: self.auth_result),
            get_session_data=Mock(return_value=None))
        self.registration_handler = Mock()
        self.identity_handler = Mock()
        self.login_handler = Mock()

        # do the dance to hook it up to the hs global
        self.handlers = Mock(auth_handler=self.auth_handler,
                             registration_handler=self.registration_handler,
                             identity_handler=self.identity_handler,
                             login_handler=self.login_handler)
        self.hs = Mock()
        self.hs.hostname = "supergbig~testing~thing.com"
        self.hs.get_auth = Mock(return_value=self.auth)
        self.hs.get_handlers = Mock(return_value=self.handlers)
        self.hs.config.enable_registration = True
        # init the thing we're testing
        self.servlet = CreateUserRestServlet(self.hs)

    @defer.inlineCallbacks
    def test_POST_createuser_with_valid_user(self):
        user_id = "@someone:interesting"
        token = "my token"
        self.request.args = {"access_token": "i_am_an_app_service"}
        self.request_data = json.dumps({
            "localpart": "someone",
            "displayname": "someone interesting",
            "duration_seconds": 200
        })

        self.registration_handler.get_or_create_user = Mock(
            return_value=(user_id, token))

        (code, result) = yield self.servlet.on_POST(self.request)
        self.assertEquals(code, 200)

        det_data = {
            "user_id": user_id,
            "access_token": token,
            "home_server": self.hs.hostname
        }
        self.assertDictContainsSubset(det_data, result)
Exemplo n.º 2
0
class CreateUserServletTestCase(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)),
            path='/_matrix/client/api/v1/createUser'
        )
        self.request.args = {}
        self.request.requestHeaders.getRawHeaders = mock_getRawHeaders()

        self.registration_handler = Mock()

        self.appservice = Mock(sender="@as:test")
        self.datastore = Mock(
            get_app_service_by_token=Mock(return_value=self.appservice)
        )

        # do the dance to hook things up to the hs global
        handlers = Mock(
            registration_handler=self.registration_handler,
        )
        self.hs = Mock()
        self.hs.hostname = "superbig~testing~thing.com"
        self.hs.get_datastore = Mock(return_value=self.datastore)
        self.hs.get_handlers = Mock(return_value=handlers)
        self.servlet = CreateUserRestServlet(self.hs)

    @defer.inlineCallbacks
    def test_POST_createuser_with_valid_user(self):
        user_id = "@someone:interesting"
        token = "my token"
        self.request.args = {
            "access_token": "i_am_an_app_service"
        }
        self.request_data = json.dumps({
            "localpart": "someone",
            "displayname": "someone interesting",
            "duration_seconds": 200
        })

        self.registration_handler.get_or_create_user = Mock(
            return_value=(user_id, token)
        )

        (code, result) = yield self.servlet.on_POST(self.request)
        self.assertEquals(code, 200)

        det_data = {
            "user_id": user_id,
            "access_token": token,
            "home_server": self.hs.hostname
        }
        self.assertDictContainsSubset(det_data, result)
Exemplo n.º 3
0
class CreateUserServletTestCase(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)),
            path='/_matrix/client/api/v1/createUser')
        self.request.args = {}
        self.request.requestHeaders.getRawHeaders = mock_getRawHeaders()

        self.registration_handler = Mock()

        self.appservice = Mock(sender="@as:test")
        self.datastore = Mock(get_app_service_by_token=Mock(
            return_value=self.appservice))

        # do the dance to hook things up to the hs global
        handlers = Mock(registration_handler=self.registration_handler, )
        self.hs = Mock()
        self.hs.hostname = "superbig~testing~thing.com"
        self.hs.get_datastore = Mock(return_value=self.datastore)
        self.hs.get_handlers = Mock(return_value=handlers)
        self.servlet = CreateUserRestServlet(self.hs)

    @defer.inlineCallbacks
    def test_POST_createuser_with_valid_user(self):
        user_id = "@someone:interesting"
        token = "my token"
        self.request.args = {"access_token": "i_am_an_app_service"}
        self.request_data = json.dumps({
            "localpart": "someone",
            "displayname": "someone interesting",
            "duration_seconds": 200
        })

        self.registration_handler.get_or_create_user = Mock(
            return_value=(user_id, token))

        (code, result) = yield self.servlet.on_POST(self.request)
        self.assertEquals(code, 200)

        det_data = {
            "user_id": user_id,
            "access_token": token,
            "home_server": self.hs.hostname
        }
        self.assertDictContainsSubset(det_data, result)
Exemplo n.º 4
0
class CreateUserServletTestCase(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)),
            path='/_matrix/client/api/v1/createUser'
        )
        self.request.args = {}

        self.appservice = None
        self.auth = Mock(get_appservice_by_req=Mock(
            side_effect=lambda x: defer.succeed(self.appservice))
        )

        self.auth_result = (False, None, None, None)
        self.auth_handler = Mock(
            check_auth=Mock(side_effect=lambda x, y, z: self.auth_result),
            get_session_data=Mock(return_value=None)
        )
        self.registration_handler = Mock()
        self.identity_handler = Mock()
        self.login_handler = Mock()

        # do the dance to hook it up to the hs global
        self.handlers = Mock(
            auth_handler=self.auth_handler,
            registration_handler=self.registration_handler,
            identity_handler=self.identity_handler,
            login_handler=self.login_handler
        )
        self.hs = Mock()
        self.hs.hostname = "supergbig~testing~thing.com"
        self.hs.get_auth = Mock(return_value=self.auth)
        self.hs.get_handlers = Mock(return_value=self.handlers)
        self.hs.config.enable_registration = True
        # init the thing we're testing
        self.servlet = CreateUserRestServlet(self.hs)

    @defer.inlineCallbacks
    def test_POST_createuser_with_valid_user(self):
        user_id = "@someone:interesting"
        token = "my token"
        self.request.args = {
            "access_token": "i_am_an_app_service"
        }
        self.request_data = json.dumps({
            "localpart": "someone",
            "displayname": "someone interesting",
            "duration_seconds": 200
        })

        self.registration_handler.get_or_create_user = Mock(
            return_value=(user_id, token)
        )

        (code, result) = yield self.servlet.on_POST(self.request)
        self.assertEquals(code, 200)

        det_data = {
            "user_id": user_id,
            "access_token": token,
            "home_server": self.hs.hostname
        }
        self.assertDictContainsSubset(det_data, result)