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()); } }
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 Throw_When_ContestNameIsInvalidApi() { var options = Utils.GetOptions(nameof(Throw_When_ContestNameIsInvalidApi)); 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>(); var newPhotoDTO = new Mock <NewPhotoDTO>().Object; newPhotoDTO.Title = "New photo"; newPhotoDTO.Description = "New photo descriptions"; newPhotoDTO.PhotoUrl = "www.newphoto.com"; newPhotoDTO.Description = null; 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.CreateApiAsync(newPhotoDTO)); } }
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)); } }
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")); } }
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)); } }
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"); } }
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"))); } }
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 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()); } }
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"))); } }
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 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); } }
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 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 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 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); } }
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>)); } }
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 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)); } }
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 ThrowsWhen_Category_NotFound() { var options = Utils.GetOptions(nameof(ThrowsWhen_Category_NotFound)); using (var actContext = new PhotoContestContext(options)) { var sut = new CategoryService(actContext); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.FindCategoryByNameAsync("Wrong name")); } }
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)); } }
public static string GetUnreadNotificationsCount() { var userId = Thread.CurrentPrincipal.Identity.GetUserId(); if (userId == null) { return ""; } var dbContext = new PhotoContestContext(); var count = dbContext.Notifications.Where(n => n.RecipientId == userId && n.IsRead == false).Count(); return count.ToString(); }
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)); } }
public ReviewService(PhotoContestContext dbContext, IPhotoService photoService, IUserService userService, IHttpContextAccessor contextAccessor, UserManager <User> userManager, SignInManager <User> signInManager) { this.dbContext = dbContext; this.photoService = photoService; this.userService = userService; this.userManager = userManager; this.signInManager = signInManager; this.contextAccessor = contextAccessor; }
public ContestService(PhotoContestContext dbContext, IHttpContextAccessor contextAccessor, IUserService userService, ICategoryService categoryService, UserManager <User> userManager, SignInManager <User> signInManager) { this.dbContext = dbContext; this.contextAccessor = contextAccessor; this.userService = userService; this.categoryService = categoryService; this.userManager = userManager; this.signInManager = signInManager; }
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); } }
public async Task Return_NameOf_CreatedCategoryAsync() { var options = Utils.GetOptions(nameof(Return_NameOf_CreatedCategoryAsync)); var category = new Mock <Category>(); category.Name = "Test category"; using (var actContext = new PhotoContestContext(options)) { var sut = new CategoryService(actContext); var result = await sut.CreateAsync("Test category"); Assert.AreEqual(result, category.Name); } }