async def test_load_success() -> None:
    key = key_for_otp_sha(_OTP_SHA)
    await managers.otp_redis.set(key=key,
                                 value=json.dumps(
                                     {"symptoms_started_on": "2020-03-01"}))
    actual = await validate_otp_token(otp_sha=_OTP_SHA)
    assert actual.symptoms_started_on == date(year=2020, month=3, day=1)
async def otp() -> OtpData:
    # Key is 12345
    otp = OtpData(symptoms_started_on=date.today())
    # Authorize this otp
    await managers.otp_redis.set(
        key_for_otp_sha(sha256("12345".encode("utf-8")).hexdigest()), OtpDataSchema().dumps(otp),
    )
    return otp
Ejemplo n.º 3
0
def _key_for_otp(otp: str) -> str:
    """
    Return database key associated with the given OTP.

    :param otp: the OTP whose key is to be computed.
    :return: the database key associated with the given OTP.
    """
    otp_sha = sha256(otp.encode("utf-8")).hexdigest()
    return key_for_otp_sha(otp_sha)
Ejemplo n.º 4
0
async def test_upload_otp_complete(
    client: TestClient,
    otp: OtpData,
    auth_headers: Dict[str, str],
    upload_data: Dict,
    include_infos: bool,
    include_summaries: bool,
) -> None:

    otp_sha = sha256("12345".encode("utf-8")).hexdigest()

    if not include_infos:
        upload_data["exposure_detection_summaries"][0]["exposure_info"] = []
    if not include_summaries:
        upload_data["exposure_detection_summaries"] = []

    auth_headers.update(CONTENT_TYPE_HEADER)
    response = await client.post(
        "/v1/ingestion/upload",
        json=upload_data,
        headers=auth_headers,
    )

    assert await managers.otp_redis.get(key_for_otp_sha(otp_sha)) is None

    assert response.status == 204
    assert Upload.objects.count() == 1
    upload = Upload.objects.first()
    assert upload.to_publish is True
    assert upload.symptoms_started_on == otp.symptoms_started_on
    assert len(upload.keys) == 14
    assert upload.keys[0].key_data == upload_data["teks"][0]["key_data"]
    assert upload.keys[0].rolling_start_number == upload_data["teks"][0][
        "rolling_start_number"]
    assert upload.keys[0].rolling_period == 144
    assert upload.keys[1].key_data == upload_data["teks"][1]["key_data"]
    assert upload.keys[1].rolling_start_number == upload_data["teks"][1][
        "rolling_start_number"]
    assert await managers.analytics_redis.llen(config.ANALYTICS_QUEUE_KEY) == 1
    enqueued_message = await managers.analytics_redis.lpop(
        config.ANALYTICS_QUEUE_KEY)
    assert enqueued_message
    content = json.loads(enqueued_message)

    assert content == dict(
        version=1,
        payload=dict(
            province="AG",
            exposure_detection_summaries=upload_data[
                "exposure_detection_summaries"],
        ),
    )
async def validate_otp_token(otp_sha: str, delete: bool = False) -> OtpData:
    """
    Load an OtpData model from the database.

    :param otp_sha: the sha256 of the OTP code.
    :param delete: if true, deletes the key right after retrieving its content.
    :return: the deserialized OtpData model associated with the given sha256 string.
    :raises: UnauthorizedOtpException if there is no OtpData associated with the given sha256.
    """

    key = key_for_otp_sha(otp_sha)

    if not delete:
        data = await managers.otp_redis.get(key)
    else:
        pipe = managers.otp_redis.pipeline()
        pipe.get(key)
        pipe.delete(key)
        data = (await pipe.execute())[0]
    if data is None:
        raise UnauthorizedOtpException()
    return OtpDataSchema().loads(data)
Ejemplo n.º 6
0
def test_key_for_otp_sha() -> None:
    sha = sha256("an-otp".encode("utf-8")).hexdigest()
    assert key_for_otp_sha(sha) == f"~otp:{sha}"