def test_get_action_permissions(users, user_action):
    user_id = users[0].id
    action_id = user_action.id

    # by default, only the user who created an action has access to it
    assert action_permissions.get_action_permissions_for_users(
        action_id=action_id) == {
            users[1].id: Permissions.GRANT
        }
    assert not action_permissions.action_is_public(action_id)

    action_permissions.set_action_public(action_id)
    assert action_permissions.get_action_permissions_for_users(
        action_id=action_id) == {
            users[1].id: Permissions.GRANT
        }
    assert action_permissions.action_is_public(action_id)

    sampledb.db.session.add(
        UserActionPermissions(user_id=user_id,
                              action_id=action_id,
                              permissions=Permissions.WRITE))
    sampledb.db.session.commit()
    assert action_permissions.get_action_permissions_for_users(
        action_id=action_id) == {
            user_id: Permissions.WRITE,
            users[1].id: Permissions.GRANT
        }
    assert action_permissions.action_is_public(action_id)
def test_get_user_action_permissions_with_group_project(users, user_action):
    user_id = users[0].id
    action_id = user_action.id
    project_id = sampledb.logic.projects.create_project(
        "Example Project", "", users[1].id).id
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.NONE
    action_permissions.set_project_action_permissions(
        project_id=project_id,
        action_id=action_id,
        permissions=Permissions.GRANT)
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.NONE
    group_id = sampledb.logic.groups.create_group("Example Group", "",
                                                  users[1].id).id
    sampledb.logic.projects.add_group_to_project(project_id, group_id,
                                                 Permissions.READ)
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.NONE
    sampledb.logic.groups.add_user_to_group(group_id=group_id, user_id=user_id)
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.READ
    sampledb.db.session.add(
        UserActionPermissions(user_id=user_id,
                              action_id=action_id,
                              permissions=Permissions.WRITE))
    sampledb.db.session.commit()
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.WRITE
    sampledb.logic.projects.update_group_project_permissions(
        project_id, group_id, Permissions.GRANT)
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.GRANT
def test_get_instrument_responsible_user_action_permissions(user, instrument, instrument_action):
    user_id = user.id
    action_id = instrument_action.id
    instrument.responsible_users.append(user)
    sampledb.db.session.add(instrument)
    sampledb.db.session.add(UserActionPermissions(user_id=user_id, action_id=action_id, permissions=Permissions.WRITE))
    sampledb.db.session.commit()
    assert action_permissions.get_user_action_permissions(user_id=user_id, action_id=action_id) == Permissions.GRANT
def test_get_user_action_permissions(user, independent_action):
    user_id = user.id
    action_id = independent_action.id
    sampledb.db.session.add(
        UserActionPermissions(user_id=user_id,
                              action_id=action_id,
                              permissions=Permissions.WRITE))
    sampledb.db.session.commit()
    assert action_permissions.get_user_action_permissions(
        user_id=user_id, action_id=action_id) == Permissions.WRITE