def test_log_stderr(self): Utils.prepare_test(connect_logger=True) with Utils.nostderr() as err: Logger.error("FOO_CAT", "Log message") self.assertIn("FOO_CAT", err.buffer) self.assertIn("Log message", err.buffer)
def test_submit_broken_transaction(self, gen_i_mock, a_s_mock, g_f_s_mock, g_i_mock): Utils.start_contest() self._insert_data() self.handler.generate_input(token="token", task="poldo", _ip="1.1.1.1") Database.c.execute( "INSERT INTO outputs (id, input, path, size, result) " "VALUES ('outputid', 'inputid', '/output', 42," ":result)", { "result": b'{"score":0.5,"feedback":{"a":1},' b'"validation":{"b":2}}' }, ) Database.c.execute("INSERT INTO sources (id, input, path, size) " "VALUES ('sourceid', 'inputid', '/source', 42)") with self.assertRaises(BadRequest) as ex: self.handler.submit(output_id="outputid", source_id="sourceid", _ip="1.1.1.1") self.assertIn("Error inserting the submission", ex.exception.response.data.decode()) self.assertIsNone(Database.get_submission("subid"))
def test_get_user_invalid_token(self): Utils.start_contest() with self.assertRaises(Forbidden) as ex: self.handler.get_user(token="invalid token", _ip="1.1.1.1") response = ex.exception.response.data.decode() self.assertIn("No such user", response)
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_sumbit_not_matching(self, g_f_s_mock, g_i_mock): Utils.start_contest() self._insert_data(token="token", task="poldo") self._insert_data(token="token2", task="poldo") backup = Logger.LOG_LEVEL Logger.LOG_LEVEL = 9001 self.handler.generate_input(token='token', task='poldo', _ip='1.1.1.1') g_i_mock.return_value = ("inputid2", "/path") self.handler.generate_input(token='token2', task='poldo', _ip='1.1.1.1') Database.c.execute( "INSERT INTO outputs (id, input, path, size, result) " "VALUES ('outputid', 'inputid', '/output', 42, '{}')") Database.c.execute("INSERT INTO sources (id, input, path, size) " "VALUES ('sourceid', 'inputid2', '/source', 42)") with self.assertRaises(Forbidden) as ex: self.handler.submit(output_id='outputid', source_id='sourceid', _ip='1.1.1.1') self.assertIn("The provided pair of source-output is invalid", ex.exception.response.data.decode()) Logger.LOG_LEVEL = backup
def test_submit(self, g_f_s_mock, g_i_mock): Utils.start_contest() self._insert_data() self.handler.generate_input(token='token', task='poldo', _ip='1.1.1.1') Database.c.execute( "INSERT INTO outputs (id, input, path, size, result) " "VALUES ('outputid', 'inputid', '/output', 42," ":result)", { "result": b'{\"score\":0.5,\"feedback\":{\"a\":1},' b'\"validation\":{\"b\":2}}' }) Database.c.execute("INSERT INTO sources (id, input, path, size) " "VALUES ('sourceid', 'inputid', '/source', 42)") response = self.handler.submit(output_id='outputid', source_id='sourceid', _ip='1.1.1.1') self.assertEqual("token", response["token"]) self.assertEqual("poldo", response["task"]) self.assertEqual(21, response["score"]) self.assertEqual("inputid", response["input"]["id"]) self.assertEqual("sourceid", response["source"]["id"]) self.assertEqual("outputid", response["output"]["id"]) self.assertEqual(1, response["feedback"]["a"]) self.assertEqual(2, response["output"]["validation"]["b"]) user_task = Database.get_user_task("token", "poldo") self.assertEqual(21, user_task["score"]) self.assertEqual(response["id"], Database.get_submission(response["id"])["id"])
def setUp(self): Utils.prepare_test() self.handler = ContestHandler() self.inputid = "inputid" self.log_backup = Logger.LOG_LEVEL Logger.LOG_LEVEL = 9001 # disable the logs
def test_get_source_invalid_id(self): Utils.start_contest() with self.assertRaises(Forbidden) as ex: self.handler.get_source(source_id="invalid source", _ip="1.1.1.1") response = ex.exception.response.data.decode() self.assertIn("No such source", response)
def test_get_user_no_current_attempt(self): Utils.start_contest(since=100, duration=200) Database.add_user("token", "", "") Database.add_task("poldo", "", "", 42, 1) Database.add_user_task("token", "poldo") res = self.handler.get_user(token="token", _ip="1.1.1.1") self.assertEqual(None, res["tasks"]["poldo"]["current_input"])
def setUp(self): Utils.prepare_test() self.admin_handler = AdminHandler() Database.set_meta("admin_token", "ADMIN-TOKEN") self.log_backup = Logger.LOG_LEVEL Logger.LOG_LEVEL = 9001 # disable the logs
def test_start_reset(self): Utils.start_contest(since=-100) out = self.admin_handler.start("reset", admin_token="ADMIN-TOKEN", _ip="1.2.3.4") self.assertIsNone(out["start_time"]) self.assertIsNone(Database.get_meta("start_time"))
def test_get_logs_by_category(self): Utils.prepare_test(connect_logger=True) TestLogger.load_logs() start_date = datetime.datetime.now().timestamp() - 10 end_date = datetime.datetime.now().timestamp() + 10 logs = Logger.get_logs(Logger.DEBUG, "CATEGORY", start_date, end_date) self.assertEqual(2, len(logs))
def test_read_from_disk_transaction_failed(self, add_task_mock): path = Utils.new_tmp_dir() self._prepare_contest_dir(path) Config.statementdir = Utils.new_tmp_dir() Config.contest_path = path with self.assertRaises(Exception) as ex: ContestManager.read_from_disk() self.assertEqual("ops...", ex.exception.args[0]) self.assertIsNone(Database.get_meta("contest_duration"))
def test_get_logs_by_date(self): Utils.prepare_test(connect_logger=True) TestLogger.load_logs() start_date = TestLogger.VERY_FAR_IN_TIME - 10 end_date = TestLogger.VERY_FAR_IN_TIME + 10 logs = Logger.get_logs(Logger.DEBUG, None, start_date, end_date) self.assertEqual(1, len(logs))
def test_upload_output_invalid_input(self): Utils.start_contest() with self.assertRaises(Forbidden) as ex: self.handler.upload_output(input_id="invalid input", _ip="1.1.1.1", _file_content="foo", _file_name="bar") self.assertIn("No such input", ex.exception.response.data.decode())
def test_get_source(self): Utils.start_contest() Database.add_user("token", "", "") Database.add_task("poldo", "", "", 42, 1) Database.add_input("inputid", "token", "poldo", 1, "/path", 42) Database.add_source("sourceid", "inputid", "/path", 42) res = self.handler.get_source(source_id="sourceid", _ip="1.1.1.1") self.assertEqual("sourceid", res["id"])
def test_generate_input_invalid_task(self): Utils.start_contest() self._insert_data() with self.assertRaises(Forbidden) as ex: self.handler.generate_input(token='token', task='invalid task', _ip='1.1.1.1') self.assertIn("No such task", ex.exception.response.data.decode())
def test_start_now_already_started(self): Utils.start_contest() with self.assertRaises(Forbidden) as ex: self.admin_handler.start(start_time="now", admin_token="ADMIN-TOKEN", _ip="1.2.3.4") self.assertIn("Contest has already been started", ex.exception.response.data.decode())
def test_upload_output_check_failed(self, gen_mock, eval_mock): Utils.start_contest() self._insert_data() with self.assertRaises(InternalServerError): self.handler.upload_output( input_id="inputid", _ip="1.1.1.1", file={"content": "foobar".encode(), "name": "output.txt"}, )
def test_generate_input_invalid_token(self): Utils.start_contest() self._insert_data() with self.assertRaises(Forbidden) as ex: self.handler.generate_input(token="invalid token", task="poldo", _ip="1.1.1.1") self.assertIn("No such user", ex.exception.response.data.decode())
def test_log(self): Utils.prepare_test(connect_logger=True) Logger.log(Logger.DEBUG, "FOO_CAT", "Log message") Logger.c.execute("SELECT * FROM logs WHERE category = 'FOO_CAT'") row = Logger.c.fetchone() self.assertEqual("FOO_CAT", row[1]) self.assertEqual(Logger.DEBUG, int(row[2])) self.assertEqual("Log message", row[3])
def test_get_submissions_invalid_task(self): Utils.start_contest() Database.add_user("token", "", "") with self.assertRaises(Forbidden) as ex: self.handler.get_submissions(token="token", task="invalid task", _ip="1.1.1.1") response = ex.exception.response.data.decode() self.assertIn("No such task", response)
def test_get_input(self): Database.add_user("token", "", "") Database.add_task("poldo", "", "", 42, 1) Database.add_input("inputid", "token", "poldo", 1, "/path", 42) Utils.start_contest() res = self.handler.get_input(input_id="inputid", _ip="1.1.1.1") self.assertEqual("inputid", res["id"]) self.assertEqual("token", res["token"]) self.assertEqual("poldo", res["task"])
def test_upload_output_invalid_file_name(self, gen_mock): Utils.start_contest() self._insert_data() with self.assertRaises(BadRequest): self.handler.upload_output( input_id="inputid", _ip="1.1.1.1", file={"content": "foobar".encode(), "name": ".."}, )
def test_submit_invalid_output(self): Utils.start_contest() self._insert_data() with self.assertRaises(Forbidden) as ex: self.handler.submit(output_id='invalid output', source_id='invalid source', _ip='1.1.1.1') self.assertIn("No such output", ex.exception.response.data.decode())
def test_get_logs_by_level(self): Utils.prepare_test(connect_logger=True) TestLogger.load_logs() start_date = datetime.datetime.now().timestamp() - 10 end_date = datetime.datetime.now().timestamp() + 10 logs = Logger.get_logs(Logger.DEBUG, None, start_date, end_date) self.assertEqual(4, len(logs)) logs = Logger.get_logs(Logger.WARNING, None, start_date, end_date) self.assertEqual(2, len(logs))
def test_import_contest_removes_file(self): path = Utils.new_tmp_dir() self._prepare_contest_dir(path) Config.statementdir = Utils.new_tmp_dir() self._write_file(Config.statementdir, "poldo", "foobar") self.assertTrue( os.path.isfile(os.path.join(Config.statementdir, "poldo"))) ContestManager.import_contest(path) self.assertTrue( os.path.isdir(os.path.join(Config.statementdir, "poldo")))
def test_get_output(self): Database.add_user("token", "", "") Database.add_task("poldo", "", "", 42, 1) Database.add_input("inputid", "token", "poldo", 1, "/path", 42) Database.add_output("outputid", "inputid", "/path", 42, b'{"validation":42}') Utils.start_contest() res = self.handler.get_output(output_id="outputid", _ip="1.1.1.1") self.assertEqual("outputid", res["id"]) self.assertEqual(42, res["validation"])
def test_start_now_but_started_in_future(self): Utils.start_contest(since=-100) out = self.admin_handler.start("now", admin_token="ADMIN-TOKEN", _ip="1.2.3.4") start_time = dateutil.parser.parse(out["start_time"]).timestamp() self.assertTrue(start_time >= datetime.utcnow().timestamp() - 10) start_time_db = datetime.fromtimestamp( Database.get_meta("start_time", type=int), timezone.utc) self.assertEqual(start_time, start_time_db.timestamp())
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))