def test_task_is_queued(self): models = [ LogEntry, GaeDatastoreUser, Group, Site ] with sleuth.fake('django.apps.apps.get_models', models): bucket = 'testapp/19991231-235900' with sleuth.fake('djangae.contrib.backup.views.get_backup_path', bucket): with self.settings( DJANGAE_BACKUP_ENABLED=True, DJANGAE_BACKUP_GCS_BUCKET='testapp', DJANGAE_BACKUP_NAME='testapp-bk', DJANGAE_BACKUP_EXCLUDE_APPS=[ 'sites' ], DJANGAE_BACKUP_EXCLUDE_MODELS=[ 'gauth_datastore.Group' ]): create_datastore_backup(None) # assert task was triggered tasks = self.taskqueue_stub.get_filtered_tasks() self.assertEqual(len(tasks), 1) self.assertEqual(tasks[0].target, GAE_BUILTIN_MODULE) self.assertEqual(tasks[0].url, '/_ah/datastore_admin/backup.create?' 'name=testapp-bk' '&gs_bucket_name=testapp%2F19991231-235900' '&filesystem=gs' '&kind=django_admin_log' '&kind=djangae_gaedatastoreuser' )
def test_get_params_propogate(self): request = RequestFactory().get('/?kind=django_admin_log&bucket=foobar') request.META[_TASK_NAME_HEADER] = "test" with sleuth.fake("djangae.tasks.decorators.is_in_task", True): with sleuth.fake('djangae.contrib.backup.views.backup_datastore', None) as backup_fn: create_datastore_backup(request) self.assertTrue(backup_fn.called) self.assertEqual(backup_fn.calls[0][1], { 'bucket': 'foobar', 'kinds': [u'django_admin_log'] })
def test_valid_credentials_log_user(self): live_server_domain = self.live_server_url.split('://')[-1] session = self.client.session session['oauth-state'] = 'somestate' session[REDIRECT_FIELD_NAME] = '/next_url' session.save() fake_token = { 'access_token': '9999', 'token_type': 'Bearer', 'expires_in': '30', 'scope': ['some.scope another.scope'], 'id_token': 'someencryptedstuff' } fake_profile = { 'id': '1', 'email': '*****@*****.**' } idinfo = { 'sub': '1', 'iss': 'accounts.google.com', 'email': '*****@*****.**' } with sleuth.fake('djangae.contrib.googleauth.views.OAuth2Session.fetch_token', return_value=fake_token), \ sleuth.fake('djangae.contrib.googleauth.views.OAuth2Session.authorized', return_value=True), \ sleuth.fake('djangae.contrib.googleauth.views.OAuth2Session.get', return_value=fake_profile), \ sleuth.watch('djangae.contrib.googleauth.backends.oauth2.OAuthBackend.authenticate') as mocked_auth, \ sleuth.watch('django.contrib.auth.login') as mocked_login, \ sleuth.fake('google.oauth2.id_token.verify_token', idinfo): state = json.dumps({ "hostname": "app.com", }) response = self.client.get( reverse("googleauth_oauth2callback"), data={"state": state}, HTTP_HOST=live_server_domain ) # check authenticate and login function are called self.assertTrue(mocked_auth.called) self.assertTrue(mocked_login.called) self.assertEqual(response.status_code, 302) self.assertTrue(session[REDIRECT_FIELD_NAME] in response.url) user = User.objects.get(email="*****@*****.**") self.assertFalse(user.has_usable_password())
def test_scan_list_same_string_in_two_groups(self): """Regression test: previously when string was occuring in two or more different groups only last one was saved, which is wrong.""" marshall = ScanMarshall.objects.create() with patch('__builtin__.open', mock_open()): with sleuth.fake('os.path.exists', return_value=True): with sleuth.fake('os.path.splitext', return_value=["some_fake_name", "html"]): with sleuth.fake('fluent.scanner.parse_file', [ ("Monday", "", "", "public"), ("Monday", "", "", "website"), ]): _scan_list(marshall, uuid.uuid4(), ['some_fake_name.html']) self.assertEquals(MasterTranslation.objects.get().used_by_groups_in_code_or_templates, {"public", "website"})
def test_redirect_to_authorization_url(self): """ Access to a page that is login_required should redirect to the authorization url """ live_server_domain = self.live_server_url.split('://')[-1] protected_url = '/protected' response = self.client.get(protected_url, HTTP_HOST=live_server_domain) self.assertTrue(reverse("googleauth_oauth2login") in response.url) self.assertEqual(302, response.status_code) with sleuth.fake( 'djangae.contrib.googleauth.views.OAuth2Session.authorization_url', return_value=('oauthauthurl', 'oauthstate') ) as auth_url: response = self.client.get(response.url, HTTP_HOST=live_server_domain) # check OAuthSession has been called properly self.assertTrue(auth_url.called) self.assertEqual(auth_url.calls[0].args[1], 'https://accounts.google.com/o/oauth2/v2/auth') self.assertTrue(set({ "prompt": 'select_account', "include_granted_scopes": 'true', }.items()).issubset(set(auth_url.calls[0].kwargs.items()))) self.assertEqual(set(json.loads(auth_url.calls[0].kwargs['state']).keys()), {'hostname', 'token'}) # check session contains correct keys and values self.assertEqual(self.client.session.get('oauth-state'), 'oauthstate') self.assertEqual(self.client.session.get('next'), protected_url) # check that we're redirecting to authorization url returned from the session instance self.assertEqual(response.status_code, 302) self.assertTrue('oauthauthurl' in response.url)
def test_allowed_if_in_cron(self): """ If the view is being called by the GAE cron, then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") with sleuth.fake("djangae.environment.is_in_cron", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_task(self): """ If we're in an App Engine task then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") with sleuth.fake("djangae.environment.is_in_task", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_admin_user(self): """ If we're logged in as an admin of the GAE application then we should allow through. """ @task_or_admin_only def view(request): return HttpResponse("Hello") with sleuth.fake("google.appengine.api.users.is_current_user_admin", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_cron(self): """ If the view is being called by the GAE cron, then it should allow the request through. """ @task_or_admin_only def view(request): return HttpResponse("Hello") with sleuth.fake("djangae.environment.is_in_cron", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_task(self): """ If we're in an App Engine task then it should allow the request through. """ @task_or_admin_only def view(request): return HttpResponse("Hello") with sleuth.fake("djangae.environment.is_in_task", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_admin_user(self): """ If we're logged in as an admin of the GAE application then we should allow through. """ @task_or_admin_only def view(request): return HttpResponse("Hello") with sleuth.fake("djangae.environment.users.is_current_user_admin", True): response = view(None) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_task(self): """ If we're in an App Engine task then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") request = self.factory.get("/") with sleuth.fake("djangae.tasks.decorators.is_in_task", True): response = view(request) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_cron(self): """ If the view is being called by the GAE cron, then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") request = self.factory.get("/") with sleuth.fake("djangae.tasks.decorators.is_in_cron", True): response = view(request) self.assertEqual(response.status_code, 200)
def test_no_bucket_setting_and_no_default_bucket(self): with self.assertRaises(AttributeError): settings.DJANGAE_BACKUP_GCS_BUCKET with sleuth.fake('google.appengine.api.app_identity.get_default_gcs_bucket_name', None): bucket = app_identity.get_default_gcs_bucket_name() self.assertIsNone(bucket) with self.assertRaisesRegexp(Exception, 'DJANGAE_BACKUP_GCS_BUCKET'): get_gcs_bucket()
def test_no_bucket_setting_and_no_default_bucket(self): with self.assertRaises(AttributeError): settings.DJANGAE_BACKUP_GCS_BUCKET with sleuth.fake('djangae.environment.default_gcs_bucket_name', None): bucket = environment.default_gcs_bucket_name() self.assertIsNone(bucket) with self.assertRaisesRegexp(Exception, 'DJANGAE_BACKUP_GCS_BUCKET'): get_gcs_bucket()
def test_allowed_if_in_cron(self): """ If the view is being called by the GAE cron, then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") request = self.factory.get("/") request.META[_CRON_TASK_HEADER] = "test" with sleuth.fake("djangae.environment.is_in_cron", True): response = view(request) self.assertEqual(response.status_code, 200)
def test_allowed_if_in_task(self): """ If we're in an App Engine task then it should allow the request through. """ @task_only def view(request): return HttpResponse("Hello") request = self.factory.get("/") request.META[_TASK_NAME_HEADER] = "test" with sleuth.fake("djangae.environment.is_in_task", True): response = view(request) self.assertEqual(response.status_code, 200)
def test_no_bucket_setting_and_no_default_bucket(self): with self.assertRaises(AttributeError): settings.DJANGAE_BACKUP_GCS_BUCKET with sleuth.fake( 'google.appengine.api.app_identity.get_default_gcs_bucket_name', None): bucket = app_identity.get_default_gcs_bucket_name() self.assertIsNone(bucket) with self.assertRaisesRegexp(Exception, 'DJANGAE_BACKUP_GCS_BUCKET'): get_gcs_bucket()
def test_no_config_raises(self): from django.core.exceptions import ImproperlyConfigured with sleuth.fake("djangae.storage.project_id", return_value=None): with self.assertRaises(ImproperlyConfigured): CloudStorage()