public void TestFromCSEntryChangeUpdate()
        {
            IAttributeAdapter schemaItem = UnitTestControl.Schema["user"].AttributeAdapters.First(t => t.FieldName == "aliases");

            CSEntryChange x = CSEntryChange.Create();

            x.ObjectModificationType = ObjectModificationType.Update;

            List <ValueChange> changes = new List <ValueChange>();

            changes.Add(ValueChange.CreateValueAdd("*****@*****.**"));
            changes.Add(ValueChange.CreateValueDelete("*****@*****.**"));

            x.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("aliases", changes));

            UserUpdateTemplate ux = new UserUpdateTemplate();

            ux.Aliases = new List <string>()
            {
                "*****@*****.**", "*****@*****.**"
            };
            schemaItem.UpdateField(x, ux);

            CollectionAssert.AreEqual(new string[] { "*****@*****.**",
                                                     "*****@*****.**" }, ux.Aliases.ToArray());
        }
        /// <summary>
        /// Creates an AttributeChange of type Update, with ValueChanges created for the specified value adds and deletes
        /// </summary>
        /// <param name="csentry">The CSEntryChange to add the AttributeChange to</param>
        /// <param name="attributeName">The name of the attribute</param>
        /// <param name="valueAdds">The values to add in the update operation</param>
        /// <param name="valueDeletes">The values to delete in the update operation</param>
        public static void CreateAttributeUpdate(this CSEntryChange csentry, string attributeName, IList <object> valueAdds, IList <object> valueDeletes)
        {
            List <ValueChange> valueChanges = new List <ValueChange>();

            if (valueAdds != null)
            {
                foreach (object value in valueAdds)
                {
                    valueChanges.Add(ValueChange.CreateValueAdd(value));
                }
            }

            if (valueDeletes != null)
            {
                foreach (object value in valueDeletes)
                {
                    valueChanges.Add(ValueChange.CreateValueDelete(value));
                }
            }

            if (valueChanges.Count > 0)
            {
                csentry.CreateAttributeUpdate(attributeName, valueChanges);
            }
        }
        public void Rename()
        {
            string id = null;

            try
            {
                string dn = $"{Guid.NewGuid()}@{UnitTestControl.TestParameters.Domain}";
                User   e  = new User
                {
                    PrimaryEmail = dn,
                    Password     = Guid.NewGuid().ToString(),
                    Name         =
                    {
                        GivenName  = "test",
                        FamilyName = "test"
                    }
                };

                e  = UnitTestControl.TestParameters.UsersService.Add(e);
                id = e.Id;
                System.Threading.Thread.Sleep(2000);

                CSEntryChange cs = CSEntryChange.Create();
                cs.ObjectModificationType = ObjectModificationType.Update;
                cs.DN         = dn;
                cs.ObjectType = SchemaConstants.User;

                string newDN = $"{Guid.NewGuid()}@{UnitTestControl.TestParameters.Domain}";

                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("DN", new List <ValueChange>()
                {
                    ValueChange.CreateValueAdd(newDN), ValueChange.CreateValueDelete(dn)
                }));

                cs.AnchorAttributes.Add(AnchorAttribute.Create("id", id));
                CSEntryChangeResult result =
                    ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.User], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                System.Threading.Thread.Sleep(2000);
                e = UnitTestControl.TestParameters.UsersService.Get(id);
                Assert.AreEqual(newDN, e.PrimaryEmail);
            }
            finally
            {
                if (id != null)
                {
                    UnitTestControl.TestParameters.UsersService.Delete(id);
                }
            }
        }
        /// <summary>
        /// Converts an enumeration of values to a list of ValueChanges with the modification type set to 'delete'
        /// </summary>
        /// <typeparam name="T">The type of the data in the list</typeparam>
        /// <param name="list">The list of values to convert to ValueChanges</param>
        /// <returns>A list of ValueChange objects with their modification types set to 'delete'</returns>
        public static IList <ValueChange> ToValueChangeDelete <T>(this IEnumerable <T> list)
        {
            List <ValueChange> changes = new List <ValueChange>();

            foreach (T value in list)
            {
                changes.Add(ValueChange.CreateValueDelete(value));
            }

            return(changes);
        }
 /// <summary>
 /// Create a ValueChange for the specified value based on the modification type specified by this object
 /// </summary>
 /// <param name="newValue">The value to create the value change for</param>
 /// <param name="modificationType">The type of modification to perform</param>
 /// <returns>A new ValueChange object</returns>
 protected ValueChange CreateValueChange(object newValue, ValueModificationType modificationType)
 {
     if (modificationType == ValueModificationType.Delete)
     {
         return(ValueChange.CreateValueDelete(newValue));
     }
     else
     {
         return(ValueChange.CreateValueAdd(newValue));
     }
 }
Beispiel #6
0
        public void ContactRename()
        {
            string id = null;

            try
            {
                string       dn = Guid.NewGuid().ToString();
                ContactEntry e  = new ContactEntry();

                e.Emails.Add(new EMail()
                {
                    Address = "*****@*****.**", Label = "work"
                });

                e.ExtendedProperties.Add(new ExtendedProperty(dn, ApiInterfaceContact.DNAttributeName));

                e  = UnitTestControl.TestParameters.ContactsService.Add(e, UnitTestControl.TestParameters.Domain);
                id = e.SelfUri.Content;

                CSEntryChange cs = CSEntryChange.Create();
                cs.ObjectModificationType = ObjectModificationType.Update;
                cs.DN         = dn;
                cs.ObjectType = SchemaConstants.Contact;

                string newDN = Guid.NewGuid().ToString();

                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("DN", new List <ValueChange>()
                {
                    ValueChange.CreateValueAdd(newDN), ValueChange.CreateValueDelete(dn)
                }));

                cs.AnchorAttributes.Add(AnchorAttribute.Create("id", id));
                CSEntryChangeResult result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.Contact], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                System.Threading.Thread.Sleep(5000);
                e = UnitTestControl.TestParameters.ContactsService.GetContact(id);
                Assert.AreEqual(newDN, e.ExtendedProperties.Single(t => t.Name == ApiInterfaceContact.DNAttributeName).Value);
                var x = CSEntryChangeQueue.Take();
            }
            finally
            {
                if (id != null)
                {
                    UnitTestControl.TestParameters.ContactsService.Delete(id);
                }
            }
        }
 /// <summary>
 /// Removes a reference to the source object on the specified target object
 /// </summary>
 /// <param name="sourceObject">The object to remove the reference to</param>
 /// <param name="targetObject">The object to remove the reference from</param>
 private void RemoveReference(MAObjectHologram sourceObject, MAObjectHologram targetObject)
 {
     if (this.BackLinkAttribute.IsMultivalued)
     {
         targetObject.UpdateAttributeValue(this.BackLinkAttribute, new List <ValueChange>()
         {
             ValueChange.CreateValueDelete(sourceObject.ObjectID)
         });
     }
     else
     {
         targetObject.DeleteAttribute(this.BackLinkAttribute);
     }
 }
        private IList <ValueChange> DeleteFromRole(AttributeChange change, IList <AclRule> existingRules, string role, string calendarId)
        {
            List <ValueChange> valueChanges = new List <ValueChange>();

            IList <string> deletes;

            if (change.ModificationType == AttributeModificationType.Delete || change.ModificationType == AttributeModificationType.Replace)
            {
                deletes = existingRules.Where(t => t.Role == role).Select(t => t.Scope.Value).ToList();
            }
            else
            {
                deletes = change.GetValueDeletes <string>();
            }

            foreach (string valueToDelete in deletes)
            {
                AclRule matchedRule = existingRules.FirstOrDefault(t => t.Role == role && valueToDelete.Equals(t.Scope.Value, StringComparison.CurrentCultureIgnoreCase));

                if (matchedRule == null)
                {
                    Logger.WriteLine($"{valueToDelete} does not have role {role} on calendar {calendarId}. The request to delete the value will be ignored");
                    valueChanges.Add(ValueChange.CreateValueDelete(valueToDelete));

                    continue;
                }

                if (matchedRule.Role == "owner" && calendarId.Equals(matchedRule.Scope.Value, StringComparison.CurrentCultureIgnoreCase))
                {
                    Debug.WriteLine($"Ignoring default ACL for {valueToDelete} to role {role} on calendar {calendarId}");
                    continue;
                }

                try
                {
                    this.config.ResourcesService.DeleteCalendarAclRule(this.config.CustomerID, calendarId, matchedRule.Id);
                    Logger.WriteLine($"Removed {valueToDelete} from role {role} on calendar {calendarId}");
                }
                catch
                {
                    Logger.WriteLine($"Failed to remove {valueToDelete} to role {role} on calendar {calendarId}");
                    throw;
                }

                valueChanges.Add(ValueChange.CreateValueDelete(valueToDelete));
            }

            return(valueChanges);
        }
        private AttributeChange ApplyDelegateChanges(CSEntryChange csentry)
        {
            this.GetUserDelegateChanges(csentry, out IList <string> adds, out IList <string> deletes);

            if (adds.Count == 0 && deletes.Count == 0)
            {
                return(null);
            }

            AttributeChange     change       = null;
            IList <ValueChange> valueChanges = new List <ValueChange>();

            try
            {
                if (deletes != null)
                {
                    foreach (string delete in deletes)
                    {
                        Logger.WriteLine($"Removing delegate {delete}");
                        this.config.GmailService.RemoveDelegate(csentry.DN, delete);
                        valueChanges.Add(ValueChange.CreateValueDelete(delete));
                    }
                }

                foreach (string add in adds)
                {
                    Logger.WriteLine($"Adding delegate {add}");
                    this.config.GmailService.AddDelegate(csentry.DN, add);
                    valueChanges.Add(ValueChange.CreateValueAdd(add));
                }
            }
            finally
            {
                if (valueChanges.Count > 0)
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        change = AttributeChange.CreateAttributeUpdate(this.attributeName, valueChanges);
                    }
                    else
                    {
                        change = AttributeChange.CreateAttributeAdd(this.attributeName, valueChanges.Where(u => u.ModificationType == ValueModificationType.Add).Select(t => t.Value).ToList());
                    }
                }
            }

            return(change);
        }
Beispiel #10
0
        private void AddValueChange(ValueModificationType type, object value)
        {
            switch (type)
            {
            case ValueModificationType.Add:
                this.ValueChanges.Add(ValueChange.CreateValueAdd(value));
                break;

            case ValueModificationType.Delete:
                this.ValidateValueChangeType(type);
                this.ValueChanges.Add(ValueChange.CreateValueDelete(value));

                break;

            case ValueModificationType.Unconfigured:
            default:
                throw new UnknownOrUnsupportedModificationTypeException(type);
            }
        }
        private static ValueChange GetValueChange(XElement element, ExtendedAttributeType attributeType)
        {
            ValueModificationType modificationType = ValueModificationType.Unconfigured;
            string value = null;


            foreach (var child in element.Elements())
            {
                if (child.Name.LocalName == "modification-type")
                {
                    string modificationTypeString = (string)child;

                    if (!Enum.TryParse <ValueModificationType>(modificationTypeString, out modificationType))
                    {
                        throw new InvalidCastException(string.Format("Cannot convert '{0}' to type {1}", modificationTypeString, typeof(ValueModificationType).Name));
                    }
                }
                else if (child.Name.LocalName == "value")
                {
                    value = (string)child;
                }
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("The value-change value was blank");
            }

            switch (modificationType)
            {
            case ValueModificationType.Add:
                return(ValueChange.CreateValueAdd(TypeConverter.ConvertData(value, attributeType)));

            case ValueModificationType.Delete:
                return(ValueChange.CreateValueDelete(TypeConverter.ConvertData(value, attributeType)));

            case ValueModificationType.Unconfigured:
            default:
                throw new UnknownOrUnsupportedModificationTypeException(modificationType);
            }
        }
        public void Rename()
        {
            Group e = null;

            try
            {
                e = UnitTestControl.CreateGroup();

                CSEntryChange cs = CSEntryChange.Create();
                cs.ObjectModificationType = ObjectModificationType.Update;
                cs.ObjectType             = SchemaConstants.Group;
                cs.DN = e.Email;

                string newDN = $"{Guid.NewGuid()}@{UnitTestControl.TestParameters.Domain}";

                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("DN", new List <ValueChange>()
                {
                    ValueChange.CreateValueAdd(newDN), ValueChange.CreateValueDelete(e.Email)
                }));

                cs.AnchorAttributes.Add(AnchorAttribute.Create("id", e.Id));
                CSEntryChangeResult result =
                    ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.Group], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                System.Threading.Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);
                e = UnitTestControl.TestParameters.GroupsService.Get(e.Id);
                Assert.AreEqual(newDN, e.Email);
            }
            finally
            {
                UnitTestControl.Cleanup(e);
            }
        }
        public void TestCSEntryChangeAttributeUpdateValueChangeRemoveAddDelete()
        {
            ObjectModificationType objectModificationType = ObjectModificationType.Update;
            AcmaSchemaAttribute    testAttribute          = ActiveConfig.DB.GetAttribute("mailAlternateAddresses");
            CSEntryChange          csentry;

            List <ValueChange> startValues = new List <ValueChange>()
            {
                ValueChange.CreateValueAdd("*****@*****.**"), ValueChange.CreateValueAdd("*****@*****.**")
            };
            List <ValueChange> changes = new List <ValueChange>()
            {
                ValueChange.CreateValueDelete("*****@*****.**")
            };
            List <ValueChange> expectedValues = new List <ValueChange>()
            {
                ValueChange.CreateValueAdd("*****@*****.**")
            };

            csentry = CreateNewCSEntry(objectModificationType);
            csentry.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate(testAttribute.Name, startValues));
            csentry.AttributeChanges.UpdateAttribute(objectModificationType, testAttribute, changes);
            TestAttributeChangeResults(csentry, testAttribute, expectedValues);
        }
        /// <summary>
        /// Converts an enumeration of values to a list of ValueChanges
        /// </summary>
        /// <typeparam name="T">The type of the data in the list</typeparam>
        /// <param name="list">The list of values to convert to ValueChanges</param>
        /// <param name="modificationType">The modification type to apply to the values in the ValueChange</param>
        /// <returns>A list of ValueChange objects</returns>
        public static IList <ValueChange> ToValueChange <T>(this IEnumerable <T> list, ValueModificationType modificationType)
        {
            List <ValueChange> changes = new List <ValueChange>();

            foreach (T value in list)
            {
                switch (modificationType)
                {
                case ValueModificationType.Add:
                    changes.Add(ValueChange.CreateValueAdd(value));
                    break;

                case ValueModificationType.Delete:
                    changes.Add(ValueChange.CreateValueDelete(value));
                    break;

                case ValueModificationType.Unconfigured:
                default:
                    throw new NotSupportedException("The modification type is unknown or unsupported");
                }
            }

            return(changes);
        }
Beispiel #15
0
        public void TestSerialization()
        {
            AcmaCSEntryChange toSerialize = new AcmaCSEntryChange();

            toSerialize.ObjectModificationType = ObjectModificationType.Update;
            toSerialize.ObjectType             = "person";
            toSerialize.DN = "testDN";
            toSerialize.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("sn", "myValue"));
            toSerialize.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("mailAlternateAddresses", new List <object>()
            {
                "myValue1", "myValue2"
            }));
            toSerialize.AttributeChanges.Add(AttributeChange.CreateAttributeDelete("accountName"));
            toSerialize.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("activeExpiryDate", 900L));
            toSerialize.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("displayName", new List <ValueChange>()
            {
                ValueChange.CreateValueAdd("addValue"), ValueChange.CreateValueDelete("deleteValue")
            }));
            toSerialize.AnchorAttributes.Add(AnchorAttribute.Create("myAnchor1", "99"));
            toSerialize.AnchorAttributes.Add(AnchorAttribute.Create("myAnchor2", "my second test anchor"));


            AcmaCSEntryChange deserialized = UnitTestControl.XmlSerializeRoundTrip <AcmaCSEntryChange>(toSerialize);

            Assert.AreEqual(toSerialize.ObjectModificationType, deserialized.ObjectModificationType);
            Assert.AreEqual(toSerialize.ObjectType, deserialized.ObjectType);
            Assert.AreEqual(toSerialize.DN, deserialized.DN);

            Assert.AreEqual(toSerialize.AttributeChanges[0].Name, deserialized.AttributeChanges[0].Name);
            Assert.AreEqual(toSerialize.AttributeChanges[0].ModificationType, deserialized.AttributeChanges[0].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[0].ValueChanges[0].Value, deserialized.AttributeChanges[0].ValueChanges[0].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[0].ValueChanges[0].ModificationType, deserialized.AttributeChanges[0].ValueChanges[0].ModificationType);

            Assert.AreEqual(toSerialize.AttributeChanges[1].Name, deserialized.AttributeChanges[1].Name);
            Assert.AreEqual(toSerialize.AttributeChanges[1].ModificationType, deserialized.AttributeChanges[1].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[1].ValueChanges[0].Value, deserialized.AttributeChanges[1].ValueChanges[0].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[1].ValueChanges[0].ModificationType, deserialized.AttributeChanges[1].ValueChanges[0].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[1].ValueChanges[1].Value, deserialized.AttributeChanges[1].ValueChanges[1].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[1].ValueChanges[1].ModificationType, deserialized.AttributeChanges[1].ValueChanges[1].ModificationType);

            Assert.AreEqual(toSerialize.AttributeChanges[2].Name, deserialized.AttributeChanges[2].Name);
            Assert.AreEqual(toSerialize.AttributeChanges[2].ModificationType, deserialized.AttributeChanges[2].ModificationType);

            Assert.AreEqual(toSerialize.AttributeChanges[3].Name, deserialized.AttributeChanges[3].Name);
            Assert.AreEqual(toSerialize.AttributeChanges[3].ModificationType, deserialized.AttributeChanges[3].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[3].ValueChanges[0].Value, deserialized.AttributeChanges[3].ValueChanges[0].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[3].ValueChanges[0].ModificationType, deserialized.AttributeChanges[3].ValueChanges[0].ModificationType);

            Assert.AreEqual(toSerialize.AttributeChanges[4].Name, deserialized.AttributeChanges[4].Name);
            Assert.AreEqual(toSerialize.AttributeChanges[4].ModificationType, deserialized.AttributeChanges[4].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[4].ValueChanges[0].Value, deserialized.AttributeChanges[4].ValueChanges[0].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[4].ValueChanges[0].ModificationType, deserialized.AttributeChanges[4].ValueChanges[0].ModificationType);
            Assert.AreEqual(toSerialize.AttributeChanges[4].ValueChanges[1].Value, deserialized.AttributeChanges[4].ValueChanges[1].Value);
            Assert.AreEqual(toSerialize.AttributeChanges[4].ValueChanges[1].ModificationType, deserialized.AttributeChanges[4].ValueChanges[1].ModificationType);

            Assert.AreEqual(toSerialize.AnchorAttributes[0].Name, deserialized.AnchorAttributes[0].Name);
            Assert.AreEqual(toSerialize.AnchorAttributes[0].Value, deserialized.AnchorAttributes[0].Value);

            Assert.AreEqual(toSerialize.AnchorAttributes[1].Name, deserialized.AnchorAttributes[1].Name);
            Assert.AreEqual(toSerialize.AnchorAttributes[1].Value, deserialized.AnchorAttributes[1].Value);
        }
Beispiel #16
0
        public void UpdateCalendarWithAcls()
        {
            CSEntryChange cs = CSEntryChange.Create();

            cs.ObjectModificationType = ObjectModificationType.Add;
            cs.DN         = Guid.NewGuid().ToString();
            cs.ObjectType = SchemaConstants.Calendar;

            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("buildingId", "testbuilding1"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("capacity", 33L));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("name", "test-name"));

            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("owner", new List <object> {
                this.CreateAddress("owner1"), this.CreateAddress("owner2")
            }));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("reader", new List <object> {
                this.CreateAddress("reader1"), this.CreateAddress("reader2")
            }));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("writer", new List <object> {
                this.CreateAddress("writer1"), this.CreateAddress("writer2")
            }));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("freeBusyReader", this.CreateAddress("freebusyreader")));

            string id = null;

            try
            {
                CSEntryChangeResult result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.Calendar], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                id = result.AnchorAttributes["id"].GetValueAdd <string>();

                Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);

                CalendarResource c = UnitTestControl.TestParameters.ResourcesService.GetCalendar(UnitTestControl.TestParameters.CustomerID, id);

                List <AclRule> acls = UnitTestControl.TestParameters.ResourcesService.GetCalendarAclRules(UnitTestControl.TestParameters.CustomerID, c.ResourceEmail).ToList();

                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner1")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner2")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "reader" && t.Scope.Value == this.CreateAddress("reader1")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "reader" && t.Scope.Value == this.CreateAddress("reader2")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "writer" && t.Scope.Value == this.CreateAddress("writer1")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "writer" && t.Scope.Value == this.CreateAddress("writer2")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "freeBusyReader" && t.Scope.Value == this.CreateAddress("freebusyreader")));

                cs = CSEntryChange.Create();
                cs.ObjectModificationType = ObjectModificationType.Update;
                cs.DN         = "*****@*****.**";
                cs.ObjectType = SchemaConstants.Calendar;

                cs.AnchorAttributes.Add(AnchorAttribute.Create("id", id));
                cs.AnchorAttributes.Add(AnchorAttribute.Create("resourceEmail", result.AnchorAttributes["resourceEmail"].GetValueAdd <string>()));

                cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("owner", new List <object> {
                    this.CreateAddress("owner3"), this.CreateAddress("owner4")
                }));
                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("reader", new List <ValueChange> {
                    ValueChange.CreateValueAdd(this.CreateAddress("reader3"))
                }));
                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("writer", new List <ValueChange> {
                    ValueChange.CreateValueDelete(this.CreateAddress("writer1"))
                }));
                cs.AttributeChanges.Add(AttributeChange.CreateAttributeDelete("freeBusyReader"));

                result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.Calendar], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);

                acls = UnitTestControl.TestParameters.ResourcesService.GetCalendarAclRules(UnitTestControl.TestParameters.CustomerID, c.ResourceEmail).ToList();

                Assert.IsNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner1")));
                Assert.IsNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner2")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner3")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("owner4")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "reader" && t.Scope.Value == this.CreateAddress("reader1")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "reader" && t.Scope.Value == this.CreateAddress("reader2")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "reader" && t.Scope.Value == this.CreateAddress("reader3")));
                Assert.IsNull(acls.FirstOrDefault(t => t.Role == "owner" && t.Scope.Value == this.CreateAddress("writer1")));
                Assert.IsNotNull(acls.FirstOrDefault(t => t.Role == "writer" && t.Scope.Value == this.CreateAddress("writer2")));
                Assert.IsNull(acls.FirstOrDefault(t => t.Role == "freeBusyReader" && t.Scope.Value == this.CreateAddress("freebusyreader")));
            }
            finally
            {
                if (id != null)
                {
                    UnitTestControl.TestParameters.ResourcesService.DeleteCalendar(UnitTestControl.TestParameters.CustomerID, id);
                }
            }
        }
Beispiel #17
0
        public void UpdateCalendar()
        {
            CalendarResource calendar = new CalendarResource();

            calendar.ResourceId             = Guid.NewGuid().ToString("n");
            calendar.ResourceName           = "test-name";
            calendar.BuildingId             = "testbuilding2";
            calendar.Capacity               = 9;
            calendar.FloorName              = "G";
            calendar.FloorSection           = "39b";
            calendar.ResourceCategory       = "OTHER";
            calendar.ResourceDescription    = "internal description 1";
            calendar.UserVisibleDescription = "my description 2";
            calendar.FeatureInstances       = new List <FeatureInstance>()
            {
                new FeatureInstance()
                {
                    Feature = new Feature()
                    {
                        Name = "Test1"
                    }
                },
                new FeatureInstance()
                {
                    Feature = new Feature()
                    {
                        Name = "Test2"
                    }
                },
            };

            UnitTestControl.TestParameters.ResourcesService.AddCalendar(UnitTestControl.TestParameters.CustomerID, calendar);

            CSEntryChange cs = CSEntryChange.Create();

            cs.ObjectModificationType = ObjectModificationType.Update;
            cs.DN         = "*****@*****.**";
            cs.ObjectType = SchemaConstants.Calendar;
            cs.AnchorAttributes.Add(AnchorAttribute.Create("id", calendar.ResourceId));

            cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("buildingId", new List <ValueChange>()
            {
                ValueChange.CreateValueAdd("testbuilding1")
            }));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("capacity", 33L));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("floorName", "G"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("floorSection", "33B"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("resourceCategory", "CONFERENCE_ROOM"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("resourceDescription", "internal description"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeReplace("userVisibleDescription", "user description"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate("features", new List <ValueChange>()
            {
                ValueChange.CreateValueAdd("Test3"),
                ValueChange.CreateValueDelete("Test1"),
            }));

            string id = calendar.ResourceId;

            try
            {
                CSEntryChangeResult result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.Calendar], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);

                CalendarResource c = UnitTestControl.TestParameters.ResourcesService.GetCalendar(UnitTestControl.TestParameters.CustomerID, id);
                Assert.AreEqual(cs.DN, "*****@*****.**");
                Assert.AreEqual("test-name", c.ResourceName);
                Assert.AreEqual("testbuilding1", c.BuildingId);
                Assert.AreEqual(33, c.Capacity);
                Assert.AreEqual("G", c.FloorName);
                Assert.AreEqual("33B", c.FloorSection);
                Assert.AreEqual("CONFERENCE_ROOM", c.ResourceCategory);
                Assert.AreEqual("internal description", c.ResourceDescription);
                Assert.AreEqual("user description", c.UserVisibleDescription);
                CollectionAssert.AreEquivalent(new string[] { "Test2", "Test3" }, ApiInterfaceCalendar.GetFeatureNames(c).ToList());
            }
            finally
            {
                if (id != null)
                {
                    UnitTestControl.TestParameters.ResourcesService.DeleteCalendar(UnitTestControl.TestParameters.CustomerID, id);
                }
            }
        }
        public void DeleteStringValuesAsValueDeleteOnObjectUpdate()
        {
            User u = UserTests.CreateUser();

            // Create the initial object
            CSEntryChange cs = CSEntryChange.Create();

            cs.ObjectModificationType = ObjectModificationType.Update;
            cs.DN         = u.PrimaryEmail;
            cs.ObjectType = SchemaConstants.User;
            cs.AnchorAttributes.Add(AnchorAttribute.Create("id", u.Id));

            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd($"{TestSchemaName}_TestSVString", "string1"));
            cs.AttributeChanges.Add(AttributeChange.CreateAttributeAdd($"{TestSchemaName}_TestMVString", new List <object> {
                "test2", "test3"
            }));

            try
            {
                CSEntryChangeResult result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.User], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);

                User e = UnitTestControl.TestParameters.UsersService.Get(u.Id);
                Assert.AreEqual(cs.DN, e.PrimaryEmail);

                Assert.AreEqual("string1", (string)e.CustomSchemas[TestSchemaName]["TestSVString"]);
                CollectionAssert.AreEquivalent(new List <object> {
                    "test2", "test3"
                }, GetReturnedValues <string>("TestMVString", e));

                // Remove all the values with a 'value delete'

                cs = CSEntryChange.Create();
                cs.ObjectModificationType = ObjectModificationType.Update;
                cs.DN         = u.PrimaryEmail;
                cs.ObjectType = SchemaConstants.User;
                cs.AnchorAttributes.Add(AnchorAttribute.Create("id", u.Id));

                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate($"{TestSchemaName}_TestSVString", new List <ValueChange> {
                    ValueChange.CreateValueDelete("string1")
                }));
                cs.AttributeChanges.Add(AttributeChange.CreateAttributeUpdate($"{TestSchemaName}_TestMVString", new List <ValueChange> {
                    ValueChange.CreateValueDelete("test2"), ValueChange.CreateValueDelete("test3")
                }));

                result = ExportProcessor.PutCSEntryChange(cs, UnitTestControl.Schema.GetSchema().Types[SchemaConstants.User], UnitTestControl.TestParameters);

                if (result.ErrorCode != MAExportError.Success)
                {
                    Assert.Fail(result.ErrorName);
                }

                Thread.Sleep(UnitTestControl.PostGoogleOperationSleepInterval);

                e = UnitTestControl.TestParameters.UsersService.Get(u.Id);
                Assert.AreEqual(cs.DN, e.PrimaryEmail);

                if (e.CustomSchemas != null && e.CustomSchemas.ContainsKey(TestSchemaName))
                {
                    Assert.IsFalse(e.CustomSchemas[TestSchemaName].ContainsKey("TestSVString"));
                    Assert.IsFalse(e.CustomSchemas[TestSchemaName].ContainsKey("TestMVString"));
                }
            }
            finally
            {
                if (u?.Id != null)
                {
                    UnitTestControl.TestParameters.UsersService.Delete(u.Id);
                }
            }
        }
        private static void TestMultivalueRoundTrip <T1, T2>(string fieldName, IList <T1> expectedGoogleValue, IList <T2> expectedFimValues, T1 valueToAdd)
        {
            string testAttributeName            = $"{UserCustomSchemaTests.TestSchemaName}_{fieldName}";
            AdapterCustomSchemaField schemaItem = GetFieldAdapter(testAttributeName);

            User u = DeserializeTestUser();

            CSEntryChange x = CSEntryChange.Create();

            x.ObjectModificationType = ObjectModificationType.Add;

            // Convert the user object into a series of attribute changes

            IList <AttributeChange> result = schemaItem.CreateAttributeChanges(x.DN, ObjectModificationType.Add, u).ToList();

            AttributeChange change = result.FirstOrDefault(t => t.Name == testAttributeName);

            Assert.IsNotNull(change);
            CollectionAssert.AreEquivalent(expectedFimValues.ToArray(), change.GetValueAdds <T2>().ToArray());

            // Reverse the process and convert the attribute changes onto a new user
            x.AttributeChanges.Add(change);

            User ux = new User();

            schemaItem.UpdateField(x, ux);

            List <T1> returnedValues = UserCustomSchemaTests.GetReturnedValues <T1>(fieldName, ux);

            CollectionAssert.AreEquivalent(expectedGoogleValue.ToArray(), returnedValues);

            // Add a value
            ValueChange add1 = ValueChange.CreateValueAdd(valueToAdd);

            change = AttributeChange.CreateAttributeUpdate(testAttributeName, new List <ValueChange>()
            {
                add1
            });
            x = CSEntryChange.Create();
            x.ObjectModificationType = ObjectModificationType.Update;
            x.AttributeChanges.Add(change);
            schemaItem.UpdateField(x, ux);

            returnedValues = UserCustomSchemaTests.GetReturnedValues <T1>(fieldName, ux);

            IList <object> e = expectedGoogleValue.Cast <object>().ToList();

            e.Add(valueToAdd);

            CollectionAssert.AreEquivalent(e.ToArray(), returnedValues);

            // Remove a value

            ValueChange delete1 = ValueChange.CreateValueDelete(valueToAdd);

            change = AttributeChange.CreateAttributeUpdate(testAttributeName, new List <ValueChange>()
            {
                delete1
            });
            x = CSEntryChange.Create();
            x.ObjectModificationType = ObjectModificationType.Update;
            x.AttributeChanges.Add(change);
            schemaItem.UpdateField(x, ux);

            returnedValues = UserCustomSchemaTests.GetReturnedValues <T1>(fieldName, ux);

            CollectionAssert.AreEquivalent(expectedGoogleValue.ToArray(), returnedValues);

            // Delete the attribute

            change = AttributeChange.CreateAttributeDelete(testAttributeName);
            x      = CSEntryChange.Create();
            x.ObjectModificationType = ObjectModificationType.Update;
            x.AttributeChanges.Add(change);

            schemaItem.UpdateField(x, ux);
            Assert.AreEqual(Constants.NullValuePlaceholder, (string)ux.CustomSchemas[TestSchemaName][fieldName]);
        }
        private AttributeChange ApplySendAsChanges(CSEntryChange csentry)
        {
            this.GetUserSendAsChanges(csentry, out IList <string> adds, out IList <string> deletes);

            if (adds.Count == 0 && deletes.Count == 0)
            {
                return(null);
            }

            AttributeChange     change       = null;
            IList <ValueChange> valueChanges = new List <ValueChange>();

            try
            {
                if (deletes != null)
                {
                    foreach (string delete in deletes)
                    {
                        Logger.WriteLine($"Removing send as address {delete}");
                        MailAddress address = new MailAddress(delete);
                        this.config.GmailService.RemoveSendAs(csentry.DN, address.Address);
                        valueChanges.Add(ValueChange.CreateValueDelete(delete));
                    }
                }

                foreach (string add in adds)
                {
                    Logger.WriteLine($"Adding send as address {add}");

                    MailAddress address = new MailAddress(add);
                    SendAs      sendAs  = new SendAs
                    {
                        DisplayName = address.DisplayName,
                        SendAsEmail = address.Address,
                    };

                    if (this.config.MakeNewSendAsAddressesDefault)
                    {
                        sendAs.IsDefault = true;
                    }

                    this.config.GmailService.AddSendAs(csentry.DN, sendAs);
                    valueChanges.Add(ValueChange.CreateValueAdd(add));
                }
            }
            finally
            {
                if (valueChanges.Count > 0)
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        change = AttributeChange.CreateAttributeUpdate(this.attributeName, valueChanges);
                    }
                    else
                    {
                        change = AttributeChange.CreateAttributeAdd(this.attributeName, valueChanges.Where(u => u.ModificationType == ValueModificationType.Add).Select(t => t.Value).ToList());
                    }
                }
            }

            return(change);
        }
        private AttributeChange ApplyUserAliasChanges(CSEntryChange csentry, User user)
        {
            this.GetUserAliasChanges(csentry, out IList <string> aliasAdds, out IList <string> aliasDeletes, out bool deletingAll);

            if (aliasAdds.Count == 0 && aliasDeletes.Count == 0)
            {
                return(null);
            }

            AttributeChange     change       = null;
            IList <ValueChange> valueChanges = new List <ValueChange>();

            try
            {
                if (aliasDeletes != null)
                {
                    foreach (string alias in aliasDeletes)
                    {
                        if (user.PrimaryEmail == null || !user.PrimaryEmail.Equals(alias, StringComparison.CurrentCultureIgnoreCase))
                        {
                            Logger.WriteLine($"Removing alias {alias}", LogLevel.Debug);

                            try
                            {
                                this.config.UsersService.RemoveAlias(csentry.DN, alias);
                            }
                            catch (Google.GoogleApiException ex)
                            {
                                if (ex.HttpStatusCode == System.Net.HttpStatusCode.NotFound ||
                                    ex.Error?.Message == "Invalid Input: resource_id")
                                {
                                    Logger.WriteLine($"Alias {alias} does not exist on object");
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }

                        valueChanges.Add(ValueChange.CreateValueDelete(alias));
                    }
                }

                foreach (string alias in aliasAdds)
                {
                    if (!csentry.DN.Equals(alias, StringComparison.CurrentCultureIgnoreCase))
                    {
                        Logger.WriteLine($"Adding alias {alias}", LogLevel.Debug);
                        this.config.UsersService.AddAlias(csentry.DN, alias);
                    }

                    valueChanges.Add(ValueChange.CreateValueAdd(alias));
                }
            }
            finally
            {
                if (valueChanges.Count > 0)
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        if (deletingAll && valueChanges.Count == aliasDeletes?.Count)
                        {
                            change = AttributeChange.CreateAttributeDelete("aliases");
                        }
                        else
                        {
                            change = AttributeChange.CreateAttributeUpdate("aliases", valueChanges);
                        }
                    }
                    else
                    {
                        change = AttributeChange.CreateAttributeAdd("aliases", valueChanges.Where(u => u.ModificationType == ValueModificationType.Add).Select(t => t.Value).ToList());
                    }
                }
            }

            return(change);
        }
Beispiel #22
0
        private AttributeChange ApplyGroupAliasChanges(CSEntryChange csentry, Group group)
        {
            this.GetGroupAliasChanges(csentry, out IList <string> aliasAdds, out IList <string> aliasDeletes, out bool deletingAll);

            if (aliasAdds.Count == 0 && aliasDeletes.Count == 0)
            {
                return(null);
            }

            AttributeChange     change       = null;
            IList <ValueChange> valueChanges = new List <ValueChange>();

            try
            {
                if (aliasDeletes != null)
                {
                    foreach (string alias in aliasDeletes)
                    {
                        if (!group.Email.Equals(alias, StringComparison.CurrentCultureIgnoreCase))
                        {
                            Logger.WriteLine($"Removing alias {alias}", LogLevel.Debug);
                            this.config.GroupsService.RemoveAlias(csentry.DN, alias);
                        }

                        valueChanges.Add(ValueChange.CreateValueDelete(alias));
                    }
                }

                foreach (string alias in aliasAdds)
                {
                    if (!csentry.DN.Equals(alias, StringComparison.CurrentCultureIgnoreCase))
                    {
                        Logger.WriteLine($"Adding alias {alias}", LogLevel.Debug);
                        this.config.GroupsService.AddAlias(csentry.DN, alias);
                    }

                    valueChanges.Add(ValueChange.CreateValueAdd(alias));
                }
            }
            finally
            {
                if (valueChanges.Count > 0)
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        if (deletingAll && valueChanges.Count == aliasDeletes?.Count)
                        {
                            change = AttributeChange.CreateAttributeDelete("aliases");
                        }
                        else
                        {
                            change = AttributeChange.CreateAttributeUpdate("aliases", valueChanges);
                        }
                    }
                    else
                    {
                        change = AttributeChange.CreateAttributeAdd("aliases", valueChanges.Where(u => u.ModificationType == ValueModificationType.Add).Select(t => t.Value).ToList());
                    }
                }
            }

            return(change);
        }