def login_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) data = {'session_key': request.session.session_key} status = 200 else: data = {'details': 'Invalid credential details. Please ensure you are using your {environment} login.'.format( environment=settings.WRISTBAND_ENV)} status = 401 # forbidden return JsonResponse(data=data, status=status)
def test_login(mock_object_id, dummy_user_class): """ Only checks that the session key value is the user pk and it's a string The rest is Django code """ mock_object_id.return_value = 1 user = dummy_user_class(username='******', pk=1) user.backend = 'test_backend' session = DummySession() session[MOCK_SESSION_KEY] = 'user_pk' mock_request = mock.Mock(session=session) login(mock_request, user) assert session[MOCK_SESSION_KEY] == '1' assert isinstance(session[MOCK_SESSION_KEY], str)
def test_login(mock_object_id, dummy_user_class): """ Only checks that the session key value is the user pk and it's a string The rest is Django code """ mock_object_id.return_value = 1 user = dummy_user_class(username='******', pk=1) user.backend = 'test_backend' session = DummySession() session[MOCK_SESSION_KEY] = 'user_pk' mock_request = mock.Mock(session=session) login(mock_request, user) assert session[MOCK_SESSION_KEY] == '1' assert isinstance(session[MOCK_SESSION_KEY], str)
def login_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) data = {'session_key': request.session.session_key} status = 200 else: data = { 'details': 'Invalid credential details. Please ensure you are using your {environment} login.' .format(environment=settings.WRISTBAND_ENV) } status = 401 # forbidden return JsonResponse(data=data, status=status)
def login(self, **credentials): """ Sets the Factory to appear as if it has successfully logged into a site. Returns True if login is possible; False if the provided credentials are incorrect, or the user is inactive, or if the sessions framework is not available. """ from django.contrib.auth import authenticate from wristband.authentication.utils import login user = authenticate(**credentials) if (user and user.is_active and apps.is_installed('django.contrib.sessions')): engine = import_module(settings.SESSION_ENGINE) # Create a fake request to store login details. request = HttpRequest() if self.session: request.session = self.session else: request.session = engine.SessionStore() login(request, user) # Save the session values. request.session.save() # Set the cookie to represent the session. session_cookie = settings.SESSION_COOKIE_NAME self.cookies[session_cookie] = request.session.session_key cookie_data = { 'max-age': None, 'path': '/', 'domain': settings.SESSION_COOKIE_DOMAIN, 'secure': settings.SESSION_COOKIE_SECURE or None, 'expires': None, } self.cookies[session_cookie].update(cookie_data) return True else: return False
def login(self, **credentials): """ Sets the Factory to appear as if it has successfully logged into a site. Returns True if login is possible; False if the provided credentials are incorrect, or the user is inactive, or if the sessions framework is not available. """ from django.contrib.auth import authenticate from wristband.authentication.utils import login user = authenticate(**credentials) if (user and user.is_active and apps.is_installed('django.contrib.sessions')): engine = import_module(settings.SESSION_ENGINE) # Create a fake request to store login details. request = HttpRequest() if self.session: request.session = self.session else: request.session = engine.SessionStore() login(request, user) # Save the session values. request.session.save() # Set the cookie to represent the session. session_cookie = settings.SESSION_COOKIE_NAME self.cookies[session_cookie] = request.session.session_key cookie_data = { 'max-age': None, 'path': '/', 'domain': settings.SESSION_COOKIE_DOMAIN, 'secure': settings.SESSION_COOKIE_SECURE or None, 'expires': None, } self.cookies[session_cookie].update(cookie_data) return True else: return False