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")
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")
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")
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)
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)
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"}
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("", "")
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("", "")
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")
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")
def test_authenticate_success(model: AccessControlData): username, password = "******", "somePassword1" model.add_user(username, password) assert model.users == {username: password} assert model.authenticate(username, password)
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")
def test_create_user_existing_user(model: AccessControlData): model.add_user("Bob", "pass") with pytest.raises(KeyError): model.add_user("Bob", "otherPassword")
def test_authenticate_invalid_username(model: AccessControlData): with pytest.raises(ValueError): model.authenticate("", "pass") with pytest.raises(ValueError): model.authenticate(None, "pass")
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"}
def test_add_user_to_group_nonexistent_user(model: AccessControlData): with pytest.raises(KeyError): model.add_user_to_group("Bob", "humans")
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")
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")
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) }
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")
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)
def test_create_user(model: AccessControlData): username, password = "******", "greatPassword123#" model.add_user(username, password) assert model.users == {username: password}
def test_authenticate_invalid_password(model: AccessControlData): with pytest.raises(ValueError): model.authenticate("Bob", "") with pytest.raises(ValueError): model.authenticate("Bob", None)
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
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")
def test_can_access_invalid_user(model: AccessControlData): with pytest.raises(KeyError): model.can_access("operation", "invalid user")
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"}
def test_can_access_invalid_object(model: AccessControlData): model.add_user("Bob", "password") with pytest.raises(KeyError): model.can_access("operation", "Bob", "invalid object")
def model(): return AccessControlData.load_data()
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)