def test_admin_userpermission_change_user_permission_possible_permissions():
    '''
    Testing if it is possible to go through the entire permission set
    '''
    # Initialising
    global_var.initialise_all()

    # Creating a valid user who can change permissions
    owner = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(owner["u_id"]).permission = SLACKR_OWNER

    # Creating normal member
    user = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(user["u_id"]).permission = SLACKR_USER

    # Changing normal member to admin
    admin_userpermission_change(get_user_token_by_u_id(owner["u_id"]), \
                                                user["u_id"], SLACKR_ADMIN)
    assert get_user_by_u_id(user["u_id"]).permission == SLACKR_ADMIN

    # Changing admin to owner
    admin_userpermission_change(get_user_token_by_u_id(owner["u_id"]), \
                                                user["u_id"], SLACKR_OWNER)
    assert get_user_by_u_id(user["u_id"]).permission == SLACKR_OWNER

    # Changing owner to member
    admin_userpermission_change(get_user_token_by_u_id(owner["u_id"]), \
                                                user["u_id"], SLACKR_USER)
    assert get_user_by_u_id(user["u_id"]).permission == SLACKR_USER
def test_get_user_by_u_id():
    '''
    Ensures that get_user_by_u_id returns the correct u_id
    '''
    # Initialisation
    global_var.initialise_all()

    # Creating a user
    user = auth.auth_register("*****@*****.**", "pass123", "Raydon", "Smith")
    u_id = user["u_id"]

    assert helpers.get_user_by_u_id(u_id).u_id == u_id

    assert helpers.get_user_by_u_id(-1) == None
def user_profile(token, u_id):
    '''
    For a valid user, returns information about their email, first name,
    last name, and handle

    ValueError:
    - User with u_id is not a valid user
    '''

    # Get user object by u_id
    user = get_user_by_u_id(u_id)

    if user is None:
        raise ValueError("Invalid User ID")

    user_profile_return = {
        "u_id": user.u_id,
        "email": user.email,
        "name_first": user.name_first,
        "name_last": user.name_last,
        "handle_str": user.handle,
        "profile_img_url": user.has_photo
    }

    return user_profile_return
def test_admin_userpermission_change_invalid_permission():
    '''
    Checks when an invalid permission is sent to admin_userpermission_change
    '''
    with pytest.raises(ValueError, match='Permission ID is invalid'):
        # Initialising
        global_var.initialise_all()

        # Creating a valid user who can change permissions
        user = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
        get_user_by_u_id(user["u_id"]).permission = SLACKR_OWNER

        # Calling admin_userpermission_change with an invalid permission
        admin_userpermission_change(get_user_token_by_u_id(user["u_id"]), \
                                                         user["u_id"], -1)
def test_admin_userpermission_change_invalid_token():
    '''
    Checks when an invalid token is sent to admin_userpermission_change
    '''
    with pytest.raises(AccessError, match='Invalid token'):
        # Initialising
        global_var.initialise_all()

        # Creating a valid user who can change permissions
        user = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
        get_user_by_u_id(user["u_id"]).permission = SLACKR_USER

        # Calling admin_userpermission_change with an invalid token
        admin_userpermission_change("invalid token", \
                                                 user["u_id"], SLACKR_ADMIN)
def test_admin_userpermission_change_user_member():
    '''
    Checks when an unauthorised user is sent to admin_userpermission_change
    '''
    with pytest.raises(AccessError, \
                                match='Current user is not an admin or owner'):
        # Initialising
        global_var.initialise_all()

        # Creating a user who cannot change permissions
        user = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
        get_user_by_u_id(user["u_id"]).permission = SLACKR_USER

        # Calling admin_userpermission_change with a normal 'user' user
        admin_userpermission_change(get_user_token_by_u_id(user["u_id"]), \
                                             user["u_id"], SLACKR_OWNER)
Ejemplo n.º 7
0
def admin_userpermission_change(token, u_id, permission_id):
    '''
    Given a User by their user ID, set their permissions to new permissions
    described by permission_id
    '''

    # Getting user object
    user = get_user_by_u_id(u_id)

    # Checking validity of permission change request
    if not user:
        raise ValueError('Invalid User ID')
    if not valid_permission_id(permission_id):
        raise ValueError('Permission ID is invalid')
    if not (token_is_admin(token) or token_is_owner(token)):
        raise AccessError('Current user is not an admin or owner')

    # Changing user's permission
    user.change_permissions(permission_id)

    return {}
Ejemplo n.º 8
0
def channel_invite(token, channel_id, u_id):
    '''
    Invites a user to join a channel
    '''

    channel = get_channel_by_channel_id(channel_id)

    # Channel_id does not refer to a valid channel
    if channel is None:
        raise ValueError("Channel does not exist")

    # Inviting user is not a member of the channel
    if not channel.is_member(decode_token(token)):
        raise AccessError("Authorised user is not a member of the channel")

    # u_id is not a user
    if not get_user_by_u_id(u_id):
        raise ValueError("Invalid User ID")

    # User is added as a member
    channel.add_user(u_id)

    return {}
def test_admin_userpermission_change_user_admin():
    '''
    Testing successful case of changing permissions by an admin of slackr
    '''
    # Initialising
    global_var.initialise_all()

    # Creating a valid admin who can change permissions
    admin1 = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(admin1["u_id"]).permission = SLACKR_ADMIN

    # Creating normal member
    user = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(user["u_id"]).permission = SLACKR_USER

    # Creating another admin
    admin2 = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(admin2["u_id"]).permission = SLACKR_ADMIN

    # Creating another owner
    owner = auth_register("*****@*****.**", "pass123", "Rayden", "Smith")
    get_user_by_u_id(owner["u_id"]).permission = SLACKR_OWNER

    # Admin changing permission of member
    admin_userpermission_change(get_user_token_by_u_id(admin1["u_id"]), \
                                                 user["u_id"], SLACKR_OWNER)
    assert get_user_by_u_id(user["u_id"]).permission == SLACKR_OWNER

    # Admin changing permission of another admin
    admin_userpermission_change(get_user_token_by_u_id(admin1["u_id"]), \
                                             admin2["u_id"], SLACKR_OWNER)
    assert get_user_by_u_id(admin2["u_id"]).permission == SLACKR_OWNER

    # Admin changing permission of owner
    admin_userpermission_change(get_user_token_by_u_id(admin1["u_id"]), \
                                             owner["u_id"], SLACKR_ADMIN)
    assert get_user_by_u_id(owner["u_id"]).permission == SLACKR_ADMIN
Ejemplo n.º 10
0
 def add_owner(self, u_id):
     user = helpers.get_user_by_u_id(u_id)
     self.owners.append(user)