2020-05-29 06:54:27 +00:00
|
|
|
import pytest
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
from helpers.test_tools import TSV
|
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
|
|
instance = cluster.add_instance('instance')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2020-05-29 06:54:27 +00:00
|
|
|
instance.query("CREATE USER john")
|
|
|
|
instance.query("CREATE ROLE rx")
|
|
|
|
instance.query("CREATE ROLE ry")
|
|
|
|
|
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def reset_users_and_roles():
|
|
|
|
instance.query("CREATE USER OR REPLACE john")
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_default_roles():
|
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == ""
|
|
|
|
|
|
|
|
instance.query("GRANT rx, ry TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1], ['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("SET DEFAULT ROLE NONE TO john")
|
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == ""
|
|
|
|
|
|
|
|
instance.query("SET DEFAULT ROLE rx TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("SET DEFAULT ROLE ry TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("SET DEFAULT ROLE ALL TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1], ['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("SET DEFAULT ROLE ALL EXCEPT rx TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_alter_user():
|
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == ""
|
|
|
|
|
|
|
|
instance.query("GRANT rx, ry TO john")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1], ['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("ALTER USER john DEFAULT ROLE NONE")
|
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == ""
|
|
|
|
|
|
|
|
instance.query("ALTER USER john DEFAULT ROLE rx")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("ALTER USER john DEFAULT ROLE ALL")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['rx', 0, 1], ['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
instance.query("ALTER USER john DEFAULT ROLE ALL EXCEPT rx")
|
2020-09-16 04:26:10 +00:00
|
|
|
assert instance.query("SHOW CURRENT ROLES", user="john") == TSV([['ry', 0, 1]])
|
2020-05-29 06:54:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrong_set_default_role():
|
|
|
|
assert "There is no user `rx`" in instance.query_and_get_error("SET DEFAULT ROLE NONE TO rx")
|
|
|
|
assert "There is no user `ry`" in instance.query_and_get_error("SET DEFAULT ROLE rx TO ry")
|
|
|
|
assert "There is no role `john`" in instance.query_and_get_error("SET DEFAULT ROLE john TO john")
|
|
|
|
assert "There is no role `john`" in instance.query_and_get_error("ALTER USER john DEFAULT ROLE john")
|
|
|
|
assert "There is no role `john`" in instance.query_and_get_error("ALTER USER john DEFAULT ROLE ALL EXCEPT john")
|