Skip to content

Commit

Permalink
Create initial test for grants
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jul 6, 2022
1 parent da557d9 commit 8e9a4f3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
{%- endmacro -%}

{% macro apply_grants(relation, grant_config, should_revoke) %}
{{ return(adapter.dispatch("apply_grants", "dbt")(relation, grant_config, should_revoke)) }}
{% if grant_config %}
{{ return(adapter.dispatch("apply_grants", "dbt")(relation, grant_config, should_revoke)) }}
{% endif %}
{% endmacro %}

{% macro default__apply_grants(relation, grant_config, should_revoke=True) %}
Expand Down
4 changes: 3 additions & 1 deletion core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@ def read_manifest_for_partial_parse(self) -> Optional[Manifest]:
if not flags.PARTIAL_PARSE:
fire_event(PartialParsingNotEnabled())
return None
path = os.path.join(self.root_project.target_path, PARTIAL_PARSE_FILE_NAME)
path = os.path.join(
self.root_project.project_root, self.root_project.target_path, PARTIAL_PARSE_FILE_NAME
)

reparse_reason = None

Expand Down
3 changes: 3 additions & 0 deletions test/setup_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ psql -c "GRANT CREATE, CONNECT ON DATABASE dbt TO root WITH GRANT OPTION;"
psql -c "CREATE ROLE noaccess WITH PASSWORD 'password' NOSUPERUSER;"
psql -c "ALTER ROLE noaccess WITH LOGIN;"
psql -c "GRANT CONNECT ON DATABASE dbt TO noaccess;"
psql -c "CREATE ROLE dbt_test_user_1;"
psql -c "CREATE ROLE dbt_test_user_2;"
psql -c "CREATE ROLE dbt_test_user_3;"

psql -c 'CREATE DATABASE "dbtMixedCase";'
psql -c 'GRANT CREATE, CONNECT ON DATABASE "dbtMixedCase" TO root WITH GRANT OPTION;'
Expand Down
58 changes: 58 additions & 0 deletions tests/adapter/dbt/tests/adapter/grants/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import os
from dbt.tests.util import (
run_dbt,
get_manifest,
read_file,
relation_from_name,
)

TEST_USER_ENV_VARS = ["DBT_TEST_USER_1", "DBT_TEST_USER_2", "DBT_TEST_USER_3"]

my_model_sql = """
select 1 as fun
"""

model_schema_yml = """
version: 2
models:
- name: my_model
config:
grants:
select: ["dbt_test_user_1"]
"""


class TestModelGrants:
@pytest.fixture(scope="class")
def models(self):
return {"my_model.sql": my_model_sql, "schema.yml": model_schema_yml}

@pytest.fixture(scope="class", autouse=True)
def test_users(self, project):
test_users = []
missing = []
for env_var in TEST_USER_ENV_VARS:
user_name = os.getenv(env_var)
if not user_name:
missing.append(env_var)
else:
test_users.append(user_name)
if missing:
pytest.skip(f"Test requires env vars with test users. Missing {', '.join(missing)}.")
return test_users

def test_basic(self, project, test_users, logs_dir):
# Tests that a project with a single model works
results = run_dbt(["run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
expected = {"select": ["dbt_test_user_1"]}
assert model.config.grants == expected

log_contents = read_file(logs_dir, "dbt.log")
my_model_relation = relation_from_name(project.adapter, "my_model")
grant_log_line = f"grant select on {my_model_relation} to dbt_test_user_1;"
assert grant_log_line in log_contents

0 comments on commit 8e9a4f3

Please sign in to comment.