Exemplo n.º 1
0
    def test_get_package_by_tracking_number_and_userId(self):
        userData = {'email': 'test', 'password': '******', 'username': '******'}
        user = User(**userData).create()

        categoryData = {'user_id': user.user_id, 'name': 'test_category'}
        category = Category(**categoryData).create()

        packageData = {
            'carrier': 'ups',
            'tracking_number': '122833234',
            'name': 'test',
            'creation_date': '21-04-2021',
            'category_id': category.category_id
        }
        package = Package(**packageData).create()
        result_category = Category.getPackageByTrackingNumberAndUserId(
            '122833234', str(user.user_id))
        assert result_category[0].category_id == category.category_id
    def createPackage(json):
        user_id = json.pop('user_id', None)
        valid_params = verify_parameters(json, Package.REQUIRED_PARAMETERS)
        if valid_params and user_id:
            try:
                package_exists = Category.getPackageByTrackingNumberAndUserId(
                    json['tracking_number'], user_id)
                # does the user already has the package added in his account?
                if package_exists:
                    return jsonify(
                        message=
                        "The package you tried to create already exists in your account."
                    ), HttpStatus.BAD_REQUEST

                # user_category = Category.getCategoriesByUserId(user_id)
                # is the user trying to add the package to a category that doesn't belong to his account?
                # in theory, this should not happen but the condition is here just in case.
                # there might be better ways to implement the following verification

                # category_valid = False
                # for category in user_category:
                #     curr_category = to_dict(category)
                #     if curr_category['category_id'] == json['category_id']:
                #         category_valid = True

                # if not category_valid or not user_category:
                #     return jsonify(message="You are trying to add the package to a category that doesn't belong to your account."), HttpStatus.BAD_REQUEST

                res = requests.get(
                    "https://onlinetools.ups.com/track/v1/details/" +
                    str(json['tracking_number']),
                    headers={
                        "Content-Type":
                        "application/json",
                        "transId":
                        "1234",
                        "transactionSrc":
                        "TestTrack",
                        "AccessLicenseNumber":
                        "6D96561E525FE615",
                        "Connection":
                        "keep-alive",
                        "Access-Control-Allow-Origin":
                        "*",
                        "Access-Control-Allow-Headers":
                        "Origin, X-Requested-With, Content-Type, Accept"
                    })
                if (int(res.status_code) == HttpStatus.OK):
                    package = Package(**valid_params).create()
                    package_dict = to_dict(package)
                    result = {
                        "message": "Success!",
                        "package": package_dict,
                        "carrier_info": loads(res.text)
                    }
                    return jsonify(result), HttpStatus.CREATED
                else:
                    return jsonify(message="Invalid or expired Tracking Number"
                                   ), HttpStatus.NOT_FOUND
            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