databrickslabs / pytester

Python Testing for Databricks
https://pypi.org/project/databricks-labs-pytester/
Other
51 stars 5 forks source link

Added `make_run_as` fixture #82

Closed nfx closed 19 hours ago

nfx commented 1 day ago

make_run_as fixture

This fixture provides a function to create an account service principal via acc fixture and assign it to a workspace. The service principal is removed after the test is complete. The service principal is created with a random display name and assigned to the workspace with the default permissions.

Use the account_groups argument to assign the service principal to account groups, which have the required permissions to perform a specific action.

Example:

def test_run_as_lower_privilege_user(make_run_as, ws):
    run_as = make_run_as(account_groups=['account.group.name'])
    through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
    me = ws.current_user.me()
    assert me.user_name != through_query.my_name

Returned object has the following properties:

Example:

from pytest import fixture

@fixture
def ws(make_run_as):
    run_as = make_run_as(account_groups=['account.group.used.for.all.tests.in.this.file'])
    return run_as.ws

def test_creating_notebook_on_behalf_of_ephemeral_principal(make_notebook):
    notebook = make_notebook()
    assert notebook.exists()

See also acc, ws, make_random, env_or_skip, log_account_link.

github-actions[bot] commented 1 day ago

✅ 38/38 passed, 6 skipped, 4m59s total

Running from acceptance #135