Exemple #1
0
	async def test_validating(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()

		test_1 = Setting('test1', 'Test 1', Setting.CAT_GENERAL, type=str)
		await instance.setting_manager.register(
			test_1
		)

		with self.assertRaises(SerializationException) as context:
			await test_1.set_value(True)
		assert isinstance(context.exception, SerializationException)

		with self.assertRaises(SerializationException) as context:
			await test_1.set_value(1)
		assert isinstance(context.exception, SerializationException)

		with self.assertRaises(SerializationException) as context:
			await test_1.set_value(float(55))
		assert isinstance(context.exception, SerializationException)

		with self.assertRaises(SerializationException) as context:
			await test_1.set_value(dict())
		assert isinstance(context.exception, SerializationException)

		with self.assertRaises(SerializationException) as context:
			await test_1.set_value(list())
		assert isinstance(context.exception, SerializationException)

		value = test_1.get_value(refresh=True)
		assert value is not True
Exemple #2
0
 async def test_connection(self):
     instance = Controller.prepare(name='default').instance
     await instance.db.connect()
     with instance.db.allow_sync():
         introspector = Introspector.from_database(instance.db.engine)
         db_name = introspector.get_database_name()
         assert db_name and len(db_name) > 0
Exemple #3
0
    async def test_registering(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        self.target_called = 0

        from pyplanet.apps.core.maniaplanet.models import Player
        from pyplanet.contrib.command import Command

        test1 = Command(
            'test',
            self.target,
            aliases=['tst'],
            admin=False,
        )
        await instance.command_manager.register(test1)

        # Try to fetch the command.
        player, _ = await Player.get_or_create(login='******',
                                               nickname='sample-1',
                                               level=Player.LEVEL_MASTER)
        await instance.command_manager._on_chat(player, '/test', True)
        await instance.command_manager._on_chat(player, '/tst', True)

        assert self.target_called == 2
Exemple #4
0
	async def test_to_logins(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()
		await instance.db.initiate()

		from pyplanet.apps.core.maniaplanet.models import Player

		try:
			player1 = await Player.get(login='******')
		except:
			player1 = await Player.create(login='******', nickname='sample-1', level=Player.LEVEL_MASTER)

		try:
			player2 = await Player.get(login='******')
		except:
			player2 = await Player.create(login='******', nickname='sample-2', level=Player.LEVEL_PLAYER)

		# Try list.
		chat = instance.chat.prepare().to_players([player1, player2])
		assert 'sample-1' in chat._logins and 'sample-2' in chat._logins

		# Try unpacked list.
		chat = instance.chat.prepare().to_players(*[player1, player2])
		assert 'sample-1' in chat._logins and 'sample-2' in chat._logins

		# Try login list.
		chat = instance.chat.prepare().to_players([player1.login, player2.login])
		assert 'sample-1' in chat._logins and 'sample-2' in chat._logins

		# Try login unpacked list.
		chat = instance.chat.prepare().to_players(*[player1.login, player2.login])
		assert 'sample-1' in chat._logins and 'sample-2' in chat._logins
Exemple #5
0
    async def test_app_manager(self):
        instance = Controller.prepare(name='default').instance
        manager = instance.signals.create_app_manager(None)

        test1 = Signal(code='test1',
                       namespace='tests',
                       process_target=self.glue)
        manager.register_signal(test1)

        self.got_sync = 0
        self.got_async = 0
        self.got_glue = 0
        self.got_raw = 0

        manager.listen(test1, self.sync_listener)
        manager.listen(test1, self.async_listener)

        await test1.send(dict(glue=False))

        await manager.on_destroy()

        # This one will be ignored because the app is destroyed.
        await test1.send(dict(glue=False), raw=True)

        assert self.got_sync == 1
        assert self.got_async == 1
Exemple #6
0
	async def test_short_syntax(self):
		instance = Controller.prepare(name='default').instance
		# MOCK:
		instance.gbx.gbx_methods = ['ChatSendServerMessageToLogin', 'ChatSendServerMessage']

		prepared = instance.chat('Test')
		assert isinstance(prepared, ChatQuery)
Exemple #7
0
    async def test_listening(self):
        instance = Controller.prepare(name='default').instance

        test1 = Signal(code='test1',
                       namespace='tests',
                       process_target=self.glue)
        instance.signals.register_signal(test1)

        self.got_sync = 0
        self.got_async = 0
        self.got_glue = 0
        self.got_raw = 0

        test1.register(self.sync_listener)
        test1.register(self.async_listener)

        await test1.send(dict(glue=False), raw=True)
        await test1.send(dict(glue=False), raw=False)
        await test1.send_robust(dict(glue=False), raw=True)
        await test1.send_robust(dict(glue=False), raw=False)

        assert self.got_async == 4
        assert self.got_sync == 4
        assert self.got_glue == 2
        assert self.got_raw == 4
Exemple #8
0
	async def test_simple(self):
		instance = Controller.prepare(name='default').instance
		chat = instance.chat.prepare()
		assert isinstance(chat, ChatQuery)

		chat.to_all().message('Test')
		assert len(chat.get_formatted_message()) > len('Test')  # Must include the prefix!!
Exemple #9
0
 def __init__(self, *args, **kwargs):
     from pyplanet.conf import settings
     self.tmp_dir = settings.TMP_PATH
     self.tmp_file = os.path.join(self.tmp_dir,
                                  'test-{}.txt'.format(id(self)))
     self.instance = Controller.prepare(name='default').instance
     super().__init__(*args, **kwargs)
Exemple #10
0
	async def test_template_loading(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()
		template = await load_template('core.views/generics/list.xml')
		assert template and template.template
		assert isinstance(template.template, Template)
Exemple #11
0
    async def test_discovery(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        assert len(instance.db.registry.models.keys()) > 0
        assert len(instance.db.registry.app_models.keys()) > 0
Exemple #12
0
	async def test_template_rendering(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()
		template = await load_template('core.views/generics/list.xml')
		body = await template.render(
			title='TRY_TO_SEARCH_THIS'
		)
		assert 'TRY_TO_SEARCH_THIS' in body
Exemple #13
0
    async def test_creation(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        with instance.db.allow_sync():
            introspector = Introspector.from_database(instance.db.engine)
            metadata = introspector.introspect()
            assert len(metadata.model_names) > 0
Exemple #14
0
    async def test_registration(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()

        from pyplanet.apps.core.maniaplanet.models import Player
        await instance.permission_manager.register(
            'test1', namespace='tst1', min_level=Player.LEVEL_PLAYER)
        assert bool(await
                    instance.permission_manager.get_perm('tst1', 'test1'))
Exemple #15
0
    async def test_registering(self):
        instance = Controller.prepare(name='default').instance

        test1 = Callback(call='SampleCall',
                         code='sample_call',
                         namespace='tests',
                         target=self.handle_sample)

        test1_comp = instance.signals.get_callback('SampleCall')

        assert test1.raw_signal == test1_comp
Exemple #16
0
	async def test_registration(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()

		test_1 = Setting('test1', 'Test 1', Setting.CAT_GENERAL, type=str)
		await instance.setting_manager.register(
			test_1
		)
		setting_list = await instance.setting_manager.get_all()
		assert test_1 in setting_list
Exemple #17
0
    async def test_registering(self):
        instance = Controller.prepare(name='default').instance

        test1 = Signal(code='test1', namespace='tests')
        instance.signals.register_signal(test1)
        test2 = Signal(code='test2', namespace='tests')
        instance.signals.register_signal(test2)

        test1_comp = instance.signals.get_signal('tests:test1')
        test2_comp = instance.signals.get_signal('tests:test2')

        assert test1 == test1_comp
        assert test2 == test2_comp
Exemple #18
0
	def handle(self, *args, **options):
		if options['source_db_password'] is None:
			options['source_db_password'] = getpass('Database Password: '******'pool']).instance
		converter = get_converter(
			options['source_format'], instance=instance, db_name=options['source_db_name'],
			db_type=options['source_db_type'], db_user=options['source_db_username'],
			db_port=options['source_db_port'], db_password=options['source_db_password'],
			db_host=options['source_db_host'], prefix=options['source_db_prefix']
		)

		instance.loop.run_until_complete(self.convert(instance, converter))
Exemple #19
0
    async def test_db_migration(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        with instance.db.allow_sync():
            migration.Migration.create_table(True)
            await instance.db.migrator.migrate()

        # Reset to simulate second startup
        instance.db.migrator.pass_migrations = set()
        with instance.db.allow_sync():
            await instance.db.migrator.migrate()
        assert len(instance.db.migrator.pass_migrations) == 0
Exemple #20
0
	async def test_query_conversion(self):
		instance = Controller.prepare(name='default').instance
		# MOCK:
		instance.gbx.gbx_methods = ['ChatSendServerMessageToLogin', 'ChatSendServerMessage']

		chat = instance.chat.prepare().to_players(['test-1', 'test-2']).message('Test')
		assert chat.gbx_query.method == 'ChatSendServerMessageToLogin'
		assert len(chat.gbx_query.args) == 2

		chat = instance.chat.prepare().message('Test')
		assert chat.gbx_query.method == 'ChatSendServerMessage'
		assert len(chat.gbx_query.args) == 1

		chat = instance.chat.prepare().to_all().message('Test')
		assert chat.gbx_query.method == 'ChatSendServerMessage'
		assert len(chat.gbx_query.args) == 1
Exemple #21
0
    async def test_checking(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        from pyplanet.apps.core.maniaplanet.models import Player
        await instance.permission_manager.register(
            'sample1', namespace='tst1', min_level=Player.LEVEL_PLAYER)
        await instance.permission_manager.register(
            'sample2', namespace='tst1', min_level=Player.LEVEL_OPERATOR)
        await instance.permission_manager.register(
            'sample3', namespace='tst1', min_level=Player.LEVEL_ADMIN)
        await instance.permission_manager.register(
            'sample4', namespace='tst1', min_level=Player.LEVEL_MASTER)

        # Try to fetch the command.
        try:
            player1 = await Player.get(login='******')
        except:
            player1 = await Player.create(login='******',
                                          nickname='sample-1',
                                          level=Player.LEVEL_MASTER)
        try:
            player2 = await Player.get(login='******')
        except:
            player2 = await Player.create(login='******',
                                          nickname='sample-2',
                                          level=Player.LEVEL_PLAYER)

        assert await instance.permission_manager.has_permission(
            player1, 'tst1:sample1')
        assert await instance.permission_manager.has_permission(
            player1, 'tst1:sample2')
        assert await instance.permission_manager.has_permission(
            player1, 'tst1:sample3')
        assert await instance.permission_manager.has_permission(
            player1, 'tst1:sample4')

        assert await instance.permission_manager.has_permission(
            player2, 'tst1:sample1')
        assert not await instance.permission_manager.has_permission(
            player2, 'tst1:sample2')
        assert not await instance.permission_manager.has_permission(
            player2, 'tst1:sample3')
        assert not await instance.permission_manager.has_permission(
            player2, 'tst1:sample4')
Exemple #22
0
	async def test_saving(self):
		instance = Controller.prepare(name='default').instance
		await instance.db.connect()
		await instance.apps.discover()

		test_1 = Setting('test1', 'Test 1', Setting.CAT_GENERAL, type=str)
		await instance.setting_manager.register(
			test_1
		)

		expected = 'test1'
		await test_1.set_value('test1')
		cached = await test_1.get_value(refresh=False)
		real = await test_1.get_value(refresh=True)

		assert cached == expected
		assert real == expected
Exemple #23
0
    async def test_params(self):
        instance = Controller.prepare(name='default').instance
        await instance.db.connect()
        await instance.apps.discover()
        await instance.db.initiate()

        self.target_called = 0

        from pyplanet.apps.core.maniaplanet.models import Player
        from pyplanet.contrib.command import Command

        test1 = Command(
            'test',
            self.target,
            aliases=['tst'],
            admin=False,
        ).add_param('test', type=str, required=True)
        test2 = Command(
            'test2',
            self.num_target,
            admin=True,
        ).add_param('numbers', type=int, nargs='*', required=True)
        await instance.command_manager.register(test1, test2)

        # Try to fetch the command.
        player, _ = await Player.get_or_create(login='******',
                                               nickname='sample-1',
                                               level=Player.LEVEL_MASTER)
        await instance.command_manager._on_chat(player, '/test ok', True)
        await instance.command_manager._on_chat(player, '/tst okok', True)

        assert self.target_called == 2
        self.target_called = 0

        await instance.command_manager._on_chat(player, '//test2 1 2 3 4 5',
                                                True)
        await instance.command_manager._on_chat(player, '/admin test2 4 5',
                                                True)

        assert self.target_called == 7
Exemple #24
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.instance = Controller.prepare(name='default').instance
Exemple #25
0
 async def test_init(self):
     instance = Controller.prepare(name='default').instance
     assert instance.storage
     assert instance.storage.driver
     assert isinstance(instance.storage.driver, StorageDriver)
Exemple #26
0
 async def test_driver_interface(self):
     instance = Controller.prepare(name='default').instance
     assert type(instance.storage.driver.openable()) is bool
Exemple #27
0
 async def setUp(self):
     self.instance = Controller.prepare(name='default').instance
     await self.instance._start()
     asyncio.ensure_future(self.instance.gbx.listen())