예제 #1
0
 def _create_bot(self, user, name, plays_race='T'):
     with open(self.test_bot_zip_path, 'rb') as bot_zip, open(self.test_bot_datas['bot1'][0]['path'], 'rb') as bot_data:
         bot = Bot(user=user, name=name, bot_zip=File(bot_zip), bot_data=File(bot_data), plays_race=plays_race,
                   type='python')
         bot.full_clean()
         bot.save()
         return bot
예제 #2
0
 def _create_active_bot(self, user, name, plays_race='T'):
     with open(self.test_bot_zip_path, 'rb') as bot_zip:
         bot = Bot(user=user,
                   name=name,
                   bot_zip=File(bot_zip),
                   plays_race=plays_race,
                   type='python',
                   active=True)
         bot.full_clean()
         bot.save()
         return bot
예제 #3
0
 def disable_and_send_crash_alert(bot: Bot):
     bot.active = False
     bot.save()
     try:
         send_mail(  # todo: template this
             'AI Arena - ' + bot.name + ' deactivated due to crashing',
             'Dear ' + bot.user.username + ',\n'
                                            '\n'
                                            'We are emailing you to let you know that your bot '
                                            '"' + bot.name + '" has reached our consecutive crash limit and hence been deactivated.\n'
                                                              'Please log into aiarena.net at your convenience to address the issue.\n'
                                                              'Bot logs are available for download when logged in on the bot''s page here: '
             + settings.SITE_PROTOCOL + '://' + Site.objects.get_current().domain
             + reverse('bot', kwargs={'pk': bot.id}) + '\n'
                                                        '\n'
                                                        'Kind regards,\n'
                                                        'AI Arena Staff',
             settings.DEFAULT_FROM_EMAIL,
             [bot.user.email],
             fail_silently=False,
         )
     except Exception as e:
         logger.exception(e)
예제 #4
0
    def test_bot_creation_and_update(self):
        # set the configured per user limits for this test
        config.MAX_USER_BOT_COUNT_ACTIVE_PER_RACE = 1
        config.MAX_USER_BOT_COUNT = 4

        # required for active bot
        self._create_open_season()

        # create bot along with bot data
        with open(self.test_bot_zip_path, 'rb') as bot_zip, open(
                self.test_bot_datas['bot1'][0]['path'], 'rb') as bot_data:
            bot1 = Bot(user=self.regularUser1,
                       name='testbot',
                       bot_zip=File(bot_zip),
                       bot_data=File(bot_data),
                       plays_race='T',
                       type='python',
                       active=True)
            bot1.full_clean()
            bot1.save()

        # test display id regen
        prev_bot_display_id = bot1.game_display_id
        bot1.regen_game_display_id()
        self.assertNotEqual(bot1.game_display_id, prev_bot_display_id)

        bot1.refresh_from_db()
        # check hashes
        self.assertEqual(self.test_bot_zip_hash, bot1.bot_zip_md5hash)
        self.assertEqual(self.test_bot_datas['bot1'][0]['hash'],
                         bot1.bot_data_md5hash)

        # check the bot file now exists
        self.assertTrue(
            os.path.isfile('./private-media/bots/{0}/bot_zip'.format(bot1.id)))

        with open(self.test_bot_zip_path, 'rb') as bot_zip:
            bot1.bot_zip = File(bot_zip)
            bot1.save()

        # check the bot file backup now exists
        self.assertTrue(
            os.path.isfile('./private-media/bots/{0}/bot_zip_backup'.format(
                bot1.id)))

        # test max bots for user
        for i in range(1, config.MAX_USER_BOT_COUNT):
            self._create_bot(self.regularUser1, 'testbot{0}'.format(i))
        with self.assertRaisesMessage(
                ValidationError, 'Maximum bot count of {0} already reached. '
                'No more bots may be added for this user.'.format(
                    config.MAX_USER_BOT_COUNT)):
            self._create_bot(self.regularUser1,
                             'testbot{0}'.format(config.MAX_USER_BOT_COUNT))

        # test active bots per race limit for user
        # all bots should be the same race, so just pick any
        inactive_bot = Bot.objects.filter(user=self.regularUser1,
                                          active=False)[0]
        with self.assertRaisesMessage(
                ValidationError,
                'Too many active bots playing that race already exist for this user.'
                ' You are allowed 1 active bot(s) per race.'):
            inactive_bot.active = True
            inactive_bot.full_clean()  # run validation

        # test updating bot_zip
        with open(self.test_bot_zip_updated_path, 'rb') as bot_zip_updated:
            bot1.bot_zip = File(bot_zip_updated)
            bot1.save()

        bot1.refresh_from_db()
        self.assertEqual(self.test_bot_zip_updated_hash, bot1.bot_zip_md5hash)

        # test updating bot_data
        # using bot2's data instead here so it's different
        with open(self.test_bot_datas['bot2'][0]['path'],
                  'rb') as bot_data_updated:
            bot1.bot_data = File(bot_data_updated)
            bot1.save()

        bot1.refresh_from_db()
        self.assertEqual(self.test_bot_datas['bot2'][0]['hash'],
                         bot1.bot_data_md5hash)