public async Task GetUserByIdAsync_WithValidData_ShouldReturnUser()
        {
            var expected = "TestUser";
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

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

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

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

            Assert.Equal(user, userGet);
        }
        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 GetUserRolesAsync_WithValidDataButNoRoles_ShouldReturnUserRolesEmptyList()
        {
            var expected = new List <string>();
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

            var user = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
                FirstName       = "Test",
                LastName        = "Test2",
            };

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

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

            Assert.Equal(expected, userGet);
        }
        public async Task UpdateUserFirstAndLastNameAsync_WithValidData_ShouldUpdateFirstandLastNames()
        {
            var expected = "UpdatedFName UpdatedLName";
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

            var user = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
                FirstName       = "test",
                LastName        = "test",
            };

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

            // Assert
            var userGet = await this.userService.UpdateUserFirstAndLastNameAsync(guid, "UpdatedFName", "UpdatedLName");

            Assert.Equal(expected, userGet.FirstName + " " + userGet.LastName);
        }
        public async Task GetUserLastNameAsync_WithInValidData_ShouldReturnNull()
        {
            var expected = "Test";
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

            var user = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
                FirstName       = "Test",
                LastName        = "Test2",
            };

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

            // Assert
            var userGet = await this.userService.GetUserLastNameAsync("InvalidID");

            Assert.Null(userGet);
        }
        public async Task UpdateProfilePictureUrl_WithValidData_ShouldUpdateProfilePicUrl()
        {
            var expected = "TestUrlEdit.com";
            var guid     = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

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

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

            // Assert
            var userGet = await this.userService.UpdateProfilePictureUrl(guid, "TestUrlEdit.com");

            Assert.Equal(expected, userGet);
        }
示例#7
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 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);
        }
示例#9
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);
        }
        public async Task GetUserRolesAsync_WithInValidData_ShouldReturnNull()
        {
            var expected = new List <string>()
            {
                "User", "Administrator"
            };
            var guid = Guid.NewGuid().ToString();

            var moqHttpContextAccessor = new Mock <IHttpContextAccessor>();

            var context = InitializeContext.CreateContextForInMemory();

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

            var user = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
                FirstName       = "Test",
                LastName        = "Test2",
            };

            var roleUser = new ApplicationRole()
            {
                Name = "User",
            };

            var roleAdmin = new ApplicationRole()
            {
                Name = "Administrator",
            };

            context.Roles.Add(roleUser);
            context.Roles.Add(roleAdmin);
            context.UserRoles.Add(new Microsoft.AspNetCore.Identity.IdentityUserRole <string>()
            {
                RoleId = roleUser.Id,
                UserId = guid,
            });
            context.UserRoles.Add(new Microsoft.AspNetCore.Identity.IdentityUserRole <string>()
            {
                RoleId = roleAdmin.Id,
                UserId = guid,
            });

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

            // Assert
            var userGet = await this.userService.GetUserRolesAsync("InvalidID");

            Assert.Null(userGet);
        }
        public async Task GetCurrentUserId_WithValidData_ShouldReturnUserId()
        {
            var guid = Guid.NewGuid().ToString();

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

            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 context = InitializeContext.CreateContextForInMemory();

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

            var user = new ApplicationUser()
            {
                Id              = guid,
                UserName        = "******",
                ProfilePhotoUrl = "TestUrl.com",
                FirstName       = "Test",
                LastName        = "Test2",
            };

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

            // Assert
            var userGet = this.userService.GetCurrentUserId();

            Assert.Equal(guid, userGet);
        }
        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);
        }