Example #1
0
def test_valid_reset():

    '''
    Testing if auth_passwordreset functions return expected outputs 
    if passed the correct password and reset code.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**", 
            "password": hashPassword("validPassword"), 
            "name_first": "validFirst", 
            "name_last": "validLast", 
            "handle": "validFirstvalidLast", 
            "u_id": 8501, 
            "resetCode": 1234, 
            "is_admin": 1, 
            "img_url": None}],
        "tokens": [], 
        "channels": [],
        }

    checkPassword("validNewPassword")
    assert resetPasswordFunc(data, 1234, "validNewPassword") is None
    assert data['users'][0]['password'] == hashPassword("validNewPassword")
    assert data['users'][0]['resetCode'] is None
Example #2
0
def test_valid_channel_invite():

    '''
    Testing if channel_invite functions return expected outputs 
    if passed correct u_id and channel_id.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**", 
            "password": hashPassword("validPassword"), 
            "name_first": "validFirst", 
            "name_last": "validLast", 
            "handle": "validFirstvalidLast", 
            "u_id": 1234, 
            "resetCode": None, 
            "is_admin": 1, 
            "img_url": None}],
        "tokens": ["validToken"], 
        "channels": [{
            "channel_id": 5000,
            "members": [1234],
        }],
        }

    assert userIsInChannel(data, 5000, 1234)
    assert validUser(data, 1234)
    
    with pytest.raises(errors.ValueError):
        addUserToChannel(data, 5000, 1234)
Example #3
0
def test_invalid_logout():
    '''
    Testing if auth_logout functions return expected outputs 
    if passed an incorrect password and email.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 8501,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }],
        "tokens": ["validToken"],
        "channels": [],
    }

    assert logoutFunc(data, "invalidToken") is False
    assert logoutFunc(data, "") is False
    assert logoutFunc(data, None) is False
Example #4
0
def test_valid_channel_details():
    '''
    Testing if channel_details functions return expected outputs 
    if passed correct name, u_id and is_public.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 1234,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }],
        "tokens": ["validToken"],
        "channels": [{
            "channel_id": 5000,
            "name": "validChannelName",
            "owners": [1234],
            "members": [1234],
        }],
    }

    channelReturn = getDetails(data, 5000)
    assert channelReturn['name'] == data['channels'][0]['name']
    assert channelReturn['owner_members'] == data['channels'][0]['owners']
    assert channelReturn['all_members'] == data['channels'][0]['members']
Example #5
0
def test_invalid_channel_remove_user_not_member():
    '''
    Testing if channel_leave functions return expected outputs 
    if passed an valid channel_id and incorrect u_id.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 1234,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }, {
            "u_id": 5678
        }],
        "tokens": ["validToken"],
        "channels": [{
            "channel_id": 5000,
            "name": "validChannelName",
            "owners": [1234],
            "members": [1234],
        }],
    }

    with pytest.raises(errors.ValueError):
        removeUserFromChannel(data, 5000, 5678)
Example #6
0
def test_valid_channel_remove():
    '''
    Testing if channel_leave functions return expected outputs 
    if passed correct channel_id and u_id.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 1234,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }, {
            "u_id": 5678
        }],
        "tokens": ["validToken"],
        "channels": [{
            "channel_id": 5000,
            "name": "validChannelName",
            "owners": [1234],
            "members": [1234, 5678],
        }],
    }

    assert removeUserFromChannel(data, 5000, 5678)
Example #7
0
def create():
    data = getData()
    email, password, nameFirst, nameLast = (request.form[x]
                                            for x in ('email', 'password',
                                                      'name_first',
                                                      'name_last'))

    handle = createHandle(
        nameFirst, nameLast
    )  # In the form 'firstNamelastName' or if already taken: 'firstNamelastName123'.
    u_id = createU_ID(data)  # In the form '123456'.

    if emailExists(email, data):
        raise ValueError('Email entered is already being used.')

    handle = uniqueHandle(data, handle)

    checkEmail(email)
    checkPassword(password)
    checkName(nameFirst, nameLast)

    createUser(data, email, hashPassword(password), nameFirst, nameLast,
               handle, u_id, PERMISSION3)

    data['users'][0][
        'is_admin'] = PERMISSION1  # The very first user who signs up is an owner

    data['tokens'].append(generateToken(u_id))
    print(data)

    return sendSuccess({
        'u_id': u_id,
        'token': generateToken(u_id),
    })
Example #8
0
def test_invalid_code():

    '''
    Testing if auth_passwordreset functions return expected outputs 
    if passed a correct password but an invalid reset code.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**", 
            "password": hashPassword("validPassword"), 
            "name_first": "validFirst", 
            "name_last": "validLast", 
            "handle": "validFirstvalidLast", 
            "u_id": 8501, 
            "resetCode": 1234, 
            "is_admin": 1, 
            "img_url": None}],
        "tokens": [], 
        "channels": [],
        }

    checkPassword("validNewPassword")
    with pytest.raises(errors.ValueError): 
        resetPasswordFunc(data, 4321, "validNewPassword")
Example #9
0
def resetPasswordFunc(data, resetCode, newPassword):
    for user in data['users']:
        if user['resetCode'] == resetCode:
            user['password'] = hashPassword(newPassword)
            user['resetCode'] = None
            return None
    raise ValueError('Reset code is not valid.')
Example #10
0
def test_valid_channel_create():

    '''
    Testing if channel_create functions return expected outputs 
    if passed correct name, u_id and is_public.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**", 
            "password": hashPassword("validPassword"), 
            "name_first": "validFirst", 
            "name_last": "validLast", 
            "handle": "validFirstvalidLast", 
            "u_id": 1234, 
            "resetCode": None, 
            "is_admin": 1, 
            "img_url": None}],
        "tokens": ["validToken"], 
        "channels": [],
        }

    assert channelNameCheck("validChannelName") is True
    assert channelsCreate(data, 1234, "validChannelName", True) == data['channels'][0]['channel_id']
Example #11
0
def test_invalid_login_email_correct_password():
    '''
    Testing if auth_login functions raises a Value Error if 
    if passed an incorrect email and correct password.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 8501,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }],
        "tokens": [],
        "channels": [],
    }

    with pytest.raises(errors.ValueError):
        loginFunc(data, "*****@*****.**", "validPassword")
Example #12
0
def test_valid_login():
    '''
    Testing if auth_login functions return expected outputs 
    if passed the correct password and email.
    '''

    data = {
        "users": [{
            "email": "*****@*****.**",
            "password": hashPassword("validPassword"),
            "name_first": "validFirst",
            "name_last": "validLast",
            "handle": "validFirstvalidLast",
            "u_id": 8501,
            "resetCode": None,
            "is_admin": 1,
            "img_url": None
        }],
        "tokens": [],
        "channels": [],
    }

    assert loginFunc(data, "*****@*****.**",
                     "validPassword") == data['users'][0]