Exemplo n.º 1
0
 def test_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json'
     )
     segmentio.track_segmentio_event(request)
     self.assert_events_emitted()
Exemplo n.º 2
0
 def test_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json'
     )
     segmentio.track_segmentio_event(request)
     self.assert_events_emitted()
Exemplo n.º 3
0
 def post_segmentio_event(self, **kwargs):
     """Post a fake Segment event to the view that processes it"""
     request = self.create_request(
         data=self.create_segmentio_event_json(**kwargs),
         content_type='application/json'
     )
     segmentio.track_segmentio_event(request)
Exemplo n.º 4
0
 def test_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json')
     User.objects.create(pk=USER_ID, username=str(sentinel.username))
     segmentio.track_segmentio_event(request)
     self.assert_events_emitted()
Exemplo n.º 5
0
 def post_segmentio_event(self, **kwargs):
     """Post a fake segment.io event to the view that processes it"""
     request = self.create_request(
         data=self.create_segmentio_event_json(**kwargs),
         content_type='application/json'
     )
     segmentio.track_segmentio_event(request)
Exemplo n.º 6
0
 def post_modified_segmentio_event(self, event):
     """Post an externally-defined fake Segment event to the view that processes it"""
     request = self.create_request(
         data=json.dumps(event),
         content_type='application/json'
     )
     segmentio.track_segmentio_event(request)
Exemplo n.º 7
0
    def test_missing_data(self):
        sample_event_raw = self.create_segmentio_event()
        del sample_event_raw['properties']['data']
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')

        segmentio.track_segmentio_event(request)
Exemplo n.º 8
0
    def test_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 9
0
    def test_missing_data(self):
        sample_event_raw = self.create_segmentio_event()
        del sample_event_raw['properties']['data']
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 10
0
    def test_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )

        segmentio.track_segmentio_event(request)
Exemplo n.º 11
0
    def test_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )

        segmentio.track_segmentio_event(request)
Exemplo n.º 12
0
    def test_missing_timestamp(self):
        sample_event_raw = self.create_event_without_fields('timestamp')
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')
        User.objects.create(pk=SEGMENTIO_TEST_USER_ID,
                            username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 13
0
 def test_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json'
     )
     User.objects.create(pk=USER_ID, username=str(sentinel.username))
     segmentio.track_segmentio_event(request)
     self.assert_events_emitted()
Exemplo n.º 14
0
    def test_missing_data(self):
        sample_event_raw = self.create_segmentio_event()
        del sample_event_raw['properties']['data']
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )

        segmentio.track_segmentio_event(request)
Exemplo n.º 15
0
    def test_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 16
0
    def test_missing_timestamp(self):
        sample_event_raw = self.create_event_without_fields('timestamp')
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )
        User.objects.create(pk=SEGMENTIO_TEST_USER_ID, username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 17
0
    def test_missing_data(self):
        sample_event_raw = self.create_segmentio_event()
        del sample_event_raw['properties']['data']
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        segmentio.track_segmentio_event(request)
Exemplo n.º 18
0
 def test_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json')
     User.objects.create(pk=USER_ID, username=str(sentinel.username))
     response = segmentio.track_segmentio_event(request)
     self.assertEquals(response.status_code, 200)
     self.assertTrue(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
Exemplo n.º 19
0
 def test_segmentio_tracking_invalid_course_id(self):
     request = self.create_request(
         data=self.create_segmentio_event_json(course_id='invalid'),
         content_type='application/json'
     )
     User.objects.create(pk=USER_ID, username=str(sentinel.username))
     response = segmentio.track_segmentio_event(request)
     self.assertEquals(response.status_code, 200)
     self.assertTrue(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
Exemplo n.º 20
0
    def test_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        response = segmentio.track_segmentio_event(request)
        self.assert_segmentio_uncommitted_response(
            response, segmentio.ERROR_MISSING_RECEIVED_AT, 400)
Exemplo n.º 21
0
    def test_missing_event_type(self):
        sample_event_raw = self.create_segmentio_event()
        del sample_event_raw['properties']['event_type']
        request = self.create_request(data=json.dumps(sample_event_raw),
                                      content_type='application/json')
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        response = segmentio.track_segmentio_event(request)
        self.assert_segmentio_uncommitted_response(
            response, segmentio.ERROR_MISSING_EVENT_TYPE, 400)
Exemplo n.º 22
0
    def test_segmentio_tracking_missing_received_at(self):
        sample_event_raw = self.create_event_without_fields('receivedAt')
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        response = segmentio.track_segmentio_event(request)
        self.assert_segmentio_uncommitted_response(response, segmentio.ERROR_MISSING_RECEIVED_AT, 400)
Exemplo n.º 23
0
    def test_segmentio_tracking_missing_event_type(self):
        sample_event_raw = self.create_segmentio_event()
        sample_event_raw['properties'] = {}
        request = self.create_request(
            data=json.dumps(sample_event_raw),
            content_type='application/json'
        )
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        response = segmentio.track_segmentio_event(request)
        self.assert_segmentio_uncommitted_response(response, segmentio.ERROR_MISSING_EVENT_TYPE, 400)
Exemplo n.º 24
0
    def test_success(self, course_id):
        middleware = TrackMiddleware()

        request = self.create_request(
            data=self.create_segmentio_event_json(event_type=str(sentinel.event_type), event={'foo': 'bar'}, course_id=course_id),
            content_type='application/json'
        )
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        middleware.process_request(request)
        # The middleware normally emits an event, make sure it doesn't in this case.
        self.assertFalse(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
        try:
            response = segmentio.track_segmentio_event(request)
            self.assertEquals(response.status_code, 200)

            expected_event = {
                'username': str(sentinel.username),
                'ip': '',
                'event_source': 'mobile',
                'event_type': str(sentinel.event_type),
                'name': str(sentinel.name),
                'event': {'foo': 'bar'},
                'agent': str(sentinel.user_agent),
                'page': None,
                'time': datetime.strptime("2014-08-27T16:33:39.215Z", "%Y-%m-%dT%H:%M:%S.%fZ"),
                'host': 'testserver',
                'context': {
                    'user_id': USER_ID,
                    'course_id': course_id,
                    'org_id': 'foo',
                    'path': ENDPOINT,
                    'client': {
                        'library': {
                            'name': 'unknown',
                            'version': 'unknown'
                        },
                        'userAgent': str(sentinel.user_agent)
                    },
                    'received_at': datetime.strptime("2014-08-27T16:33:39.100Z", "%Y-%m-%dT%H:%M:%S.%fZ"),
                },
            }
        finally:
            middleware.process_response(request, None)

        self.mock_tracker.send.assert_called_once_with(expected_event)  # pylint: disable=maybe-no-member
Exemplo n.º 25
0
 def test_secret_mismatch(self):
     request = self.create_request(key='y')
     response = segmentio.track_segmentio_event(request)
     self.assert_segmentio_uncommitted_response(
         response, segmentio.ERROR_UNAUTHORIZED, 401)
Exemplo n.º 26
0
 def test_no_secret_provided(self):
     request = self.request_factory.post(ENDPOINT)
     response = segmentio.track_segmentio_event(request)
     self.assert_segmentio_uncommitted_response(
         response, segmentio.ERROR_UNAUTHORIZED, 401)
Exemplo n.º 27
0
 def test_get_request(self):
     request = self.request_factory.get(ENDPOINT)
     response = segmentio.track_segmentio_event(request)
     self.assertEquals(response.status_code, 405)
     self.assertFalse(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
Exemplo n.º 28
0
    def test_success(self, course_id):
        middleware = TrackMiddleware()

        request = self.create_request(data=self.create_segmentio_event_json(
            event_type=str(sentinel.event_type),
            event={'foo': 'bar'},
            course_id=course_id),
                                      content_type='application/json')
        User.objects.create(pk=USER_ID, username=str(sentinel.username))

        middleware.process_request(request)
        # The middleware normally emits an event, make sure it doesn't in this case.
        self.assertFalse(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
        try:
            response = segmentio.track_segmentio_event(request)
            self.assertEquals(response.status_code, 200)

            expected_event = {
                'username':
                str(sentinel.username),
                'ip':
                '',
                'event_source':
                'mobile',
                'event_type':
                str(sentinel.event_type),
                'name':
                str(sentinel.name),
                'event': {
                    'foo': 'bar'
                },
                'agent':
                str(sentinel.user_agent),
                'page':
                None,
                'time':
                datetime.strptime("2014-08-27T16:33:39.215Z",
                                  "%Y-%m-%dT%H:%M:%S.%fZ"),
                'host':
                'testserver',
                'context': {
                    'user_id':
                    USER_ID,
                    'course_id':
                    course_id,
                    'org_id':
                    'foo',
                    'path':
                    ENDPOINT,
                    'client': {
                        'library': {
                            'name': 'unknown',
                            'version': 'unknown'
                        },
                        'userAgent': str(sentinel.user_agent)
                    },
                    'received_at':
                    datetime.strptime("2014-08-27T16:33:39.100Z",
                                      "%Y-%m-%dT%H:%M:%S.%fZ"),
                },
            }
        finally:
            middleware.process_response(request, None)

        self.mock_tracker.send.assert_called_once_with(expected_event)  # pylint: disable=maybe-no-member
Exemplo n.º 29
0
 def test_segmentio_tracking_get_request(self):
     request = self.request_factory.get(ENDPOINT)
     response = segmentio.track_segmentio_event(request)
     self.assertEquals(response.status_code, 405)
     self.assertFalse(self.mock_tracker.send.called)  # pylint: disable=maybe-no-member
Exemplo n.º 30
0
 def test_segmentio_tracking_no_secret_provided(self):
     request = self.request_factory.post(ENDPOINT)
     response = segmentio.track_segmentio_event(request)
     self.assert_segmentio_uncommitted_response(response, segmentio.ERROR_UNAUTHORIZED, 401)
Exemplo n.º 31
0
 def test_segmentio_tracking_secret_mismatch(self):
     request = self.create_request(key='y')
     response = segmentio.track_segmentio_event(request)
     self.assert_segmentio_uncommitted_response(response, segmentio.ERROR_UNAUTHORIZED, 401)