def test_order_no_lms_user_id(self, mock_track): """ Ensure that expected values are substituted when no LMS user id was available. """ tracking_context = { 'ga_client_id': 'test-client-id', 'lms_user_id': 'test-user-id', 'lms_ip': '127.0.0.1' } self.user.tracking_context = tracking_context self.user.lms_user_id = None self.user.save() EdxOrderPlacementMixin().handle_successful_order(self.order) # ensure event is being tracked self.assertTrue(mock_track.called) # ensure event data is correct self.assert_correct_event( mock_track, self.order, ECOM_TRACKING_ID_FMT.format(self.user.id), tracking_context['ga_client_id'], tracking_context['lms_ip'], self.order.number, self.order.currency, self.order.user.email, self.order.total_excl_tax, self.order. total_excl_tax, # value for revenue field is same as total. check_traits=True, )
def assert_refund_event_fired(self, mock_track, refund, tracking_context=None): (event_user_id, event_name, event_payload), kwargs = mock_track.call_args self.assertTrue(mock_track.called) self.assertEqual(event_name, 'Order Refunded') if tracking_context is not None: expected_event_user_id = tracking_context['lms_user_id'] expected_context = { 'ip': tracking_context['lms_ip'], 'Google Analytics': { 'clientId': tracking_context['ga_client_id'] } } else: expected_event_user_id = ECOM_TRACKING_ID_FMT.format(refund.user.id) expected_context = {'ip': None, 'Google Analytics': {'clientId': None}} self.assertEqual(event_user_id, expected_event_user_id) self.assertEqual(kwargs['context'], expected_context) self.assertEqual(event_payload['orderId'], refund.order.number) expected_products = [ { 'id': line.order_line.partner_sku, 'quantity': line.quantity, } for line in refund.lines.all() ] self.assertEqual(event_payload['products'], expected_products)
def get(self, _, username): """ Returns the old-style ecommerce tracking id (ecommerce-{id}) and the newer-style LMS user id of the given LMS user, identified by username. """ try: if not username: raise User.DoesNotExist() user = User.objects.get(username=username) # If the user does not already have an LMS user id, add it. Note that we allow a missing LMS user id here # because this API only reads data from the db. called_from = u'retirement API' user.add_lms_user_id('ecommerce_missing_lms_user_id_retirement', called_from, allow_missing=True) return Response({ 'id': user.pk, 'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk), 'lms_user_id': user.lms_user_id_with_metric(usage='retirement API', allow_missing=True) }) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND, data={'message': 'Invalid user.'})
def test_refund_tracking_without_lms_user_id(self, mock_track): """Verify that a successfully placed refund is tracked, even if no LMS user id is available.""" self.refund.user.lms_user_id = None self.approve(self.refund) expected_user_id = ECOM_TRACKING_ID_FMT.format(self.refund.user.id) self.assert_refund_event_fired(mock_track, self.refund, expected_user_id=expected_user_id)
def test_successful_get(self): user = self.create_user() response = EcommerceIdView().get(None, username=user.username) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertDictEqual( response.data, { 'id': user.pk, 'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk) })
def test_get_missing_lms_user_id(self): user = self.create_user(lms_user_id=None) response = EcommerceIdView().get(None, username=user.username) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertDictEqual( response.data, { 'id': user.pk, 'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk), 'lms_user_id': None })
def test_parse_tracking_context_missing_lms_user_id(self): """ The method should parse the tracking context on the User object. """ tracking_context = { 'ga_client_id': 'test-client-id', 'lms_user_id': 'foo', 'lms_ip': '18.0.0.1', } # If no LMS user ID is provided, we should create one based on the E-Commerce ID user = self.create_user(tracking_context=tracking_context, lms_user_id=None) expected_user_id = ECOM_TRACKING_ID_FMT.format(user.id) expected = (expected_user_id, tracking_context['ga_client_id'], tracking_context['lms_ip']) self.assertEqual(parse_tracking_context(user), expected)
def test_handle_successful_order_no_context(self, mock_track): """ Ensure that expected values are substituted when no tracking_context was available. """ EdxOrderPlacementMixin().handle_successful_order(self.order) # ensure event is being tracked self.assertTrue(mock_track.called) # ensure event data is correct self.assert_correct_event(mock_track, self.order, ECOM_TRACKING_ID_FMT.format(self.user.id), None, None, self.order.number, self.order.currency, self.order.total_excl_tax)
def get(self, _, username): """ Returns the ecommerce tracking id of the given LMS user, identified by username. """ try: if not username: raise User.DoesNotExist() user = User.objects.get(username=username) return Response( { 'id': user.pk, 'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk) } ) except User.DoesNotExist: return Response( status=status.HTTP_404_NOT_FOUND, data={'message': 'Invalid user.'} )
def get(self, _, username): """ Returns the old-style ecommerce tracking id (ecommerce-{id}) and the newer-style LMS user id of the given LMS user, identified by username. """ try: if not username: raise User.DoesNotExist() user = User.objects.get(username=username) return Response({ 'id': user.pk, 'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk), 'lms_user_id': user.lms_user_id_with_metric(usage='retirement API') }) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND, data={'message': 'Invalid user.'})