Example #1
0
    def setUp(self):
        self.default = DEFAULT_CONF_FILE
        with open(self.default, 'w') as f:
            f.write('{"bus": {"dsn": "mqtt://test@localhost"}}')
        kwargs = {'config': ''}
        self.nyuki = Nyuki(**kwargs)

        self.apiconf = ApiConfiguration()
        self.apiconf.nyuki = self.nyuki
Example #2
0
    def setUp(self):
        self.default = DEFAULT_CONF_FILE
        with open(self.default, 'w') as f:
            f.write('{"bus": {"jid": "test@localhost", "password": "******"}}')
        kwargs = {'config': ''}
        self.nyuki = Nyuki(**kwargs)

        self.apiconf = ApiConfiguration()
        self.apiconf.nyuki = self.nyuki
Example #3
0
class TestNyuki(TestCase):

    def setUp(self):
        self.default = DEFAULT_CONF_FILE
        with open(self.default, 'w') as f:
            f.write('{"bus": {"jid": "test@localhost", "password": "******"}}')
        kwargs = {'config': ''}
        self.nyuki = Nyuki(**kwargs)

        self.apiconf = ApiConfiguration()
        self.apiconf.nyuki = self.nyuki

    def tearDown(self):
        os.remove(self.default)

    @ignore_loop
    def test_001_update_config(self):
        assert_not_equal(self.nyuki.config['bus']['password'], 'new_password')
        self.nyuki.update_config({
            'bus': {
                'password': '******'
            }
        })
        eq_(self.nyuki.config['bus']['password'], 'new_password')

        # Check read-only
        self.nyuki.save_config()
        with open(self.default, 'r') as f:
            eq_(f.read(), '{"bus": {"jid": "test@localhost", "password": "******"}}')

    @ignore_loop
    def test_003_get_rest_configuration(self):
        response = self.apiconf.get(None)
        eq_(json.loads(bytes.decode(response.body)), self.nyuki._config)

    @patch('nyuki.bus.XmppBus.stop')
    async def test_004_patch_rest_configuration(self, bus_stop_mock):
        req = Mock()
        async def json():
            return {
                'bus': {'jid': 'updated@localhost'},
                'new': True
            }
        req.headers = {'Content-Type': 'application/json'}
        req.json = json
        await self.apiconf.patch(req)
        eq_(self.nyuki._config['new'], True)
        eq_(self.nyuki._config['bus']['jid'], 'updated@localhost')
        # finish coroutines
        await exhaust_callbacks(self.loop)
        bus_stop_mock.assert_called_once_with()

    @ignore_loop
    def test_005a_custom_schema_fail(self):
        with assert_raises(ValidationError):
            self.nyuki.register_schema({
                'type': 'object',
                'required': ['port'],
                'properties': {
                    'port': {
                        'type': 'integer',
                    }
                }
            })

    @ignore_loop
    def test_005b_custom_schema_ok(self):
        self.nyuki._config['port'] = 4000
        self.nyuki.register_schema({
            'type': 'object',
            'required': ['port'],
            'properties': {
                'port': {'type': 'integer'}
            }
        })
        # Base + API + Bus + custom
        eq_(len(self.nyuki._schemas), 4)

    async def test_005_stop(self):
        with patch.object(self.nyuki._services, 'stop') as mock:
            # Do not really close the loop as it would break other tests
            with patch.object(self.nyuki, '_stop_loop'):
                await self.nyuki.stop()
            mock.assert_called_once_with()
        assert_true(self.nyuki.is_stopping)
Example #4
0
class TestNyuki(TestCase):
    def setUp(self):
        self.default = DEFAULT_CONF_FILE
        with open(self.default, 'w') as f:
            f.write('{"bus": {"dsn": "mqtt://test@localhost"}}')
        kwargs = {'config': ''}
        self.nyuki = Nyuki(**kwargs)

        self.apiconf = ApiConfiguration()
        self.apiconf.nyuki = self.nyuki

    def tearDown(self):
        os.remove(self.default)

    @ignore_loop
    def test_001_update_config(self):
        assert_not_equal(self.nyuki.config['bus']['dsn'],
                         'mqtt://new@localhost')
        self.nyuki.update_config({'bus': {'dsn': 'mqtt://new@localhost'}})
        eq_(self.nyuki.config['bus']['dsn'], 'mqtt://new@localhost')

        # Check read-only
        self.nyuki.save_config()
        with open(self.default, 'r') as f:
            eq_(f.read(), '{"bus": {"dsn": "mqtt://test@localhost"}}')

    @ignore_loop
    def test_003_get_rest_configuration(self):
        response = self.apiconf.get(None)
        eq_(json.loads(bytes.decode(response.body)), self.nyuki._config)

    @patch('nyuki.bus.MqttBus.stop')
    async def test_004_patch_rest_configuration(self, bus_stop_mock):
        req = Mock()

        async def json():
            return {'bus': {'dsn': 'mqtt://new@localhost'}, 'new': True}

        req.headers = {'Content-Type': 'application/json'}
        req.json = json
        await self.apiconf.patch(req)
        eq_(self.nyuki._config['new'], True)
        eq_(self.nyuki._config['bus']['dsn'], 'mqtt://new@localhost')
        # finish coroutines
        await exhaust_callbacks(self.loop)
        bus_stop_mock.assert_called_once_with()

    @ignore_loop
    def test_005a_custom_schema_fail(self):
        self.nyuki._validate_config()
        self.nyuki.register_schema({
            'type': 'object',
            'required': ['port'],
            'properties': {
                'port': {
                    'type': 'integer',
                }
            }
        })
        with assert_raises(ValidationError):
            self.nyuki._validate_config()

    @ignore_loop
    def test_005b_custom_schema_ok(self):
        self.nyuki._config['port'] = 4000
        self.nyuki.register_schema({
            'type': 'object',
            'required': ['port'],
            'properties': {
                'port': {
                    'type': 'integer'
                }
            }
        })
        # Base + API + Bus + custom
        eq_(len(self.nyuki._schemas), 4)

    async def test_005_stop(self):
        with patch.object(self.nyuki._services, 'stop') as mock:
            # Do not really close the loop as it would break other tests
            with patch.object(self.nyuki, '_stop_loop'):
                await self.nyuki.stop()
            mock.assert_called_once_with()
        assert_true(self.nyuki.is_stopping)