def createUser(json):
        valid_params = verify_parameters(json, User.REQUIRED_PARAMETERS)
        if valid_params:
            try:
                email_exists = User.getUserByEmail(json['email'])
                username_exists = User.getUserByUsername(json['username'])

                if username_exists and email_exists:
                    return jsonify(
                        message=
                        "Username and email already taken. Please use another one."
                    ), HttpStatus.BAD_REQUEST
                elif username_exists:
                    return jsonify(
                        message="Username already taken. Please use another one."
                    ), HttpStatus.BAD_REQUEST
                elif email_exists:
                    return jsonify(
                        message="Email already taken. Please use another one."
                    ), HttpStatus.BAD_REQUEST

                valid_params['password'] = sha256.hash(
                    valid_params['password'])
                created_user = User(**valid_params).create()
                user_dict = to_dict(created_user)
                result = {
                    "message": "Success!",
                    "user": user_dict,
                }

                #sends an activation email to the user
                UserHandler.sendActivationEmail(json['email'])

                #Create unlisted category. Will serve as default category for unlisted packages
                Category(**{
                    'user_id': created_user.user_id,
                    'name': 'unlisted'
                }).create()

                #returns created user, however, if send activation email fails, user must request another email
                return jsonify(result), HttpStatus.CREATED

            except Exception as err:
                return jsonify(
                    message="Server error!",
                    error=err.__str__()), HttpStatus.INTERNAL_SERVER_ERROR
        else:
            return jsonify(message="Bad Request!"), HttpStatus.BAD_REQUEST
Example #2
0
 def test_get_user_by_username(self):
     data = {'email': 'test', 'password': '******', 'username': '******'}
     user = User(**data).create()
     result = User.getUserByUsername(user.username)
     assert user == result