def test_oauth_scopes_required_redirects_to_login_if_no_oauthsession(self): request = RequestFactory().get('/') request.user = User.objects.create_user(username='******', email='*****@*****.**') middleware = SessionMiddleware() middleware.process_request(request) request.session.save() def func(*args, **kwargs): func.called = True func.called = False decorated_func = oauth_scopes_required(scopes=[])(func) response_mocked = decorated_func(request) self.assertFalse(func.called) self.assertEquals(response_mocked.status_code, 302) self.assertTrue(reverse("googleauth_oauth2login") in response_mocked.url)
def test_oauth_scopes_required_redirects_to_login_if_anonymous(self): request = RequestFactory().get('/') request.user = AnonymousUser() middleware = SessionMiddleware() middleware.process_request(request) request.session.save() def func(*args, **kwargs): func.called = True func.called = False decorated_func = oauth_scopes_required(scopes=[])(func) response_mocked = decorated_func(request) self.assertFalse(func.called) self.assertEquals(response_mocked.status_code, 302) self.assertTrue(reverse("googleauth_oauth2login") in response_mocked.url)
def test_oauth_scopes_required_redirects_for_additional_scopes(self): scopes = self._DEFAULT_OAUTH_SCOPES + ['https://www.googleapis.com/auth/calendar'] request = RequestFactory().get('/') request.user = self.user middleware = SessionMiddleware() middleware.process_request(request) request.session.save() def func(*args, **kwargs): func.called = True func.called = False decorated_func_mock = oauth_scopes_required(scopes=scopes)(func) response_mocked = decorated_func_mock(request) self.assertFalse(func.called) # check we're redirecting to login url with the correct parameters self.assertEquals(response_mocked.status_code, 302) self.assertTrue(reverse("googleauth_oauth2login") in response_mocked.url)