def activateAccount(token):
     try:
         user = User.getUserByEmail(UserHandler.verifyUserToken(token))
         if user is None:
             pass
         User.activateUser(user)
     except Exception as err:
         return jsonify(
             message="Server error!",
             error=err.__str__()), HttpStatus.INTERNAL_SERVER_ERROR
     return jsonify(status='Success!'), HttpStatus.OK
    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
 def signIn(json):
     #validate data
     if ('email' in json) and ('password' in json):
         #get user data from db
         user = User.getUserByEmail(json['email'])
         #if user exists and password match return a valid JWT in the response otherwise return error.
         if user.active == False:
             return jsonify(Error='Please activate your account via email'
                            ), HttpStatus.NOT_FOUND
         if user and sha256.verify(json['password'], user.password):
             access_token = create_access_token(
                 identity=user.user_id)  #pragma: no mutate
             return jsonify(access_token=access_token), HttpStatus.OK
         else:
             return jsonify(Error='Email or password is incorrect'
                            ), HttpStatus.NOT_FOUND
     return jsonify(Error='Malformed body'), HttpStatus.BAD_REQUEST
Esempio n. 4
0
 def test_get_by_email(self):
     data = {'email': 'test', 'password': '******', 'username': '******'}
     user = User(**data).create()
     result = User.getUserByEmail(user.email)
     assert user == result