Command line script to manage the cpi lab journal users.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

258 lines
6.7 KiB

import pytest
def read_lines(path):
with path.open("r") as fh:
content = fh.read().strip()
lines = content.splitlines()
return [line.strip() for line in lines]
@pytest.mark.parametrize(
"value, expected",
[
("no line break", "KEY = no line break"),
("with\nline\nbreak", "KEY = with\n\tline\n\tbreak"),
],
)
def test_authz_format_ini_option(value, expected):
from elab_users.authz import format_ini_option
result = format_ini_option("KEY", value)
assert result == expected
def test_authz_parser_init():
import configparser
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
assert isinstance(parser, configparser.ConfigParser)
assert parser.elab_users == {}
assert parser.original_path is None
def test_authz_parser_optionxfrom():
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
assert parser.optionxform(123) == "123"
def test_authz_parser_read(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
parser.read(example_authz)
assert parser.original_path == example_authz
assert parser.elab_users != {}
def test_authz_parser_from_file(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
assert isinstance(parser, AuthzConfigParser)
assert parser.original_path == example_authz
assert parser.elab_users != {}
def test_authz_parser_write_to_file_raises_error():
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
with pytest.raises(IOError):
parser.write_to_file(path=None)
def test_authz_parser_write_to_file_uses_original_path(
example_authz, example_empty_file
):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
parser.original_path = example_empty_file
parser.write_to_file(path=None)
assert example_empty_file.is_file()
def test_authz_parser_write_to_file_custom_path(
example_authz, example_empty_file
):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
parser.write_to_file(path=example_empty_file)
assert example_empty_file.is_file()
def test_authz_parser_write(example_authz, example_empty_file):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
with open(example_empty_file, "w") as fh:
parser.write(fh)
original = read_lines(example_authz)
created = read_lines(example_empty_file)
assert original == created
def test_authz_parser_extract_user_info_from_config(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
super(type(parser), parser).read(example_authz)
assert parser.elab_users == {}
parser._extract_user_info_from_config()
assert parser.elab_users != {}
@pytest.mark.parametrize(
"name, group",
[
("OswaldPrucker", "administrators"),
("AlexanderDietz", "users"),
("UrmilShah", "restricted"),
("CamillaOestevold", "alumni"),
],
)
def test_authz_parser_extract_group_definitions(name, group, example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
super(type(parser), parser).read(example_authz)
parser._extract_group_definitions()
user = parser.elab_users[name]
assert user.group == group
@pytest.mark.parametrize(
"name, read, write",
[
("OswaldPrucker", [], []),
("AlexanderDietz", [], ["AlexanderDietz"]),
("UrmilShah", ["AndreasEvers"], ["UrmilShah"]),
],
)
def test_authz_parser_extract_individual_acls(
name, read, write, example_authz
):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser()
super(type(parser), parser).read(example_authz)
parser._extract_group_definitions()
parser._extract_individual_acls()
user = parser.elab_users[name]
assert user.read_acl == set(read)
assert user.write_acl == set(write)
def test_authz_parser_group_users(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
groups = parser.group_users()
assert len(groups) == 4
assert len(groups["administrators"]) == 2
assert len(groups["users"]) == 54
assert len(groups["restricted"]) == 5
assert len(groups["alumni"]) == 62
def test_authz_parser_add_journal_acl_for(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
user = parser.add_journal_acl_for("JaneDoe", "users")
assert user.name == "JaneDoe"
assert user.group == "users"
assert parser.elab_users["JaneDoe"] == user
assert "JaneDoe:/" in parser.sections()
items = parser.items("JaneDoe:/")
assert sorted(items) == [
("@administrators", "rw"),
("@alumni", ""),
("@restricted", ""),
("@users", "r"),
("JaneDoe", "rw"),
]
def test_authz_parser_move_user_to_alumni(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
user = parser.move_user_to_alumni("UrmilShah")
assert user.name == "UrmilShah"
assert user.group == "alumni"
assert user.write_acl == set()
assert user.read_acl == set()
for group, userlist in parser.items("groups"):
if group == "alumni":
assert "UrmilShah" in userlist
else:
assert "UrmilShah" not in userlist
def test_authz_parser_update_user_group_config(example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
parser.elab_users["UrmilShah"].group = "alumni"
parser._update_user_group_config()
for group, userlist in parser.items("groups"):
if group == "alumni":
assert "UrmilShah" in userlist
else:
assert "UrmilShah" not in userlist
@pytest.mark.parametrize(
"elab, read, write",
[
("AlexeyKopyshev:/", ["@users"], ["@administrators"]),
(
"AndreasEvers:/",
["@users", "UrmilShah"],
["@administrators"],
),
(
"UrmilShah:/",
["@users"],
["@administrators", "UrmilShah"],
),
],
)
def test_authz_parser_get_journal_info(elab, read, write, example_authz):
from elab_users.authz import AuthzConfigParser
parser = AuthzConfigParser.from_file(example_authz)
info = parser.get_journal_info(elab)
assert info == {"r": read, "rw": write}