Example #1
0
def test_crud_user(looker_client: mtds.LookerSDK):
    """Test creating, retrieving, updating and deleting a user.
    """

    # Create user
    user = looker_client.create_user(
        ml.WriteUser(first_name="John", last_name="Doe", is_disabled=False, locale="fr")
    )
    assert isinstance(user, ml.User)
    assert isinstance(user.id, int)
    assert user.first_name == "John"
    assert user.last_name == "Doe"
    assert not user.is_disabled
    assert user.locale == "fr"

    # sudo checks
    user_id = user.id
    looker_client.login_user(user_id)
    user = looker_client.me()
    assert user.first_name == "John"
    assert user.last_name == "Doe"
    looker_client.logout()
    user = looker_client.me()
    assert user.first_name != "John"
    assert user.last_name != "Doe"

    # Update user and check fields we didn't intend to change didn't change
    update_user = ml.WriteUser(is_disabled=True, locale="uk")
    looker_client.update_user(user_id, update_user)
    user = looker_client.user(user_id)
    assert user.first_name == "John"
    assert user.last_name == "Doe"
    assert user.locale == "uk"
    assert user.is_disabled

    # Update user and check fields we intended to wipe out are now None
    # first way to specify nulling out a field
    update_user = ml.WriteUser(first_name=ml.EXPLICIT_NULL)
    # second way
    update_user.last_name = ml.EXPLICIT_NULL
    looker_client.update_user(user_id, update_user)
    user = looker_client.user(user_id)
    assert user.first_name is None
    assert user.last_name is None

    # Try adding email creds
    looker_client.create_user_credentials_email(
        user_id, ml.WriteCredentialsEmail(email="*****@*****.**")
    )
    user = looker_client.user(user_id)
    assert isinstance(user.credentials_email, ml.CredentialsEmail)
    assert user.credentials_email.email == "*****@*****.**"

    # Delete user
    resp = looker_client.delete_user(user_id)
    assert resp == ""

    looker_client.logout()
Example #2
0
def test_netrc_does_not_override_ini_creds(sdk: mtds.LookerSDK):
    """The requests library overrides HTTP authorization headers if the auth= parameter
    is not specified, resulting in an authentication error. This test makes sure this
    does not happen when netrc files are found on the system.
    """
    me = sdk.me()
    assert isinstance(me, ml.User)
Example #3
0
def test_me_returns_correct_result(sdk: mtds.LookerSDK):
    """me() should return the current authenticated user
    """
    me = sdk.me()
    assert isinstance(me, ml.User)
    assert isinstance(me.credentials_api3, list)
    assert len(me.credentials_api3) > 0
    assert isinstance(me.credentials_api3[0], ml.CredentialsApi3)
Example #4
0
def test_me_field_filters(sdk: mtds.LookerSDK):
    """me() should return only the requested fields.
    """
    me = sdk.me("id, first_name, last_name")
    assert isinstance(me, ml.User)
    assert isinstance(me.id, int)
    assert isinstance(me.first_name, str)
    assert me.first_name != ""
    assert isinstance(me.last_name, str)
    assert me.last_name != ""
    assert not me.display_name
    assert not me.email
    assert not me.personal_space_id
Example #5
0
def test_crud_dashboard(sdk: mtds.LookerSDK, queries, dashboards):
    """Test creating, retrieving, updating and deleting a user.
    """
    qhash: Dict[Union[str, int], ml.Query] = {}
    for idx, q in enumerate(queries):
        limit = "10"
        request = create_query_request(q, limit)
        key = q.get("id") or str(idx)
        qhash[key] = sdk.create_query(request)

    for d in dashboards:
        dashboard = sdk.create_dashboard(
            ml.WriteDashboard(
                description=d.get("description"),
                hidden=d.get("hidden"),
                query_timezone=d.get("query_timezone"),
                refresh_interval=d.get("refresh_interval"),
                title=d.get("title"),
                background_color=d.get("background_color"),
                load_configuration=d.get("load_configuration"),
                lookml_link_id=d.get("lookml_link_id"),
                show_filters_bar=d.get("show_filters_bar"),
                show_title=d.get("show_title"),
                slug=d.get("slug"),
                space_id=d.get("space_id") or sdk.me().home_space_id,
                text_tile_text_color=d.get("text_tile_text_color"),
                tile_background_color=d.get("tile_background_color"),
                tile_text_color=d.get("tile_text_color"),
                title_color=d.get("title_color"),
            ))

        assert isinstance(dashboard, ml.Dashboard)

        if d.get("background_color"):
            assert d["background_color"] == dashboard.background_color

        if d.get("text_tile_text_color"):
            assert d["text_tile_text_color"] == dashboard.text_tile_text_color

        if d.get("tile_background_color"):
            assert d[
                "tile_background_color"] == dashboard.tile_background_color

        if d.get("tile_text_color"):
            assert d["tile_text_color"] == dashboard.tile_text_color

        if d.get("title_color"):
            assert d["title_color"] == dashboard.title_color

        # Update dashboard
        assert isinstance(dashboard.id, str)
        update_response = sdk.update_dashboard(dashboard.id,
                                               ml.WriteDashboard(deleted=True))
        assert update_response.deleted
        assert update_response.title == dashboard.title

        dashboard = sdk.update_dashboard(dashboard.id,
                                         ml.WriteDashboard(deleted=False))
        assert isinstance(dashboard.id, str)
        assert not dashboard.deleted

        if d.get("filters"):
            for f in d["filters"]:
                filter = sdk.create_dashboard_filter(
                    ml.WriteCreateDashboardFilter(
                        dashboard_id=dashboard.id,
                        name=f.get("name"),
                        title=f.get("title"),
                        type=f.get("type"),
                        default_value=f.get("default_value"),
                        model=f.get("model"),
                        explore=f.get("explore"),
                        dimension=f.get("dimension"),
                        row=f.get("row"),
                        listens_to_filters=f.get("listens_to_filters"),
                        allow_multiple_values=f.get("allow_multiple_values"),
                        required=f.get("required"),
                    ))
                assert isinstance(filter, ml.DashboardFilter)
                assert filter.name == f.get("name")
                assert filter.title == f.get("title")
                assert filter.type == f.get("type")
                assert filter.default_value == f.get("default_value")
                assert filter.model == f.get("model")
                assert filter.explore == f.get("explore")
                assert filter.dimension == f.get("dimension")
                assert filter.row == f.get("row")
                assert filter.allow_multiple_values == f.get(
                    "allow_multiple_values", False)
                assert filter.required == f.get("required", False)

        if d.get("tiles"):
            for t in d["tiles"]:
                tile = sdk.create_dashboard_element(
                    ml.WriteDashboardElement(
                        body_text=t.get("body_text"),
                        dashboard_id=dashboard.id,
                        look=t.get("look"),
                        look_id=t.get("look_id"),
                        merge_result_id=t.get("merge_result_id"),
                        note_display=t.get("note_display"),
                        note_state=t.get("note_state"),
                        note_text=t.get("note_text"),
                        query=t.get("query"),
                        query_id=get_query_id(qhash, t.get("query_id")),
                        refresh_interval=t.get("refresh_interval"),
                        subtitle_text=t.get("subtitle_text"),
                        title=t.get("title"),
                        title_hidden=t.get("title_hidden"),
                        type=t.get("type"),
                    ))

                assert isinstance(tile, ml.DashboardElement)
                assert tile.dashboard_id == dashboard.id
                assert tile.title == t.get("title")
                assert tile.type == t.get("type")