def test_create_and_apply_pending(self): pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 0) role = self.commander_user.get_role(self.instance) fp, _ = FieldPermission.objects.get_or_create( model_name='Plot', field_name='udf:Test unauth', permission_level=FieldPermission.WRITE_WITH_AUDIT, role=role, instance=self.instance) self.plot.udfs['Test unauth'] = 'c' self.plot.save_with_user(self.commander_user) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual(reloaded_plot.udfs['Test unauth'], None) pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 1) fp.permission_level = FieldPermission.WRITE_DIRECTLY fp.save() approve_or_reject_audit_and_apply(pending[0], self.commander_user, True) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual(reloaded_plot.udfs['Test unauth'], 'c')
def test_reject_insert_rejects_updates(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) insert_audit = Audit.objects.filter(model='Plot')\ .get(field='id') field_audits = Audit.objects.filter(model='Plot')\ .exclude(field='id') for audit in field_audits: approve_or_reject_audit_and_apply( audit, self.commander_user, approved=True) approve_or_reject_audit_and_apply(insert_audit, self.commander_user, False) # need to refresh the field_audits collection from the db # because references are broken # why doesn't this work? why are there 5 values in field_audits_ids? # field_audit_ids = field_audits.values_list('id', flat=True) field_audit_ids = [field_audit.id for field_audit in field_audits] field_audits = Audit.objects.filter(pk__in=field_audit_ids) for field_audit in field_audits: attached_review_audit = Audit.objects.get(pk=field_audit.ref.pk) self.assertEqual(attached_review_audit.action, Audit.Type.PendingReject) self.assertNotEqual(None, Audit.objects.get( model=field_audit.model, field=field_audit.field, model_id=field_audit.model_id, action=Audit.Type.PendingApprove))
def test_record_is_created_when_nullables_are_still_pending(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( new_plot.audits(), self.commander_user, True) insert_audit = Audit.objects.filter(model='Tree')\ .get(field='id') field_audits = Audit.objects.filter(model='Tree')\ .filter(field__in=['readonly', 'diameter', 'plot']) for audit in field_audits: approve_or_reject_audit_and_apply( audit, self.commander_user, approved=True) approve_or_reject_audit_and_apply(insert_audit, self.commander_user, True) real_tree = Tree.objects.get(pk=new_tree.pk) self.assertEqual(real_tree.plot_id, new_plot.pk) self.assertEqual(real_tree.diameter, 10) self.assertEqual(real_tree.height, None) self.assertNotEqual(real_tree.readonly, True)
def test_create_and_apply_pending(self): pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 0) role = self.commander_user.get_role(self.instance) fp, __ = FieldPermission.objects.get_or_create( model_name="Plot", field_name="udf:Test unauth", permission_level=FieldPermission.WRITE_WITH_AUDIT, role=role, instance=self.instance, ) self.plot.udfs["Test unauth"] = "c" self.plot.save_with_user(self.commander_user) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual(reloaded_plot.udfs["Test unauth"], None) pending = self.plot.audits().filter(requires_auth=True) self.assertEqual(len(pending), 1) fp.permission_level = FieldPermission.WRITE_DIRECTLY fp.save() approve_or_reject_audit_and_apply(pending[0], self.commander_user, True) reloaded_plot = Plot.objects.get(pk=self.plot.pk) self.assertEqual(reloaded_plot.udfs["Test unauth"], "c")
def test_approve_insert_without_required_raises_integrity_error(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply(new_plot.audits(), self.commander_user, True) diameter_audit = Audit.objects.get(model='Tree', field='diameter', model_id=new_tree.pk) insert_audit = Audit.objects.get(model='Tree', model_id=new_tree.pk, field='id') approve_or_reject_audit_and_apply(diameter_audit, self.commander_user, approved=True) self.assertRaises(IntegrityError, approve_or_reject_audit_and_apply, insert_audit, self.commander_user, True)
def test_accept(self): # Setup plot_length_orig = self.plot.length plot_length_new = plot_length_orig + 1.0 self.plot.length = plot_length_new self.plot.save_with_user(self.pending_user) # Generated a single audit audit = Audit.objects.filter(requires_auth=True)[0] # Should match the model self.assertEqual(audit.model_id, self.plot.pk) # Users who don't have direct field access can't accept # the edit self.assertRaises(AuthorizeException, approve_or_reject_audit_and_apply, audit, self.observer_user, approved=True) # User with write access can apply the change approve_or_reject_audit_and_apply(audit, self.direct_user, approved=True) # Reload from DB audit = Audit.objects.get(pk=audit.pk) # Audit should be marked as processed self.assertIsNotNone(audit.ref) # Ref'd audit should note approval refaudit = Audit.objects.get(pk=audit.ref.pk) self.assertEqual(refaudit.user, self.direct_user) self.assertEqual(refaudit.action, Audit.Type.PendingApprove) # The object should be updated self.assertEqual( Plot.objects.get(pk=self.plot.pk).length, plot_length_new) ohash = Plot.objects.get(pk=self.plot.pk).hash # Can't approve a pending edit twice self.assertRaises(Exception, approve_or_reject_audit_and_apply, audit, self.direct_user, approved=True) # Can't reject a pending edit once approved self.assertRaises(Exception, approve_or_reject_audit_and_apply, audit, self.direct_user, approved=False) # Nothing was changed, no audits were added self.assertEqual(ohash, Plot.objects.get(pk=self.plot.pk).hash)
def test_reject(self): # Setup plot_length_orig = self.plot.length plot_length_new = plot_length_orig + 1.0 self.plot.length = plot_length_new self.plot.save_with_user(self.pending_user) # Generated a single audit audit = Audit.objects.filter(requires_auth=True)[0] # Should match the model self.assertTrue(audit.requires_auth) self.assertEqual(audit.model_id, self.plot.pk) # Users who don't have direct field access can't reject # the edit self.assertRaises(AuthorizeException, approve_or_reject_audit_and_apply, audit, self.observer_user, approved=False) # User with write access can reject the change approve_or_reject_audit_and_apply( audit, self.direct_user, approved=False) # Reload from DB audit = Audit.objects.get(pk=audit.pk) # Audit should be marked as processed self.assertIsNotNone(audit.ref) # Ref'd audit should note rejection refaudit = Audit.objects.get(pk=audit.ref.pk) self.assertEqual(refaudit.user, self.direct_user) self.assertEqual(refaudit.action, Audit.Type.PendingReject) # The object shouldn't have changed self.assertEqual(Plot.objects.get(pk=self.plot.pk).length, plot_length_orig) ohash = Plot.objects.get(pk=self.plot.pk).hash # Can't reject a pending edit twice self.assertRaises(Exception, approve_or_reject_audit_and_apply, audit, self.direct_user, approved=False) # Can't approve a pending edit once rejected self.assertRaises(Exception, approve_or_reject_audit_and_apply, audit, self.direct_user, approved=False) # Nothing was changed, no audits were added self.assertEqual(ohash, Plot.objects.get(pk=self.plot.pk).hash)
def _approve_or_reject_pending_edit(request, instance, user, pending_edit_id, approve): audit = Audit.objects.get(pk=pending_edit_id, instance=instance) approve_or_reject_audit_and_apply(audit, user, approve) updated_plot = extract_plot_from_audit(audit) # Reject remaining audits on specified field if # we approved this audit # TODO: Should this logic be moved to app_or_rej_audit_and_ap? if approve: for pending_audit in updated_plot.get_active_pending_audits()\ .filter(field=audit.field): approve_or_reject_audit_and_apply(pending_audit, user, False) return context_dict_for_plot(request, updated_plot)
def _approve_or_reject_pending_edit( request, instance, user, pending_edit_id, approve): audit = Audit.objects.get(pk=pending_edit_id, instance=instance) approve_or_reject_audit_and_apply(audit, user, approve) updated_plot = extract_plot_from_audit(audit) # Reject remaining audits on specified field if # we approved this audit # TODO: Should this logic be moved to app_or_rej_audit_and_ap? if approve: for pending_audit in updated_plot.get_active_pending_audits()\ .filter(field=audit.field): approve_or_reject_audit_and_apply(pending_audit, user, False) return plot_to_dict(updated_plot, longform=True)
def test_pending_udf_audits(self): UserDefinedFieldDefinition.objects.create( instance=self.instance, model_type='Plot', datatype=json.dumps({'type': 'choice', 'choices': ['1', '2', '3']}), iscollection=False, name='times climbed') set_write_permissions(self.instance, self.commander_user, 'Plot', ['udf:times climbed']) FieldPermission.objects.create( model_name='Plot', field_name='udf:times climbed', permission_level=FieldPermission.WRITE_WITH_AUDIT, role=self.pending_user.get_instance_user(self.instance).role, instance=self.instance) initial_plot = Plot(geom=self.p1, instance=self.instance) initial_plot.udfs['times climbed'] = '2' initial_plot.save_with_user(self.pending_user) udf_audit = Audit.objects.get(model='Plot', field='udf:times climbed', model_id=initial_plot.pk) approve_or_reject_audit_and_apply(udf_audit, self.commander_user, approved=True) geom_audit = Audit.objects.get(model='Plot', field='geom', model_id=initial_plot.pk) approve_or_reject_audit_and_apply(geom_audit, self.commander_user, approved=True) readonly_audit = Audit.objects.get(model='Plot', field='readonly', model_id=initial_plot.pk) approve_or_reject_audit_and_apply(readonly_audit, self.commander_user, approved=True) insert_audit = Audit.objects.get(model='Plot', field='id', model_id=initial_plot.pk) approve_or_reject_audit_and_apply(insert_audit, self.commander_user, approved=True) new_plot = Plot.objects.get(pk=initial_plot.pk) self.assertEqual(new_plot.pk, initial_plot.pk) self.assertEqual(new_plot.readonly, False) self.assertEqual(new_plot.geom, self.p1) self.assertEqual(new_plot.udfs['times climbed'], '2')
def test_approve_insert_without_required_raises_integrity_error(self): new_plot = Plot(geom=self.p1, instance=self.instance) new_plot.save_with_user(self.pending_user) new_tree = Tree(plot=new_plot, instance=self.instance, diameter=10, height=10, readonly=False) new_tree.save_with_user(self.pending_user) approve_or_reject_audits_and_apply( new_plot.audits(), self.commander_user, True) diameter_audit = Audit.objects.get(model='Tree', field='diameter', model_id=new_tree.pk) insert_audit = Audit.objects.get(model='Tree', model_id=new_tree.pk, field='id') approve_or_reject_audit_and_apply( diameter_audit, self.commander_user, approved=True) self.assertRaises(IntegrityError, approve_or_reject_audit_and_apply, insert_audit, self.commander_user, True)