def test_error_role_is_not_demo(self): """ Attacker is not role DEMO, Error.""" attacker = self.p1b params = self.default_params with self.assertRaises(MeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You are not DEMO") self.assertEqual(len(self.bomb_queue), 0)
def test_good_bomb(self): """ Good attacker set good bomb. """ attacker = self.p1a later = Util.utc_to_chi(datetime.now()) params = self.default_params Bomb.handler(attacker, params) self.assertEqual(len(self.taskqueue_stub.get_filtered_tasks(\ queue_names='bomb')), 1)
def test_error_attacker_is_disarm(self): """ Attacker is diarmed trying to set a bomb: use p2a as attacker """ attacker = self.p1a attacker.disarm = True attacker.put() params = self.default_params with self.assertRaises(MeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You are DISARM") self.assertEqual(len(self.bomb_queue), 0)
def test_error_attacker_is_dead(self): """ Attacker is DEAD, Error. """ attacker = self.p1a attacker.state = "DEAD" attacker.put() params = self.default_params with self.assertRaises(MeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You are DEAD") self.assertEqual(len(self.bomb_queue), 0)
def test_error_attacker_bomb_time_invalid(self): """ Attacker wants to set a bomb but his bomb quota for today is expired""" attacker = self.p1a attacker.can_set_after = Util.next_day() params = self.default_params with self.assertRaises(TimeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You cannot set bomb before time {}.".\ format(Util.utc_to_chi(attacker.can_set_after).strftime(\ "%m-%d %I:%M%p"))) self.assertEqual(len(self.bomb_queue), 0)
def test_error_time_is_before_now(self): """ Attacker wants to set a bomb back in time """ attacker = self.p1a now = Util.utc_to_chi(datetime.now()) - timedelta(hours=1) params = ["Here", str(now.month), str(now.day),\ str(now.hour), str(now.minute)] with self.assertRaises(TimeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You cannot set {} before time {}."\ .format(now.strftime("%m-%d %I:%M%p"),\ Util.utc_to_chi(datetime.now()).strftime("%m-%d %I:%M%p"))) self.assertEqual(len(self.bomb_queue), 0)
def inner_handler(cls, message): """ Return [(number, msg),...]""" action, params = CommandHandler.get_command(message.Body) attacker = Util.get_attacker(message.From) if action == "KILL": return Kill.handler(attacker, params) elif action[1:] == "REPLY": ref = params.pop(0)[:-1] return Reply.handler(ref, params, attacker) elif action == "BOMB": return Bomb.handler(attacker, params) elif action == "INVUL": return Invul.handler(attacker, params) elif action == "DISARM": return Disarm.handler(attacker, params) elif action == "SNIPE": if message.From != WEI_HAN: raise CommandError(action) sniper = Player.query(Player.codename == params[0]).get() if sniper == None: raise DbError(params[0]) return Snipe.handler(sniper, params[1]) elif action == "?": msg = "Guide for SAMSU Assassins:\n" msg += "KILL <target codename>\n" msg += "BOMB <mm> <dd> <hour> <min> <place>\n" msg += "INVUL <target codename> <mm> <dd> <hour> <min>\n" msg += "DISARM <target codename>\n" msg += "SNIPE - send message and picture to {}\n".format(WEI_HAN) msg += "REPLY - [REPLY <number>] Y or [REPLY <number>] N\n" msg += "If you receive an UNKNOWN ERROR or don't get a message that you expect, contact Wei Han at 312-731-0539." return [(attacker.key.id(), msg)] else: raise CommandError(action)