Exemple #1
0
        public ITestProperty SetValue(string textEntry)
        {
            AssertIsVisible();
            AssertIsModifiable();
            ResetLastMessage();

            INakedObjectAdapter nakedObjectAdapter = owningObject.NakedObject;

            try {
                field.GetNakedObject(nakedObjectAdapter);

                var parseableFacet = field.ReturnSpec.GetFacet <IParseableFacet>();

                INakedObjectAdapter newValue = parseableFacet.ParseTextEntry(textEntry, manager);

                IConsent consent = ((IOneToOneAssociationSpec)field).IsAssociationValid(nakedObjectAdapter, newValue);
                LastMessage = consent.Reason;

                Assert.IsFalse(consent.IsVetoed, string.Format("Content: '{0}' is not valid. Reason: {1}", textEntry, consent.Reason));

                ((IOneToOneAssociationSpec)field).SetAssociation(nakedObjectAdapter, textEntry.Trim().Equals("") ? null : newValue);
            }
            catch (InvalidEntryException) {
                Assert.Fail("Entry not recognised " + textEntry);
            }
            return(this);
        }
 private bool ConsentHandler(IConsent consent, Context.Context context, Cause cause) {
     if (consent.IsVetoed) {
         context.Reason = consent.Reason;
         context.ErrorCause = cause;
         return false;
     }
     return true;
 }
 public ITestAction AssertIsEnabled()
 {
     ResetLastMessage();
     AssertIsVisible();
     IConsent canUse = actionSpec.IsUsable(owningObject.NakedObject);
     LastMessage = canUse.Reason;
     Assert.IsTrue(canUse.IsAllowed, "Action '" + Name + "' is disabled: " + canUse.Reason);
     return this;
 }
 public ITestAction AssertIsDisabled()
 {
     ResetLastMessage();
     if (actionSpec.IsVisible(owningObject.NakedObject)) {
         IConsent canUse = actionSpec.IsUsable(owningObject.NakedObject);
         LastMessage = canUse.Reason;
         Assert.IsFalse(canUse.IsAllowed, "Action '" + Name + "' is usable: " + canUse.Reason);
     }
     return this;
 }
Exemple #5
0
        public ITestProperty AssertIsUnmodifiable()
        {
            ResetLastMessage();
            IConsent isUsable = field.IsUsable(owningObject.NakedObject);

            LastMessage = isUsable.Reason;

            bool canUse = isUsable.IsAllowed;

            Assert.IsFalse(canUse, "Field '" + Name + "' in " + owningObject.NakedObject + " is modifiable");
            return(this);
        }
Exemple #6
0
 public static ConsentDto BuildDto(IConsent entity) =>
 new ConsentDto
 {
     Id         = entity.Id,
     Current    = entity.Current,
     CreateDate = entity.CreateDate,
     Source     = entity.Source,
     Context    = entity.Context,
     Action     = entity.Action,
     State      = (int)entity.State,
     Comment    = entity.Comment,
 };
        public ITestAction AssertIsValidWithParms(params object[] parameters)
        {
            ResetLastMessage();
            AssertIsVisible();
            AssertIsEnabled();

            object[] parsedParameters = ParsedParameters(parameters);

            INakedObjectAdapter[] parameterObjectsAdapter = parsedParameters.AsTestNakedArray(manager).Select(x => x == null ? null : x.NakedObject).ToArray();
            IConsent canExecute = actionSpec.IsParameterSetValid(owningObject.NakedObject, parameterObjectsAdapter);
            Assert.IsTrue(canExecute.IsAllowed, "Action '" + Name + "' is unusable: " + canExecute.Reason);
            return this;
        }
        public override IConsent IsUsable(INakedObjectAdapter target)
        {
            bool     isPersistent    = target.ResolveState.IsPersistent();
            IConsent disabledConsent = IsUsableDeclaratively(isPersistent);

            if (disabledConsent != null)
            {
                return(disabledConsent);
            }

            var viewModelFacet = target.Spec.GetFacet <IViewModelFacet>();

            if (viewModelFacet != null)
            {
                // all fields on a non-editable view model are disabled
                if (!viewModelFacet.IsEditView(target))
                {
                    return(new Veto(Resources.NakedObjects.FieldDisabled));
                }
            }

            var immutableFacet = GetFacet <IImmutableFacet>();

            if (immutableFacet != null)
            {
                WhenTo when = immutableFacet.Value;
                if (when == WhenTo.UntilPersisted && !isPersistent)
                {
                    return(new Veto(Resources.NakedObjects.FieldDisabledUntil));
                }
                if (when == WhenTo.OncePersisted && isPersistent)
                {
                    return(new Veto(Resources.NakedObjects.FieldDisabledOnce));
                }
                ITypeSpec tgtSpec = target.Spec;
                if (tgtSpec.IsAlwaysImmutable() || (tgtSpec.IsImmutableOncePersisted() && isPersistent))
                {
                    return(new Veto(Resources.NakedObjects.FieldDisabled));
                }
            }
            var    f      = GetFacet <IDisableForContextFacet>();
            string reason = f == null ? null : f.DisabledReason(target);

            if (reason == null)
            {
                var fs = GetFacet <IDisableForSessionFacet>();
                reason = fs == null ? null : fs.DisabledReason(Session, target, LifecycleManager, MetamodelManager);
            }

            return(GetConsent(reason));
        }
        private static ElementDescriptor MenuActionAsElementDescriptor(this HtmlHelper html, IMenuActionImmutable menuAction, INakedObject nakedObject, bool isEdit)
        {
            IActionSpecImmutable actionIm   = menuAction.Action;
            IActionSpec          actionSpec = html.Framework().MetamodelManager.GetActionSpec(actionIm);

            if (nakedObject == null)
            {
                IObjectSpecImmutable objectIm = actionIm.OwnerSpec; //This is the spec for the service

                if (!objectIm.Service)
                {
                    throw new Exception("Action is not on a known object or service");
                }
                //TODO: Add method to IServicesManager to get a service by its IObjectSpec (or IObjectSpecImmutable)
                ITypeSpec objectSpec = html.Framework().MetamodelManager.GetSpecification(objectIm);
                nakedObject = html.Framework().ServicesManager.GetServices().Single(s => s.Spec == objectSpec);
            }

            var actionContext = new ActionContext(false, nakedObject, actionSpec);

            RouteValueDictionary attributes;
            string tagType;
            string value;

            if (!actionContext.Action.IsVisible(actionContext.Target))
            {
                return(null);
            }
            IConsent consent = actionContext.Action.IsUsable(actionContext.Target);

            if (consent.IsVetoed)
            {
                tagType = html.GetVetoedAction(actionContext, consent, out value, out attributes);
            }
            else if (isEdit)
            {
                tagType = html.GetActionAsButton(actionContext, out value, out attributes);
            }
            else
            {
                tagType = html.GetActionAsForm(actionContext, html.Framework().GetObjectTypeName(actionContext.Target.Object), new { id = html.Framework().GetObjectId(actionContext.Target) }, out value, out attributes);
            }

            return(new ElementDescriptor {
                TagType = tagType,
                Value = value,
                Attributes = attributes
            });
        }
Exemple #10
0
        public ITestProperty AssertParseableFieldEntryIsValid(string text)
        {
            AssertIsVisible();
            AssertIsModifiable();
            ResetLastMessage();

            INakedObjectAdapter nakedObjectAdapter = owningObject.NakedObject;

            field.GetNakedObject(nakedObjectAdapter);
            var parseableFacet                     = field.ReturnSpec.GetFacet <IParseableFacet>();
            INakedObjectAdapter newValue           = parseableFacet.ParseTextEntry(text, manager);
            IConsent            isAssociationValid = ((IOneToOneAssociationSpec)field).IsAssociationValid(owningObject.NakedObject, newValue);

            LastMessage = isAssociationValid.Reason;
            Assert.IsTrue(isAssociationValid.IsAllowed, "Content was unexpectedly invalidated");
            return(this);
        }
        public ITestAction AssertIsInvalidWithParms(params object[] parameters)
        {
            ResetLastMessage();

            object[] parsedParameters = ParsedParameters(parameters);

            if (actionSpec.IsVisible(owningObject.NakedObject)) {
                IConsent canUse = actionSpec.IsUsable(owningObject.NakedObject);
                LastMessage = canUse.Reason;
                if (canUse.IsAllowed) {
                    INakedObjectAdapter[] parameterObjectsAdapter = parsedParameters.AsTestNakedArray(manager).Select(x => x == null ? null : x.NakedObject).ToArray();
                    IConsent canExecute = actionSpec.IsParameterSetValid(owningObject.NakedObject, parameterObjectsAdapter);
                    LastMessage = canExecute.Reason;
                    Assert.IsFalse(canExecute.IsAllowed, "Action '" + Name + "' is usable and executable");
                }
            }
            return this;
        }
Exemple #12
0
        public ITestProperty ClearValue()
        {
            AssertIsVisible();
            AssertIsModifiable();
            ResetLastMessage();

            INakedObjectAdapter nakedObjectAdapter = owningObject.NakedObject;

            try {
                IConsent consent = ((IOneToOneAssociationSpec)field).IsAssociationValid(nakedObjectAdapter, null);
                LastMessage = consent.Reason;
                Assert.IsFalse(consent.IsVetoed, string.Format("Content: 'null' is not valid. Reason: {0}", consent.Reason));
                ((IOneToOneAssociationSpec)field).SetAssociation(nakedObjectAdapter, null);
            }
            catch (InvalidEntryException) {
                Assert.Fail("Null Entry not recognised ");
            }
            return(this);
        }
Exemple #13
0
        public string ValidToPersist()
        {
            var objectSpec = Spec as IObjectSpec;

            Trace.Assert(objectSpec != null);

            IAssociationSpec[] properties = objectSpec.Properties;
            foreach (IAssociationSpec property in properties)
            {
                INakedObjectAdapter referencedObjectAdapter = property.GetNakedObject(this);
                if (property.IsUsable(this).IsAllowed&& property.IsVisible(this))
                {
                    if (property.IsMandatory && property.IsEmpty(this))
                    {
                        return(string.Format(Resources.NakedObjects.PropertyMandatory, objectSpec.ShortName, property.Name));
                    }
                    var associationSpec = property as IOneToOneAssociationSpec;
                    if (associationSpec != null)
                    {
                        IConsent valid = associationSpec.IsAssociationValid(this, referencedObjectAdapter);
                        if (valid.IsVetoed)
                        {
                            return(string.Format(Resources.NakedObjects.PropertyInvalid, objectSpec.ShortName, associationSpec.Name, valid.Reason));
                        }
                    }
                }
                if (property is IOneToOneAssociationSpec)
                {
                    if (referencedObjectAdapter != null && referencedObjectAdapter.ResolveState.IsTransient())
                    {
                        string referencedObjectMessage = referencedObjectAdapter.ValidToPersist();
                        if (referencedObjectMessage != null)
                        {
                            return(referencedObjectMessage);
                        }
                    }
                }
            }

            var validateFacet = objectSpec.GetFacet <IValidateObjectFacet>();

            return(validateFacet == null ? null : validateFacet.Validate(this));
        }
        private bool ValidateParameters(ActionContext actionContext, IDictionary<string, object> rawParms) {
            if (rawParms.Any(kvp => !actionContext.Action.Parameters.Select(p => p.Id).Contains(kvp.Key))) {
                throw new BadRequestNOSException("Malformed arguments");
            }

            bool isValid = true;
            var orderedParms = new Dictionary<string, ParameterContext>();

            // handle contributed actions 

            if (actionContext.Action.IsContributedMethod && !actionContext.Action.OnSpec.Equals(actionContext.Target.Spec)) {
                IActionParameterSpec parm = actionContext.Action.Parameters.FirstOrDefault(p => actionContext.Target.Spec.IsOfType(p.Spec));

                if (parm != null) {
                    rawParms.Add(parm.Id, actionContext.Target.Object);
                }
            }

            // check mandatory fields first as standard NO behaviour is that no validation takes place until 
            // all mandatory fields are set. 
            foreach (IActionParameterSpec parm in actionContext.Action.Parameters) {
                orderedParms[parm.Id] = new ParameterContext();

                object value = rawParms.ContainsKey(parm.Id) ? rawParms[parm.Id] : null;

                orderedParms[parm.Id].ProposedValue = value;
                orderedParms[parm.Id].Parameter = parm;
                orderedParms[parm.Id].Action = actionContext.Action;

                var stringValue = value as string;

                if (parm.IsMandatory && (value == null || (value is string && string.IsNullOrEmpty(stringValue)))) {
                    isValid = false;
                    orderedParms[parm.Id].Reason = "Mandatory"; // i18n
                }
            }

            //check for individual parameter validity, including parsing of text input
            if (isValid) {
                foreach (IActionParameterSpec parm in actionContext.Action.Parameters) {
                    try {
                        INakedObject valueNakedObject = GetValue((IObjectSpec) parm.Spec, rawParms.ContainsKey(parm.Id) ? rawParms[parm.Id] : null);

                        orderedParms[parm.Id].ProposedNakedObject = valueNakedObject;

                        IConsent consent = parm.IsValid(actionContext.Target, valueNakedObject);
                        if (!consent.IsAllowed) {
                            orderedParms[parm.Id].Reason = consent.Reason;
                            isValid = false;
                        }
                    }
                    catch (InvalidEntryException) {
                        isValid = false;
                        orderedParms[parm.Id].ErrorCause = Cause.WrongType;
                        orderedParms[parm.Id].Reason = "Invalid Entry"; // i18n 
                    }
                }
            }

            // check for validity of whole set, including any 'co-validation' involving multiple parameters
            if (isValid) {
                IConsent consent = actionContext.Action.IsParameterSetValid(actionContext.Target, orderedParms.Select(kvp => kvp.Value.ProposedNakedObject).ToArray());
                if (!consent.IsAllowed) {
                    actionContext.Reason = consent.Reason;
                    isValid = false;
                }
            }

            actionContext.VisibleParameters = orderedParms.Select(p => p.Value).ToArray();

            return isValid;
        }
Exemple #15
0
 private static string GetVetoedAction(ActionContext actionContext, IConsent consent, out string value, out RouteValueDictionary attributes) {
     value = actionContext.Action.Name;
     attributes = new RouteValueDictionary(new {
         id = actionContext.GetActionId(),
         @class =  actionContext.GetActionClass(), 
         title = consent.Reason
     });
     return "div";
 }
        public IConsentSurface IsUsable(INakedObjectSurface target)
        {
            IConsent consent = assoc.IsUsable(((NakedObjectWrapper)target).WrappedNakedObject);

            return(new ConsentWrapper(consent));
        }
 public Consent(IConsent consent)
 {
     this.consent = consent;
 }
Exemple #18
0
        public IConsentFacade IsUsable(IObjectFacade target)
        {
            IConsent consent = assoc.IsUsable(((ObjectFacade)target).WrappedNakedObject);

            return(new ConsentFacade(consent));
        }
 public ConsentFacade(IConsent consent) => this.consent = consent ?? throw new NullReferenceException($"{nameof(consent)} is null");
 public ConsentWrapper(IConsent consent)
 {
     SurfaceUtils.AssertNotNull(consent, "Consent is null");
     this.consent = consent;
 }
 public ConsentFacade(IConsent consent)
 {
     FacadeUtils.AssertNotNull(consent, "Consent is null");
     this.consent = consent;
 }
Exemple #22
0
 /// <summary>
 ///     Determines whether the consent is revoked.
 /// </summary>
 public static bool IsRevoked(this IConsent consent) => (consent.State & ConsentState.Revoked) > 0;
Exemple #23
0
 /// <summary>
 ///     Determines whether the consent is granted.
 /// </summary>
 public static bool IsGranted(this IConsent consent) => (consent.State & ConsentState.Granted) > 0;
 public ConsentFacade(IConsent consent) {
     FacadeUtils.AssertNotNull(consent, "Consent is null");
     this.consent = consent;
 }
Exemple #25
0
 public ConsentWrapper(IConsent consent) {
     this.consent = consent;
 }
        public void CanCrudConsent()
        {
            // can register
            IConsent consent = ConsentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Granted, "no comment");

            Assert.AreNotEqual(0, consent.Id);

            Assert.IsTrue(consent.Current);
            Assert.AreEqual("user/1234", consent.Source);
            Assert.AreEqual("app1", consent.Context);
            Assert.AreEqual("do-something", consent.Action);
            Assert.AreEqual(ConsentState.Granted, consent.State);
            Assert.AreEqual("no comment", consent.Comment);

            Assert.IsTrue(consent.IsGranted());

            // can register more
            ConsentService.RegisterConsent("user/1234", "app1", "do-something-else", ConsentState.Granted, "no comment");
            ConsentService.RegisterConsent("user/1236", "app1", "do-something", ConsentState.Granted, "no comment");
            ConsentService.RegisterConsent("user/1237", "app2", "do-something", ConsentState.Granted, "no comment");

            // can get by source
            IConsent[] consents = ConsentService.LookupConsent(source: "user/1235").ToArray();
            Assert.IsEmpty(consents);

            consents = ConsentService.LookupConsent(source: "user/1234").ToArray();
            Assert.AreEqual(2, consents.Length);
            Assert.IsTrue(consents.All(x => x.Source == "user/1234"));
            Assert.IsTrue(consents.Any(x => x.Action == "do-something"));
            Assert.IsTrue(consents.Any(x => x.Action == "do-something-else"));

            // can get by context
            consents = ConsentService.LookupConsent(context: "app3").ToArray();
            Assert.IsEmpty(consents);

            consents = ConsentService.LookupConsent(context: "app2").ToArray();
            Assert.AreEqual(1, consents.Length);

            consents = ConsentService.LookupConsent(context: "app1").ToArray();
            Assert.AreEqual(3, consents.Length);
            Assert.IsTrue(consents.Any(x => x.Action == "do-something"));
            Assert.IsTrue(consents.Any(x => x.Action == "do-something-else"));

            // can get by action
            consents = ConsentService.LookupConsent(action: "do-whatever").ToArray();
            Assert.IsEmpty(consents);

            consents = ConsentService.LookupConsent(context: "app1", action: "do-something").ToArray();
            Assert.AreEqual(2, consents.Length);
            Assert.IsTrue(consents.All(x => x.Action == "do-something"));
            Assert.IsTrue(consents.Any(x => x.Source == "user/1234"));
            Assert.IsTrue(consents.Any(x => x.Source == "user/1236"));

            // can revoke
            consent = ConsentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Revoked, "no comment");

            consents = ConsentService.LookupConsent(source: "user/1234", context: "app1", action: "do-something").ToArray();
            Assert.AreEqual(1, consents.Length);
            Assert.IsTrue(consents[0].Current);
            Assert.AreEqual(ConsentState.Revoked, consents[0].State);

            // can filter
            consents = ConsentService.LookupConsent(context: "app1", action: "do-", actionStartsWith: true).ToArray();
            Assert.AreEqual(3, consents.Length);
            Assert.IsTrue(consents.All(x => x.Context == "app1"));
            Assert.IsTrue(consents.All(x => x.Action.StartsWith("do-")));

            // can get history
            consents = ConsentService.LookupConsent(source: "user/1234", context: "app1", action: "do-something", includeHistory: true).ToArray();
            Assert.AreEqual(1, consents.Length);
            Assert.IsTrue(consents[0].Current);
            Assert.AreEqual(ConsentState.Revoked, consents[0].State);
            Assert.IsTrue(consents[0].IsRevoked());
            Assert.IsNotNull(consents[0].History);
            IConsent[] history = consents[0].History.ToArray();
            Assert.AreEqual(1, history.Length);
            Assert.IsFalse(history[0].Current);
            Assert.AreEqual(ConsentState.Granted, history[0].State);

            // cannot be stupid
            Assert.Throws <ArgumentException>(() =>
                                              ConsentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Granted | ConsentState.Revoked, "no comment"));
        }