def test_user_post_save_with_template(self, mock_notifications, mock_member, mock_settings): user = Dojo_User() user.id = 1 group = Dojo_Group() group.id = 1 template = Mock(Notifications(template=False, user=user)) role = Role.objects.get(id=Roles.Reader) system_settings_group = System_Settings() system_settings_group.default_group = group system_settings_group.default_group_role = role mock_settings.get.return_value = system_settings_group save_mock_member = Mock(return_value=Dojo_Group_Member()) mock_member.return_value = save_mock_member mock_notifications.objects.get.return_value = template user_post_save(None, user, True) mock_member.assert_called_with(group=group, user=user, role=role) save_mock_member.save.assert_called_once() mock_notifications.objects.get.assert_called_with(template=True) template.save.assert_called_once()
def test_user_post_save_email_pattern_does_not_match( self, mock_notifications, mock_member, mock_settings): user = Dojo_User() user.id = 1 user.email = '*****@*****.**' group = Dojo_Group() group.id = 1 role = Role.objects.get(id=Roles.Reader) system_settings_group = System_Settings() system_settings_group.default_group = group system_settings_group.default_group_role = role system_settings_group.default_group_email_pattern = '.*@example.com' save_mock_notifications = Mock(return_value=Notifications()) mock_notifications.return_value = save_mock_notifications mock_notifications.objects.get.side_effect = Exception( "Mock no templates") mock_settings.get.return_value = system_settings_group save_mock_member = Mock(return_value=Dojo_Group_Member()) mock_member.return_value = save_mock_member user_post_save(None, user, True) mock_member.assert_not_called() save_mock_member.save.assert_not_called()
def test_user_post_save_without_template(self, mock_notifications, mock_member, mock_settings): user = Dojo_User() user.id = 1 group = Dojo_Group() group.id = 1 role = Role.objects.get(id=Roles.Reader) system_settings_group = System_Settings() system_settings_group.default_group = group system_settings_group.default_group_role = role mock_settings.get.return_value = system_settings_group save_mock_member = Mock(return_value=Dojo_Group_Member()) mock_member.return_value = save_mock_member save_mock_notifications = Mock(return_value=Notifications()) mock_notifications.return_value = save_mock_notifications mock_notifications.objects.get.side_effect = Exception( "Mock no templates") user_post_save(None, user, True) mock_member.assert_called_with(group=group, user=user, role=role) save_mock_member.save.assert_called_once() mock_notifications.assert_called_with(user=user) save_mock_notifications.save.assert_called_once()
def setUpTestData(cls): cls.user = Dojo_User() cls.user.id = 1 cls.group = Dojo_Group() cls.group.id = 1 cls.system_settings_email = System_Settings() cls.system_settings_email.staff_user_email_pattern = '.*@example.com' cls.role = Role.objects.get(id=Roles.Reader) cls.system_settings_group = System_Settings() cls.system_settings_group.default_group = cls.group cls.system_settings_group.default_group_role = cls.role
def get_system_setting(setting): try: system_settings = System_Settings.objects.get() except: system_settings = System_Settings() return getattr(system_settings, setting, None)
def system_settings(request): try: system_settings_obj = System_Settings.objects.get() except: system_settings_obj = System_Settings() celery_status = get_celery_worker_status() celery_bool = True celery_stat = celery_status.keys()[0] celery_msg = celery_status.values()[0] if celery_status.keys()[0] == 'ERROR': celery_bool = False """ **** To be Finished JIRA Status info **** jira_bool = True jira_msg = 'None' if not celery_bool: jira_bool = False jira_msg = 'Celery is not working properly' else: try: jira_server = jform.cleaned_data.get('url').rstrip('/') jira = JIRA(server=jform.cleaned_data.get('url').rstrip('/'), basic_auth=(jform.cleaned_data.get('username'), jform.cleaned_data.get('password'))) new_j = jform.save(commit=False) new_j.url = jira_server new_j.save() messages.add_message(request, messages.SUCCESS, 'JIRA Configuration Successfully Created.', extra_tags='alert-success') return HttpResponseRedirect(reverse('jira', )) except: messages.add_message(request, messages.ERROR, 'Unable to authenticate. Please check the URL, username, and password.', extra_tags='alert-danger') """ form = SystemSettingsForm(instance=system_settings_obj) if request.method == 'POST': form = SystemSettingsForm(request.POST, instance=system_settings_obj) if form.is_valid(): new_settings = form.save() messages.add_message(request, messages.SUCCESS, 'Settings saved.', extra_tags='alert-success') return HttpResponseRedirect(reverse('system_settings', )) add_breadcrumb(title="Application settings", top_level=False, request=request) return render( request, 'dojo/system_settings.html', { 'form': form, 'celery_bool': celery_bool, 'celery_msg': celery_msg, 'celery_status': celery_stat })
def get_from_db(self, *args, **kwargs): # logger.debug('refreshing system_settings from db') try: from_db = super(System_Settings_Manager, self).get(*args, **kwargs) except: from dojo.models import System_Settings # this mimics the existing code that was in filters.py and utils.py. # cases I have seen triggering this is for example manage.py collectstatic inside a docker build where mysql is not available # logger.debug('unable to get system_settings from database, constructing (new) default instance. Exception was:', exc_info=True) return System_Settings() return from_db
def create(): settings = System_Settings() settings.save() p = Product() p.Name = 'Test Product' p.Description = 'Product for Testing Apply Template functionality' p.prod_type = Product_Type.objects.get(id=1) p.save() e = Engagement() e.product = p e.target_start = timezone.now() e.target_end = e.target_start + datetime.timedelta(days=5) e.save() tt = Test_Type() tt.name = 'Temporary Test' tt.save() t = Test() t.engagement = e t.test_type = tt t.target_start = timezone.now() t.target_end = t.target_start + datetime.timedelta(days=5) t.save() user = FindingTemplateTestUtil.create_user(True) f = Finding() f.title = 'Finding for Testing Apply Template functionality' f.severity = 'High' f.description = 'Finding for Testing Apply Template Functionality' f.test = t f.reporter = user f.last_reviewed = timezone.now() f.last_reviewed_by = user f.save()
def create(): settings = System_Settings() settings.save() p = Product() p.Name = 'Test Product' p.Description = 'Product for Testing Apply Template functionality' p.save() e = Engagement() e.product = p e.target_start = timezone.now() e.target_end = e.target_start + datetime.timedelta(days=5) e.save() tt = Test_Type() tt.name = 'Temporary Test' tt.save() t = Test() t.engagement = e t.test_type = tt t.target_start = timezone.now() t.target_end = t.target_start + datetime.timedelta(days=5) t.save() user = FindingTemplateTestUtil.create_user(True) f = Finding() f.title = 'Finding for Testing Apply Template functionality' f.severity = 'High' f.description = 'Finding for Testing Apply Template Functionality' f.test = t f.reporter = user f.last_reviewed = timezone.now() f.last_reviewed_by = user f.save()
def system_settings(request): try: system_settings_obj = System_Settings.objects.get() except: system_settings_obj = System_Settings() form = SystemSettingsForm(instance=system_settings_obj) if request.method == 'POST': form = SystemSettingsForm(request.POST, instance=system_settings_obj) if form.is_valid(): new_settings = form.save() messages.add_message(request, messages.SUCCESS, 'Settings saved.', extra_tags='alert-success') return HttpResponseRedirect(reverse('system_settings', )) add_breadcrumb(title="Application settings", top_level=False, request=request) return render(request, 'dojo/system_settings.html', {'form': form})
def test_system_settings_update(self): try: # although the unittests are run after initial data has been loaded, for some reason in travis sometimes the settings aren't present system_settings = System_Settings.objects.get() except System_Settings.DoesNotExist: system_settings = System_Settings() system_settings.enable_jira = True system_settings.save() system_settings = System_Settings.objects.get() self.assertEquals(system_settings.enable_jira, True) system_settings.enable_jira = False system_settings.save() system_settings = System_Settings.objects.get() self.assertEquals(system_settings.enable_jira, False) system_settings.enable_jira = True system_settings.save() system_settings = System_Settings.objects.get(no_cache=True) self.assertEquals(system_settings.enable_jira, True)
def setUpTestData(cls): cls.user = Dojo_User() cls.user.id = 1 cls.system_settings_email = System_Settings() cls.system_settings_email.staff_user_email_pattern = '.*@example.com'
def system_settings(request): try: system_settings_obj = System_Settings.objects.get() except: system_settings_obj = System_Settings() # Celery needs to be set with the setting: CELERY_RESULT_BACKEND = 'db+sqlite:///dojo.celeryresults.sqlite' if hasattr(settings, 'CELERY_RESULT_BACKEND'): # Check the status of Celery by sending calling a celery task celery_bool = get_celery_worker_status() if celery_bool: celery_msg = "Celery is processing tasks." celery_status = "Running" else: celery_msg = "Celery does not appear to be up and running. Please ensure celery is running." celery_status = "Not Running" else: celery_bool = False celery_msg = "Celery needs to have the setting CELERY_RESULT_BACKEND = 'db+sqlite:///dojo.celeryresults.sqlite' set in settings.py." celery_status = "Unkown" """ **** To be Finished JIRA Status info **** jira_bool = True jira_msg = 'None' if not celery_bool: jira_bool = False jira_msg = 'Celery is not working properly' else: try: jira_server = jform.cleaned_data.get('url').rstrip('/') jira = JIRA(server=jform.cleaned_data.get('url').rstrip('/'), basic_auth=(jform.cleaned_data.get('username'), jform.cleaned_data.get('password'))) new_j = jform.save(commit=False) new_j.url = jira_server new_j.save() messages.add_message(request, messages.SUCCESS, 'JIRA Configuration Successfully Created.', extra_tags='alert-success') return HttpResponseRedirect(reverse('jira', )) except: messages.add_message(request, messages.ERROR, 'Unable to authenticate. Please check the URL, username, and password.', extra_tags='alert-danger') """ form = SystemSettingsForm(instance=system_settings_obj) if request.method == 'POST': form = SystemSettingsForm(request.POST, instance=system_settings_obj) if form.is_valid(): new_settings = form.save() messages.add_message(request, messages.SUCCESS, 'Settings saved.', extra_tags='alert-success') return HttpResponseRedirect(reverse('system_settings', )) add_breadcrumb(title="Application settings", top_level=False, request=request) return render(request, 'dojo/system_settings.html', {'form': form, 'celery_bool': celery_bool, 'celery_msg': celery_msg, 'celery_status': celery_status})