Esempio n. 1
0
        public void ApplicationCount()
        {
            var userId        = Guid.NewGuid();
            var table         = new AzureTable <ApplicationInfoData>(CloudStorageAccount.DevelopmentStorageAccount);
            var random        = new Random();
            var expectedCount = random.Next(15);

            for (int i = 0; i < expectedCount; i++)
            {
                var ua = new ApplicationInfoData(Guid.NewGuid())
                {
                    Owner = userId,
                };

                table.AddOrUpdateEntity(ua);
            }

            var user = new User()
            {
                Identifier = userId,
            };

            var appCore = new ApplicationCore();
            var count   = appCore.ApplicationCount(user);

            Assert.AreEqual <int>(expectedCount, count);
        }
Esempio n. 2
0
        public void AddOrUpdateEntitiesValidationThrows()
        {
            var table    = new AzureTable <EntityWithDataStore>(CloudStorageAccount.DevelopmentStorageAccount, new TestStoreValidator());
            var entities = new List <EntityWithDataStore>();
            var entity   = new EntityWithDataStore()
            {
                PartitionKey = StringHelper.ValidString(),
                RowKey       = StringHelper.ValidString(),
                ToTest       = 11
            };

            entities.Add(entity);
            entity = new EntityWithDataStore()
            {
                PartitionKey = StringHelper.ValidString(),
                RowKey       = StringHelper.ValidString(),
                ToTest       = -100
            };
            entities.Add(entity);
            entity = new EntityWithDataStore()
            {
                PartitionKey = StringHelper.ValidString(),
                RowKey       = StringHelper.ValidString(),
                ToTest       = -22
            };
            entities.Add(entity);
            table.AddOrUpdateEntity(entities);
        }
Esempio n. 3
0
        public ContactGroup Save(ContactGroup contact)
        {
            Contract.Requires <ArgumentNullException>(null != contact);
            Contract.Requires <ArgumentNullException>(null != contact.Owner);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != contact.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != contact.Owner.Identifier);

            using (new PerformanceMonitor())
            {
                var row   = contact.Convert();
                var table = new AzureTable <ContactGroupRow>(ServerConfiguration.Default, new ContactGroupRowValidator());
                var data  = table.QueryBy(row.PartitionKey, row.RowKey);

                if (null == data)
                {
                    table.AddEntity(row);
                }
                else
                {
                    data.Name = row.Name;
                    table.AddOrUpdateEntity(data);

                    row = data;
                }

                return(row.Convert());
            }
        }
Esempio n. 4
0
        public static PayPalPaymentConfirmation ProcessResponse(PayPalPaymentConfirmation payment)
        {
            Contract.Requires <ArgumentNullException>(null != payment);

            if (string.IsNullOrWhiteSpace(payment.Response))
            {
                log.Log("Empty response returned from PayPal.");
            }
            else if (payment.Response.StartsWith("SUCCESS", StringComparison.InvariantCultureIgnoreCase))
            {
                payment.Successful = true;

                payment = PaymentCore.ProcessSuccessfulResponse(payment);

                var preferenceTable = new AzureTable <UserPreferenceRow>(ServerConfiguration.Default);
                var preference      = preferenceTable.QueryBy(payment.Application.Identifier.ToString(), payment.User.Identifier.ToString());

                preference = preference ?? new UserPreferenceRow(payment.Application.Identifier, payment.User.Identifier);

                preference.MaxiumAllowedApplications = preference.MaxiumAllowedApplications.HasValue ? preference.MaxiumAllowedApplications + 1 : 1;
                preferenceTable.AddOrUpdateEntity(preference);
            }
            else
            {
                payment.Successful = false;

                log.Log("Error Accepting payment response.");
            }

            var table = new AzureTable <PayPalPaymentConfirmationRow>(ServerConfiguration.Default);

            table.AddEntity(payment.Convert());

            return(payment);
        }
Esempio n. 5
0
        public void AddUpdateEntitiesDeleteEntities()
        {
            Random random = new Random();
            var    table  = new AzureTable <EntityWithDataStore>(CloudStorageAccount.DevelopmentStorageAccount, new TestStoreValidator());

            table.DeleteIfExist();
            table.EnsureExist();
            var entities = new List <EntityWithDataStore>();
            var entity   = new EntityWithDataStore()
            {
                RowKey       = Guid.NewGuid().ToString(),
                PartitionKey = Guid.NewGuid().ToString(),
                ToTest       = random.Next()
            };

            entities.Add(entity);
            entity = new EntityWithDataStore()
            {
                RowKey       = Guid.NewGuid().ToString(),
                PartitionKey = Guid.NewGuid().ToString(),
                ToTest       = random.Next()
            };
            entities.Add(entity);
            entity = new EntityWithDataStore()
            {
                RowKey       = Guid.NewGuid().ToString(),
                PartitionKey = Guid.NewGuid().ToString(),
                ToTest       = random.Next()
            };
            entities.Add(entity);
            table.AddOrUpdateEntity(entities);

            var returned = from data in table.Query
                           select data;

            Assert.AreEqual <int>(entities.Count, returned.ToList().Count);

            foreach (var e in entities)
            {
                e.ToTest = random.Next();
            }

            table.AddOrUpdateEntity(entities);

            table.DeleteEntity(entities);
        }
Esempio n. 6
0
        /// <summary>
        /// Save Profile Page
        /// </summary>
        /// <param name="page">Page</param>
        /// <returns>Profile Page</returns>
        public ProfilePage Save(ProfilePage page)
        {
            Contract.Requires <ArgumentNullException>(null != page);
            Contract.Requires <ArgumentException>(Guid.Empty != page.ApplicationIdentifier);
            Contract.Requires <ArgumentException>(Guid.Empty != page.OwnerIdentifier);
            Contract.Requires <ArgumentException>(!string.IsNullOrWhiteSpace(page.Handle));

            var row = page.Convert();

            var            table      = new AzureTable <UserProfileRow>(ServerConfiguration.Default);
            UserProfileRow oldProfile = null;
            UserProfileRow newProfile = null;

            try
            {
                oldProfile = table.QueryBy(row.PartitionKey, page.ExistingHandle.ToLowerInvariant());
            }
            catch (Exception ex)
            {
                Logging.Log(ex, EventTypes.Warning, (int)ServiceFault.ProfileDoesntExist);
            }

            try
            {
                newProfile = table.QueryBy(row.PartitionKey, page.Handle.ToLowerInvariant());
            }
            catch (Exception ex)
            {
                Logging.Log(ex, EventTypes.Warning, (int)ServiceFault.ProfileDoesntExist);
            }

            if (null == newProfile)
            {
                table.AddEntity(row);
            }
            else if (newProfile.OwnerIdentifier == row.OwnerIdentifier)
            {
                table.AddOrUpdateEntity(row);
            }
            else
            {
                throw new InvalidOperationException("User doesn't own profile");
            }

            if (null != oldProfile)
            {
                if (oldProfile.OwnerIdentifier == row.OwnerIdentifier)
                {
                    table.DeleteBy(oldProfile.PartitionKey, oldProfile.RowKey);
                }
                else
                {
                    throw new InvalidOperationException("User doesn't own profile");
                }
            }

            return(page);
        }
        /// <summary>
        /// Execute
        /// </summary>
        protected override void Execute(object state)
        {
            using (new PerformanceMonitor())
            {
                try
                {
                    var item = new DataManagerLog(this.GetType());

                    var table  = new AzureTable <DataManagerLog>(ServerConfiguration.Default);
                    var latest = (from data in table.QueryByPartition(item.PartitionKey).ToList()
                                  orderby data.StartTime
                                  select data).FirstOrDefault();

                    // Check if there's any task to execute
                    var performTask = null == latest || (latest.CompletionTime.HasValue ?
                                                         DateTime.UtcNow.Subtract(latest.CompletionTime.Value) >= period || !latest.Successful :
                                                         DateTime.UtcNow.Subtract(latest.StartTime) >= retryInterval);

                    if (performTask)
                    {
                        log.Log(string.Format("{0} Task Started.", DateTime.UtcNow));

                        item.StartTime = DateTime.UtcNow;

                        table.AddEntity(item);

                        // Start the backup
                        try
                        {
                            this.Execute();
                            item.Successful = true;
                        }
                        catch (Exception ex)
                        {
                            log.Log(ex, EventTypes.Critical, (int)ServiceFault.Unknown);
                            item.Successful = false;
                        }
                        finally
                        {
                            item.CompletionTime = DateTime.UtcNow;
                        }

                        // Update entry in table
                        table.AddOrUpdateEntity(item);
                        log.Log(string.Format("{0} Task Completed. Success: {1}", DateTime.UtcNow, item.Successful));
                    }
                    else
                    {
                        log.Log(string.Format("{0} No Action Required.", DateTime.UtcNow));
                    }
                }
                catch (Exception ex)
                {
                    log.Log(ex, EventTypes.Error, (int)ServiceFault.Unknown);
                }
            }
        }
Esempio n. 8
0
        public void AddOrUpdateEntityNullPartitionKey()
        {
            var table  = new AzureTable <Entity>(CloudStorageAccount.DevelopmentStorageAccount);
            var entity = new Entity()
            {
                PartitionKey = null,
                RowKey       = StringHelper.ValidString()
            };

            table.AddOrUpdateEntity(entity);
        }
Esempio n. 9
0
        public void AddOrUpdateEntityInvalidRowKey()
        {
            var table  = new AzureTable <Entity>(CloudStorageAccount.DevelopmentStorageAccount);
            var entity = new Entity()
            {
                RowKey       = StringHelper.NullEmptyWhiteSpace(),
                PartitionKey = StringHelper.ValidString()
            };

            table.AddOrUpdateEntity(entity);
        }
Esempio n. 10
0
        public void AddUpdateEntityDeleteEntity()
        {
            Random random = new Random();
            var    table  = new AzureTable <EntityWithDataStore>(CloudStorageAccount.DevelopmentStorageAccount, new TestStoreValidator());

            table.EnsureExist();
            var entity = new EntityWithDataStore()
            {
                RowKey       = Guid.NewGuid().ToString(),
                PartitionKey = Guid.NewGuid().ToString(),
                ToTest       = random.Next()
            };

            table.AddOrUpdateEntity(entity);

            var returned = table.QueryBy(entity.PartitionKey, entity.RowKey);

            Assert.IsNotNull(returned);
            Assert.AreEqual <string>(entity.PartitionKey, returned.PartitionKey);
            Assert.AreEqual <string>(entity.RowKey, returned.RowKey);
            Assert.AreEqual <int>(entity.ToTest, returned.ToTest);

            entity.ToTest = random.Next();

            table.AddOrUpdateEntity(entity);

            var updated = (from data in table.Query
                           where data.PartitionKey == entity.PartitionKey && data.RowKey == entity.RowKey
                           select data).SingleOrDefault();

            Assert.IsNotNull(returned);
            Assert.AreEqual <string>(entity.PartitionKey, updated.PartitionKey);
            Assert.AreEqual <string>(entity.RowKey, updated.RowKey);
            Assert.AreEqual <int>(entity.ToTest, updated.ToTest);

            table.DeleteEntity(entity);
        }
Esempio n. 11
0
        public Configuration Save(Configuration configuration, UserApplication editor)
        {
            Contract.Requires(null != configuration);
            Contract.Requires(null != configuration.Token);
            Contract.Requires(null != editor);
            Contract.Requires(Guid.Empty != configuration.Token.ApplicationId);
            Contract.Requires(Guid.Empty != editor.User.Identifier);
            Contract.Requires(Guid.Empty != editor.Application.Identifier);
            Contract.Requires(!string.IsNullOrWhiteSpace(configuration.Value));
            Contract.Requires(!string.IsNullOrWhiteSpace(configuration.Key));

            using (new PerformanceMonitor())
            {
                var app = new Abc.Services.Contracts.Application()
                {
                    Identifier = configuration.Token.ApplicationId,
                };

                if (!this.UserIsAssociated(editor, app))
                {
                    throw new SecurityException("Editor is not associated with Application.");
                }

                var table  = new AzureTable <ApplicationConfiguration>(ServerConfiguration.Default);
                var save   = configuration.Convert();
                var exists = table.QueryBy(save.PartitionKey, save.RowKey);

                save.LastUpdatedBy = editor.User.Identifier;
                save.LastUpdatedOn = DateTime.UtcNow;
                if (null == exists)
                {
                    save.CreatedBy = editor.User.Identifier;
                    save.CreatedOn = DateTime.UtcNow;
                    table.AddEntity(save);
                }
                else
                {
                    save.CreatedOn = exists.CreatedOn;
                    save.CreatedBy = exists.CreatedBy;
                    table.AddOrUpdateEntity(save);
                }

                return(new Configuration()
                {
                    Key = configuration.Key,
                    Value = configuration.Value,
                });
            }
        }
Esempio n. 12
0
        public UserApplication Save(UserApplication data, UserApplication editor)
        {
            Contract.Requires <ArgumentNullException>(null != data);
            Contract.Requires <ArgumentNullException>(null != editor);
            Contract.Requires <ArgumentNullException>(null != editor.Application);
            Contract.Requires <ArgumentNullException>(null != editor.User);
            Contract.Requires <ArgumentNullException>(null != data.Application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier);

            Contract.Ensures(Contract.Result <UserApplication>() != null);

            using (new PerformanceMonitor())
            {
                if (!this.UserIsAssociated(editor, data.Application))
                {
                    throw new SecurityException("Editor is not associated with Application.");
                }

                var table = new AzureTable <UserApplicationData>(ServerConfiguration.Default, new UserApplicationValidation());

                var existing = table.QueryBy(data.User.Identifier.ToString(), data.Application.Identifier.ToString());

                if (null == existing)
                {
                    existing               = data.Convert();
                    existing.CreatedOn     = DateTime.UtcNow;
                    existing.LastUpdatedOn = DateTime.UtcNow;
                    existing.CreatedBy     = editor.User.Identifier;
                    existing.LastUpdatedBy = editor.User.Identifier;
                    table.AddEntity(existing);
                }
                else
                {
                    var createdBy = existing.CreatedBy;
                    var createdOn = existing.CreatedOn;
                    existing               = data.Convert();
                    existing.CreatedBy     = createdBy;
                    existing.CreatedOn     = createdOn;
                    existing.LastUpdatedOn = DateTime.UtcNow;
                    existing.LastUpdatedBy = editor.User.Identifier;

                    table.AddOrUpdateEntity(existing);
                }

                return(data);
            }
        }
Esempio n. 13
0
        public void AddOrUpdateEntitiesEntityNull()
        {
            var table    = new AzureTable <Entity>(CloudStorageAccount.DevelopmentStorageAccount);
            var entities = new List <Entity>();
            var entity   = new Entity()
            {
                PartitionKey = StringHelper.ValidString(),
                RowKey       = StringHelper.ValidString()
            };

            entities.Add(entity);
            entities.Add(null);
            entity = new Entity()
            {
                PartitionKey = StringHelper.ValidString(),
                RowKey       = StringHelper.ValidString()
            };
            entities.Add(entity);
            table.AddOrUpdateEntity(entities);
        }
Esempio n. 14
0
        public void DeleteByRowSingle()
        {
            var random = new Random();
            var table  = new AzureTable <EntityWithDataStore>(CloudStorageAccount.DevelopmentStorageAccount, new TestStoreValidator());

            table.EnsureExist();
            var entity = new EntityWithDataStore()
            {
                RowKey       = Guid.NewGuid().ToString(),
                PartitionKey = Guid.NewGuid().ToString(),
                ToTest       = random.Next()
            };

            table.AddOrUpdateEntity(entity);

            table.DeleteByRow(entity.RowKey);

            var returned = table.QueryByPartition(entity.PartitionKey);

            Assert.IsNotNull(returned);
            var list = returned.ToList();

            Assert.AreEqual <int>(0, list.Count());
        }
Esempio n. 15
0
        public UserPreference Save(UserPreference userPreference)
        {
            Contract.Requires <ArgumentNullException>(null != userPreference);
            Contract.Requires <ArgumentNullException>(null != userPreference.Application);
            Contract.Requires <ArgumentNullException>(null != userPreference.User);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != userPreference.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != userPreference.User.Identifier);

            using (new PerformanceMonitor())
            {
                var table  = new AzureTable <UserPreferenceRow>(ServerConfiguration.Default);
                var exists = table.QueryBy(userPreference.Application.Identifier.ToString(), userPreference.User.Identifier.ToString());

                if (null == exists)
                {
                    table.AddEntity(userPreference.Convert());
                }
                else
                {
                    if (null != userPreference.CurrentApplication && Guid.Empty != userPreference.CurrentApplication.Identifier)
                    {
                        exists.CurrentApplicationIdentifier = userPreference.CurrentApplication.Identifier;
                    }

                    if (null != userPreference.TimeZone)
                    {
                        exists.TimeZone = userPreference.TimeZone.ToSerializedString();
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.TwitterHandle))
                    {
                        exists.TwitterHandle = userPreference.TwitterHandle;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.GitHubHandle))
                    {
                        exists.GitHubHandle = userPreference.GitHubHandle;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.AbcHandle))
                    {
                        userPreference.AbcHandle = Regex.Replace(userPreference.AbcHandle, @"\s", string.Empty);
                        if (!string.IsNullOrWhiteSpace(userPreference.AbcHandle))
                        {
                            if (exists.AbcHandle != userPreference.AbcHandle)
                            {
                                var profile = new ProfilePage()
                                {
                                    ApplicationIdentifier = userPreference.Application.Identifier,
                                    OwnerIdentifier       = userPreference.User.Identifier,
                                    ExistingHandle        = exists.AbcHandle,
                                    Handle = userPreference.AbcHandle,
                                };

                                try
                                {
                                    profile = this.Save(profile);

                                    exists.AbcHandle = profile.Handle;
                                }
                                catch (Exception ex)
                                {
                                    Logging.Log(ex, EventTypes.Warning, (int)ServiceFault.CannotCreateProfile);
                                }
                            }
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.City))
                    {
                        exists.City = userPreference.City;
                    }

                    if (!string.IsNullOrWhiteSpace(userPreference.Country))
                    {
                        exists.Country = userPreference.Country;
                    }

                    exists.MaxiumAllowedApplications = userPreference.MaximumAllowedApplications ?? exists.MaxiumAllowedApplications ?? 1;

                    table.AddOrUpdateEntity(exists);
                }

                return(this.Get(userPreference));
            }
        }
Esempio n. 16
0
        public ApplicationInformation Save(ApplicationInformation data, UserApplication editor, Abc.Services.Contracts.Application application)
        {
            Contract.Requires <ArgumentNullException>(null != data);
            Contract.Requires <ArgumentNullException>(null != editor);
            Contract.Requires <ArgumentNullException>(null != editor.Application);
            Contract.Requires <ArgumentNullException>(null != editor.User);
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);

            Contract.Ensures(Contract.Result <ApplicationInformation>() != null);

            using (new PerformanceMonitor())
            {
                var source        = new DomainSource();
                var editorData    = source.GetUserById(application.Identifier, editor.User.Identifier);
                var serverDetails = ((IConvert <Details>)data).Convert();
                if (data.IsNew)
                {
                    if (this.PermitApplicationCreation(application, editor.User))
                    {
                        if (data.IsNew)
                        {
                            serverDetails.IsValid = true;
                            app.Create(serverDetails);

                            if (userApplicationCache.ContainsKey(editor.User.Identifier))
                            {
                                userApplicationCache.Remove(editor.User.Identifier);
                            }
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to create an application without authorization.");
                    }
                }
                else
                {
                    if (editorData.RoleValue == (int)RoleType.Manager)
                    {
                        app.Update(serverDetails);

                        if (userApplicationCache.ContainsKey(editor.User.Identifier))
                        {
                            userApplicationCache.Remove(editor.User.Identifier);
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to update an application without authorization.");
                    }
                }

                var appInfoData = ((IConvert <ApplicationInfoData>)data).Convert();
                appInfoData.Load(editorData);

                var table = new AzureTable <ApplicationInfoData>(ServerConfiguration.Default, new ApplicationInfoValidator());

                var existing = this.Get(appInfoData);

                if (null != existing)
                {
                    existing.Environment = appInfoData.Environment;
                    existing.Name        = appInfoData.Name;
                    existing.Description = appInfoData.Description;
                    existing.Deleted     = appInfoData.Deleted;
                    existing.Active      = appInfoData.Active;
                    appInfoData          = existing;
                }
                else
                {
                    appInfoData.CreatedBy = editor.User.Identifier;
                    appInfoData.Owner     = editor.User.Identifier;
                    appInfoData.CreatedOn = DateTime.UtcNow;
                }

                appInfoData.LastUpdatedBy = editor.User.Identifier;
                appInfoData.LastUpdatedOn = DateTime.UtcNow;

                table.AddOrUpdateEntity(appInfoData);

                if (data.IsNew && editorData.RoleValue == (int)RoleType.Client)
                {
                    var save = new Abc.Services.Contracts.Application()
                    {
                        Identifier = serverDetails.ApplicationId,
                    };

                    var ua = new UserApplication()
                    {
                        Application = save,
                        User        = editor.User,
                        Deleted     = false,
                        Active      = true,
                    };

                    this.Save(ua, editor);
                }

                return(data);
            }
        }
Esempio n. 17
0
        public void AddOrUpdateEntitiesNull()
        {
            var table = new AzureTable <Entity>(CloudStorageAccount.DevelopmentStorageAccount);

            table.AddOrUpdateEntity((IEnumerable <Entity>)null);
        }