Example #1
0
 def test_decode_initial_value(self):
     """
     Test for initializing the transport with a well-encoded initial value
     """
     value = b'1234'
     trans = TLambda.TLambdaServerTransport(base64.b64encode(value))
     assert_trans_read_content(trans, value)
Example #2
0
 def test_getvalue_initial_value(self):
     """
     Test for calling `getvalue` from a transport that has been initialized with an initial value
     """
     value = b'1234'
     trans = TLambda.TTransformTransport(value)
     assert trans.getvalue() == value
Example #3
0
 def test_init_invalid_value(self):
     """
     Test for initializing with non-base64 encoded value
     """
     value = b'12342134525'
     with pytest.raises(binascii.Error):
         TLambda.TLambdaServerTransport(value)
Example #4
0
 def test_getvalue_initial_value(self):
     """
     Test for calling `getvalue` from a transport that has been initialized with an initial value
     """
     value = b'1234'
     trans = TLambda.TLambdaServerTransport(base64.b64encode(value))
     assert trans.getvalue() == base64.b64encode(value)
Example #5
0
 def test_read_initial_value(self):
     """
     Test for reading from the transport after it has been initialized with an initial value
     """
     value = b'1234'
     trans = TLambda.TTransformTransport(value)
     assert_trans_read_content(trans, value)
Example #6
0
 def test_flush_empty(self):
     """
     Test for flushing an empty transport (no data has been written)
     """
     trans = TLambda.TTransformTransport()
     trans.flush()
     assert_trans_read_empty(trans)
Example #7
0
    def test_read_after_flush_function_error(self):
        """
        Tests reading data from the transport after writing a request and flushing
        it, which triggers the request to be sent and the response to be received and decoded.
        In this test the Server has an error and the response received indicates a Lambda
        error
        """
        response = {
            'StatusCode': 500,
            'FunctionError': 'Internal Server Error',
            'Payload': io.BytesIO(b'internal lambda error')
        }
        client_mock_obj = self._create_test_mock(response)

        with unittest.mock.patch('boto3.client',
                                 side_effect=[client_mock_obj]) as client_mock:
            trans = TLambda.TLambdaClientTransport(
                function_name=self.TEST_FUNCTION_NAME)
            trans.write(self.TEST_PAYLOAD)
            with pytest.raises(TLambda.LambdaServerError):
                trans.flush()
            assert_trans_read_empty(trans)
            client_mock.assert_called_once()
            self._validate_invocation(client_mock_obj, self.TEST_FUNCTION_NAME,
                                      self.TEST_PAYLOAD)
Example #8
0
 def test_isOpen(self):
     """
     Test for the isOpen function (True before closing, False after)
     """
     trans = TLambda.TTransformTransport()
     assert trans.isOpen()
     trans.close()
     assert not trans.isOpen()
Example #9
0
 def test_getvalue_write_no_flush(self):
     """
     Test for calling `getvalue` for a transport after writing to it without flushing (should be empty)
     """
     value = b'1234'
     trans = TLambda.TLambdaServerTransport()
     trans.write(value)
     assert trans.getvalue() == b''
Example #10
0
 def test_write_no_flush(self):
     """
     Assert that no data is transformed and set for read until flush
     """
     value = b'1234'
     trans = TLambda.TTransformTransport()
     trans.write(value)
     assert_trans_read_empty(trans)
Example #11
0
 def test_write_flush_read(self):
     """
     Assert that data is transformed after flush and ready to be read
     """
     value = b'1234'
     trans = TLambda.TTransformTransport()
     trans.write(value)
     trans.flush()
     assert_trans_read_content(trans, value)
Example #12
0
 def test_getvalue_write_with_flush(self):
     """
     Test for calling `getvalue` for a transport after writing to it and flushing
     """
     value = b'1234'
     trans = TLambda.TLambdaServerTransport()
     trans.write(value)
     trans.flush()
     assert trans.getvalue() == base64.b64encode(value)
Example #13
0
    def test_close(self):
        """
        Test for closing the transport, and attempting operations after it has been closed.
        """
        trans = TLambda.TTransformTransport()
        trans.close()
        with pytest.raises(ValueError):
            trans.write('123')

        with pytest.raises(ValueError):
            trans.read(1)
Example #14
0
 def test_read_no_write(self):
     """
     Test for reading data from the transport without data being written to if first
     """
     client_mock_obj = unittest.mock.NonCallableMagicMock()
     with unittest.mock.patch('boto3.client',
                              side_effect=[client_mock_obj]) as client_mock:
         trans = TLambda.TLambdaClientTransport(
             function_name=self.TEST_FUNCTION_NAME)
         assert_trans_read_empty(trans)
         client_mock.assert_called_once()
Example #15
0
 def test_read_no_flush(self):
     """
     Test for reading data from the transport after writing data to the transport
     but not flushing it
     """
     client_mock_obj = unittest.mock.NonCallableMagicMock()
     with unittest.mock.patch('boto3.client',
                              side_effect=[client_mock_obj]) as client_mock:
         trans = TLambda.TLambdaClientTransport(
             function_name=self.TEST_FUNCTION_NAME)
         trans.write(self.TEST_PAYLOAD)
         assert_trans_read_empty(trans)
         client_mock.assert_called_once()
Example #16
0
    def test_read_after_flush_valid_response(self):
        """
        Sanity test. Tests reading data from the transport after writing a request and flushing
        it, which triggers the request to be sent and the response to be received and decoded
        """
        response = self._create_server_response(self.TEST_RESPONSE_PAYLOAD)
        client_mock_obj = self._create_test_mock(response)

        with unittest.mock.patch('boto3.client',
                                 side_effect=[client_mock_obj]) as client_mock:
            trans = TLambda.TLambdaClientTransport(
                function_name=self.TEST_FUNCTION_NAME)
            trans.write(self.TEST_PAYLOAD)
            trans.flush()
            assert_trans_read_content(trans, self.TEST_RESPONSE_PAYLOAD)
            client_mock.assert_called_once()
            self._validate_invocation(client_mock_obj, self.TEST_FUNCTION_NAME,
                                      self.TEST_PAYLOAD)
Example #17
0
    def test_read_after_flush_invalid_response(self, response):
        """
        Tests reading data from the transport after writing a request and flushing
        it, which triggers the request to be sent and the response to be received and decoded.
        In this test the Server returns a response, but it is encoded wrong and the client
        cannot decode it.
        """
        client_mock_obj = self._create_test_mock(response)

        with unittest.mock.patch('boto3.client',
                                 side_effect=[client_mock_obj]) as client_mock:
            trans = TLambda.TLambdaClientTransport(
                function_name=self.TEST_FUNCTION_NAME)
            trans.write(self.TEST_PAYLOAD)
            with pytest.raises(TLambda.PayloadDecodeError):
                trans.flush()
            assert_trans_read_empty(trans)
            client_mock.assert_called_once()
            self._validate_invocation(client_mock_obj, self.TEST_FUNCTION_NAME,
                                      self.TEST_PAYLOAD)
Example #18
0
 def test_getvalue_empty(self):
     """
     Test for calling the `getvalue` function when the transport's read buffer is empty
     """
     trans = TLambda.TLambdaServerTransport()
     assert trans.getvalue() == b''
Example #19
0
 def test_empty_initial(self):
     """
     Test for initializing the transport without initial value
     """
     trans = TLambda.TLambdaServerTransport()
     assert_trans_read_empty(trans)
Example #20
0
 def test_getvalue_empty(self):
     """
     Test for calling `getvalue` on an empty transport
     """
     trans = TLambda.TTransformTransport()
     assert trans.getvalue() == b''
Example #21
0
 def test_read_empty(self):
     """
     Test for reading from the transport when it is empty
     """
     trans = TLambda.TTransformTransport()
     assert_trans_read_empty(trans)