Beispiel #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
Beispiel #2
0
    def test_002_bad_conf_file(self):
        conf = os.path.join(self.dir.name, 'myconf.json')
        with open(conf, 'w') as f:
            f.write('{"bus": {"name": "test"')

        with assert_raises(ValueError):
            kwargs = {'config': conf}
            self.nyuki = Nyuki(**kwargs)
Beispiel #3
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
Beispiel #4
0
    def test_001_copy_default(self):
        # Default conf file
        with open(self.default, 'w') as f:
            f.write('{"bus": {"dsn": "mqtt://test@localhost"}}')

        # Our conf file does not exist yet
        conf = os.path.join(self.dir.name, 'myconf.json')
        with patch('nyuki.config.DEFAULT_CONF_FILE', self.default):
            kwargs = {'config': conf}
            self.nyuki = Nyuki(**kwargs)

        # Check our conf is created from default
        with open(conf, 'r') as f:
            eq_(f.read(), '{"bus": {"dsn": "mqtt://test@localhost"}}')
Beispiel #5
0
 def setUp(self):
     kwargs = {
         'bus': {
             'jid': 'test@localhost',
             'password': '******',
             'host': '127.0.0.1',
             'port': 5555
         },
         'api': {
             'host': 'localhost',
             'port': 8082
         },
         'logging': 'DEBUG'
     }
     self.nyuki = Nyuki(**kwargs)
     self.nyuki.config_filename = 'unit_test_conf.json'
Beispiel #6
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)
Beispiel #7
0
class TestNyuki(TestCase):

    def setUp(self):
        kwargs = {
            'bus': {
                'jid': 'test@localhost',
                'password': '******',
                'host': '127.0.0.1',
                'port': 5555
            },
            'api': {
                'host': 'localhost',
                'port': 8082
            },
            'logging': 'DEBUG'
        }
        self.nyuki = Nyuki(**kwargs)
        self.nyuki.config_filename = 'unit_test_conf.json'

    def tearDown(self):
        if os.path.isfile(self.nyuki.config_filename):
            os.remove(self.nyuki.config_filename)

    @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')

    @ignore_loop
    def test_002_save_config(self):
        assert_false(os.path.isfile(self.nyuki.config_filename))
        self.nyuki.save_config()
        assert_true(os.path.isfile(self.nyuki.config_filename))
        with open(self.nyuki.config_filename) as file:
            conf = json.loads(file.read())
        eq_(self.nyuki.config, conf)

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

    @patch('nyuki.bus.Bus.stop')
    async def test_004_patch_rest_configuration(self, bus_stop_mock):
        await self.nyuki.Configuration.patch(self.nyuki, {
            'bus': {'jid': 'updated@localhost'},
            'new': True
        })
        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)
Beispiel #8
0
 def test_001_missing_default_file(self):
     with assert_raises(FileNotFoundError):
         Nyuki(**{'config': ''})
Beispiel #9
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)