예제 #1
0
async def test_remove_uploaded_file_challenge__remove_null_file():
    files = {
        "challenge_file": (
            "test_file.txt",
            io.StringIO("Goodbye world"),
        )
    }

    challenge_data = {
        "name": "challenge old",
        "description": None,
        "flag": "secret flag",
        "file_name": None,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post(
            f"/challenges/{db_challenge.id}/remove-file", files=files)

    assert response.status_code == 401
    assert response.json() == {"detail": "There is no file for this challenge"}

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #2
0
async def test_get_challenge__success():
    challenge_data = {
        "name": "challenge1",
        "description": None,
        "flag": "secret flag",
        "file_name": None,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

        challenge_details = challenge_schemas.Details.from_orm(db_challenge)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.get(f"/challenges/{challenge_details.id}")

    assert response.status_code == 200
    assert response.json() == challenge_details

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #3
0
async def test_create_challenge__already_exists():
    challenge_data = {
        "name": "challenge1",
        "description": None,
        "flag": "secret flag",
        "file_name": None,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post("/challenges/", json=challenge_data)

    assert response.status_code == 409
    assert response.json() == {
        "detail": f"The name: { challenge_data['name'] } is already taken"
    }

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #4
0
async def test_create_attempt__success():
    team_data = {
        "name": "team1",
    }

    challenge_data = {
        "name": "attempt1",
        "description": "Some description",
        "flag": "secret flag",
        "file_name": None,
    }

    db_team = Team(**team_data)
    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_team)
        session.add(db_challenge)

        session.commit()

        session.refresh(db_team)
        session.refresh(db_challenge)

        attempt_data = {
            "flag": "flag1",
            "team_id": db_team.id,
            "challenge_id": db_challenge.id,
        }

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post("/attempts/", json=attempt_data)

    with dal.get_session_ctx() as session:
        db_attempt = (session.query(Attempt).filter(
            and_(
                Attempt.flag == attempt_data["flag"],
                Attempt.team_id == attempt_data["team_id"],
                Attempt.challenge_id == attempt_data["challenge_id"],
            )).first())

        attempt_details = attempt_schemas.Details.from_orm(db_attempt)

    assert response.status_code == 200
    assert response.json() == attempt_details

    with dal.get_session_ctx() as session:
        session.delete(db_attempt)
        session.delete(db_challenge)
        session.delete(db_team)

        session.commit()
예제 #5
0
async def test_get_attempt__success():
    team_data = {
        "name": "team1",
    }

    challenge_data = {
        "name": "attempt1",
        "description": "Some description",
        "flag": "secret flag",
        "file_name": None,
    }

    db_team = Team(**team_data)
    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_team)
        session.add(db_challenge)

        session.flush()

        attempt_data = {
            "flag": "flag1",
            "team_id": db_team.id,
            "challenge_id": db_challenge.id,
        }

        db_attempt = Attempt(**attempt_data)
        session.add(db_attempt)

        session.commit()

        session.refresh(db_team)
        session.refresh(db_challenge)
        session.refresh(db_attempt)

        attempt_details = attempt_schemas.Details.from_orm(db_attempt)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.get(f"/attempts/{db_attempt.id}")

    assert response.status_code == 200
    assert response.json() == attempt_details

    with dal.get_session_ctx() as session:
        session.delete(db_attempt)
        session.delete(db_challenge)
        session.delete(db_team)

        session.commit()
예제 #6
0
async def test_upload_file_challenge__success():
    challenge_data = {
        "name": "challenge old",
        "description": None,
        "flag": "secret flag",
        "file_name": None,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    files = {
        "challenge_file": (
            "test_file.txt",
            io.StringIO("Goodbye world"),
        )
    }

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post(
            f"/challenges/{db_challenge.id}/upload-file", files=files)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.refresh(db_challenge)

        challenge_update = challenge_schemas.Details.from_orm(db_challenge)

    assert response.status_code == 200
    assert response.json() == challenge_update

    filepath = os.path.join(constants.UPLOAD_FILE_LOCATION,
                            challenge_update.file_name)

    assert os.path.isfile(filepath)

    os.remove(filepath)

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #7
0
async def test_upload_file_challenge__already_uploaded():
    filename = "test_file.txt"

    files = {
        "challenge_file": (
            filename,
            io.StringIO("Goodbye world"),
        )
    }

    challenge_data = {
        "name": "challenge old",
        "description": None,
        "flag": "secret flag",
        "file_name": filename,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post(
            f"/challenges/{db_challenge.id}/upload-file", files=files)

    assert response.status_code == 401
    assert response.json() == {
        "detail": "There is already a file associated with this challenge"
    }

    filepath = os.path.join(constants.UPLOAD_FILE_LOCATION,
                            db_challenge.file_name)
    assert not os.path.isfile(filepath)

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #8
0
async def test_remove_uploaded_file_challenge__success():
    challenge_data = {
        "name": "challenge old",
        "description": None,
        "flag": "secret flag",
        "file_name": "test_file.txt",
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    filepath = os.path.join(constants.UPLOAD_FILE_LOCATION,
                            db_challenge.file_name)

    open(filepath, "w").close()

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.post(
            f"/challenges/{db_challenge.id}/remove-file")

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.refresh(db_challenge)

        challenge_update = challenge_schemas.Details.from_orm(db_challenge)

    assert response.status_code == 200
    assert response.json() == challenge_update

    assert not os.path.isfile(filepath)

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #9
0
async def test_delete_challenge__success():
    challenge_data = {
        "name": "challenge1",
        "description": None,
        "flag": "secret flag",
        "file_name": None,
    }

    db_challenge = Challenge(**challenge_data)

    with dal.get_session_ctx() as session:
        session.add(db_challenge)
        session.commit()
        session.refresh(db_challenge)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.delete(f"/challenges/{db_challenge.id}")

    assert response.status_code == 204

    with dal.get_session_ctx() as session:
        session.delete(db_challenge)
        session.commit()
예제 #10
0
async def test_update_attempt__success():
    team_datas = [
        {
            "name": "team1",
        },
        {
            "name": "team2",
        },
    ]

    challenge_datas = [
        {
            "name": "attempt1",
            "description": "Some description",
            "flag": "secret flag",
            "file_name": None,
        },
        {
            "name": "attempt2",
            "description": "Some description",
            "flag": "secret flag",
            "file_name": None,
        },
    ]

    db_team1 = Team(**team_datas[0])
    db_team2 = Team(**team_datas[1])

    db_challenge1 = Challenge(**challenge_datas[0])
    db_challenge2 = Challenge(**challenge_datas[1])

    with dal.get_session_ctx() as session:
        session.add(db_team1)
        session.add(db_team2)

        session.add(db_challenge1)
        session.add(db_challenge2)

        session.flush()

        old_attempt_data = {
            "flag": "flag1",
            "team_id": db_team1.id,
            "challenge_id": db_challenge1.id,
        }

        new_attempt_data = {
            "flag": "flag2",
            "team_id": db_team2.id,
            "challenge_id": db_challenge2.id,
        }

        db_attempt = Attempt(**old_attempt_data)
        session.add(db_attempt)

        session.commit()

        session.refresh(db_team1)
        session.refresh(db_team2)
        session.refresh(db_challenge1)
        session.refresh(db_challenge2)
        session.refresh(db_attempt)

    async with AsyncClient(app=app, base_url=BASE_URL) as client:
        response = await client.put(f"/attempts/{db_attempt.id}",
                                    json=new_attempt_data)

    with dal.get_session_ctx() as session:
        session.add(db_attempt)
        session.refresh(db_attempt)

        attempt_details = attempt_schemas.Details.from_orm(db_attempt)

    assert response.status_code == 200
    assert response.json() == attempt_details

    with dal.get_session_ctx() as session:
        session.delete(db_attempt)
        session.delete(db_challenge1)
        session.delete(db_challenge2)
        session.delete(db_team1)
        session.delete(db_team2)

        session.commit()