我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.contrib.auth.SESSION_KEY。
def shortcut_login(self, **credentials): user = authenticate(**credentials) if not user: raise ValueError("User {0} was not authenticated".format(user)) session_auth_hash = '' if hasattr(user, 'get_session_auth_hash'): session_auth_hash = user.get_session_auth_hash() # Mimicking django.contrib.auth functionality self.set_session_data({AUTH_ID_SESSION_KEY: user.pk, AUTH_HASH_SESSION_KEY: session_auth_hash, AUTH_BACKEND_SESSION_KEY: user.backend})
def __setitem__(self, key, value): if key == auth.SESSION_KEY: self.user_id = value super(SessionStore, self).__setitem__(key, value)
def setUp(self): self.client = Client() logged_in = self.client.session.has_key(SESSION_KEY) self.assertFalse(logged_in)
def test_valid_POST_input_authenticates_user(self): handler = MagicMock() user_logged_in.connect(handler) user = self.create_valid_user() self.client.post(reverse('login'), data={'username': 'hans', 'password': 'ihaveapassword'}) self.assertEqual(handler.call_count, 1) self.assertEqual(self.client.session[SESSION_KEY], str(user.pk))
def test_logout_view(self): self.create_valid_user_logged_in() self.client.logout() self.assertNotIn(SESSION_KEY, self.client.session)
def setUpClass(cls): super(IntegrationTests, cls).setUpClass() # we do not display cls.display = Display(visible=0, size=(1024, 768)) cls.display.start() cls.selenium = WebDriver() # we create a user password = "password" cls.user = User(username="UserForLiveTests") cls.user.set_password(password) cls.user.save() # we log him in # source http://stackoverflow.com/questions/22494583/login-with-code-when-using-liveservertestcase-with-django # we need a session session = SessionStore() session[SESSION_KEY] = cls.user.id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = cls.user.get_session_auth_hash() session.save() # the cookie dict cls.cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'secure': False, 'path': '/', } # we launch centrifugo cls.centrifugo = subprocess.Popen(["centrifugo --config=tests/config.json --port={0}".format(getattr(settings, "CENTRIFUGO_PORT", 8802))], stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid) # we create participants cls.participant1 = Participant.objects.create(id=cls.user.id) cls.participant2 = Participant.objects.create(id=2) cls.participant3 = Participant.objects.create(id=3) cls.participant4 = Participant.objects.create(id=4) # we create a fake request cls.request = RequestFactory() cls.request.rest_messaging_participant = cls.participant1 # and wait for it to run time.sleep(4)
def login(self, password='password'): response = self.client.post('/login/', { 'username': 'testclient', 'password': password, }) self.assertTrue(SESSION_KEY in self.client.session) return response
def logout(self): response = self.client.get('/admin/logout/') self.assertEqual(response.status_code, 200) self.assertTrue(SESSION_KEY not in self.client.session)
def confirm_logged_out(self): self.assertTrue(SESSION_KEY not in self.client.session)
def test_assertion_consumer_service(self): # Get initial number of users initial_user_count = User.objects.count() settings.SAML_CONFIG = conf.create_conf( sp_host='sp.example.com', idp_hosts=['idp.example.com'], metadata_file='remote_metadata_one_idp.xml', ) self.init_cookies() # session_id should start with a letter since it is a NCName session_id = "a0123456789abcdef0123456789abcdef" came_from = '/another-view/' self.add_outstanding_query(session_id, came_from) # this will create a user saml_response = auth_response(session_id, 'student') response = self.client.post(reverse('saml2_acs'), { 'SAMLResponse': self.b64_for_post(saml_response), 'RelayState': came_from, }) self.assertEqual(response.status_code, 302) location = response['Location'] url = urlparse(location) self.assertEqual(url.path, came_from) self.assertEqual(User.objects.count(), initial_user_count + 1) user_id = self.client.session[SESSION_KEY] user = User.objects.get(id=user_id) self.assertEqual(user.username, 'student') # let's create another user and log in with that one new_user = User.objects.create(username='teacher', password='not-used') session_id = "a1111111111111111111111111111111" came_from = '' # bad, let's see if we can deal with this saml_response = auth_response(session_id, 'teacher') self.add_outstanding_query(session_id, '/') response = self.client.post(reverse('saml2_acs'), { 'SAMLResponse': self.b64_for_post(saml_response), 'RelayState': came_from, }) self.assertEqual(response.status_code, 302) location = response['Location'] url = urlparse(location) # as the RelayState is empty we have redirect to LOGIN_REDIRECT_URL self.assertEqual(url.path, settings.LOGIN_REDIRECT_URL) self.assertEqual(force_text(new_user.id), self.client.session[SESSION_KEY])
def test_assertion_consumer_service(self): # Get initial number of users initial_user_count = User.objects.count() settings.SAML_CONFIG = conf.create_conf( sp_host='sp.example.com', idp_hosts=['idp.example.com'], metadata_file='remote_metadata_one_idp.xml', ) self.init_cookies() # session_id should start with a letter since it is a NCName session_id = "a0123456789abcdef0123456789abcdef" came_from = '/another-view/' self.add_outstanding_query(session_id, came_from) # this will create a user saml_response = auth_response(session_id, 'student') response = self.client.post(reverse('saml2_acs'), { 'SAMLResponse': self.b64_for_post(saml_response), 'RelayState': came_from, }) self.assertEquals(response.status_code, 302) location = response['Location'] url = urlparse(location) self.assertEquals(url.path, came_from) self.assertEquals(User.objects.count(), initial_user_count + 1) user_id = self.client.session[SESSION_KEY] user = User.objects.get(id=user_id) self.assertEquals(user.username, 'student') # let's create another user and log in with that one new_user = User.objects.create(username='teacher', password='not-used') session_id = "a1111111111111111111111111111111" came_from = '' # bad, let's see if we can deal with this saml_response = auth_response(session_id, 'teacher') self.add_outstanding_query(session_id, '/') response = self.client.post(reverse('saml2_acs'), { 'SAMLResponse': self.b64_for_post(saml_response), 'RelayState': came_from, }) self.assertEquals(response.status_code, 302) location = response['Location'] url = urlparse(location) # as the RelayState is empty we have redirect to LOGIN_REDIRECT_URL self.assertEquals(url.path, '/accounts/profile/') self.assertEquals(force_text(new_user.id), self.client.session[SESSION_KEY])