def test_already_associated_exception_populates_dashboard_with_error(self): # Instrument the pipeline with an exception. We test that the # exception is raised correctly separately, so it's ok that we're # raising it artificially here. This makes the linked=True artificial # in the final assert because in practice the account would be # unlinked, but getting that behavior is cumbersome here and already # covered in other tests. Using linked=True does, however, let us test # that the duplicate error has no effect on the state of the controls. request, strategy = self.get_request_and_strategy( auth_entry=pipeline.AUTH_ENTRY_LOGIN, redirect_uri='social:complete') strategy.request.backend.auth_complete = mock.MagicMock(return_value=self.fake_auth_complete(strategy)) user = self.create_user_models_for_existing_account( strategy, '*****@*****.**', 'password', self.get_username()) self.assert_social_auth_exists_for_user(user, strategy) self.client.get('/login') self.client.get(pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN)) actions.do_complete(request.backend, social_views._do_login, # pylint: disable=protected-access request=request) with self._patch_edxmako_current_request(strategy.request): signin_user(strategy.request) login_user(strategy.request) actions.do_complete(request.backend, social_views._do_login, # pylint: disable=protected-access user=user, request=request) # Monkey-patch storage for messaging; pylint: disable=protected-access request._messages = fallback.FallbackStorage(request) middleware.ExceptionMiddleware().process_exception( request, exceptions.AuthAlreadyAssociated(self.provider.backend_name, 'account is already in use.')) self.assert_account_settings_context_looks_correct( account_settings_context(request), duplicate=True, linked=True)
def _get_mock_request(): """ Get mock django HttpRequest object. """ request = RequestFactory().get('/') request.session = cache.SessionStore() # Monkey-patch storage for messaging; pylint: disable=protected-access request._messages = fallback.FallbackStorage(request) return request
def get_mocked_sso_backend(self): """ Get a mocked request backend with mocked strategy. """ # Monkey-patch storage for messaging; pylint: disable=protected-access self.request._messages = fallback.FallbackStorage(self.request) strategy_mock = mock.MagicMock(request=self.request) backend = mock.MagicMock( name=None, strategy=strategy_mock, ) return backend
def get_mocked_sso_backend(self): """ Get a mocked request backend with mocked strategy. """ request_factory = RequestFactory() request = request_factory.get('/') request.session = cache.SessionStore() # Monkey-patch storage for messaging; pylint: disable=protected-access request._messages = fallback.FallbackStorage(request) strategy_mock = mock.MagicMock(request=request) backend = mock.MagicMock( name=None, strategy=strategy_mock, ) return backend
def fake_request(url="/", data={}, user="", method="POST"): "Make a fake request; defaults to POST." methods = {"POST": RequestFactory().post, "GET": RequestFactory().get} assert method in methods request = methods[method](url, data) # Mimic messaging system request.session = {} messages = fallback.FallbackStorage(request=request) request._messages = messages request.user = user return request
def fake_request(url, data, user, method="POST", rmeta={}): "Make a fake request; defaults to POST." methods = {"POST": RequestFactory().post, "GET": RequestFactory().get, 'PUT': RequestFactory().put} assert method in methods request = methods[method](url, data) # Mimic messaging system request.session = {} messages = fallback.FallbackStorage(request=request) request._messages = messages # Update the META info request.META.update(rmeta) request.user = user return request