Beispiel #1
0
def test_add_object_to_group_redundantly(model: AccessControlData):
    # We only support one group per object. Adding an object to
    # two groups (whether they are the same or not), should result
    # in an error
    model.add_object_to_group("stick", "wood")

    with pytest.raises(KeyError):
        model.add_object_to_group("stick", "wood")
Beispiel #2
0
def test_can_access_failure(model: AccessControlData):
    model.add_user("bob", "pass")
    model.add_user_to_group("bob", "humans")
    model.add_object_to_group("Handmade's Tail", "books")
    model.add_access("read", "humans", "books")

    assert not model.can_access("write", "bob", "Handmade's Tail")
Beispiel #3
0
def test_can_access_empty_permissions(model: AccessControlData):
    model.add_user("bob", "pass")
    model.add_user_to_group("bob", "humans")
    model.add_object_to_group("Handmade's Tail", "books")

    assert not model.permissions
    assert not model.can_access("read", "bob", "Handmade's Tail")
Beispiel #4
0
def test_add_user_to_group_invalid_inputs(model: AccessControlData):
    with pytest.raises(ValueError):
        model.add_user("", "usergroup")

    with pytest.raises(ValueError):
        model.add_user(None, "usergroup")

    with pytest.raises(ValueError):
        model.add_user("user", "")

    with pytest.raises(ValueError):
        model.add_user("user", None)
Beispiel #5
0
def test_add_object_to_group_invalid_inputs(model: AccessControlData):
    with pytest.raises(ValueError):
        model.add_object_to_group("", "objectgroup")

    with pytest.raises(ValueError):
        model.add_object_to_group(None, "objectgroup")

    with pytest.raises(ValueError):
        model.add_object_to_group("object", "")

    with pytest.raises(ValueError):
        model.add_object_to_group("object", None)
Beispiel #6
0
def test_add_user_to_group_redundantly(model: AccessControlData):
    # Adding a user to the same group twice should not result
    # in redundant storage of the user in that group
    model.add_user("Bob", "greatPassword123#")
    model.add_user_to_group("Bob", "people")
    model.add_user_to_group("Bob", "humans")
    model.add_user_to_group("Bob", "people")

    assert model.user_to_groups["Bob"] == {"people", "humans"}
    assert model.get_users_in_group("people") == {"Bob"}
Beispiel #7
0
def test_can_access_invalid_params(model: AccessControlData):
    with pytest.raises(ValueError):
        model.can_access("", "username")

    with pytest.raises(ValueError):
        model.can_access("operation", "")

    with pytest.raises(ValueError):
        model.can_access("", "")
Beispiel #8
0
def test_add_access_invalid_param(model: AccessControlData):
    with pytest.raises(ValueError):
        model.add_access("", "valid_user_group")

    with pytest.raises(ValueError):
        model.add_access("valid_op", "")

    with pytest.raises(ValueError):
        model.add_access("", "")
Beispiel #9
0
def test_authenticate_nonexistent_user(model: AccessControlData):
    model.add_user("user1", "password1")
    model.add_user("user2", "password2")
    model.add_user("user3", "password3")

    assert model.users == {
        "user1": "password1",
        "user2": "password2",
        "user3": "password3",
    }

    with pytest.raises(KeyError):
        model.authenticate("user4", "password4")
Beispiel #10
0
def test_add_multiple_objects_to_groups(model: AccessControlData):
    # Add users to their respective groups
    model.add_object_to_group("table", "furniture")
    model.add_object_to_group("chair", "furniture")
    model.add_object_to_group("fork", "utensils")
    model.add_object_to_group("spoon", "utensils")
    model.add_object_to_group("plates", "utensils")
    model.add_object_to_group("pencil", "stationery")
    model.add_object_to_group("paper", "stationery")
    model.add_object_to_group("eraser", "stationery")

    # Ensure all groups are as expected
    assert model.get_objects_in_group("furniture") == \
        { "table", "chair" }
    assert model.get_objects_in_group("utensils") == \
        { "fork", "spoon", "plates" }
    assert model.get_objects_in_group("stationery") == \
        { "pencil", "paper", "eraser" }
    assert not model.get_objects_in_group("nonexistent_group")
Beispiel #11
0
def test_authenticate_success(model: AccessControlData):
    username, password = "******", "somePassword1"
    model.add_user(username, password)

    assert model.users == {username: password}
    assert model.authenticate(username, password)
Beispiel #12
0
def test_create_user_invalid_username(model: AccessControlData):
    with pytest.raises(ValueError):
        model.add_user("", "somePassword")

    with pytest.raises(ValueError):
        model.add_user(None, "somePassword")
Beispiel #13
0
def test_create_user_existing_user(model: AccessControlData):
    model.add_user("Bob", "pass")

    with pytest.raises(KeyError):
        model.add_user("Bob", "otherPassword")
Beispiel #14
0
def test_authenticate_invalid_username(model: AccessControlData):
    with pytest.raises(ValueError):
        model.authenticate("", "pass")

    with pytest.raises(ValueError):
        model.authenticate(None, "pass")
Beispiel #15
0
def test_add_user_to_group_success(model: AccessControlData):
    model.add_user("Bob", "greatPassword123#")
    model.add_user_to_group("Bob", "people")

    assert model.user_to_groups["Bob"] == {"people"}
    assert model.get_users_in_group("people") == {"Bob"}
Beispiel #16
0
def test_add_user_to_group_nonexistent_user(model: AccessControlData):
    with pytest.raises(KeyError):
        model.add_user_to_group("Bob", "humans")
Beispiel #17
0
def test_add_access_duplicate_permissions_all_2(model: AccessControlData):
    model.add_access("read", "humans", None)
    
    with pytest.raises(ValueError):
        model.add_access("read", "humans", "books")
Beispiel #18
0
def test_add_access_duplicate_permissions(model: AccessControlData):
    model.add_access("read", "humans", "books")

    # The permission already exists, so it will raise an error
    with pytest.raises(ValueError):
        model.add_access("read", "humans", "books")
Beispiel #19
0
def test_add_access_success(model: AccessControlData):
    model.add_access("read", "humans", "books")
    model.add_access("walk", "humans", None)
    assert model.permissions == \
        { ("read", "humans", "books"), ("walk", "humans", None) }
Beispiel #20
0
def test_can_access(model: AccessControlData):
    # Create dummy users
    model.add_user("Bob", "pass")
    model.add_user("John", "pass")
    model.add_user("Robert", "pass")
    model.add_user("Winnie", "pass")
    model.add_user("Tigger", "pass")

    # Add users to groups
    model.add_user_to_group("Bob", "humans")
    model.add_user_to_group("Bob", "workers")
    model.add_user_to_group("Winnie", "bear")
    model.add_user_to_group("Winnie", "animals")
    model.add_user_to_group("Tigger", "tiger")
    model.add_user_to_group("Tigger", "animals")
    model.add_user_to_group("John", "humans")
    model.add_user_to_group("John", "student")
    model.add_user_to_group("Robert", "humans")
    model.add_user_to_group("Robert", "worker")
    model.add_user_to_group("Robert", "student")
    
    # Add objects to groups
    model.add_object_to_group("Winnie", "animals")
    model.add_object_to_group("Harry Potter", "books")
    model.add_object_to_group("Sherlock Holmes", "books")
    model.add_object_to_group("Avengers", "good movies")
    model.add_object_to_group("A serious man", "bad movies")
    model.add_object_to_group("water", "liquid")
    model.add_object_to_group("red bull", "liquid")

    # Add permissions
    model.add_access("drink", "animals", "liquid")
    model.add_access("hunt", "animals")
    model.add_access("speak", "humans")
    model.add_access("read", "humans", "books")
    model.add_access("watch", "humans", "good movies")
    model.add_access("study", "student")
    model.add_access("drink", "student", "liquid")
    model.add_access("protest", "worker")
    model.add_access("get underpaid", "worker")

    # Check for various permissions
    assert model.can_access("hunt", "Winnie")
    assert not model.can_access("hunt", "Tigger", "Winnie")
    assert model.can_access("watch", "Bob", "Avengers")
    assert not model.can_access("watch", "Bob")
    assert not model.can_access("watch", "Bob", "A serious man")
    assert model.can_access("drink", "Tigger", "water")
    assert model.can_access("drink", "John", "red bull")
    assert not model.can_access("drink", "John", "Avengers")
    assert not model.can_access("watch", "John", "A serious man")
    assert model.can_access("protest", "Robert")
    assert not model.can_access("protest", "John")
    assert not model.can_access("protest", "Winnie")
    assert not model.can_access("get overpaid", "John")
    assert model.can_access("drink", "John", "water")
    assert model.can_access("drink", "Winnie", "red bull")
    assert model.can_access("speak", "Robert")
    assert not model.can_access("speak", "Tigger")
    assert model.can_access("study", "John")
    assert not model.can_access("study", "Bob")
Beispiel #21
0
import sys
from persistence import AccessControlData

if not 3 <= len(sys.argv) <= 4:
    print("Failure: Incorrect number of arguments")
    exit(1)

access_control = AccessControlData.load_data()

try:
    access_control.add_access(*sys.argv[1:])
    access_control.save_data()
    print("Success")
except Exception as exc:
    print(exc)
Beispiel #22
0
def test_create_user(model: AccessControlData):
    username, password = "******", "greatPassword123#"
    model.add_user(username, password)

    assert model.users == {username: password}
Beispiel #23
0
def test_authenticate_invalid_password(model: AccessControlData):
    with pytest.raises(ValueError):
        model.authenticate("Bob", "")

    with pytest.raises(ValueError):
        model.authenticate("Bob", None)
Beispiel #24
0
def test_authenticate_failure(model: AccessControlData):
    username, password = "******", "somePassword1"
    model.add_user(username, password)

    assert model.users == {username: password}
    assert model.authenticate(username, "incorrect password") == False
Beispiel #25
0
def test_add_multiple_users_to_groups(model: AccessControlData):
    # Create dummy users
    model.add_user("Bob", "password")
    model.add_user("Jane Doe", "password")
    model.add_user("John Doe", "password")
    model.add_user("Winnie the Pooh", "password")
    model.add_user("Tigger", "password")

    # Add users to their respective groups
    model.add_user_to_group("Bob", "humans")
    model.add_user_to_group("Bob", "workers")
    model.add_user_to_group("Jane Doe", "humans")
    model.add_user_to_group("John Doe", "humans")
    model.add_user_to_group("John Doe", "workers")
    model.add_user_to_group("Winnie the Pooh", "humans")  # ?
    model.add_user_to_group("Winnie the Pooh", "animals")
    model.add_user_to_group("Tigger", "animals")

    # Ensure all groups are as expected
    assert model.get_users_in_group("humans") == \
        { "Bob", "Jane Doe", "John Doe", "Winnie the Pooh" }
    assert model.get_users_in_group("workers") == \
        { "Bob", "John Doe" }
    assert model.get_users_in_group("animals") == \
        { "Winnie the Pooh", "Tigger" }
    assert not model.get_users_in_group("nonexistent_group")
Beispiel #26
0
def test_can_access_invalid_user(model: AccessControlData):
    with pytest.raises(KeyError):
        model.can_access("operation", "invalid user")
Beispiel #27
0
def test_add_object_to_group_success(model: AccessControlData):
    model.add_object_to_group("stick", "wood")

    assert model.object_to_group["stick"] == "wood"
    assert model.get_objects_in_group("wood") == {"stick"}
Beispiel #28
0
def test_can_access_invalid_object(model: AccessControlData):
    model.add_user("Bob", "password")
    
    with pytest.raises(KeyError):
        model.can_access("operation", "Bob", "invalid object")
Beispiel #29
0
def model():
    return AccessControlData.load_data()
Beispiel #30
0
def test_create_user_invalid_password(model: AccessControlData):
    with pytest.raises(ValueError):
        model.add_user("someUsername", "")

    with pytest.raises(ValueError):
        model.add_user("someUsername", None)