Esempio n. 1
0
        public async Task ChangeUserAchievementsAsync_ValidAssignAchievementsToUserModel(AssignAchievementsToUserModel model)
        {
            //Arrange
            var user              = UserDumbData.GetRandomEntity();
            var userAchievements  = UserAchievementDumbData.GetRandomEntities(5, user);
            var returnpagingInfo  = ReturnPagingInfoDumbData.GetForModel(new PagingInfo(), userAchievements);
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(user);
            _userAchievementRepository.Setup(x => x.GetCountAchievementsByUserAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(RandomHelper.GetRandomNumber());
            _userAchievementRepository.Setup(x => x.GetAllAchievementsByUserAsync(It.IsAny <PagingInfo>(), It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(returnpagingInfo));
            _userAchievementRepository.Setup(x => x.AddAsync(It.IsAny <UserAchievement>(), cancellationToken)).Returns(Task.CompletedTask);
            _userAchievementRepository.Setup(x => x.Delete(It.IsAny <UserAchievement>()));
            _achievementRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken))
            .Returns(Task.FromResult(AchievementDumbData.GetRandomEntity()));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _userAchievementService.ChangeUserAchievementsAsync(model, user.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userAchievementRepository.Verify(x => x.GetCountAchievementsByUserAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userAchievementRepository.Verify(x => x.GetAllAchievementsByUserAsync(It.IsAny <PagingInfo>(), It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userAchievementRepository.Verify(x => x.AddAsync(It.IsAny <UserAchievement>(), cancellationToken), Times.Once);
            _userAchievementRepository.Verify(x => x.Delete(It.IsAny <UserAchievement>()), Times.Once);
            _achievementRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }
        public async Task AddAsync_ValidRequestAchievementModel_ReturnsIResponse(CreateRequestAchievementModel model)
        {
            //Arrange
            var user              = UserDumbData.GetRandomEntity();
            var achievement       = AchievementDumbData.GetRandomEntity();
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _userRepository.Setup(x => x.GetAdminsEmailsAsync(cancellationToken)).Returns(Task.FromResult(new List <string>() as ICollection <string>));
            _achievementRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(achievement));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);
            _emailService.Setup(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>())).Returns(Task.CompletedTask);
            _requestAchievementRepository.Setup(x => x.AddAsync(It.IsAny <RequestAchievement>(), cancellationToken)).Returns(Task.FromResult(_mapper.Map <RequestAchievement>(model)));

            string key             = "RequestAchievementPage";
            var    localizedString = new LocalizedString(key, key);

            _stringLocalizer.Setup(_ => _[key]).Returns(localizedString);

            // Act
            var response = await _requestAchievementService.AddAsync(model, user.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userRepository.Verify(x => x.GetAdminsEmailsAsync(cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
            _emailService.Verify(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>()), Times.Once);
            _stringLocalizer.Verify(_ => _[key], Times.Once);
            _achievementRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _requestAchievementRepository.Verify(x => x.AddAsync(It.IsAny <RequestAchievement>(), cancellationToken), Times.Once);

            response.Should().BeEquivalentTo(new OkResponse());
        }
        public async Task DeclineOrderRequestAsync_ValidGuid(Guid id)
        {
            //Arrange
            var user         = UserDumbData.GetRandomEntity();
            var order        = OrderDumbData.GetRandomEntity();
            var requestOrder = RequestOrderDumbData.GetEntity(user.Id, order.Id);

            id = requestOrder.Id;
            var cancellationToken = new CancellationToken();

            _requestOrderRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(requestOrder));
            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _orderRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(order));
            _userRepository.Setup(x => x.Update(It.IsAny <User>()));
            _requestOrderRepository.Setup(x => x.Update(It.IsAny <RequestOrder>()));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _requestOrderService.DeclineOrderRequestAsync(id, cancellationToken);

            // Assert
            _requestOrderRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _orderRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userRepository.Verify(x => x.Update(It.IsAny <User>()), Times.Once);
            _requestOrderRepository.Verify(x => x.Update(It.IsAny <RequestOrder>()), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }
        public async Task UpdateAchievementAsync_UpdateAchievementModel_ReturnsReadAchievementModel(UpdateAchievementModel model)
        {
            //Arrange
            var achievement       = AchievementDumbData.GetEntity(model);
            var user              = UserDumbData.GetRandomEntity();
            var userAchievements  = UserAchievementDumbData.GetRandomEntities(5, user, achievement);
            var returnPagingInfo  = ReturnPagingInfoDumbData.GetForModel(new PagingInfo(), userAchievements);
            var expectedValue     = _mapper.Map <ReadAchievementModel>(achievement);
            var cancellationToken = new CancellationToken();

            _achievementRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(achievement));
            _userAchievementRepository.Setup(x => x.GetAllUsersByAchievementAsync(It.IsAny <PagingInfo>(), It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(returnPagingInfo));
            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _achievementRepository.Setup(x => x.Update(It.IsAny <Achievement>()));
            _fileService.Setup(x => x.AddOrUpdateFileByIdAsync(It.IsAny <IFormFile>(), It.IsAny <Guid?>(), cancellationToken)).Returns(Task.FromResult(expectedValue.IconId));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            var response = await _achievementService.UpdateAchievementAsync(model, achievement.Id, cancellationToken);

            // Assert
            _achievementRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _achievementRepository.Verify(x => x.Update(It.IsAny <Achievement>()), Times.Once);
            _fileService.Verify(x => x.AddOrUpdateFileByIdAsync(It.IsAny <IFormFile>(), It.IsAny <Guid?>(), cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
            response.Should().BeEquivalentTo(expectedValue);
        }
Esempio n. 5
0
        public async Task GetLastThankAsync()
        {
            //Arrange
            var cancellationToken = new CancellationToken();
            var user = UserDumbData.GetRandomEntity();

            _thankRepository.Setup(x => x.GetLastThankAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(ThankDumbData.GetRandomEntity()));

            // Act
            await _thankService.GetLastThankAsync(user.Id, cancellationToken);

            // Assert
            _thankRepository.Verify(x => x.GetLastThankAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
        }
        public async Task GetShortUserByIdAsync_ValidGuids_ReturnsReadShortUserModel()
        {
            //Arrange
            var expectedUser      = UserDumbData.GetRandomEntity();
            var expectedValue     = _mapper.Map <ReadShortUserModel>(expectedUser);
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(expectedUser));

            // Act
            var response = await _userService.GetShortUserByIdAsync(expectedUser.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            response.Should().BeEquivalentTo(expectedValue);
        }
        public async Task AddAsync_CreateRequestOrderModel_ReturnsOkResponse(CreateRequestOrderModel model, Guid userId)
        {
            //Arrange
            var user = UserDumbData.GetRandomEntity();

            userId = user.Id;
            var order = OrderDumbData.GetRandomEntity();

            model.OrderId = order.Id;
            var enoughXP          = user.XP < order.Price;
            var cancellationToken = new CancellationToken();

            ICollection <string> emailCollection = new List <string> {
                RandomHelper.GetRandomString(),
                                 RandomHelper.GetRandomString(),
            };

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _orderRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(order));
            _userRepository.Setup(x => x.GetAdminsEmailsAsync(cancellationToken)).Returns(Task.FromResult(emailCollection));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);
            _emailService.Setup(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>())).Returns(Task.CompletedTask);
            string key             = "RequestOrderPage";
            var    localizedString = new LocalizedString(key, key);

            _stringLocalizer.Setup(_ => _[key]).Returns(localizedString);

            // Act
            var response = await _requestOrderService.AddAsync(model, userId, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _orderRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            if (enoughXP)
            {
                response.Should().BeOfType <NotAllowedResponse>();
            }
            else
            {
                _userRepository.Verify(x => x.GetAdminsEmailsAsync(cancellationToken), Times.Once);
                _stringLocalizer.Verify(_ => _[key], Times.Once);
                _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
                _emailService.Verify(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>()), Times.Once);
                response.Should().BeOfType <OkResponse>();
            }
        }
Esempio n. 8
0
        public async Task GetAllAchievementsByUserAsync(PagingInfo pagingInfo)
        {
            //Arrange
            var user              = UserDumbData.GetRandomEntity();
            var returnPage        = ReturnPagingInfoDumbData.GetForModel(pagingInfo, UserAchievementDumbData.GetRandomEntities(5, user));
            var expectedValue     = ReturnPagingInfoDumbData.GetWithModels <ReadUserAchievementModel, UserAchievement>(returnPage, _mapper);
            var cancellationToken = new CancellationToken();

            _userAchievementRepository.Setup(x => x.GetAllAchievementsByUserAsync(It.IsAny <PagingInfo>(), It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(returnPage);

            // Act
            var response = await _userAchievementService.GetAllAchievementsByUserAsync(pagingInfo, user.Id, cancellationToken);

            // Assert
            _userAchievementRepository.Verify(x => x.GetAllAchievementsByUserAsync(It.IsAny <PagingInfo>(), It.IsAny <Guid>(), cancellationToken), Times.Once);

            response.Should().BeEquivalentTo(expectedValue);
        }
        public async Task DeleteUserAsync_ValidGuids()
        {
            //Arrange
            var user = UserDumbData.GetRandomEntity();
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(user);
            _userRepository.Setup(x => x.Delete(It.IsAny <User>()));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _userService.DeleteUserAsync(user.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userRepository.Verify(x => x.Delete(It.IsAny <User>()), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }
Esempio n. 10
0
        public async Task AddAsync_ValidCreateThankModel(CreateThankModel model)
        {
            //Arrange
            var cancellationToken = new CancellationToken();

            var user = UserDumbData.GetRandomEntity();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _thankRepository.Setup(x => x.AddAsync(It.IsAny <Thank>(), cancellationToken)).Returns(Task.CompletedTask);
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _thankService.AddAsync(model, user.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _thankRepository.Verify(x => x.AddAsync(It.IsAny <Thank>(), cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }
Esempio n. 11
0
        public async Task GetFullUserByIdAsync_ValidGuids_ReturnsReadFullUserModel()
        {
            //Arrange
            var expectedUser  = UserDumbData.GetRandomEntity();
            var badgetCount   = RandomHelper.GetRandomNumber();
            var expectedValue = _mapper.Map <ReadFullUserModel>(expectedUser);

            expectedValue.BadgesCount = badgetCount;
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(expectedUser));
            _userAchievementRepository.Setup(x => x.GetCountAchievementsByUserAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(badgetCount);

            // Act
            var response = await _userService.GetFullUserByIdAsync(expectedUser.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            response.Should().BeEquivalentTo(expectedValue);
        }
Esempio n. 12
0
        public async Task UpdatePasswordAsync_ValidUpdatePasswordAsyncModels(string newPassword)
        {
            //Arrange
            var user = UserDumbData.GetRandomEntity();
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _userRepository.Setup(x => x.Update(It.IsAny <User>()));
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);
            _hasher.Setup(x => x.GetHash(It.IsAny <string>())).Returns(newPassword);

            // Act
            await _userService.UpdatePasswordAsync(user.Id, newPassword, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userRepository.Verify(x => x.Update(It.IsAny <User>()), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
            _hasher.Verify(x => x.GetHash(It.IsAny <string>()), Times.Once);
        }
Esempio n. 13
0
        public async Task AddAsync_ValidUserId_AchievementId()
        {
            //Arrange
            var user              = UserDumbData.GetRandomEntity();
            var achievement       = AchievementDumbData.GetRandomEntity();
            var cancellationToken = new CancellationToken();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(user);
            _achievementRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).ReturnsAsync(achievement);
            _userAchievementRepository.Setup(x => x.AddAsync(It.IsAny <UserAchievement>(), cancellationToken)).Returns(Task.CompletedTask);
            _eventRepository.Setup(x => x.AddAsync(It.IsAny <Event>(), cancellationToken)).Returns(Task.CompletedTask);
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _userAchievementService.AddAsync(user.Id, achievement.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _userAchievementRepository.Verify(x => x.AddAsync(It.IsAny <UserAchievement>(), cancellationToken), Times.Once);
            _achievementRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _eventRepository.Verify(x => x.AddAsync(It.IsAny <Event>(), cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }