当前位置: 首页>>代码示例>>Python>>正文


Python TrackMiddleware.encrypt_session_key方法代码示例

本文整理汇总了Python中track.middleware.TrackMiddleware.encrypt_session_key方法的典型用法代码示例。如果您正苦于以下问题:Python TrackMiddleware.encrypt_session_key方法的具体用法?Python TrackMiddleware.encrypt_session_key怎么用?Python TrackMiddleware.encrypt_session_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在track.middleware.TrackMiddleware的用法示例。


在下文中一共展示了TrackMiddleware.encrypt_session_key方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TrackMiddlewareTestCase

# 需要导入模块: from track.middleware import TrackMiddleware [as 别名]
# 或者: from track.middleware.TrackMiddleware import encrypt_session_key [as 别名]

#.........这里部分代码省略.........
        self.track_middleware.process_request(request)
        self.assertTrue(self.mock_server_track.called)
        self.mock_server_track.reset_mock()

        request = self.request_factory.get('/some/excluded/url')
        self.track_middleware.process_request(request)
        self.assertFalse(self.mock_server_track.called)

    def test_default_request_context(self):
        context = self.get_context_for_path('/courses/')
        self.assertEquals(context, {
            'user_id': '',
            'session': '',
            'username': '',
            'ip': '127.0.0.1',
            'host': 'testserver',
            'agent': '',
            'path': '/courses/',
            'org_id': '',
            'course_id': '',
            'client_id': None,
        })

    def get_context_for_path(self, path):
        """Extract the generated event tracking context for a given request for the given path."""
        request = self.request_factory.get(path)
        return self.get_context_for_request(request)

    def get_context_for_request(self, request):
        """Extract the generated event tracking context for the given request."""
        self.track_middleware.process_request(request)
        try:
            captured_context = tracker.get_tracker().resolve_context()
        finally:
            self.track_middleware.process_response(request, None)

        self.assertEquals(
            tracker.get_tracker().resolve_context(),
            {}
        )

        return captured_context

    def test_request_in_course_context(self):
        captured_context = self.get_context_for_path('/courses/test_org/test_course/test_run/foo')
        expected_context_subset = {
            'course_id': 'test_org/test_course/test_run',
            'org_id': 'test_org',
        }
        self.assert_dict_subset(captured_context, expected_context_subset)

    def assert_dict_subset(self, superset, subset):
        """Assert that the superset dict contains all of the key-value pairs found in the subset dict."""
        for key, expected_value in subset.iteritems():
            self.assertEquals(superset[key], expected_value)

    def test_request_with_user(self):
        user_id = 1
        username = sentinel.username

        request = self.request_factory.get('/courses/')
        request.user = User(pk=user_id, username=username)

        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {
            'user_id': user_id,
            'username': username,
        })

    def test_request_with_session(self):
        request = self.request_factory.get('/courses/')
        SessionMiddleware().process_request(request)
        request.session.save()
        session_key = request.session.session_key
        expected_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(len(session_key), len(expected_session_key))
        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {
            'session': expected_session_key,
        })

    @override_settings(SECRET_KEY='85920908f28904ed733fe576320db18cabd7b6cd')
    def test_session_key_encryption(self):
        session_key = '665924b49a93e22b46ee9365abf28c2a'
        expected_session_key = '3b81f559d14130180065d635a4f35dd2'
        encrypted_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(encrypted_session_key, expected_session_key)

    def test_request_headers(self):
        ip_address = '10.0.0.0'
        user_agent = 'UnitTest/1.0'

        factory = RequestFactory(REMOTE_ADDR=ip_address, HTTP_USER_AGENT=user_agent)
        request = factory.get('/some-path')
        context = self.get_context_for_request(request)

        self.assert_dict_subset(context, {
            'ip': ip_address,
            'agent': user_agent,
        })
开发者ID:Appius,项目名称:edx-platform,代码行数:104,代码来源:test_middleware.py

示例2: TrackMiddlewareTestCase

# 需要导入模块: from track.middleware import TrackMiddleware [as 别名]
# 或者: from track.middleware.TrackMiddleware import encrypt_session_key [as 别名]

#.........这里部分代码省略.........

        self.assertEquals(context['ip'], '11.22.33.44')

    def get_context_for_path(self, path):
        """Extract the generated event tracking context for a given request for the given path."""
        request = self.request_factory.get(path)
        return self.get_context_for_request(request)

    def get_context_for_request(self, request):
        """Extract the generated event tracking context for the given request."""
        self.track_middleware.process_request(request)
        try:
            captured_context = tracker.get_tracker().resolve_context()
        finally:
            self.track_middleware.process_response(request, None)

        self.assertEquals(
            tracker.get_tracker().resolve_context(),
            {}
        )

        return captured_context

    def test_request_in_course_context(self):
        captured_context = self.get_context_for_path('/courses/test_org/test_course/test_run/foo')
        expected_context_subset = {
            'course_id': 'test_org/test_course/test_run',
            'org_id': 'test_org',
        }
        self.assert_dict_subset(captured_context, expected_context_subset)

    def assert_dict_subset(self, superset, subset):
        """Assert that the superset dict contains all of the key-value pairs found in the subset dict."""
        for key, expected_value in subset.iteritems():
            self.assertEquals(superset[key], expected_value)

    def test_request_with_user(self):
        user_id = 1
        username = sentinel.username

        request = self.request_factory.get('/courses/')
        request.user = User(pk=user_id, username=username)

        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {
            'user_id': user_id,
            'username': username,
        })

    def test_request_with_session(self):
        request = self.request_factory.get('/courses/')
        SessionMiddleware().process_request(request)
        request.session.save()
        session_key = request.session.session_key
        expected_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(len(session_key), len(expected_session_key))
        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {
            'session': expected_session_key,
        })

    @override_settings(SECRET_KEY='85920908f28904ed733fe576320db18cabd7b6cd')
    def test_session_key_encryption(self):
        session_key = '665924b49a93e22b46ee9365abf28c2a'
        expected_session_key = '3b81f559d14130180065d635a4f35dd2'
        encrypted_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(encrypted_session_key, expected_session_key)

    def test_request_headers(self):
        ip_address = '10.0.0.0'
        user_agent = 'UnitTest/1.0'
        client_id_header = '123.123'

        factory = RequestFactory(
            REMOTE_ADDR=ip_address, HTTP_USER_AGENT=user_agent, HTTP_X_EDX_GA_CLIENT_ID=client_id_header
        )
        request = factory.get('/some-path')
        context = self.get_context_for_request(request)

        self.assert_dict_subset(context, {
            'ip': ip_address,
            'agent': user_agent,
            'client_id': client_id_header
        })

    @patch('track.middleware.TRUNCATION_LENGTH', 6)
    def test_student_answer_request(self):
        request = self.request_factory.post('/dummy')
        request.POST = {'student_answer': ['firstanswer', 'secondanswer']}
        self.track_middleware.process_request(request)
        event = '{"POST": {"student_answer": ["fir", "sec"]}, "GET": {}}'
        self.mock_server_track.assert_called_with(request, request.META['PATH_INFO'], event)

    @patch('track.middleware.TRUNCATION_LENGTH', 20)
    def test_submission_request(self):
        request = self.request_factory.post('/dummy')
        request.POST = {'{"submission":["first open response", "second open response"]}': []}
        self.track_middleware.process_request(request)
        event = '{"POST": {"submission": ["first open", "second ope"]}, "GET": {}}'
        self.mock_server_track.assert_called_with(request, request.META['PATH_INFO'], event)
开发者ID:caesar2164,项目名称:edx-platform,代码行数:104,代码来源:test_middleware.py

示例3: TrackMiddlewareTestCase

# 需要导入模块: from track.middleware import TrackMiddleware [as 别名]
# 或者: from track.middleware.TrackMiddleware import encrypt_session_key [as 别名]

#.........这里部分代码省略.........
    @override_settings(TRACKING_IGNORE_URL_PATTERNS=[])
    def test_reading_filtered_urls_from_settings(self):
        request = self.request_factory.get("/event")
        self.track_middleware.process_request(request)
        self.assertTrue(self.mock_server_track.called)

    @override_settings(TRACKING_IGNORE_URL_PATTERNS=[r"^/some/excluded.*"])
    def test_anchoring_of_patterns_at_beginning(self):
        request = self.request_factory.get("/excluded")
        self.track_middleware.process_request(request)
        self.assertTrue(self.mock_server_track.called)
        self.mock_server_track.reset_mock()

        request = self.request_factory.get("/some/excluded/url")
        self.track_middleware.process_request(request)
        self.assertFalse(self.mock_server_track.called)

    def test_default_request_context(self):
        context = self.get_context_for_path("/courses/")
        self.assertEquals(
            context,
            {
                "accept_language": "",
                "referer": "",
                "user_id": "",
                "session": "",
                "username": "",
                "ip": "127.0.0.1",
                "host": "testserver",
                "agent": "",
                "path": "/courses/",
                "org_id": "",
                "course_id": "",
                "client_id": None,
            },
        )

    def get_context_for_path(self, path):
        """Extract the generated event tracking context for a given request for the given path."""
        request = self.request_factory.get(path)
        return self.get_context_for_request(request)

    def get_context_for_request(self, request):
        """Extract the generated event tracking context for the given request."""
        self.track_middleware.process_request(request)
        try:
            captured_context = tracker.get_tracker().resolve_context()
        finally:
            self.track_middleware.process_response(request, None)

        self.assertEquals(tracker.get_tracker().resolve_context(), {})

        return captured_context

    def test_request_in_course_context(self):
        captured_context = self.get_context_for_path("/courses/test_org/test_course/test_run/foo")
        expected_context_subset = {"course_id": "test_org/test_course/test_run", "org_id": "test_org"}
        self.assert_dict_subset(captured_context, expected_context_subset)

    def assert_dict_subset(self, superset, subset):
        """Assert that the superset dict contains all of the key-value pairs found in the subset dict."""
        for key, expected_value in subset.iteritems():
            self.assertEquals(superset[key], expected_value)

    def test_request_with_user(self):
        user_id = 1
        username = sentinel.username

        request = self.request_factory.get("/courses/")
        request.user = User(pk=user_id, username=username)

        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {"user_id": user_id, "username": username})

    def test_request_with_session(self):
        request = self.request_factory.get("/courses/")
        SessionMiddleware().process_request(request)
        request.session.save()
        session_key = request.session.session_key
        expected_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(len(session_key), len(expected_session_key))
        context = self.get_context_for_request(request)
        self.assert_dict_subset(context, {"session": expected_session_key})

    @override_settings(SECRET_KEY="85920908f28904ed733fe576320db18cabd7b6cd")
    def test_session_key_encryption(self):
        session_key = "665924b49a93e22b46ee9365abf28c2a"
        expected_session_key = "3b81f559d14130180065d635a4f35dd2"
        encrypted_session_key = self.track_middleware.encrypt_session_key(session_key)
        self.assertEquals(encrypted_session_key, expected_session_key)

    def test_request_headers(self):
        ip_address = "10.0.0.0"
        user_agent = "UnitTest/1.0"

        factory = RequestFactory(REMOTE_ADDR=ip_address, HTTP_USER_AGENT=user_agent)
        request = factory.get("/some-path")
        context = self.get_context_for_request(request)

        self.assert_dict_subset(context, {"ip": ip_address, "agent": user_agent})
开发者ID:mrstephencollins,项目名称:edx-platform,代码行数:104,代码来源:test_middleware.py


注:本文中的track.middleware.TrackMiddleware.encrypt_session_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。