def test_tethys_apps_context_feedback(self, mock_get_active_app): mock_args = mock.MagicMock() app = TethysApp() app.id = 'foo.id' app.name = 'foo.name' app.icon = 'foo.icon' app.color = '#foobar' app.tags = 'tags' app.description = 'foo.description' app.enable_feedback = True app.feedback_emails.append('foo.feedback') mock_get_active_app.return_value = app context = tethys_apps_context(mock_args) mock_get_active_app.assert_called_once() self.assertEqual('foo.id', context['tethys_app']['id']) self.assertEqual('foo.name', context['tethys_app']['name']) self.assertEqual('foo.icon', context['tethys_app']['icon']) self.assertEqual('#foobar', context['tethys_app']['color']) self.assertEqual('tags', context['tethys_app']['tags']) self.assertEqual('foo.description', context['tethys_app']['description']) self.assertTrue('enable_feedback' in context['tethys_app']) self.assertTrue('feedback_emails' in context['tethys_app']) self.assertEqual(True, context['tethys_app']['enable_feedback']) self.assertEqual(['foo.feedback'], context['tethys_app']['feedback_emails'])
def test_tethys_apps_context_feedback(self, mock_get_active_app): mock_args = mock.MagicMock() app = TethysApp() app.id = 'foo.id' app.name = models.CharField(max_length=200, default='') app.name.value = 'foo.name' app.icon = models.CharField(max_length=200, default='') app.icon.value = 'foo.icon' app.color = models.CharField(max_length=10, default='') app.color.value = '#foobar' app.tags = models.CharField(max_length=200, blank=True, default='') app.tags.value = 'tags' app.description = models.TextField(max_length=1000, blank=True, default='') app.description.value = 'foo.description' app.enable_feedback = models.BooleanField(default=True) app.enable_feedback.value = True app.feedback_emails = ListField(default='', blank=True) app.feedback_emails.append('foo.feedback') mock_get_active_app.return_value = app context = tethys_apps_context(mock_args) mock_get_active_app.assert_called_once() self.assertEqual('foo.id', context['tethys_app']['id']) self.assertEqual('foo.name', context['tethys_app']['name'].value) self.assertEqual('foo.icon', context['tethys_app']['icon'].value) self.assertEqual('#foobar', context['tethys_app']['color'].value) self.assertEqual('tags', context['tethys_app']['tags'].value) self.assertEqual('foo.description', context['tethys_app']['description'].value) self.assertTrue('enable_feedback' in context['tethys_app']) self.assertTrue('feedback_emails' in context['tethys_app']) self.assertEqual(True, context['tethys_app']['enable_feedback'].value) self.assertEqual(['foo.feedback'], context['tethys_app']['feedback_emails'])
def test_tethys_apps_context(self, mock_get_active_app): mock_args = mock.MagicMock() app = TethysApp() app.id = 'foo.id' app.name = 'foo.name' app.icon = 'foo.icon' app.color = '#foobar' app.tags = 'tags' app.description = 'foo.description' app.enable_feedback = models.BooleanField(default=True) app.enable_feedback.value = False mock_get_active_app.return_value = app context = tethys_apps_context(mock_args) mock_get_active_app.assert_called_once() self.assertEqual('foo.id', context['tethys_app']['id']) self.assertEqual('foo.name', context['tethys_app']['name']) self.assertEqual('foo.icon', context['tethys_app']['icon']) self.assertEqual('#foobar', context['tethys_app']['color']) self.assertEqual('tags', context['tethys_app']['tags']) self.assertEqual('foo.description', context['tethys_app']['description']) self.assertFalse('enable_feedback' in context['tethys_app']) self.assertFalse('feedback_emails' in context['tethys_app'])
def setUpClass(cls): cls.app_model = TethysApp(name='test_app_job_manager', package='test_app_job_manager') cls.app_model.save() cls.user_model = User.objects.create_user( username='******', email='*****@*****.**', password='******') cls.group_model = Group.objects.create(name='test_group_job_manager') cls.group_model.user_set.add(cls.user_model) cls.scheduler = CondorScheduler( name='test_scheduler', host='localhost', ) cls.scheduler.save() cls.tethysjob = TethysJob( name='test_tethysjob', description='test_description', user=cls.user_model, label='test_app_job_manager', ) cls.tethysjob.save() cls.tethysjob.groups.add(cls.group_model)
def setUp(self): from tethys_apps.models import TethysApp self.src_dir = os.path.dirname( os.path.dirname(os.path.dirname(__file__))) self.root_app_path = os.path.join(self.src_dir, 'apps', 'tethysapp-test_app') self.app_model = TethysApp(name='test_app', package='test_app') self.app_model.save()
def sync_tethys_app_db(): """ Sync installed apps with database. """ # Get the harvester harvester = SingletonAppHarvester() try: # Make pass to remove apps that were uninstalled db_apps = TethysApp.objects.all() installed_app_packages = [app.package for app in harvester.apps] for db_apps in db_apps: if db_apps.package not in installed_app_packages: db_apps.delete() # Make pass to add apps to db that are newly installed installed_apps = harvester.apps for installed_app in installed_apps: # Query to see if installed app is in the database db_apps = TethysApp.objects.\ filter(package__exact=installed_app.package).\ all() # If the app is not in the database, then add it if len(db_apps) == 0: app = TethysApp( name=installed_app.name, package=installed_app.package, description=installed_app.description, enable_feedback=installed_app.enable_feedback, feedback_emails=installed_app.feedback_emails, index=installed_app.index, icon=installed_app.icon, root_url=installed_app.root_url, color=installed_app.color, tags=installed_app.tags ) app.save() # If the app is in the database, update developer-first attributes elif len(db_apps) == 1: db_app = db_apps[0] db_app.index = installed_app.index db_app.icon = installed_app.icon db_app.root_url = installed_app.root_url db_app.color = installed_app.color db_app.save() # More than one instance of the app in db... (what to do here?) elif len(db_apps) >= 2: continue except Exception as e: log.error(e)
def test_get_quota_aq(self, mock_rq, mock_aq): rq = mock.MagicMock() rq.active = True mock_rq.objects.get.return_value = rq aq = mock.MagicMock() aq.value = 100 mock_aq.objects.get.return_value = aq ret = utilities.get_quota(TethysApp(), 'codename') self.assertEquals(100, ret['quota'])
def test_get_resource_not_available(self, mock_rq, mock_get_quota): rq = mock.MagicMock() mock_rq.objects.get.return_value = rq rqh = mock.MagicMock() rq.handler.return_value = rqh rqh.get_current_use.return_value = 6 mock_get_quota.return_value = {'quota': 3} ret = utilities.get_resource_available(TethysApp(), 'codename') self.assertEqual(0, ret['resource_available'])
def test_get_quota_impose_default(self, mock_rq, mock_aq): rq = mock.MagicMock() rq.active = True rq.default = 100 mock_rq.objects.get.return_value = rq mock_aq.objects.get.side_effect = TethysAppQuota.DoesNotExist mock_aq.DoesNotExist = TethysAppQuota.DoesNotExist ret = utilities.get_quota(TethysApp(), 'codename') self.assertEquals(100, ret['quota'])
def test_enforce_quota_applies_to_app(self, mock_RQ, mock_active_app, mock_passes_quota): mock_RQ.objects.get.return_value = mock.MagicMock( codename='foo', applies_to='tethys_apps.models.TethysApp') mock_request = mock.MagicMock(spec=HttpRequest) mock_active_app.return_value = mock.MagicMock( TethysApp(name='Test App')) ret = a_controller(mock_request) mock_passes_quota.assert_called() self.assertEqual('Success', ret)
def test_get_resource_available_app(self, mock_rq, mock_get_quota): rq = mock.MagicMock() rq.units = 'gb' mock_rq.objects.get.return_value = rq rqh = mock.MagicMock() rq.handler.return_value = rqh rqh.get_current_use.return_value = 1 mock_get_quota.return_value = {'quota': 5} ret = utilities.get_resource_available(TethysApp(), 'codename') self.assertEqual(4, ret['resource_available']) self.assertEqual('gb', ret['units'])
def test_clear_workspace_successful(self, mock_redirect, mock_message, mock_app, mock_user, mock_guw, mock_get_app_class): # noqa: E501 mock_request = mock.MagicMock(method='POST', POST='clear-workspace-submit') mock_request.user.username = '******' mock_user.objects.get.return_value = mock.MagicMock(User(username='******')) app = TethysApp(name='app_name') mock_app.objects.get.return_value = app mock_get_app_class.return_value = app app.pre_delete_user_workspace = mock.MagicMock() app.post_delete_user_workspace = mock.MagicMock() mock_guw.return_value = mock.MagicMock() clear_workspace(mock_request, 'ThisIsMe', 'root_url') mock_message.assert_called_once_with(mock_request, 'Your workspace has been successfully cleared.') mock_redirect.assert_called_once_with('user:manage_storage', username=mock_request.user.username)
def test_manage_storage_successful(self, mock_render, mock_harvester, mock_user, mock_convert_storage, _, __): mock_request = mock.MagicMock() mock_request.user.username = '******' app = TethysApp(name="app_name") mock_harvester().apps = [app] mock_user.objects.get.return_value = mock.MagicMock() mock_convert_storage.return_value = '0 bytes' expected_context = {'apps': mock_harvester().apps, 'context_user': mock_request.user, 'current_use': '0 bytes', 'quota': None, } manage_storage(mock_request, 'ThisIsMe') mock_render.assert_called_once_with(mock_request, 'tethys_portal/user/manage_storage.html', expected_context)
def test_clear_workspace_display(self, mock_render, mock_app, mock_get_app_class, _): mock_request = mock.MagicMock() app = TethysApp(name='app_name') mock_get_app_class.return_value = app mock_app.objects.get.return_value = app expected_context = { 'app_name': mock_app.objects.get().name, 'change_url': '/admin/tethys_apps/tethysapp/app_name/change/' } clear_workspace(mock_request, 'app_name') mock_render.assert_called_once_with( mock_request, 'tethys_portal/admin/tethys_app/clear_workspace.html', expected_context)
def setUp(self): user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.resource_quota_handler = WorkspaceQuotaHandler(user) self.resource_quota_user = ResourceQuota( codename='test_user_codename', name='test_name', description='test_description', default=1.21, units='gb', applies_to='django.contrib.auth.models.User', impose_default=False, help='Exceeded quota', _handler='tethys_quotas.handlers.workspace.WorkspaceQuotaHandler') self.resource_quota_user.save() self.entity_quota_user = UserQuota( resource_quota=self.resource_quota_user, entity=user, value=100, ) self.entity_quota_user.save() self.resource_quota_app = ResourceQuota( codename='test_app_codename', name='test_name', description='test_description', default=1.21, units='gb', applies_to='tethys_apps.models.TethysApp', impose_default=True, help='Exceeded quota', _handler='tethys_quotas.handlers.workspace.WorkspaceQuotaHandler') self.resource_quota_app.save() self.app_model = TethysApp(name='Test App') self.app_model.save() self.entity_quota_app = TethysAppQuota( resource_quota=self.resource_quota_app, entity=self.app_model, value=200, ) self.entity_quota_app.save()
def test_clear_workspace_successful(self, mock_redirect, mock_message, mock_app, mock_gaw, mock_get_app_class): mock_request = mock.MagicMock(method='POST', POST='clear-workspace-submit') app = TethysApp(name='app_name') mock_get_app_class.return_value = app mock_app.objects.get.return_value = app app.pre_delete_app_workspace = mock.MagicMock() app.post_delete_app_workspace = mock.MagicMock() mock_gaw.return_value = mock.MagicMock() clear_workspace(mock_request, 'myapp') mock_message.assert_called_once_with( mock_request, 'Your workspace has been successfully cleared.') mock_redirect.assert_called_once_with( '/admin/tethys_apps/tethysapp/myapp/change/')
def setUp(self): from tethys_apps.models import TethysApp self.src_dir = os.path.dirname( os.path.dirname(os.path.dirname(__file__))) self.root_app_path = os.path.join(self.src_dir, 'apps', 'tethysapp-test_app') self.app_model = TethysApp(name='test_app', package='test_app') self.app_model.save() from django.contrib.auth.models import ContentType, Group, Permission app_content_type_id = ContentType.objects.get(app_label='tethys_apps', model='tethysapp').pk self.perm_model = Permission(name='Test Perm | Test', content_type_id=app_content_type_id, codename='test_perm:test') self.perm_model.save() self.group_model = Group(name='test_group') self.group_model.save()
def sync_with_tethys_db(self): """ Sync installed apps with database. """ from django.conf import settings from tethys_apps.models import TethysApp try: # Make pass to add apps to db that are newly installed # Query to see if installed app is in the database db_apps = TethysApp.objects.\ filter(package__exact=self.package).all() # If the app is not in the database, then add it if len(db_apps) == 0: db_app = TethysApp( name=self.name, package=self.package, description=self.description, enable_feedback=self.enable_feedback, feedback_emails=self.feedback_emails, index=self.index, icon=self.icon, root_url=self.root_url, color=self.color, tags=self.tags ) db_app.save() # custom settings db_app.add_settings(self.custom_settings()) # dataset services settings db_app.add_settings(self.dataset_service_settings()) # spatial dataset services settings db_app.add_settings(self.spatial_dataset_service_settings()) # wps settings db_app.add_settings(self.web_processing_service_settings()) # persistent store settings db_app.add_settings(self.persistent_store_settings()) db_app.save() # If the app is in the database, update developer-first attributes elif len(db_apps) == 1: db_app = db_apps[0] db_app.index = self.index db_app.icon = self.icon db_app.root_url = self.root_url db_app.color = self.color db_app.save() if hasattr(settings, 'DEBUG') and settings.DEBUG: db_app.name = self.name db_app.description = self.description db_app.tags = self.tags db_app.enable_feedback = self.enable_feedback db_app.feedback_emails = self.feedback_emails db_app.save() # custom settings db_app.add_settings(self.custom_settings()) # dataset services settings db_app.add_settings(self.dataset_service_settings()) # spatial dataset services settings db_app.add_settings(self.spatial_dataset_service_settings()) # wps settings db_app.add_settings(self.web_processing_service_settings()) # persistent store settings db_app.add_settings(self.persistent_store_settings()) db_app.save() # More than one instance of the app in db... (what to do here?) elif len(db_apps) >= 2: pass except Exception as e: tethys_log.error(e)