public void GiveAchievement_BadPersonDetails_Fails()
        {
            //give an achievement with incorrect person details.
            var  fakeContext  = new Mock <IDatabaseContext>();
            Guid key          = Guid.NewGuid();
            var  achievements = new[]
            {
                new Achievement
                {
                    Key = key,
                }
            };

            fakeContext.Setup(x => x.List <Achievement>())
            .Returns(achievements.AsQueryable());

            //this is the only person in the database
            var people = new[] { new Person {
                                     Id = 100
                                 } };

            fakeContext.Setup(x => x.List <Person>())
            .Returns(people.AsQueryable());

            AchievementManagement mng = new AchievementManagement(fakeContext.Object);
            var allocation            = new AchievementAllocation();

            allocation.AchievementKey = key;
            allocation.IsManual       = true;
            //this is the wrong person
            allocation.CustomPersonKey = "1";

            mng.GiveAchievement(allocation);
        }
        public void GiveAchievement_InactiveAchievement_Fails()
        {
            //give an achievement that is currently inactive
            var key         = Guid.NewGuid();
            var fakeContext = new Mock <IDatabaseContext>();
            var mng         = new AchievementManagement(fakeContext.Object);

            fakeContext.Setup(x => x.List <Achievement>()).Returns(
                new[]
            {
                new Achievement()
                {
                    Key    = key,
                    Season = new Season
                    {
                        EndDate   = DateTime.Now.AddDays(2),
                        IsActive  = true,
                        StartDate = DateTime.Now,
                    }
                }
            }.AsQueryable());

            var allocation = new AchievementAllocation
            {
                AchievementKey = key,
            };

            mng.GiveAchievement(allocation);
        }
        public void GiveAchievement_NonExistingAchievement_Fails()
        {
            var fakeContext = new Mock <IDatabaseContext>();
            var mng         = new AchievementManagement(fakeContext.Object);

            var allocation = new AchievementAllocation
            {
                AchievementKey = Guid.NewGuid(),
            };

            mng.GiveAchievement(allocation);
        }
        public void GiveAchievement_NoActiveSeasons_Fails()
        {
            var key         = Guid.NewGuid();
            var fakeContext = new Mock <IDatabaseContext>();
            var mng         = new AchievementManagement(fakeContext.Object);

            var season1 = new Season
            {
                StartDate    = DateTime.Now.AddDays(-100),
                EndDate      = DateTime.Now.AddDays(-88),
                IsActive     = true,
                Achievements = new []
                {
                    new Achievement
                    {
                        Key        = key,
                        IsApproved = true,
                    }
                }
            };
            var season2 = new Season
            {
                StartDate    = DateTime.Now.AddDays(-87),
                EndDate      = DateTime.Now.AddDays(-70),
                IsActive     = true,
                Achievements = new []
                {
                    new Achievement
                    {
                        Key        = key,
                        IsApproved = true,
                    }
                }
            };

            fakeContext.Setup(x => x.List <Season>()).Returns(
                new[]
            {
                season1, season2
            }.AsQueryable());

            var allocation = new AchievementAllocation()
            {
                AchievementKey = key,
            };

            mng.GiveAchievement(allocation);
        }
示例#5
0
        public void GiveAchievement(AchievementAllocation allocation)
        {
            //obvious check
            if (allocation == null)
            {
                throw new ArgumentNullException(nameof(allocation));
            }
            //do some not so obvious things to self diagnose
            allocation.Validate();

            //validate this key
            var key = Context.List <ApiKey>().Single(x => x.Key == allocation.ApiKey);

            if (!key.IsActive)
            {
                throw new UnauthorizedAccessException("Api Key has been disabled, please contact your administrator");
            }

            //get the achievement that matches the key
            IEnumerable <Achievement> achievements = key.AccountGroup.Achievements
                                                     .Where(x => x.Key == allocation.AchievementKey);
            //this gets the only active achievement that can be applied
            Season      activeSeason        = key.AccountGroup.CurrentActiveSeason;
            Achievement relevantAchievement = activeSeason.Achievements.SingleOrDefault(x => x.Key == allocation.AchievementKey);

            if (relevantAchievement == null)
            {
                throw new BusinessLogicException("There is no relevant achievement with the created date specified");
            }

            AccountGroupPerson targetPersonJoin = key.AccountGroup.AccountGroupPeople.SingleOrDefault(x => x.CustomKey == allocation.CustomPersonKey);

            if (targetPersonJoin == null)
            {
                throw new UnauthorizedAccessException("Person does not exist, have you setup their customKey correctly?");
            }

            GiveAchievement(targetPersonJoin, relevantAchievement, allocation.CreatedDate);
        }
        public void GiveAchievement_BadAccountGroupDetails_Fails()
        {
            //give an achievement to the incorrect account group
            var fakeContext = new Mock <IDatabaseContext>();
            var season      = new Season
            {
                Id = 10000,
            };
            Guid achievementKey = Guid.NewGuid();
            var  achievements   = new[]
            {
                new Achievement
                {
                    Key      = achievementKey,
                    Season   = season,
                    SeasonId = season.Id
                }
            };

            fakeContext.Setup(x => x.List <Achievement>())
            .Returns(achievements.AsQueryable());

            //this is the only person in the database
            var person = new Person {
                Id = 100
            };
            var accountGroup = new AccountGroup()
            {
            };

            person.AddToGroup(accountGroup, 12);
            var people = new[] { person };

            fakeContext.Setup(x => x.List <Person>())
            .Returns(people.AsQueryable());

            season.AccountGroup = accountGroup;

            Season currentSeason = new Season
            {
                IsActive     = true,
                StartDate    = DateTime.Now.AddDays(-1),
                EndDate      = DateTime.Now.AddDays(1),
                Achievements = new[]
                {
                    new Achievement
                    {
                        Key = achievementKey,
                    }
                }
            };
            string customKey = person.Id.ToString();
            Guid   apiKey    = Guid.NewGuid();

            fakeContext.Setup(x => x.List <ApiKey>()).Returns(new[]
            {
                new ApiKey
                {
                    Key          = apiKey,
                    IsActive     = true,
                    AccountGroup = new AccountGroup
                    {
                        Seasons      = new [] { currentSeason },
                        Achievements = new List <Achievement>()
                        {
                            new Achievement
                            {
                                Key = achievementKey,
                            }
                        },
                        AccountGroupPeople = new []
                        {
                            new AccountGroupPerson
                            {
                                CustomKey = customKey,
                                Person    = person
                            }
                        }
                    }
                }
            }.AsQueryable());

            AchievementManagement mng = new AchievementManagement(fakeContext.Object);
            var allocation            = new AchievementAllocation();

            allocation.CustomPersonKey = "no such key";
            allocation.AchievementKey  = achievementKey;
            allocation.ApiKey          = apiKey;

            mng.GiveAchievement(allocation);
        }