|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import datajoint as dj |
| 4 | +from . import schema, CONN_INFO_ROOT, PREFIX |
| 5 | +from . import schema_privileges |
| 6 | + |
| 7 | +namespace = locals() |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def schema_priv(connection_test): |
| 12 | + schema_priv = dj.Schema( |
| 13 | + context=schema_privileges.LOCALS_PRIV, |
| 14 | + connection=connection_test, |
| 15 | + ) |
| 16 | + schema_priv(schema_privileges.Parent) |
| 17 | + schema_priv(schema_privileges.Child) |
| 18 | + schema_priv(schema_privileges.NoAccess) |
| 19 | + schema_priv(schema_privileges.NoAccessAgain) |
| 20 | + yield schema_priv |
| 21 | + if schema_priv.is_activated(): |
| 22 | + schema_priv.drop() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def connection_djsubset(connection_root, db_creds_root, schema_priv): |
| 27 | + user = "djsubset" |
| 28 | + conn = dj.conn(**db_creds_root, reset=True) |
| 29 | + schema_priv.activate(f"{PREFIX}_schema_privileges") |
| 30 | + conn.query( |
| 31 | + f""" |
| 32 | + CREATE USER IF NOT EXISTS '{user}'@'%%' |
| 33 | + IDENTIFIED BY '{user}' |
| 34 | + """ |
| 35 | + ) |
| 36 | + conn.query( |
| 37 | + f""" |
| 38 | + GRANT SELECT, INSERT, UPDATE, DELETE |
| 39 | + ON `{PREFIX}_schema_privileges`.`#parent` |
| 40 | + TO '{user}'@'%%' |
| 41 | + """ |
| 42 | + ) |
| 43 | + conn.query( |
| 44 | + f""" |
| 45 | + GRANT SELECT, INSERT, UPDATE, DELETE |
| 46 | + ON `{PREFIX}_schema_privileges`.`__child` |
| 47 | + TO '{user}'@'%%' |
| 48 | + """ |
| 49 | + ) |
| 50 | + conn_djsubset = dj.conn( |
| 51 | + host=db_creds_root["host"], |
| 52 | + user=user, |
| 53 | + password=user, |
| 54 | + reset=True, |
| 55 | + ) |
| 56 | + yield conn_djsubset |
| 57 | + conn.query(f"DROP USER {user}") |
| 58 | + conn.query(f"DROP DATABASE {PREFIX}_schema_privileges") |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def connection_djview(connection_root, db_creds_root): |
| 63 | + """ |
| 64 | + A connection with only SELECT privilege to djtest schemas. |
| 65 | + Requires connection_root fixture so that `djview` user exists. |
| 66 | + """ |
| 67 | + connection = dj.conn( |
| 68 | + host=db_creds_root["host"], |
| 69 | + user="djview", |
| 70 | + password="djview", |
| 71 | + reset=True, |
| 72 | + ) |
| 73 | + yield connection |
| 74 | + |
| 75 | + |
| 76 | +class TestUnprivileged: |
| 77 | + def test_fail_create_schema(self, connection_djview): |
| 78 | + """creating a schema with no CREATE privilege""" |
| 79 | + with pytest.raises(dj.DataJointError): |
| 80 | + return dj.Schema( |
| 81 | + "forbidden_schema", namespace, connection=connection_djview |
| 82 | + ) |
| 83 | + |
| 84 | + def test_insert_failure(self, connection_djview, schema_any): |
| 85 | + unprivileged = dj.Schema( |
| 86 | + schema_any.database, namespace, connection=connection_djview |
| 87 | + ) |
| 88 | + unprivileged.spawn_missing_classes() |
| 89 | + assert issubclass(Language, dj.Lookup) and len(Language()) == len( |
| 90 | + schema.Language() |
| 91 | + ), "failed to spawn missing classes" |
| 92 | + with pytest.raises(dj.DataJointError): |
| 93 | + Language().insert1(("Socrates", "Greek")) |
| 94 | + |
| 95 | + def test_failure_to_create_table(self, connection_djview, schema_any): |
| 96 | + unprivileged = dj.Schema( |
| 97 | + schema_any.database, namespace, connection=connection_djview |
| 98 | + ) |
| 99 | + |
| 100 | + @unprivileged |
| 101 | + class Try(dj.Manual): |
| 102 | + definition = """ # should not matter really |
| 103 | + id : int |
| 104 | + --- |
| 105 | + value : float |
| 106 | + """ |
| 107 | + |
| 108 | + with pytest.raises(dj.DataJointError): |
| 109 | + Try().insert1((1, 1.5)) |
| 110 | + |
| 111 | + |
| 112 | +class TestSubset: |
| 113 | + def test_populate_activate(self, connection_djsubset, schema_priv): |
| 114 | + schema_priv.activate( |
| 115 | + f"{PREFIX}_schema_privileges", create_schema=True, create_tables=False |
| 116 | + ) |
| 117 | + schema_privileges.Child.populate() |
| 118 | + assert schema_privileges.Child.progress(display=False)[0] == 0 |
0 commit comments