def test_track_braze_event_with_request_error(self): """ If the request receives an error, the function should log an exception message and NOT send an event.""" with mock.patch('ecommerce.extensions.analytics.utils.requests.post', side_effect=RequestException): with mock.patch( 'ecommerce.extensions.analytics.utils.logger.exception' ) as mock_exception: user = self.create_user() track_braze_event(user, 'edx.bi.ecommerce.cart.viewed', {}) mock_exception.assert_called_with( 'Failed to send event to Braze due to request exception.')
def get(self, request): # pylint: disable=unused-argument basket = request.basket try: self.fire_segment_events(request, basket) self.verify_enterprise_needs(basket) response = self.get_payment_api_response() # If there are no products in the basket, no need to send the event if len(response.data['products']) != 0: properties = self._get_cart_viewed_event_properties(basket, response.data) track_braze_event(request.user, 'edx.bi.ecommerce.cart.viewed', properties) return response except RedirectException as e: return Response({'redirect': e.response.url})
def test_track_braze_event_without_braze_settings(self): """ If the braze settings aren't set, the function should log a debug message and NOT send an event.""" with mock.patch('ecommerce.extensions.analytics.utils.logger.debug' ) as mock_debug: user = self.create_user() self.assertIsNone( track_braze_event(user, 'edx.bi.ecommerce.cart.viewed', {})) mock_debug.assert_called_with( 'Failed to send event to Braze: Missing required settings.')
def test_track_braze_event_with_response_error(self): """ If the response receives an error, the function should log a debug message and NOT send an event.""" braze_url = 'https://{url}/users/track'.format( url=getattr(settings, 'BRAZE_EVENT_REST_ENDPOINT')) httpretty.register_uri( httpretty.POST, braze_url, body=json.dumps({ 'events_processed': 0, 'message': 'Braze encountered an error.' }), content_type='application/json', status=500, ) with mock.patch('ecommerce.extensions.analytics.utils.logger.debug' ) as mock_debug: user = self.create_user() track_braze_event(user, 'edx.bi.ecommerce.cart.viewed', {}) mock_debug.assert_called_with( 'Failed to send event [%s] to Braze: %s', 'edx.bi.ecommerce.cart.viewed', 'Braze encountered an error.')
def test_track_braze_event_success(self): """ If the braze settings aren't set, the function should log a debug message and NOT send an event.""" braze_url = 'https://{url}/users/track'.format( url=getattr(settings, 'BRAZE_EVENT_REST_ENDPOINT')) httpretty.register_uri( httpretty.POST, braze_url, body=json.dumps({ 'events_processed': 1, 'message': 'success' }), content_type='application/json', ) with mock.patch('ecommerce.extensions.analytics.utils.logger.debug' ) as mock_debug: user = self.create_user() self.assertIsNone( track_braze_event(user, 'edx.bi.ecommerce.cart.viewed', {'prop': 123})) mock_debug.assert_not_called()