Exemple #1
0
    def test_handle_event(self, mock_create):
        # Setup
        notifier_config = {"url": "https://localhost/api/", "username": "******", "password": "******"}

        event = Event("type-1", {"k1": "v1"})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.OK

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)
        time.sleep(0.5)  # handle works in a thread so give it a bit to finish

        # Verify
        self.assertEqual(1, mock_create.call_count)
        self.assertEqual(1, mock_connection.request.call_count)

        request_args = mock_connection.request.call_args[0]
        self.assertEqual("POST", request_args[0])
        self.assertEqual("/api/", request_args[1])

        expected_body = {"event_type": event.event_type, "payload": event.payload, "call_report": None}

        request_kwargs = mock_connection.request.call_args[1]
        parsed_body = json.loads(request_kwargs["body"])
        self.assertEqual(parsed_body, expected_body)

        headers = request_kwargs["headers"]
        self.assertTrue("Authorization" in headers)
Exemple #2
0
    def test_handle_event_unparsable_url(self, mock_create):
        # Test
        http.handle_event({'url': '!@#$%'}, Event('type-1',
                                                  {}))  # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #3
0
    def test_handle_event_missing_url(self, mock_create, mock_task_ser):
        # Test
        mock_task_ser.return_value = 'serialized data!'
        http.handle_event({}, Event('type-1', {}))  # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #4
0
    def test_handle_event_unparsable_url(self, mock_create, mock_task_ser):
        # Test
        mock_task_ser.return_value = 'serialized data!'
        http.handle_event({'url': '!@#$%'}, Event('type-1', {}))  # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #5
0
    def test_handle_event(self, mock_thread):
        # Setup
        notifier_config = {'key': 'value'}
        mock_event = mock.Mock(spec=Event)
        event_data = mock_event.data.return_value

        # Test
        http.handle_event(notifier_config, mock_event)
        mock_thread.assert_called_once_with(
            target=http._send_post,
            args=[notifier_config, event_data]
        )
Exemple #6
0
    def test_handle_event(self, mock_thread):
        # Setup
        notifier_config = {'key': 'value'}
        mock_event = mock.Mock(spec=Event)
        event_data = mock_event.data.return_value

        # Test
        http.handle_event(notifier_config, mock_event)
        mock_thread.assert_called_once_with(
            target=http._send_post,
            args=[notifier_config, event_data]
        )
Exemple #7
0
    def test_handle_event(self, mock_send_post, mock_jutil, mock_json):
        # Setup
        notifier_config = {'key': 'value'}
        mock_event = mock.Mock(spec=Event)
        event_data = mock_event.data.return_value

        # Test
        http.handle_event(notifier_config, mock_event)
        mock_json.dumps.assert_called_once_with(event_data, default=mock_jutil.default)
        mock_send_post.assert_called_once_with(
            notifier_config,
            mock_json.dumps.return_value
        )
Exemple #8
0
    def test_handle_event_with_serialize_error(self, mock_create):
        # Setup
        notifier_config = {'url': 'https://localhost/api/'}

        event = Event('type-1', {'k1': 'v1', '_id': _test_objid()})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.NOT_FOUND

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)  # should not throw TypeError
Exemple #9
0
    def test_handle_event_with_serialize_error(self, mock_create):
        # Setup
        notifier_config = {"url": "https://localhost/api/"}

        event = Event("type-1", {"k1": "v1", "_id": _test_objid()})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.NOT_FOUND

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)  # should not throw TypeError
    def test_handle_event_with_serialize_error(self, mock_create):
        # Setup
        notifier_config = {'url' : 'https://localhost/api/'}

        event = Event('type-1', {'k1' : 'v1', '_id': _test_objid()})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.NOT_FOUND

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event) # should not throw TypeError
Exemple #11
0
    def test_handle_event(self, mock_create, mock_task_ser):
        # Setup
        notifier_config = {
            'url': 'https://localhost/api/',
            'username': '******',
            'password': '******',
        }

        event = Event('type-1', {'k1': 'v1'})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()
        mock_task_ser.return_value = None

        mock_response.status = httplib.OK

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)
        time.sleep(.5)  # handle works in a thread so give it a bit to finish

        # Verify
        self.assertEqual(1, mock_create.call_count)
        self.assertEqual(1, mock_connection.request.call_count)

        request_args = mock_connection.request.call_args[0]
        self.assertEqual('POST', request_args[0])
        self.assertEqual('/api/', request_args[1])

        expected_body = {
            'event_type': event.event_type,
            'payload': event.payload,
            'call_report': None
        }

        request_kwargs = mock_connection.request.call_args[1]
        parsed_body = json.loads(request_kwargs['body'])
        self.assertEqual(parsed_body, expected_body)

        headers = request_kwargs['headers']
        self.assertTrue('Authorization' in headers)
Exemple #12
0
    def test_handle_event(self, mock_create, mock_task_ser):
        # Setup
        notifier_config = {
            'url': 'https://localhost/api/',
            'username': '******',
            'password': '******',
        }

        event = Event('type-1', {'k1': 'v1'})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()
        mock_task_ser.return_value = None

        mock_response.status = httplib.OK

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)
        time.sleep(.5)  # handle works in a thread so give it a bit to finish

        # Verify
        self.assertEqual(1, mock_create.call_count)
        self.assertEqual(1, mock_connection.request.call_count)

        request_args = mock_connection.request.call_args[0]
        self.assertEqual('POST', request_args[0])
        self.assertEqual('/api/', request_args[1])

        expected_body = {'event_type': event.event_type,
                         'payload': event.payload,
                         'call_report': None}

        request_kwargs = mock_connection.request.call_args[1]
        parsed_body = json.loads(request_kwargs['body'])
        self.assertEqual(parsed_body, expected_body)

        headers = request_kwargs['headers']
        self.assertTrue('Authorization' in headers)
Exemple #13
0
    def test_handle_event_with_error(self, mock_create):
        # Setup
        notifier_config = {'url': 'https://localhost/api/'}

        event = Event('type-1', {'k1': 'v1'})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.NOT_FOUND

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event)  # should not error
        time.sleep(.5)

        # Verify
        self.assertEqual(1, mock_create.call_count)
        self.assertEqual(1, mock_connection.request.call_count)
Exemple #14
0
    def test_handle_event_with_error(self, mock_create):
        # Setup
        notifier_config = {'url' : 'https://localhost/api/'}

        event = Event('type-1', {'k1' : 'v1'})

        mock_connection = mock.Mock()
        mock_response = mock.Mock()

        mock_response.status = httplib.NOT_FOUND

        mock_connection.getresponse.return_value = mock_response
        mock_create.return_value = mock_connection

        # Test
        http.handle_event(notifier_config, event) # should not error
        time.sleep(.5)

        # Verify
        self.assertEqual(1, mock_create.call_count)
        self.assertEqual(1, mock_connection.request.call_count)
Exemple #15
0
    def test_handle_event_missing_url(self, mock_create):
        # Test
        http.handle_event({}, Event('type-1', {}))  # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #16
0
    def test_handle_event_unparsable_url(self, mock_create):
        # Test
        http.handle_event({"url": "!@#$%"}, Event("type-1", {}))  # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #17
0
    def test_handle_event_missing_url(self, mock_create):
        # Test
        http.handle_event({}, Event('type-1', {})) # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)
Exemple #18
0
    def test_handle_event_unparsable_url(self, mock_create):
        # Test
        http.handle_event({'url' : '!@#$%'}, Event('type-1', {})) # should not error

        # Verify
        self.assertEqual(0, mock_create.call_count)