Esempio n. 1
0
def login():
    data = request.get_json()
    missing_fields = UserValidator().login_fields(data)

    if missing_fields:
        return make_response(jsonify(missing_fields), 400)

    validate_user = UserValidator(data)
    reg_email = re.compile(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$")

    if not re.match(reg_email, str(data['email'])):
        return make_response(jsonify({"error": validate_user.valid_email()}),
                             422)

    credentials = {"email": data['email'], "password": data['password']}

    # if auth:
    log_user = User().log_in_user(credentials)

    if isinstance(log_user, int):
        auth_token = User().encode_auth_token(log_user)
        return make_response(
            jsonify({
                "status":
                201,
                "auth_token":
                auth_token.decode('utf-8'),
                "message":
                "{} has been successfully logged in".format(data['email'])
            }), 201)
    else:
        return make_response(log_user)
    def test_check_fields(self):
        """ Test that check_fields method works correctly """
        fields = [
            'first_name', 'last_name', 'othername', 'email', 'phoneNumber',
            'username', 'password', 'confirm_password'
        ]
        user = {
            "last_name": "Mwangi",
            "username": "******",
            "othername": "Mash",
            "email": "*****@*****.**",
            "password": "******",
            "confirm_password": "******"
        }

        validator = UserValidator(user)
        self.assertEqual(validator.check_fields(user, fields), {
            "error": "You missed the first_name key, value pair",
            "status": 400
        })
    def test_user_fields_exists(self):
        user = {
            "last_name": "Mwangi",
            "othername": "Mash",
            "username": "******",
            "email": "*****@*****.**",
            "password": "******",
            "confirm_password": "******"
        }
        validate = UserValidator(user)

        user2 = {
            "password": "******",
        }

        validate2 = UserValidator(user2)

        # test missing sign up fields
        self.assertEqual(validate.signup_fields(user), {
            "error": 'You missed the first_name key, value pair',
            "status": 400
        })

        # test missing log in fields
        self.assertEqual(validate2.login_fields(user2), {
            "error": 'You missed the email key, value pair',
            "status": 400
        })
    def test_errorHandler(self):
        """ Test that errorHandler method works correctly """

        validator = UserValidator(self.user)
        self.assertEqual(
            validator.errorHandler('first_name'),
            "Your first name should be between 4 to 24 characters long!")
        self.assertEqual(
            validator.errorHandler('last_name'),
            "Your last name should be between 4 to 24 characters long!")
        self.assertEqual(
            validator.errorHandler('othername'),
            "Your othername should be between 4 to 24 characters long!")
        self.assertEqual(
            validator.errorHandler('username'),
            "Your username should be between 4 to 24 characters long!")
        self.assertEqual(validator.errorHandler('phoneNumber'),
                         "Use valid numbers for phone number")
        self.assertEqual(validator.errorHandler('email'),
                         "Invalid email address!")
        self.assertEqual(validator.errorHandler('unmatching_pass'),
                         "Your passwords don't match!")
        self.assertEqual(validator.errorHandler('password'), "Weak password!")
Esempio n. 5
0
def update_account(userId):
    data = request.get_json()

    if UserValidator().signup_fields(data):
        return make_response(jsonify(UserValidator().signup_fields(data)), 400)
    else:
        # Validate user
        validate_user = UserValidator(data)
        validation_methods = [
            validate_user.valid_email, validate_user.valid_name,
            validate_user.valid_phoneNumber, validate_user.validate_password,
            validate_user.matching_password
        ]

        for error in validation_methods:
            if error():
                return make_response(jsonify({"error": error()}), 422)

    user_data = {
        "first_name": data['first_name'],
        "last_name": data['last_name'],
        "othername": data['othername'],
        "email": data['email'],
        "phoneNumber": data['phoneNumber'],
        "username": data['username'],
        "password": data['password']
    }

    update_user = User().update_user(userId, user_data)
    if isinstance(update_user, dict):
        return make_response(jsonify(update_user))
    else:
        return make_response(
            jsonify(
                {"message":
                 f"user {user_data['email']} updated successfully"}))
Esempio n. 6
0
def registration():
    data = request.get_json()

    # Validate user
    validate_user = UserValidator(data)
    validation_methods = [
        validate_user.valid_email, validate_user.valid_name,
        validate_user.valid_phoneNumber, validate_user.validate_password,
        validate_user.matching_password
    ]

    for error in validation_methods:
        if error():
            return make_response(jsonify({
                "error": error(),
                "status": 422
            }), 422)

    # Register user
    user_data = {
        "first_name": data['first_name'],
        "last_name": data['last_name'],
        "othername": data['othername'],
        "email": data['email'],
        "phoneNumber": data['phoneNumber'],
        "username": data['username'],
        "password": data['password']
    }

    reg_user = User(user_data)
    if reg_user.save_user():
        return make_response(jsonify(reg_user.save_user()), 409)
    else:
        id = reg_user.fetch_user_id(user_data['username'])
        auth_token = reg_user.encode_auth_token(id[0])
        return make_response(
            jsonify({
                "status":
                201,
                "message":
                "{} registered successfully".format(data['email']),
                "username":
                data['username'],
                "auth_token":
                auth_token.decode('utf-8')
            }), 201)
    def test_invalid_data(self):

        # test short first name
        user = {**self.user}
        user['first_name'] = 'D'
        validator = UserValidator(user)
        self.assertEqual(
            validator.valid_name(),
            "Your first name should be between 4 to 24 characters long!")

        # test short last name
        user2 = {**self.user}
        user2['last_name'] = 'm'
        valid2 = UserValidator(user2)
        self.assertEqual(
            valid2.valid_name(),
            "Your last name should be between 4 to 24 characters long!")

        # test invalid email address
        user3 = {**self.user}
        user3['email'] = 'abc.com'
        valid3 = UserValidator(user3)
        self.assertEqual(valid3.valid_email(), 'Invalid email address!')

        # test weak password
        user4 = {**self.user}
        user4['password'] = '******'
        valid4 = UserValidator(user4)
        self.assertEqual(valid4.validate_password(), 'Weak password!')

        # test non-matching password
        user5 = {**self.user}
        user5['password'] = '******'
        user5['confirm_password'] = '******'
        valid5 = UserValidator(user5)
        self.assertEqual(valid5.matching_password(),
                         "Your passwords don't match!")

        # test valid phone number
        user6 = {**self.user}
        user6['phoneNumber'] = 'w3143l;kj'
        valid6 = UserValidator(user6)
        self.assertEqual(valid6.valid_phoneNumber(),
                         "Use valid numbers for phone number")