Beispiel #1
0
        public async Task ThrowWhen_User_IsJury()
        {
            var options = Utils.GetOptions(nameof(ThrowWhen_User_IsJury));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Juries.AddRangeAsync(Utils.SeedJuries());

                await arrContext.SaveChangesAsync();

                arrContext.Contests.First().IsOpen = false;
                await arrContext.SaveChangesAsync();

                var userToInvite = arrContext.Users.Skip(8).First();
                userService.Setup(u => u.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToInvite));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var userToInvite = actContext.Users.Skip(8).First().UserName;
                var sut          = new ContestService(actContext, contextAccessor, userService.Object, categoryService, userManager, signManager);

                await Assert.ThrowsExceptionAsync <ArgumentException>(() =>
                                                                      sut.InviteAsync(actContext.Contests.Last().Name, userToInvite));
            }
        }
        public async Task GetReviewForSpecificUser()
        {
            var options = Utils.GetOptions(nameof(GetReviewForSpecificUser));
            var userStore = new Mock<IUserStore<User>>();
            var userManager = new Mock<UserManager<User>>(userStore.Object, null, null, null,
                null, null, null, null, null);
            var contextAccessor = new Mock<IHttpContextAccessor>();
            var userPrincipalFactory = new Mock<IUserClaimsPrincipalFactory<User>>().Object;
            var signManager = new Mock<SignInManager<User>>(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock<HttpContext>();
            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var userService = new Mock<IUserService>();
            var photoService = new Mock<IPhotoService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());
                await arrContext.Reviews.AddRangeAsync(Utils.SeedReviews());
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());
                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var userToGet = await actContext.Users.Skip(2).FirstAsync();
                userManager.Setup(x => x.GetUserName(signManager.Context.User)).Returns(userToGet.UserName);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(userToGet));

                var sut = new ReviewService(actContext, photoService.Object, userService.Object, contextAccessor.Object, userManager.Object, signManager);
                var result = await sut.GetForUserAsync(userToGet.UserName);
                var reviews = await actContext.Reviews.Include(r => r.Photo).Include(r => r.Evaluator).Where(r=>r.Photo.UserId == userToGet.Id).ToListAsync();
                Assert.AreEqual(result.Count(), reviews.Count());
            }
        }
Beispiel #3
0
        public async Task Throw_When_InvalidPhotoID()
        {
            var options = Utils.GetOptions(nameof(Throw_When_InvalidPhotoID));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var contestService       = new Mock <IContestService>();
            var userService          = new Mock <IUserService>();
            var userContestService   = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.DeleteAsync(Guid.Parse("55a9993c-c499-459c-84a6-d1dd31c240fd")));
            }
        }
        public async Task Return_UserContests_Current()
        {
            var options = Utils.GetOptions(nameof(Return_UserContests_Current));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.SaveChangesAsync();

                var user = arrContext.Users.Skip(4).First();
                userManager.Setup(u => u.GetUserName(signManager.Context.User)).Returns(user.UserName);
                userService.Setup(u => u.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(user));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var contestToEnroll    = actContext.Contests.Last();
                var user               = actContext.Users.Skip(4).First();
                var allUserContests    = actContext.UserContests.Where(uc => uc.UserId == user.Id);
                var allUserContestsDTO = new List <ContestDTO>();
                foreach (var userContest in allUserContests)
                {
                    var contest = await actContext.Contests
                                  .Include(c => c.Category)
                                  .Include(c => c.Status)
                                  .FirstAsync(c => c.Id == userContest.ContestId && c.IsDeleted == false);

                    allUserContestsDTO.Add(new ContestDTO(contest));
                }
                var sut    = new ContestService(actContext, contextAccessor.Object, userService.Object, categoryService, userManager.Object, signManager);
                var result = await sut.GetByUserAsync("current");

                Assert.AreEqual(result.Count(), allUserContestsDTO.Where(c => c.Status != "Finished").Count());
                Assert.AreEqual(string.Join(",", allUserContestsDTO.Where(c => c.Status != "Finished")), string.Join(",", result));
            }
        }
        public async Task Return_AllOpenContests()
        {
            var options = Utils.GetOptions(nameof(Return_AllOpenContests));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>().Object;
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new ContestService(actContext, contextAccessor, userService, categoryService, userManager, signManager);
                var result = await sut.AllOpenViewAsync();

                Assert.AreEqual(actContext.Contests.Where(c => c.IsOpen == true).Count(), result.Count());
                Assert.AreEqual(string.Join(",", actContext.Contests.Where(c => c.IsOpen == true).Select(c => new ContestDTO(c))), string.Join(",", result));
            }
        }
Beispiel #6
0
        public async Task ReturnAllUserContests()
        {
            var options        = Utils.GetOptions(nameof(ReturnAllUserContests));
            var contestService = new Mock <IContestService>().Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new UserContestService(actContext, contestService);
                var result = await sut.GetAllUserContestsAsync();

                var userContest = await actContext.UserContests.ToListAsync();

                Assert.AreEqual(result.Count(), userContest.Count());
            }
        }
Beispiel #7
0
        public async Task ChangeRankToDictator()
        {
            var options = Utils.GetOptions(nameof(ChangeRankToDictator));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new UserService(actContext, userManager, signManager);
                var userToChangeRank = actContext.Users.Include(u => u.Rank).Skip(2).First();
                userToChangeRank.OverallPoints += 1100;
                await sut.ChangeRank();

                Assert.AreEqual(userToChangeRank.Rank.Name, "Wise and Benevolent Photo Dictator");
            }
        }
Beispiel #8
0
        public async Task DeleteUser()
        {
            var options     = Utils.GetOptions(nameof(DeleteUser));
            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new UserService(actContext, userManager, signManager);
                var result = await sut.DeleteAsync(actContext.Users.First().UserName);

                Assert.IsTrue(result);
                Assert.IsTrue(actContext.Users.First().IsDeleted);
            }
        }
        public async Task ReturnCorrectUser()
        {
            var options = Utils.GetOptions(nameof(ReturnCorrectUser));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut       = new UserService(actContext, userManager, signManager);
                var userToGet = actContext.Users.Last();
                var result    = await sut.GetUserByUsernameAsync(userToGet.UserName);

                Assert.AreEqual(userToGet, result);
                Assert.AreEqual(userToGet.Id, result.Id);
                Assert.IsInstanceOfType(result, typeof(User));
            }
        }
Beispiel #10
0
        public async Task DeleteReviewSuccessfully()
        {
            var options     = Utils.GetOptions(nameof(DeleteReviewSuccessfully));
            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var userService  = new Mock <IUserService>();
            var photoService = new Mock <IPhotoService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Reviews.AddRangeAsync(Utils.SeedReviews());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new ReviewService(actContext, photoService.Object, userService.Object, contextAccessor.Object, userManager.Object, signManager);

                var result = await sut.DeleteAsync(Guid.Parse("8198e13a-30cb-4f4b-99f0-acf31a70b02d"));

                Assert.IsTrue(result);
            }
        }
Beispiel #11
0
        public async Task Throw_When_InvalidIdOfPhoto()
        {
            var options = Utils.GetOptions(nameof(Throw_When_InvalidIdOfPhoto));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var contestService       = new Mock <IContestService>();
            var userService          = new Mock <IUserService>();
            var userContestService   = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetAsync(Guid.Parse("3c1996fe-f204-42b7-9ea9-03a7196968e8")));
            }
        }
Beispiel #12
0
        public async Task Throw_When_ReviewID_IsNotValid()
        {
            var options     = Utils.GetOptions(nameof(Throw_When_ReviewID_IsNotValid));
            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var userService  = new Mock <IUserService>();
            var photoService = new Mock <IPhotoService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new ReviewService(actContext, photoService.Object, userService.Object, contextAccessor.Object, userManager.Object, signManager);

                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.DeleteAsync(Guid.Parse("8198e13a-30cb-4f4b-99f0-acf31a70b02d")));
            }
        }
Beispiel #13
0
        public async Task Return_True_IfDeleted()
        {
            var options = Utils.GetOptions(nameof(Return_True_IfDeleted));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>().Object;
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new ContestService(actContext, contextAccessor, userService, categoryService, userManager, signManager);
                var result = await sut.DeleteAsync(actContext.Contests.First().Name);

                Assert.IsTrue(actContext.Contests.First().IsDeleted);
            }
        }
        public async Task KeepSameSecondName()
        {
            var options = Utils.GetOptions(nameof(KeepSameSecondName));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;
            var updateDTO            = new UpdateUserDTO()
            {
                FirstName = "John"
            };

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut          = new UserService(actContext, userManager, signManager);
                var userToUpdate = actContext.Users.Last();
                var result       = await sut.UpdateAsync(updateDTO, userToUpdate.UserName);

                var lastNameChecker = userToUpdate.LastName;
                Assert.AreEqual(updateDTO.FirstName, result.FirstName);
                Assert.AreEqual(lastNameChecker, result.LastName);
            }
        }
Beispiel #15
0
        public async Task ChangeStatusTo_Finished()
        {
            var options = Utils.GetOptions(nameof(ChangeStatusTo_Finished));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut     = new ContestService(actContext, contextAccessor, userService.Object, categoryService, userManager, signManager);
                var contest = actContext.Contests.Include(c => c.Status).First();
                contest.Phase1   = DateTime.UtcNow.AddDays(-5);
                contest.Phase2   = DateTime.UtcNow.AddHours(-5);
                contest.Finished = DateTime.UtcNow;
                await sut.ChangeStatus();

                Assert.AreEqual(contest.Status.Name, "Finished");
            }
        }
        public async Task GetAllReviewsBaseEntities()
        {
            var options     = Utils.GetOptions(nameof(GetAllReviewsBaseEntities));
            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var userService  = new Mock <IUserService>();
            var photoService = new Mock <IPhotoService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Reviews.AddRangeAsync(Utils.SeedReviews());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new ReviewService(actContext, photoService.Object, userService.Object, contextAccessor.Object, userManager.Object, signManager);
                var result = await sut.GetAllReviewsAsync();

                var reviews = await actContext.Reviews.ToListAsync();

                Assert.AreEqual(reviews.Count(), result.Count());
            }
        }
        public async Task Throw_When_InvalidUsername()
        {
            var options = Utils.GetOptions(nameof(Throw_When_InvalidUsername));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new UserService(actContext, userManager, signManager);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetUserByUsernameAsync(""));
            }
        }
        public async Task ThrowsWhen_Phase_IsWrong()
        {
            var options = Utils.GetOptions(nameof(ThrowsWhen_Phase_IsWrong));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>().Object;
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new ContestService(actContext, contextAccessor, userService, categoryService, userManager, signManager);

                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetAllOpenAsync("Wrong"));
            }
        }
Beispiel #19
0
        public async Task GetAllPhotosWithInfo()
        {
            var options = Utils.GetOptions(nameof(GetAllPhotosWithInfo));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService = new Mock <IContestService>();
            var userService    = new Mock <IUserService>();

            var userContestService = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Reviews.AddRangeAsync(Utils.SeedReviews());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var userToGet = await actContext.Users.Include(u => u.Rank).Skip(3).FirstAsync();

                userManager.Setup(x => x.GetUserName(signManager.Context.User)).Returns(userToGet.UserName);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToGet));
                var contest = await actContext.Contests.Include(c => c.Status).Include(c => c.Category).FirstOrDefaultAsync(c => c.Name == "Best look");

                contestService.Setup(x => x.FindContestByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(contest));
                var sut    = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                var result = await sut.GetAllWithCommentsAndScoreAsync("Best look");

                var photos = await actContext.Photos
                             .Include(p => p.User)
                             .Include(p => p.Contest)
                             .ThenInclude(c => c.Category)
                             .ToListAsync();

                Assert.AreEqual(photos.Count(), result.Count());
            }
        }
        public async Task GetAllPhotosForCertainUser()
        {
            var options = Utils.GetOptions(nameof(GetAllPhotosForCertainUser));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService     = new Mock <IContestService>();
            var userService        = new Mock <IUserService>();
            var userContestService = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();

                var userToGet = await arrContext.Users.Skip(2).FirstAsync();

                userManager.Setup(x => x.GetUserName(signManager.Context.User)).Returns(userToGet.UserName);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToGet));
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var userToGet = await actContext.Users.Skip(2).FirstAsync();

                var sut    = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                var result = await sut.GetPhotosForUserAsync();

                var photos = await actContext.Photos
                             .Include(p => p.User)
                             .Include(p => p.Contest)
                             .ThenInclude(c => c.Category)
                             .Include(p => p.Contest)
                             .ThenInclude(c => c.Status)
                             .Where(p => p.IsDeleted == false && p.UserId == userToGet.Id)
                             .Select(p => new PhotoDTO(p))
                             .ToListAsync();

                Assert.AreEqual(photos.Count(), result.Count());
                Assert.IsInstanceOfType(result, typeof(List <PhotoDTO>));
            }
        }
Beispiel #21
0
        public async Task Throw_When_UserInJury()
        {
            var options = Utils.GetOptions(nameof(Throw_When_UserInJury));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService = new Mock <IContestService>();
            var userService    = new Mock <IUserService>();

            var userContestService = new Mock <IUserContestService>();

            var newPhotoDTO = new Mock <NewPhotoDTO>().Object;

            newPhotoDTO.Title       = "New photo";
            newPhotoDTO.Description = "New description";
            newPhotoDTO.PhotoUrl    = "www.newphoto.com";
            newPhotoDTO.ContestName = "Birds";

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Juries.AddRangeAsync(Utils.SeedJuries());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();

                var userToGet = await arrContext.Users.FirstOrDefaultAsync(u => u.UserName == "*****@*****.**");

                userManager.Setup(x => x.GetUserName(signManager.Context.User)).Returns(userToGet.UserName);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToGet));
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var contest = await actContext.Contests.Include(c => c.Status).Include(c => c.Category).FirstOrDefaultAsync(c => c.Name == newPhotoDTO.ContestName);

                contestService.Setup(x => x.FindContestByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(contest));
                var userToGet = await actContext.Users.FirstOrDefaultAsync(u => u.UserName == "*****@*****.**");

                var sut = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.CreateAsync(newPhotoDTO));
            }
        }
Beispiel #22
0
        public async Task Throw_When_UserIsNotInContestApi()
        {
            var options = Utils.GetOptions(nameof(Throw_When_UserIsNotInContestApi));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService = new Mock <IContestService>();
            var userService    = new Mock <IUserService>();

            var userContestService = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Reviews.AddRangeAsync(Utils.SeedReviews());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var userToGet = await actContext.Users.Include(u => u.Rank).Skip(3).FirstAsync();

                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.NameIdentifier, userToGet.UserName.ToString()),
                };
                var identity        = new ClaimsIdentity(claims);
                var claimsPrincipal = new ClaimsPrincipal(identity);
                contextAccessor.Setup(x => x.HttpContext.User).Returns(claimsPrincipal);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToGet));
                var contest = await actContext.Contests.Include(c => c.Status).Include(c => c.Category).FirstOrDefaultAsync(c => c.Name == "Wild cats");

                contestService.Setup(x => x.FindContestByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(contest));
                var sut = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetAllWithCommentsAndScoreApiAsync("Wild cats"));
            }
        }
        public async Task ThrowWhen_User_AlreadyEnrolledApi()
        {
            var options = Utils.GetOptions(nameof(ThrowWhen_User_AlreadyEnrolledApi));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Juries.AddRangeAsync(Utils.SeedJuries());

                await arrContext.SaveChangesAsync();

                var userToEnroll = arrContext.Users.Skip(2).First();
                var claims       = new List <Claim>()
                {
                    new Claim(ClaimTypes.NameIdentifier, userToEnroll.UserName.ToString()),
                };
                var identity        = new ClaimsIdentity(claims);
                var claimsPrincipal = new ClaimsPrincipal(identity);
                contextAccessor.Setup(x => x.HttpContext.User).Returns(claimsPrincipal);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToEnroll));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var contestToEnroll = actContext.Contests.First();
                var userToEnroll    = actContext.Users.Skip(2).First();
                var sut             = new ContestService(actContext, contextAccessor.Object, userService.Object, categoryService, userManager.Object, signManager);

                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.EnrollApiAsync(contestToEnroll.Name));
            }
        }
Beispiel #24
0
        public async Task Return_UserContestsApi()
        {
            var options = Utils.GetOptions(nameof(Return_UserContestsApi));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.SaveChangesAsync();

                var userToEnroll = arrContext.Users.Skip(2).First();
                var claims       = new List <Claim>()
                {
                    new Claim(ClaimTypes.NameIdentifier, userToEnroll.UserName.ToString()),
                };
                var identity        = new ClaimsIdentity(claims);
                var claimsPrincipal = new ClaimsPrincipal(identity);
                contextAccessor.Setup(x => x.HttpContext.User).Returns(claimsPrincipal);
                userService.Setup(x => x.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToEnroll));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var userContests = actContext.UserContests.Include(uc => uc.Contest).Where(uc => uc.UserId == actContext.Users.Skip(2).First().Id);
                var contests     = userContests.Select(uc => uc.Contest);
                var sut          = new ContestService(actContext, contextAccessor.Object, userService.Object, categoryService, userManager.Object, signManager);
                var result       = await sut.GetUserContestsApiAsync();

                Assert.AreEqual(result.Count(), userContests.Count());
            }
        }
        public async Task UpdateTitleOnly()
        {
            var options = Utils.GetOptions(nameof(UpdateTitleOnly));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService = new Mock <IContestService>();
            var userService    = new Mock <IUserService>();

            var userContestService = new Mock <IUserContestService>();

            var updatePhotoDTO = new Mock <UpdatePhotoDTO>().Object;

            updatePhotoDTO.Title       = "New photo";
            updatePhotoDTO.Description = null;
            updatePhotoDTO.PhotoUrl    = null;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                var result = await sut.UpdateAsync(updatePhotoDTO, Guid.Parse("e165b91f-03bf-414e-88b7-c51b87775683"));

                Assert.AreEqual(result.Title, updatePhotoDTO.Title);
                Assert.AreEqual(result.Description, "Picture of a lion.");
                Assert.AreEqual(result.PhotoUrl, "/Images/lion.jpg");
                Assert.IsInstanceOfType(result, typeof(PhotoDTO));
            }
        }
Beispiel #26
0
        public async Task ReturnPhotoSuccesfully()
        {
            var options = Utils.GetOptions(nameof(ReturnPhotoSuccesfully));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var contestService       = new Mock <IContestService>();
            var userService          = new Mock <IUserService>();
            var userContestService   = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut   = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                var photo = await actContext.Photos
                            .Include(p => p.User)
                            .Include(p => p.Contest)
                            .ThenInclude(c => c.Category)
                            .Include(p => p.Contest)
                            .ThenInclude(c => c.Status)
                            .Where(p => p.IsDeleted == false)
                            .FirstOrDefaultAsync(p => p.Id == Guid.Parse("e165b91f-03bf-414e-88b7-c51b87775683"));

                var photoDTO = new PhotoDTO(photo);
                var result   = await sut.GetAsync(Guid.Parse("e165b91f-03bf-414e-88b7-c51b87775683"));

                Assert.AreEqual(photoDTO.Title, result.Title);
                Assert.AreEqual(photoDTO.Description, result.Description);
                Assert.AreEqual(photoDTO.Contest, result.Contest);
                Assert.AreEqual(photoDTO.Category, result.Category);
            }
        }
Beispiel #27
0
        public async Task Return_UpdatedContest()
        {
            var options          = Utils.GetOptions(nameof(Return_UpdatedContest));
            var updateContestDTO = new Mock <UpdateContestDTO>().Object;

            updateContestDTO.Name         = "UpdateTest";
            updateContestDTO.CategoryName = "Cars";
            updateContestDTO.IsOpen       = true;
            updateContestDTO.Phase1       = DateTime.Now.AddHours(2).ToString("dd.MM.yy HH:mm");
            updateContestDTO.Phase2       = DateTime.Now.AddHours(30).ToString("dd.MM.yy HH:mm");
            updateContestDTO.Finished     = DateTime.Now.AddHours(32).ToString("dd.MM.yy HH:mm");

            var categoryService = new Mock <ICategoryService>();
            var userService     = new Mock <IUserService>().Object;
            var userStore       = new Mock <IUserStore <User> >();
            var userManager     = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                                 null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;


            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.SaveChangesAsync();

                categoryService.Setup(c => c.FindCategoryByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(arrContext.Categories.First()));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new ContestService(actContext, contextAccessor, userService, categoryService.Object, userManager, signManager);
                var result = await sut.UpdateAsync(actContext.Contests.First().Name, updateContestDTO);

                Assert.AreEqual(updateContestDTO.Name, result.Name);
                Assert.AreEqual(updateContestDTO.Phase1, result.Phase1);
                Assert.AreEqual(updateContestDTO.Phase2, result.Phase2);
                Assert.AreEqual(updateContestDTO.Finished, result.Finished);
                Assert.AreEqual(updateContestDTO.CategoryName, result.Category);
            }
        }
        public async Task GetAllBaseEntityPhotos()
        {
            var options = Utils.GetOptions(nameof(GetAllBaseEntityPhotos));

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null);
            var contextAccessor      = new Mock <IHttpContextAccessor>();
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager.Object, contextAccessor.Object, userPrincipalFactory, null, null, null, null).Object;
            var context = new Mock <HttpContext>();

            contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
            var contestService = new Mock <IContestService>();
            var userService    = new Mock <IUserService>();

            var userContestService = new Mock <IUserContestService>();

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.SaveChangesAsync();
            };
            using (var actContext = new PhotoContestContext(options))
            {
                var sut    = new PhotoService(actContext, contextAccessor.Object, contestService.Object, userService.Object, userManager.Object, signManager, userContestService.Object);
                var result = await sut.GetAllBaseAsync();

                var photos = await actContext.Photos.Where(p => p.IsDeleted == false).ToListAsync();

                Assert.AreEqual(photos.Count(), result.Count());
                Assert.IsInstanceOfType(result, typeof(IEnumerable <Photo>));
            }
        }
Beispiel #29
0
        public async Task ThrowsWhen_Category_IsDeleted()
        {
            var options    = Utils.GetOptions(nameof(ThrowsWhen_Category_IsDeleted));
            var categories = Utils.SeedCategories();

            categories[0].IsDeleted = true;
            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.AddRangeAsync(categories);

                await arrContext.SaveChangesAsync();
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var sut = new CategoryService(actContext);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.FindCategoryByNameAsync(categories[0].Name));
            }
        }
        public async Task ReturnTrue_IfUser_ChosenSuccessfully()
        {
            var options = Utils.GetOptions(nameof(ReturnTrue_IfUser_ChosenSuccessfully));

            var categoryService = new Mock <ICategoryService>().Object;
            var userService     = new Mock <IUserService>();

            var userStore   = new Mock <IUserStore <User> >();
            var userManager = new Mock <UserManager <User> >(userStore.Object, null, null, null,
                                                             null, null, null, null, null).Object;
            var contextAccessor      = new Mock <IHttpContextAccessor>().Object;
            var userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >().Object;
            var signManager          = new Mock <SignInManager <User> >(userManager, contextAccessor, userPrincipalFactory, null, null, null, null).Object;

            using (var arrContext = new PhotoContestContext(options))
            {
                await arrContext.Users.AddRangeAsync(Utils.SeedUsers());

                await arrContext.Statuses.AddRangeAsync(Utils.SeedStatuses());

                await arrContext.Categories.AddRangeAsync(Utils.SeedCategories());

                await arrContext.Photos.AddRangeAsync(Utils.SeedPhotos());

                await arrContext.Contests.AddRangeAsync(Utils.SeedContests());

                await arrContext.UserContests.AddRangeAsync(Utils.SeedUserContests());

                await arrContext.Ranks.AddRangeAsync(Utils.SeedRanks());

                await arrContext.SaveChangesAsync();

                var userToChoose = arrContext.Users.Skip(8).First();
                userService.Setup(u => u.GetUserByUsernameAsync(It.IsAny <string>())).Returns(Task.FromResult(userToChoose));
            }
            using (var actContext = new PhotoContestContext(options))
            {
                var userToChoose = actContext.Users.Skip(8).First().UserName;
                var sut          = new ContestService(actContext, contextAccessor, userService.Object, categoryService, userManager, signManager);
                var result       = await sut.ChooseJuryAsync(actContext.Contests.First().Name, userToChoose);

                Assert.IsTrue(result);
            }
        }