Ejemplo n.º 1
0
    def test_send_success(self):
        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string)

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 200
        requests.post.return_value.text = '{"id": 768}'
        result = transport.send(self.sample_xml)

        self.assertTrue(result)
Ejemplo n.º 2
0
    def test_send_success(self):
        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string)

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 200
        requests.post.return_value.text = '{"id": 768}'
        result = transport.send(self.sample_xml)

        self.assertTrue(result)
Ejemplo n.º 3
0
    def test_valid_url_cant_connect(self):
        transport = CertificationTransport(self.unreachable_url,
                                           self.valid_option_string)
        dummy_data = BytesIO(b"some data to send")
        requests.post.side_effect = ConnectionError

        with self.assertRaises(ConnectionError):
            result = transport.send(dummy_data)
            self.assertIsNotNone(result)
        requests.post.assert_called_with(
            self.unreachable_url,
            files={'data': dummy_data},
            headers={'X_HARDWARE_ID': self.valid_secure_id},
            proxies=None)
Ejemplo n.º 4
0
    def test_invalid_url(self):
        transport = CertificationTransport(self.invalid_url,
                                           self.valid_option_string)
        dummy_data = BytesIO(b"some data to send")
        requests.post.side_effect = InvalidSchema

        with self.assertRaises(InvalidSchema):
            result = transport.send(dummy_data)
            self.assertIsNotNone(result)
        requests.post.assert_called_with(
            self.invalid_url,
            files={'data': dummy_data},
            headers={'X_HARDWARE_ID': self.valid_secure_id},
            proxies=None)
Ejemplo n.º 5
0
    def test_valid_url_cant_connect(self):
        transport = CertificationTransport(self.unreachable_url,
                                           self.valid_option_string)
        dummy_data = BytesIO(b"some data to send")
        requests.post.side_effect = ConnectionError

        with self.assertRaises(ConnectionError):
            result = transport.send(dummy_data)
            self.assertIsNotNone(result)
        requests.post.assert_called_with(self.unreachable_url,
                                         files={'data': dummy_data},
                                         headers={'X_HARDWARE_ID':
                                                  self.valid_secure_id},
                                         proxies=None)
Ejemplo n.º 6
0
    def test_invalid_url(self):
        transport = CertificationTransport(self.invalid_url,
                                           self.valid_option_string)
        dummy_data = BytesIO(b"some data to send")
        requests.post.side_effect = InvalidSchema

        with self.assertRaises(InvalidSchema):
            result = transport.send(dummy_data)
            self.assertIsNotNone(result)
        requests.post.assert_called_with(self.invalid_url,
                                         files={'data': dummy_data},
                                         headers={'X_HARDWARE_ID':
                                         self.valid_secure_id},
                                         proxies=None)
Ejemplo n.º 7
0
    def test_send_failure(self):
        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string)

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 412
        requests.post.return_value.text = 'Some error'
        #Oops, raise_for_status doesn't get fooled by my mocking,
        #so I have to mock *that* method as well..
        requests.post.return_value.raise_for_status = MagicMock(
            side_effect=HTTPError)

        with self.assertRaises(HTTPError):
            result = transport.send(self.sample_xml)
            self.assertIsNotNone(result)
Ejemplo n.º 8
0
    def test_send_failure(self):
        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string)

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 412
        requests.post.return_value.text = 'Some error'
        #Oops, raise_for_status doesn't get fooled by my mocking,
        #so I have to mock *that* method as well..
        requests.post.return_value.raise_for_status = MagicMock(
            side_effect=HTTPError)

        with self.assertRaises(HTTPError):
            result = transport.send(self.sample_xml)
            self.assertIsNotNone(result)
Ejemplo n.º 9
0
 def test_invalid_length_secure_id_are_rejected(self):
     for length in (14, 16, 20):
         dummy_id = "a" * length
         option_string = "secure_id={}".format(dummy_id)
         with self.assertRaises(InvalidSecureIDError):
             transport = CertificationTransport(self.valid_url,
                                                option_string)
             self.assertIsInstance(CertificationTransport, transport)
Ejemplo n.º 10
0
    def _submit_results(self):
        print("Submitting results to {0} for secure_id {1}".format(
              self.config.c3_url, self.config.secure_id))
        options_string = "secure_id={0}".format(self.config.secure_id)
        # Create the transport object
        try:
            transport = CertificationTransport(
                self.config.c3_url, options_string, self.config)
        except InvalidSecureIDError as exc:
            print(exc)
            return False
        # Prepare the data for submission
        data = self.exporter.get_session_data_subset(self.session)
        with tempfile.NamedTemporaryFile(mode='w+b') as stream:
            # Dump the data to the temporary file
            self.exporter.dump(data, stream)
            # Flush and rewind
            stream.flush()
            stream.seek(0)
            try:
                # Send the data, reading from the temporary file
                result = transport.send(stream)
                if 'url' in result:
                    print("Successfully sent, submission status at {0}".format(
                          result['url']))
                else:
                    print("Successfully sent, server response: {0}".format(
                          result))

            except InvalidSchema as exc:
                print("Invalid destination URL: {0}".format(exc))
            except ConnectionError as exc:
                print("Unable to connect to destination URL: {0}".format(exc))
            except HTTPError as exc:
                print(("Server returned an error when "
                       "receiving or processing: {0}").format(exc))
            except IOError as exc:
                print("Problem reading a file: {0}".format(exc))
Ejemplo n.º 11
0
    def _submit_results(self):
        print("Submitting results to {0} for secure_id {1}".format(
            self.config.c3_url, self.config.secure_id))
        options_string = "secure_id={0}".format(self.config.secure_id)
        # Create the transport object
        try:
            transport = CertificationTransport(self.config.c3_url,
                                               options_string, self.config)
        except InvalidSecureIDError as exc:
            print(exc)
            return False
        # Prepare the data for submission
        data = self.exporter.get_session_data_subset(self.session)
        with tempfile.NamedTemporaryFile(mode='w+b') as stream:
            # Dump the data to the temporary file
            self.exporter.dump(data, stream)
            # Flush and rewind
            stream.flush()
            stream.seek(0)
            try:
                # Send the data, reading from the temporary file
                result = transport.send(stream)
                if 'url' in result:
                    print("Successfully sent, submission status at {0}".format(
                        result['url']))
                else:
                    print("Successfully sent, server response: {0}".format(
                        result))

            except InvalidSchema as exc:
                print("Invalid destination URL: {0}".format(exc))
            except ConnectionError as exc:
                print("Unable to connect to destination URL: {0}".format(exc))
            except HTTPError as exc:
                print(("Server returned an error when "
                       "receiving or processing: {0}").format(exc))
            except IOError as exc:
                print("Problem reading a file: {0}".format(exc))
Ejemplo n.º 12
0
    def proxy_test(self, environment, proxies):
        test_environment = environment
        test_proxies = proxies
        test_config = PlainBoxConfig()
        test_config.environment = test_environment

        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string,
                                           config=test_config)
        dummy_data = BytesIO(b"some data to send")

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 200
        requests.post.return_value.text = '{"id": 768}'
        result = transport.send(dummy_data)

        self.assertTrue(result)

        requests.post.assert_called_with(
            self.valid_url,
            files={'data': dummy_data},
            headers={'X_HARDWARE_ID': self.valid_secure_id},
            proxies=test_proxies)
Ejemplo n.º 13
0
    def proxy_test(self, environment, proxies):
        test_environment = environment
        test_proxies = proxies
        test_config = PlainBoxConfig()
        test_config.environment = test_environment

        transport = CertificationTransport(self.valid_url,
                                           self.valid_option_string,
                                           config=test_config)
        dummy_data = BytesIO(b"some data to send")

        requests.post.return_value = MagicMock(name='response')
        requests.post.return_value.status_code = 200
        requests.post.return_value.text = '{"id": 768}'
        result = transport.send(dummy_data)

        self.assertTrue(result)

        requests.post.assert_called_with(self.valid_url,
                                         files={'data': dummy_data},
                                         headers={'X_HARDWARE_ID':
                                         self.valid_secure_id},
                                         proxies=test_proxies)
Ejemplo n.º 14
0
 def test_invalid_characters_in_secure_id_are_rejected(self):
     option_string = "secure_id=aA0#"
     with self.assertRaises(InvalidSecureIDError):
         transport = CertificationTransport(self.valid_url, option_string)
         self.assertIsInstance(CertificationTransport, transport)
Ejemplo n.º 15
0
 def test_parameter_parsing(self):
     #Makes sense since I'm overriding the base class's constructor.
     transport = CertificationTransport(self.valid_url,
                                        self.valid_option_string)
     self.assertEqual(self.valid_url, transport.url)
     self.assertEqual(self.valid_secure_id, transport.options['secure_id'])