Example #1
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
        rest_api.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)
    def test_handle_event(self, mock_create):
        # Setup
        notifier_config = {
            'url' : 'https://localhost/api/',
            'username' : 'admin',
            'password' : 'admin',
        }

        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
        rest_api.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)
Example #3
0
    def test_handle_event_unparsable_url(self, mock_create):
        # Test
        rest_api.handle_event({'url' : '!@#$%'}, Event('type-1', {})) # should not error

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

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