From 8e9a4f39bdb36b1367873a64bae360281579cce5 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Wed, 6 Jul 2022 19:01:32 -0400 Subject: [PATCH] Create initial test for grants --- .../macros/adapters/apply_grants.sql | 4 +- core/dbt/parser/manifest.py | 4 +- test/setup_db.sh | 3 + .../dbt/tests/adapter/grants/test_models.py | 58 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/adapter/dbt/tests/adapter/grants/test_models.py diff --git a/core/dbt/include/global_project/macros/adapters/apply_grants.sql b/core/dbt/include/global_project/macros/adapters/apply_grants.sql index 707097f075b..ef3dcb630d2 100644 --- a/core/dbt/include/global_project/macros/adapters/apply_grants.sql +++ b/core/dbt/include/global_project/macros/adapters/apply_grants.sql @@ -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) %} diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index e6aa424d4b2..35145aa0fb2 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -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 diff --git a/test/setup_db.sh b/test/setup_db.sh index 1b22a004eb6..de59bf0fac6 100755 --- a/test/setup_db.sh +++ b/test/setup_db.sh @@ -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;' diff --git a/tests/adapter/dbt/tests/adapter/grants/test_models.py b/tests/adapter/dbt/tests/adapter/grants/test_models.py new file mode 100644 index 00000000000..ab5b03bfe9f --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/grants/test_models.py @@ -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