def test_route_files(test_client, test_session):
    with test_client.session_transaction() as client_session:
        client_session.update(test_session)

    root_path = "web-app-prod-data"
    bucket_name = "test_bucket"
    paths = load_user_lookup(test_session)
    stubber = stubs.mock_s3_list_objects(bucket_name, paths)

    with stubber:
        response = test_client.get("/files")
        body = response.data.decode()

        assert response.status_code == 200
        assert "You're currently logged in as" in body
        assert (
            '<span class="covid-transfer-email">[email protected]</span>'
            in body)
        # Check the root path is removed from the presented link  text
        # This test may need to change if we change the download
        # behaviour
        assert f">{root_path}/local_authority" not in body
        assert "There are 10 files available to download:" in body
        assert "local_authority/haringey/people1.csv" in body
        assert "local_authority/haringey/people4.csv" in body
def test_get_files(test_session):
    """ Test mocked delete ssm param """
    root_path = "web-app-prod-data"
    bucket_name = "test_bucket"
    paths = load_user_lookup(test_session)

    stubber = stubs.mock_s3_list_objects(bucket_name, paths)

    with stubber:
        os.environ["AWS_ACCESS_KEY_ID"] = "fake"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "fake"
        matched_files = get_files(bucket_name, test_session)
        matched_keys = [matched_file["key"] for matched_file in matched_files]
        # check page 1 is there
        assert f"{root_path}/local_authority/haringey/people1.csv" in matched_keys
        # check page 2 is there
        assert f"{root_path}/local_authority/haringey/people4.csv" in matched_keys
        # check that recursive paths are returned
        assert (
            f"{root_path}/local_authority/haringey/nested/nested_people1.csv"
            in matched_keys)
        # check that page 1 of 2nd prefix is returned
        assert f"{root_path}/local_authority/barnet/people1.csv" in matched_keys
        # check that page 2 of 2nd prefix is returned
        assert f"{root_path}/local_authority/barnet/people4.csv" in matched_keys
        stubber.deactivate()
def test_create_presigned_url(test_session):
    """ Test creation of presigned url """
    bucket = "test_bucket"
    paths = load_user_lookup(test_session)
    stubber = stubs.mock_s3_list_objects(bucket, paths)

    with stubber:
        key = "test_key"
        url = create_presigned_url(bucket, key, expiration=600)
        assert "https://test_bucket.s3.amazonaws.com/test_key" in url
        stubber.deactivate()
def test_collect_files_by_date(test_session):
    bucket_name = "test_bucket"
    paths = load_user_lookup(test_session)
    now = datetime.utcnow()
    date_string = now.strftime("%d/%m/%Y")

    stubber = stubs.mock_s3_list_objects(bucket_name, paths)
    with stubber:
        os.environ["AWS_ACCESS_KEY_ID"] = "fake"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "fake"
        matched_files = get_files(bucket_name, test_session)
        collected = collect_files_by_date(matched_files)
        assert date_string in collected["by_date"]
        assert collected["count"] == 10
def test_route_upload_allowed(test_client, test_upload_session):

    bucket_name = "test_bucket"
    paths = user_custom_paths(test_upload_session, True)

    with test_client.session_transaction() as client_session:
        client_session.update(test_upload_session)

    stubber = stubs.mock_s3_list_objects(bucket_name, paths, True)
    with stubber:

        response = test_client.get("/upload")
        body = response.data.decode()
        assert response.status_code == 200
        assert '<h2 class="govuk-heading-m">File settings</h2>' in body
        assert '<h2 class="govuk-heading-m">Upload history</h2>' in body
        assert "local_authority/haringey/people1.csv" in body
        assert "local_authority/haringey/nested/nested_people1.csv" in body
        assert "local_authority/barnet/people4.csv" in body
        assert "local_authority/haringey/people1.csv-metadata.json" not in body