-
Notifications
You must be signed in to change notification settings - Fork 92
PLAT-160: Migrate test_privileges #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
A-Baji
merged 11 commits into
datajoint:dev-tests
from
ethho:dev-tests-plat-160-privileges
Dec 13, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ecbcac1
cp to tests
ethho fdd4352
Migrate TestUnprivileged
ethho 37248ff
Format with black
ethho 08fcac0
Separate fixture for DB creds dict
ethho 156d229
First pass at migrating test_privileges
ethho 05b3b00
Clean up
ethho 9b0df13
rm commented code
ethho 394cad9
Add default values for db_creds_root
ethho 36623f1
Format with black
ethho a8e20eb
Merge remote-tracking branch 'upstream/dev-tests' into dev-tests-plat…
ethho 88f7b53
Merge remote-tracking branch 'upstream/dev-tests' into dev-tests-plat…
ethho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import datajoint as dj | ||
| import inspect | ||
|
|
||
|
|
||
| class Parent(dj.Lookup): | ||
| definition = """ | ||
| id: int | ||
| """ | ||
| contents = [(1,)] | ||
|
|
||
|
|
||
| class Child(dj.Computed): | ||
| definition = """ | ||
| -> Parent | ||
| """ | ||
|
|
||
| def make(self, key): | ||
| self.insert1(key) | ||
|
|
||
|
|
||
| class NoAccess(dj.Lookup): | ||
| definition = """ | ||
| string: varchar(10) | ||
| """ | ||
|
|
||
|
|
||
| class NoAccessAgain(dj.Manual): | ||
| definition = """ | ||
| -> NoAccess | ||
| """ | ||
|
|
||
|
|
||
| LOCALS_PRIV = {k: v for k, v in locals().items() if inspect.isclass(v)} | ||
| __all__ = list(LOCALS_PRIV) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import os | ||
| import pytest | ||
| import datajoint as dj | ||
| from . import schema, CONN_INFO_ROOT, PREFIX | ||
| from . import schema_privileges | ||
|
|
||
| namespace = locals() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def schema_priv(connection_test): | ||
| schema_priv = dj.Schema( | ||
| context=schema_privileges.LOCALS_PRIV, | ||
| connection=connection_test, | ||
| ) | ||
| schema_priv(schema_privileges.Parent) | ||
| schema_priv(schema_privileges.Child) | ||
| schema_priv(schema_privileges.NoAccess) | ||
| schema_priv(schema_privileges.NoAccessAgain) | ||
| yield schema_priv | ||
| if schema_priv.is_activated(): | ||
| schema_priv.drop() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def connection_djsubset(connection_root, db_creds_root, schema_priv): | ||
| user = "djsubset" | ||
| conn = dj.conn(**db_creds_root, reset=True) | ||
| schema_priv.activate(f"{PREFIX}_schema_privileges") | ||
| conn.query( | ||
| f""" | ||
| CREATE USER IF NOT EXISTS '{user}'@'%%' | ||
| IDENTIFIED BY '{user}' | ||
| """ | ||
| ) | ||
| conn.query( | ||
| f""" | ||
| GRANT SELECT, INSERT, UPDATE, DELETE | ||
| ON `{PREFIX}_schema_privileges`.`#parent` | ||
| TO '{user}'@'%%' | ||
| """ | ||
| ) | ||
| conn.query( | ||
| f""" | ||
| GRANT SELECT, INSERT, UPDATE, DELETE | ||
| ON `{PREFIX}_schema_privileges`.`__child` | ||
| TO '{user}'@'%%' | ||
| """ | ||
| ) | ||
| conn_djsubset = dj.conn( | ||
| host=db_creds_root["host"], | ||
| user=user, | ||
| password=user, | ||
| reset=True, | ||
| ) | ||
| yield conn_djsubset | ||
| conn.query(f"DROP USER {user}") | ||
| conn.query(f"DROP DATABASE {PREFIX}_schema_privileges") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def connection_djview(connection_root, db_creds_root): | ||
| """ | ||
| A connection with only SELECT privilege to djtest schemas. | ||
| Requires connection_root fixture so that `djview` user exists. | ||
| """ | ||
| connection = dj.conn( | ||
| host=db_creds_root["host"], | ||
| user="djview", | ||
| password="djview", | ||
| reset=True, | ||
| ) | ||
| yield connection | ||
|
|
||
|
|
||
| class TestUnprivileged: | ||
| def test_fail_create_schema(self, connection_djview): | ||
| """creating a schema with no CREATE privilege""" | ||
| with pytest.raises(dj.DataJointError): | ||
| return dj.Schema( | ||
| "forbidden_schema", namespace, connection=connection_djview | ||
| ) | ||
|
|
||
| def test_insert_failure(self, connection_djview, schema_any): | ||
| unprivileged = dj.Schema( | ||
| schema_any.database, namespace, connection=connection_djview | ||
| ) | ||
| unprivileged.spawn_missing_classes() | ||
| assert issubclass(Language, dj.Lookup) and len(Language()) == len( | ||
| schema.Language() | ||
| ), "failed to spawn missing classes" | ||
| with pytest.raises(dj.DataJointError): | ||
| Language().insert1(("Socrates", "Greek")) | ||
|
|
||
| def test_failure_to_create_table(self, connection_djview, schema_any): | ||
| unprivileged = dj.Schema( | ||
| schema_any.database, namespace, connection=connection_djview | ||
| ) | ||
|
|
||
| @unprivileged | ||
| class Try(dj.Manual): | ||
| definition = """ # should not matter really | ||
| id : int | ||
| --- | ||
| value : float | ||
| """ | ||
|
|
||
| with pytest.raises(dj.DataJointError): | ||
| Try().insert1((1, 1.5)) | ||
|
|
||
|
|
||
| class TestSubset: | ||
| def test_populate_activate(self, connection_djsubset, schema_priv): | ||
| schema_priv.activate( | ||
| f"{PREFIX}_schema_privileges", create_schema=True, create_tables=False | ||
| ) | ||
| schema_privileges.Child.populate() | ||
| assert schema_privileges.Child.progress(display=False)[0] == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the value of
os.getenv("DJ_HOST")in several test modules. Best to perform this once and inject it as a fixture. Making a note to replace the patternos.getenv("DJ_HOST")with this fixture in other modules besidestest_privileges.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing that we should move to fixtures during the "big clean up".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we just import
CONN_INFO(as done in #1133)