def test_drop_contest_not_deletable(self): Utils.setup_encrypted_file() Database.del_meta("admin_token") with open(Config.encrypted_file, "wb") as f: f.write(Utils.build_pack(ruamel.yaml.dump({"deletable": False}))) with self.assertRaises(Forbidden): self.admin_handler.drop_contest(Utils.ZIP_TOKEN)
def test_upload_invalid_pack(self): enc_path = os.path.join(Utils.new_tmp_dir(), "pack.zip.enc") Config.encrypted_file = enc_path Database.del_meta("admin_token") with self.assertRaises(Forbidden): self.admin_handler.upload_pack(file={ "content": b"totally not a pack", "name": "pack.zip.enc" }) self.assertFalse(os.path.exists(enc_path))
def test_upload_pack_already_uploaded(self): path = os.path.join(Utils.new_tmp_dir(), "pack.zip.enc") with open(path, "wb") as f: f.write(b"hola!") Config.encrypted_file = path Database.del_meta("admin_token") with self.assertRaises(Forbidden): self.admin_handler.upload_pack(file={ "content": "foobar".encode(), "name": "pack.zip.enc" })
def test_drop_contest(self): Utils.setup_encrypted_file() Database.del_meta("admin_token") with open(Config.encrypted_file, "wb") as f: f.write(Utils.build_pack(ruamel.yaml.dump({"deletable": True}))) self.admin_handler.drop_contest(Utils.ZIP_TOKEN) self.assertFalse(os.path.exists(Config.storedir)) self.assertFalse(os.path.exists(Config.statementdir)) self.assertFalse(os.path.exists(Config.contest_path)) self.assertFalse(os.path.exists(Config.encrypted_file)) self.assertFalse(os.path.exists(Config.decrypted_file)) self.assertTrue(os.path.exists(Config.db))
def test_upload_pack(self): Utils.setup_encrypted_file() upload_path = os.path.join(os.path.dirname(__file__), "../assets/pack.zip.enc") os.remove(Config.encrypted_file) with open(upload_path, "rb") as f: content = f.read() Database.del_meta("admin_token") self.admin_handler.upload_pack(file={ "content": content, "name": "pack.zip.enc" }) self.assertTrue(os.path.exists(Config.encrypted_file))
def test_del_meta(self): Database.connected = False Database.connect_to_database() Database.set_meta('random_key', 4242) self.assertTrue(Database.del_meta('random_key')) self.assertIsNone(Database.get_meta('random_key'))
def start(self, start_time: str): """ POST /admin/start """ previous_start = Database.get_meta("start_time", type=int) now = int(time.time()) if previous_start and now > previous_start: BaseHandler.raise_exc(Forbidden, "FORBIDDEN", "Contest has already been started!") actual_start = None if start_time == "reset": Database.del_meta("start_time") return {"start_time": None} elif start_time == "now": actual_start = now else: actual_start = dateutil.parser.parse(start_time).timestamp() Database.set_meta("start_time", int(actual_start)) Logger.info("CONTEST", "Contest starts at " + str(actual_start)) return BaseHandler.format_dates({"start_time": actual_start}, fields=["start_time"])
def test_drop_contest_not_loaded_wrong_token(self): Utils.setup_encrypted_file() Database.del_meta("admin_token") with self.assertRaises(Forbidden): self.admin_handler.drop_contest("AAAAAA-CZKW-CCJS")
def test_del_meta_non_exist(self): Database.connected = False Database.connect_to_database() self.assertFalse(Database.del_meta('random_key')) self.assertIsNone(Database.get_meta('random_key'))
def read_from_disk(remove_enc=True): """ Load a task from the disk and load the data into the database """ try: contest = ContestManager.import_contest(Config.contest_path) except FileNotFoundError as ex: error = ( "Contest not found, you probably need to unzip it. Missing file %s" % ex.filename) Logger.warning("CONTEST", error) shutil.rmtree(Config.statementdir, ignore_errors=True) shutil.rmtree(Config.web_statementdir, ignore_errors=True) shutil.rmtree(Config.contest_path, ignore_errors=True) if remove_enc: with suppress(Exception): os.remove(Config.encrypted_file) with suppress(Exception): os.remove(Config.decrypted_file) Database.del_meta("admin_token") BaseHandler.raise_exc(UnprocessableEntity, "CONTEST", error) if not Database.get_meta("contest_imported", default=False, type=bool): Database.begin() try: Database.set_meta("contest_duration", contest["duration"], autocommit=False) Database.set_meta("contest_name", contest.get("name", "Contest"), autocommit=False) Database.set_meta( "contest_description", contest.get("description", ""), autocommit=False, ) Database.set_meta( "window_duration", # if None the contest is not USACO-style contest.get("window_duration"), autocommit=False, ) count = 0 for task in contest["tasks"]: Database.add_task( task["name"], task["description"], task["statement_path"], task["max_score"], count, autocommit=False, ) count += 1 for user in contest["users"]: Database.add_user(user["token"], user["name"], user["surname"], autocommit=False) for user in Database.get_users(): for task in Database.get_tasks(): Database.add_user_task(user["token"], task["name"], autocommit=False) Database.set_meta("contest_imported", True, autocommit=False) Database.commit() except: Database.rollback() raise else: # TODO: check that the contest is still the same pass # store the task in the ContestManager singleton ContestManager.tasks = dict( (task["name"], task) for task in contest["tasks"]) ContestManager.has_contest = True # create the queues for the task inputs for task in ContestManager.tasks: ContestManager.input_queue[task] = gevent.queue.Queue( Config.queue_size) gevent.spawn(ContestManager.worker, task)
def test_validate_token_no_token(self, read, extract): Database.del_meta("admin_token") Validators._validate_admin_token("ADMIN-TOKEN", '1.2.3.4') extract.assert_called_once_with("ADMIN-TOKEN") read.assert_called_once_with()