Esempio n. 1
0
        public async Task BuyOfferForUser_WithInValidData_ShouldNotBuyOffer()
        {
            var expected = 1;
            var guid1    = Guid.NewGuid().ToString();
            var guid2    = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();
            var moqEmailSender       = new Mock <IEmailSender>();

            var context = InitializeContext.CreateContextForInMemory();

            var user1 = new ApplicationUser()
            {
                Id       = guid1,
                UserName = "******",
            };

            var user2 = new ApplicationUser()
            {
                Id       = guid2,
                UserName = "******",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid1,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);
            this.orderService = new OrderService(context, this.offerService, moqEmailSender.Object);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            await this.offerService.ApproveOfferAsync(offer.Id);

            context.Users.Add(user1);
            context.Users.Add(user2);
            await context.SaveChangesAsync();

            // Assert
            await this.orderService.BuyOfferForUser("InvalidID", guid2, new BuyInput()
            {
                Email     = "*****@*****.**",
                FirstName = "TestFirstName",
                LastName  = "TestLastName",
                OfferId   = offer.Id,
            });

            var offerBought = await context.Orders.FirstOrDefaultAsync(x => x.OfferId == offer.Id && x.UserId == guid2);

            Assert.Null(offerBought);
        }
        public async Task GetAdminOffersForApprove_WithValidData_ShouldReturnAdminOffers()
        {
            var expected  = 1;
            var guid      = Guid.NewGuid().ToString();
            var guid2     = Guid.NewGuid().ToString();
            var guidOffer = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();
            var moqIFormFile         = new Mock <IFormFile>();

            var context = InitializeContext.CreateContextForInMemory();

            this.userService = new UserService(context, moqHttpContextAccessor.Object);

            var user1 = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            var createOfferInputModelApproved = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            var offerapproved = await this.offerService.CreateOfferAsync(createOfferInputModelApproved);

            await this.offerService.ApproveOfferAsync(offerapproved.Id);

            context.Users.Add(user1);
            await context.SaveChangesAsync();

            // Assert
            var userGet = await this.userService.GetAdminOffersForApproveCount();

            Assert.Equal(expected, userGet);
        }
        public async Task GetMessagesForOfferAsync_WithValidData_ShouldReturnMessages()
        {
            var expected = 2;
            var guid1    = Guid.NewGuid().ToString();
            var guid2    = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();
            var moqOfferService      = new Mock <IOfferService>();

            var context = InitializeContext.CreateContextForInMemory();

            this.userService     = new UserService(context, moqHttpContextAccessor.Object);
            this.messagesService = new MessagesService(context, this.userService, moqOfferService.Object);

            var sender = new ApplicationUser()
            {
                Id       = guid1,
                UserName = "******",
            };

            var reciver = new ApplicationUser()
            {
                Id       = guid2,
                UserName = "******",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid2,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            await this.offerService.ApproveOfferAsync(offer.Id);

            context.Users.Add(sender);
            context.Users.Add(reciver);
            await context.SaveChangesAsync();

            // Assert

            await this.messagesService.CreateMessageAsync(guid1, guid2, offer.Id, "TestMessage");

            await this.messagesService.CreateMessageAsync(guid1, guid2, offer.Id, "TestMessage2");

            var unreadMessages = await this.messagesService.GetMessagesForOfferAsync(offer.Id, guid1, guid2);

            Assert.Equal(expected, unreadMessages.Count);
        }
Esempio n. 4
0
        public async Task RemoveFromFavoritesAsync_WithInValidDataWithNotFoundOffer_ShouldNotAddOfferToFavorites()
        {
            var expectedOffersCount = 1;
            var guid = Guid.NewGuid().ToString();

            var moqUsersService = new Mock <IUserService>();

            moqUsersService.Setup(x => x.GetCurrentUserId())
            .Returns("CurrentUserId");

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();
            var moqIFormFile         = new Mock <IFormFile>();

            moqCloudinaryService.Setup(x => x.UploadPhotoAsync(moqIFormFile.Object, "FileName", "Folder"))
            .ReturnsAsync("http://test.com");

            var createOfferInputModel = new CreateOfferModel
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            var user = new ApplicationUser()
            {
                Id       = guid,
                UserName = "******",
            };

            var context      = InitializeContext.CreateContextForInMemory();
            var offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);

            this.favoriteService = new FavoriteService(context, offerService);

            var approvedOffer = await offerService.CreateOfferAsync(createOfferInputModel);

            await offerService.ApproveOfferAsync(approvedOffer.Id);

            context.Users.Add(user);
            await context.SaveChangesAsync();

            // Assert
            await this.favoriteService.AddToFavoritesAsync(approvedOffer.Id, guid);

            var result = await this.favoriteService.AddToFavoritesAsync("InvalidID", guid);

            var userFav = await context.UserFavorites.FirstOrDefaultAsync(x => x.OfferId == approvedOffer.Id && x.UserId == guid);

            Assert.NotNull(userFav);
            Assert.False(result);
        }
        public async Task AddReviewToOffer_WithValidData_ShouldAddReview()
        {
            var expected = 1;
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();

            var context = InitializeContext.CreateContextForInMemory();

            this.userService = new UserService(context, moqHttpContextAccessor.Object);

            var user = new ApplicationUser()
            {
                Id       = guid,
                UserName = "******",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService   = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);
            this.commentService = new CommentService(context, this.offerService);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            await this.offerService.ApproveOfferAsync(offer.Id);

            context.Users.Add(user);
            await context.SaveChangesAsync();

            // Assert
            await this.commentService.AddReviewToOffer(new CreateReviewModel()
            {
                CreatorId = guid,
                Id        = offer.Id,
                Rating    = "5",
                Review    = "Test content",
            });

            var offerComments = await this.commentService.GetOfferCommentsAsync(offer.Id);

            Assert.Equal(expected, offerComments.Count);
            Assert.Equal("Test content", offerComments[0].Description);
            Assert.Equal(guid, offerComments[0].CreatorId);
        }
Esempio n. 6
0
        public async Task <Offer> CreateOfferAsync(CreateOfferModel inputModel)
        {
            var offer = new Offer()
            {
                Name        = inputModel.Name,
                Description = inputModel.Description,
                Price       = inputModel.Price,
                PicUrl      = inputModel.PicUrl,
                CategoryId  = inputModel.CategotyName,
                CreatorId   = inputModel.CreatorId,
                BuyContent  = inputModel.BuyContent,
            };

            await this.context.Offers.AddAsync(offer);

            await this.context.SaveChangesAsync();

            return(offer);
        }
        public async Task IsOfferFavoritedByUserAsync_WithValidData_ShouldReturnFalse()
        {
            var guid = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();

            var context = InitializeContext.CreateContextForInMemory();

            this.userService = new UserService(context, moqHttpContextAccessor.Object);

            var user1 = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            context.Users.Add(user1);
            await context.SaveChangesAsync();

            // Assert
            var userGet = await this.userService.IsOfferFavoritedByUserAsync(offer.Id, guid);

            Assert.False(userGet);
        }
        public async Task GetInboxMessagesAsync_WithValidData_ShouldReturnInboxMessages()
        {
            var expected = 1;
            var guid1    = Guid.NewGuid().ToString();
            var guid2    = Guid.NewGuid().ToString();

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, "TestUser"),
                new Claim(ClaimTypes.NameIdentifier, guid2),
            };

            var identity        = new ClaimsIdentity(claims, "TestAuthType");
            var claimsPrincipal = new ClaimsPrincipal(identity);

            var mockPrincipal = new Mock <IPrincipal>();

            mockPrincipal.Setup(x => x.Identity).Returns(identity);
            mockPrincipal.Setup(x => x.IsInRole(It.IsAny <string>())).Returns(true);

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.Setup(m => m.User).Returns(claimsPrincipal);

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            moqHttpContextAccessor.Setup(x => x.HttpContext.User.IsInRole(It.IsAny <string>())).Returns(true);
            moqHttpContextAccessor.Setup(x => x.HttpContext.User).Returns(claimsPrincipal);

            var moqCategoriesService = new Mock <ICategoryService>();
            var moqCloudinaryService = new Mock <ICloudinaryService>();
            var moqOfferService      = new Mock <IOfferService>();
            var moqUsersService      = new Mock <IUserService>();

            moqUsersService.Setup(x => x.GetCurrentUserId())
            .Returns("CurrentUserId");

            var context = InitializeContext.CreateContextForInMemory();

            this.userService     = new UserService(context, moqHttpContextAccessor.Object);
            this.messagesService = new MessagesService(context, this.userService, moqOfferService.Object);

            var sender = new ApplicationUser()
            {
                Id       = guid1,
                UserName = "******",
            };

            var reciver = new ApplicationUser()
            {
                Id       = guid2,
                UserName = "******",
            };

            var createOfferInputModel = new CreateOfferModel()
            {
                Name         = "Wow Account",
                CategotyName = "Wow",
                CreatorId    = guid2,
                Description  = "Some Test Description",
                Price        = 10.00,
                PicUrl       = "link",
            };

            this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object);

            var offer = await this.offerService.CreateOfferAsync(createOfferInputModel);

            await this.offerService.ApproveOfferAsync(offer.Id);

            context.Users.Add(sender);
            context.Users.Add(reciver);
            await context.SaveChangesAsync();

            // Assert

            await this.messagesService.CreateMessageAsync(guid1, guid2, offer.Id, "TestMessage");

            await this.messagesService.CreateMessageAsync(guid1, guid2, offer.Id, "TestMessage2");

            var InboxMessages = await this.messagesService.GetInboxMessagesAsync();

            Assert.Equal(expected, InboxMessages.Count);
        }