Exemple #1
0
def _seal(key, context, in_file, out_file):
    casted_aepipe_context = ffi.cast('struct aepipe_context *',
                                     ffi.addressof(context))
    with Contexter() as contexter:
        in_file = _prep_file(contexter, in_file, os.O_RDONLY)
        out_file = _prep_file(contexter, out_file,
                              os.O_WRONLY | os.O_CREAT | os.O_TRUNC)

        _check(lib.aepipe_seal(key, casted_aepipe_context, in_file, out_file))
Exemple #2
0
    def test_user_bad_request(self):
        """
        Check the endpoint returns an error message because the user resource was bad request (HTTP 400)
        """

        with Contexter(*self.get_patches()):

            user_data = {'user_id': '105398890'}

            response = self.http_request.post(user_data)
            self.json_structure_response_code_assert(400, response)
Exemple #3
0
    def test_withdrawal_bad_request(self):
        """
        Check the endpoint returns an error message because the withdrawal resource was bad request (HTTP 400)
        """

        with Contexter(*self.get_patches()):

            withdrawal_data = {'amount': 1200}

            response = self.http_request.patch(withdrawal_data)
            self.json_structure_response_code_assert(400, response)
Exemple #4
0
    def test_user_verification_true(self):
        """
        Check the endpoint returns True response with a user validation True (HTTP 200)
        """

        with Contexter(*self.get_patches()):

            response = self.http_request.post(self.user_data)
            self.json_structure_response_code_assert(200, response)

            data = deserialize_json(response.data)
            self.assertEqual(data['is_valid'], True)
Exemple #5
0
    def test_user_verification_false(self):
        """
        Check the endpoint returns False response with a COP withdrawal (HTTP 200)
        """

        with Contexter(*self.get_patches()):

            user_data = {'user_id': '105398899', 'pin': 2093}

            response = self.http_request.post(user_data)
            self.json_structure_response_code_assert(200, response)

            data = deserialize_json(response.data)
            self.assertEqual(data['is_valid'], False)
Exemple #6
0
    def test_user_unauthorized_workflow(self):
        input_json = json.dumps(TEST_EXAMPLE_3).encode("utf-8")

        with Contexter(*self.get_patches()):
            mock_file = FileStorage(
                stream=io.BytesIO(input_json),
                filename="test_example1.json",
                content_type="application/json",
            )
            response = self.client.post(self.url,
                                        data={
                                            "json_file": mock_file,
                                        },
                                        content_type="multipart/form-data")
            self.assertEqual(response.status_code, 401)
Exemple #7
0
    def test_workflow_success(self):
        """
        Check the endpoint returns a success workflow response (HTTP 200)
        """
        workflow_json = json.dumps(WORKFLOW_MOCK).encode("utf-8")

        with Contexter(*self.get_patches()):
            workflow_data = FileStorage(
                stream=io.BytesIO(workflow_json),
                filename="workflow_data.json",
                content_type="application/json",
            )

            response = self.http_request.post(workflow_data)
            self.json_structure_response_code_assert(200, response)
Exemple #8
0
    def test_workflow_insufficient_founds_error(self):
        """
        Check the endpoint returns a insufficient founds workflow response (HTTP 409)
        """
        workflow_json = json.dumps(WORKFLOW_MOCK).encode("utf-8")

        new_patch_dict = {
            'app.api.controllers.workflow_process.get_account_balance': {
                'return_value': {
                    'user_id': '105398891',
                    'balance': 110000,
                    'account_number': '000101'
                }
            },
            'app.api.controllers.workflow_process.deposit_money': {
                'return_value': {
                    'balance': 1
                }
            },
            'app.api.controllers.workflow_process.withdraw': {
                'return_value': {
                    'balance': 23000000000
                }
            }
        }

        with Contexter(*self.get_patches(new_patch_dict)):
            workflow_data = FileStorage(
                stream=io.BytesIO(workflow_json),
                filename="workflow_data.json",
                content_type="application/json",
            )

            response = self.http_request.post(workflow_data)
            self.json_structure_response_code_assert(409, response)

            data = deserialize_json(response.data)
            messages = data['message']
            error = messages[len(messages) - 1]
            self.assertEqual(
                error,
                'ERROR MESSAGE: Insufficient funds to perform a 30 USD withdrawal, account_number 000101.'
            )
Exemple #9
0
def aepipe_ctx(key, buf, op):
    with Contexter() as ctx:
        executor = ctx << ThreadPoolExecutor(max_workers=2)

        (input_r, input_w) = os.pipe()
        (output_r, output_w) = os.pipe()

        ctx << closing_fd(output_r)
        output_closer = ctx << closing_fd(output_w)
        ctx << closing_fd(input_r)
        input_closer = ctx << closing_fd(input_w)

        aepipe_f = executor.submit(op, key, input_r, output_w)
        output_f = executor.submit(drain_into, output_r, buf)

        yield input_w

        input_closer.close()
        aepipe_f.result()

        output_closer.close()
        output_f.result()
Exemple #10
0
    def test_invalid_user_verification(self):
        """
        Check the endpoint returns a invalid user workflow response (HTTP 200)
        """
        workflow_json = json.dumps(WORKFLOW_MOCK).encode("utf-8")

        new_patch_dict = {
            'app.api.controllers.workflow_process.validate_user_account': {
                'return_value': {
                    'is_valid': False
                }
            }
        }

        with Contexter(*self.get_patches(new_patch_dict)):
            workflow_data = FileStorage(
                stream=io.BytesIO(workflow_json),
                filename="workflow_data.json",
                content_type="application/json",
            )

            response = self.http_request.post(workflow_data)
            self.json_structure_response_code_assert(401, response)
Exemple #11
0
    def test_withdrawal_cop_success(self):
        """
        Check the endpoint returns success response with a COP withdrawal (HTTP 200)
        """

        with Contexter(*self.get_patches()):

            response = self.http_request.patch(self.withdrawal_cop_data)
            self.json_structure_response_code_assert(200, response)

            collection = self.db['bank_account']
            bank_account = collection.find_one({'account_number': '000100'})

            self.assertEqual(bank_account['balance'], 70000.0)
            self.assertEqual(bank_account['account_number'], '000100')

            transactions = []
            collection = self.db['transaction']
            transactions_db = collection.find({'account_number': '000100'})
            for transaction in transactions_db:
                transactions.append(transaction)

            self.assertGreater(len(transactions),
                               2)  # Because two were stored by default
Exemple #12
0
 def run(self, options, _args):
     with Contexter() as ctx:
         self.cleanup = ctx
         options, args = self.prepare_options(options)
         requirement_set = self.super_run(options, args)
         return requirement_set
Exemple #13
0
def _unseal_unchecked(key, in_file, out_file):
    with Contexter() as contexter:
        in_file = _prep_file(contexter, in_file, os.O_RDONLY)
        out_file = _prep_file(contexter, out_file,
                              os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
        return lib.aepipe_unseal(key, in_file, out_file)