public void CreateNewAccountGroup(NewAccountGroup newAccountGroup)
        {
            if (newAccountGroup == null)
            {
                throw new ArgumentNullException(nameof(newAccountGroup));
            }

            if (String.IsNullOrEmpty(newAccountGroup.GroupName))
            {
                throw new BusinessLogicException("Input a group name");
            }

            if (String.IsNullOrEmpty(newAccountGroup.AdministratorUserName))
            {
                throw new BusinessLogicException("Who will you be known as?");
            }

            var accountGroup = new AccountGroup
            {
                CreatedDate = DateTime.Now,
                Name = newAccountGroup.GroupName,
            };

            var person = new Person
            {
                Name = newAccountGroup.AdministratorUserName,
                CreatedDate = DateTime.Now,
                Email = newAccountGroup.Email
            };

            Context.Add(person);
            Context.Add(accountGroup);
            Context.SaveChanges();
        }
        public void MyPermissions_MegaList_Works()
        {
            //person1, is disabled
            //person2 is the target source
            //person3 is just an average user across different companies
            //what is happening!!?!!??!!?!???
            Mock<IDatabaseContext> fakeContext = new Mock<IDatabaseContext>();
            var person1 = new Person { Id = 1 };
            var person2 = new Person { Id = 2 };
            var person3 = new Person { Id = 3 };

            var group1 = new AccountGroup { Id = 1 };
            var group2 = new AccountGroup { Id = 2 };
            var group3 = new AccountGroup { Id = 3, EndDate = DateTime.Today.AddDays(-1) };

            person1.AddToGroup(group1, 1);

            //person 2 is an admin user in group 2
            var p2g1 = person2.AddToGroup(group1, 2);
            p2g1.PermissionLevel = PermissionLevel.Admin;
            var p2g2 = person2.AddToGroup(group2, 3);
            p2g2.PermissionLevel = PermissionLevel.SuperAdmin;

            person3.AddToGroup(group1, 4);
            person3.AddToGroup(group2, 5);
            person3.AddToGroup(group3, 6);

            fakeContext.Setup(x => x.List<Person>()).Returns(new[] { person1, person2, person3 }.AsQueryable());

            //not sure what this is suppose to test
            var access1 = new FakeRestrictiveAccess(fakeContext.Object, 1);
            Assert.AreEqual(1, access1.MyPermissions.Count());

            //needs to return exactly an admin record and a superadmin record
            var access2 = new FakeRestrictiveAccess(fakeContext.Object, 2);
            Assert.IsTrue(access2.MyPermissions.Any(x => x.Value == PermissionLevel.Admin && x.Key.Id == 1));
            Assert.IsTrue(access2.MyPermissions.Any(x => x.Value == PermissionLevel.SuperAdmin && x.Key.Id == 2));
            Assert.IsFalse(access2.MyPermissions.Any(x => x.Value == PermissionLevel.Normal));
            Assert.IsFalse(access2.MyPermissions.Any(x => x.Value == PermissionLevel.Disable));

            //this is to test what's happening with disabled account groups
            var access3 = new FakeRestrictiveAccess(fakeContext.Object, 3);
            Assert.AreEqual(2, access3.MyPermissions.Count());
        }
        public void ActivateSeason_InvalidStartDate_ThrowsException()
        {
            var fakeContext = new Mock<IDatabaseContext>();
            Person person = new Person()
            {
                Id = 100
            };

            AccountGroupPerson personJoin = new AccountGroupPerson()
            {
                Person = person,
                PersonId = person.Id,
                PermissionLevel = PermissionLevel.Admin
            };
            AccountGroup accountGroup = new AccountGroup()
            {
                Id = 1000,
                AccountGroupPeople = new List<AccountGroupPerson>()
                {
                    personJoin,
                }
            };
            personJoin.AccountGroup = accountGroup;
            personJoin.AccountGroupId = accountGroup.Id;
            person.AccountGroupPeople = new[] { personJoin };
            Season season = new Season
            {
                Id = 2,
                IsActive = false,
                AccountGroup = accountGroup,
                StartDate = null,
                EndDate = DateTime.Today,
                AccountGroupId = accountGroup.Id,
            };

            fakeContext.Setup(x => x.List<Season>()).Returns(new[] { season }.AsQueryable());
            fakeContext.Setup(x => x.List<Person>()).Returns(new[] { person }.AsQueryable());
            SeasonManagement management = new SeasonManagement(fakeContext.Object, 100);
            management.ActivateSeason(2);
        }
        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);
        }