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_load_user_lookup(test_session):
    root_path = "web-app-prod-data"
    paths = load_user_lookup(test_session)
    assert f"{root_path}/local_authority/haringey" in paths
    assert f"{root_path}/local_authority/barnet" in paths
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": ""
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 0
    replace_paths = f"{root_path}/local_authority/haringey;"
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": replace_paths
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 1
    assert f"{root_path}/local_authority/haringey" in paths
    assert f"{root_path}/local_authority/barnet" not in paths
    replace_paths = ";web-app-prod-data/local_authority/haringey"
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": replace_paths
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 1
    assert f"{root_path}/local_authority/haringey" in paths
    assert f"{root_path}/local_authority/barnet" not in paths
    replace_paths = (f"{root_path}/local_authority/haringey;;"
                     f"{root_path}/local_authority/barnet")
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": replace_paths
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 2
    assert f"{root_path}/local_authority/haringey" in paths
    assert f"{root_path}/local_authority/barnet" in paths

    fake_root_path = "web-app-nonprod-data"
    replace_paths = (f"{fake_root_path}/local_authority/haringey;;"
                     f"{fake_root_path}/local_authority/barnet")
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": replace_paths
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 0

    replace_paths = (f"{fake_root_path}/local_authority/haringey;;"
                     f"{root_path}/local_authority/barnet")
    test_session.update({
        "attributes": [
            {
                "Name": "custom:is_la",
                "Value": "1"
            },
            {
                "Name": "custom:paths",
                "Value": replace_paths
            },
        ]
    })
    paths = load_user_lookup(test_session)
    assert len(paths) == 1
    # check paths outside approved root path are not returned
    assert f"{fake_root_path}/local_authority/haringey" not in paths
    assert f"{root_path}/local_authority/barnet" in paths