From 424acaf451a1b0d855b7170dacecc7fa92340383 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Mon, 16 Oct 2023 13:29:30 +0300 Subject: [PATCH] add lost wishlist integrational tests --- .../TestExtentions/DbInitializer.cs | 213 +++-- .../Tests/WishlistsTests.cs | 760 +++++++++++++++++- 2 files changed, 881 insertions(+), 92 deletions(-) diff --git a/ShoppingAssistantApi.Tests/TestExtentions/DbInitializer.cs b/ShoppingAssistantApi.Tests/TestExtentions/DbInitializer.cs index 6877b07..811cce6 100644 --- a/ShoppingAssistantApi.Tests/TestExtentions/DbInitializer.cs +++ b/ShoppingAssistantApi.Tests/TestExtentions/DbInitializer.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using MongoDB.Bson; +using MongoDB.Driver; using ShoppingAssistantApi.Domain.Entities; using ShoppingAssistantApi.Domain.Enums; using ShoppingAssistantApi.Infrastructure.Services.Identity; @@ -22,6 +23,8 @@ public class DbInitializer InitializeUsersAsync().Wait(); InitializeWishlistsAsync().Wait(); + InitializeMessagesAsync().Wait(); + InitializeProductsAsync().Wait(); } public async Task InitializeUsersAsync() @@ -97,94 +100,156 @@ public class DbInitializer public async Task InitializeWishlistsAsync() { var wishlistsCollection = _dbContext.Db.GetCollection("Wishlists"); - var messagesCollection = _dbContext.Db.GetCollection("Messages"); + var usersCollection = _dbContext.Db.GetCollection("Users"); - var gamingPcWishlist = new Wishlist - { - Id = ObjectId.Parse("ab79cde6f69abcd3efab65cd"), - Name = "Gaming PC", - Type = WishlistTypes.Product.ToString(), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc") - }; - await wishlistsCollection.InsertOneAsync(gamingPcWishlist); + var user1 = await (await usersCollection.FindAsync(x => x.Email!.Equals("wishlists@gmail.com"))).FirstAsync(); + var user2 = await (await usersCollection.FindAsync(x => x.Email!.Equals("test@gmail.com"))).FirstAsync(); - await messagesCollection.InsertManyAsync(new Message[] - { - new() { - WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd"), - Text = "Prompt", - Role = MessageRoles.User.ToString(), - CreatedDateUtc = DateTime.UtcNow.AddMinutes(-1), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc") - }, - new() { - WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd"), - Text = "Answer", - Role = MessageRoles.Application.ToString(), - CreatedDateUtc = DateTime.UtcNow, - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc") - }, - }); + var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd"); + var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"); - var genericWishlist = new Wishlist + var wishlists = new Wishlist[] { - Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), - Name = "Generic Wishlist Name", - Type = WishlistTypes.Product.ToString(), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6409fc"), - Messages = new Message[] + new Wishlist { - new Message - { - Text = "One Message", - Role = MessageRoles.User.ToString(), - CreatedDateUtc = DateTime.UtcNow.AddMinutes(-1), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6409fc") - } + Id = wishlistId1, + Name = "Gaming PC", + Type = WishlistTypes.Product.ToString(), + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow + }, + new Wishlist + { + Id = wishlistId2, + Name = "Generic Wishlist Name", + Type = WishlistTypes.Product.ToString(), + CreatedById = user2.Id, + CreatedDateUtc = DateTime.UtcNow } }; - await wishlistsCollection.InsertOneAsync(genericWishlist); - await messagesCollection.InsertOneAsync(new Message - { - WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), - Text = "One Message", - Role = MessageRoles.User.ToString(), - CreatedDateUtc = DateTime.UtcNow.AddMinutes(-1), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6409fc") - }); - - var mouseWishlist = new Wishlist - { - Id = ObjectId.Parse("ab79cde6f69abcd3efab95cd"), - Name = "Mouse", - Type = WishlistTypes.Product.ToString(), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc"), - }; - await wishlistsCollection.InsertOneAsync(mouseWishlist); - await messagesCollection.InsertManyAsync(new List + await wishlistsCollection.InsertManyAsync(wishlists); + } + + + public async Task InitializeMessagesAsync() + { + var messagesCollection = _dbContext.Db.GetCollection("Messages"); + var usersCollection = _dbContext.Db.GetCollection("Users"); + + var user1 = await (await usersCollection.FindAsync(x => x.Email!.Equals("wishlists@gmail.com"))).FirstAsync(); + var user2 = await (await usersCollection.FindAsync(x => x.Email!.Equals("test@gmail.com"))).FirstAsync(); + + var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd"); + var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"); + + var messages = new Message[] { - new() { - WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab95cd"), - Text = "First Message", + new Message + { + Text = "Message 1", Role = MessageRoles.User.ToString(), - CreatedDateUtc = DateTime.UtcNow.AddMinutes(-2), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc"), + WishlistId = wishlistId1, + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow }, - new() { - WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab95cd"), - Text = "Second Message", + new Message + { + Text = "Message 2", Role = MessageRoles.Application.ToString(), - CreatedDateUtc = DateTime.UtcNow.AddMinutes(-1), - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc"), + WishlistId = wishlistId1, + CreatedDateUtc = DateTime.UtcNow.AddSeconds(5) }, - new() { - WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab95cd"), - Text = "Third Message", + new Message + { + Text = "Message 3", Role = MessageRoles.User.ToString(), - CreatedDateUtc = DateTime.UtcNow, - CreatedById = ObjectId.Parse("652c3b89ae02a3135d6418fc"), + WishlistId = wishlistId1, + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow.AddSeconds(20) }, - }); + new Message + { + Text = "Message 4", + Role = MessageRoles.Application.ToString(), + WishlistId = wishlistId1, + CreatedDateUtc = DateTime.UtcNow.AddSeconds(25) + }, + new Message + { + Text = "Message 5", + Role = MessageRoles.User.ToString(), + WishlistId = wishlistId1, + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow.AddSeconds(45) + }, + new Message + { + Text = "Message 6", + Role = MessageRoles.Application.ToString(), + WishlistId = wishlistId1, + CreatedDateUtc = DateTime.UtcNow.AddSeconds(50) + }, + new Message + { + Text = "Prompt", + Role = MessageRoles.User.ToString(), + WishlistId = wishlistId2, + CreatedById = user2.Id, + CreatedDateUtc = DateTime.UtcNow + } + }; + + await messagesCollection.InsertManyAsync(messages); + } + + public async Task InitializeProductsAsync() + { + var productsCollection = _dbContext.Db.GetCollection("Products"); + var usersCollection = _dbContext.Db.GetCollection("Users"); + + var user1 = await (await usersCollection.FindAsync(x => x.Email!.Equals("wishlists@gmail.com"))).FirstAsync(); + var user2 = await (await usersCollection.FindAsync(x => x.Email!.Equals("test@gmail.com"))).FirstAsync(); + + var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd"); + var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"); + + var products = new Product[] + { + new Product + { + Name = "AMD Ryzen 5 5600G 6-Core 12-Thread Unlocked Desktop Processor with Radeon Graphics", + Description = "Features best-in-class graphics performance in a desktop processor for smooth 1080p gaming, no graphics card required", + Rating = 4.8, + Url = "https://a.co/d/5ceuIrq", + ImagesUrls = new string[] + { + "https://m.media-amazon.com/images/I/51f2hkWjTlL._AC_SL1200_.jpg", + "https://m.media-amazon.com/images/I/51iji7Gel-L._AC_SL1200_.jpg" + }, + WasOpened = false, + WishlistId = wishlistId1, + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow + }, + new Product + { + Name = "Samsung 970 EVO Plus SSD 2TB NVMe M.2 Internal Solid State Hard Drive, V-NAND Technology, Storage and Memory Expansion for Gaming, Graphics w/ Heat Control, Max Speed, MZ-V7S2T0B/AM ", + Description = "7 Year Limited Warranty: The 970 EVO Plus provides up to 1200 TBW (Terabytes Written) with 5-years of protection for exceptional endurance powered by the latest V-NAND technology and Samsung's reputation for quality ", + Rating = 4.8, + Url = "https://a.co/d/gxnuqs1", + ImagesUrls = new string[] + { + "https://m.media-amazon.com/images/I/51Brl+iYtvL._AC_SL1001_.jpg", + "https://m.media-amazon.com/images/I/51GOfLlVwoL._AC_SL1001_.jpg" + }, + WasOpened = false, + WishlistId = wishlistId1, + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow + }, + }; + + await productsCollection.InsertManyAsync(products); } } diff --git a/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs b/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs index ac89a96..5d989b7 100644 --- a/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs +++ b/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs @@ -6,7 +6,6 @@ using Newtonsoft.Json.Linq; namespace ShoppingAssistantApi.Tests.Tests; -// TODO: make errors test more descrptive public class WishlistsTests : TestsBase { // From DbInitializer @@ -16,14 +15,23 @@ public class WishlistsTests : TestsBase private const string TestingUserPassword = "Yuiop12345"; - private const string TestingWishlistId = "ab79cde6f69abcd3efab65cd"; + private const string TestingNotExistingWishlistId = "1234567890abcdef12345678"; + + private const string TestingValidWishlistName = "Gaming PC"; + + private const WishlistTypes TestingValidWishlistType = WishlistTypes.Product; + + private const string TestingUnauthorizedWishlistId = "ab6c2c2d9edf39abcd1ef9ab"; + + private const string TestingValidWishlistId = "ab79cde6f69abcd3efab65cd"; + public WishlistsTests(TestingFactory factory) : base(factory) { } [Fact] - public async Task StartPersonalWishlistAsync_ValidWishlist_ReturnsNewWishlist() + public async Task StartPersonalWishlist_ValidWishlist_ReturnsNewWishlist() { await LoginAsync(TestingUserEmail, TestingUserPassword); var mutation = new @@ -93,7 +101,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = TestingWishlistId + wishlistId = TestingValidWishlistId } }; @@ -101,8 +109,8 @@ public class WishlistsTests : TestsBase var wishlist = (WishlistDto?) jsonObject?.data?.personalWishlist?.ToObject(); Assert.NotNull(wishlist); - Assert.Equal("Gaming PC", wishlist.Name); - Assert.Equal(WishlistTypes.Product.ToString(), wishlist.Type); + Assert.Equal(TestingValidWishlistName, wishlist.Name); + Assert.Equal(TestingValidWishlistType.ToString(), wishlist.Type); Assert.Equal(TestingUserId, wishlist.CreatedById); } @@ -121,7 +129,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = TestingWishlistId, + wishlistId = TestingValidWishlistId, dto = new { text = MessageText @@ -139,7 +147,7 @@ public class WishlistsTests : TestsBase } [Fact] - public async Task GetMessagesPageFromPersonalWishlist_ValidPageNumberAndSize_ReturnsPage() + public async Task GetMessagesPageFromPersonalWishlist_ValidPageNumberAndSizeValidWishlistIdOrAuthorizedAccess_ReturnsPage() { await LoginAsync(TestingUserEmail, TestingUserPassword); var mutation = new @@ -158,7 +166,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = "ab79cde6f69abcd3efab95cd", // From DbInitializer + wishlistId = TestingValidWishlistId, pageNumber = 1, pageSize = 2 } @@ -169,12 +177,116 @@ public class WishlistsTests : TestsBase Assert.NotNull(pagedList); Assert.NotEmpty(pagedList.Items); - Assert.Equal("Third Message", pagedList.Items.FirstOrDefault()?.Text); - Assert.Equal(MessageRoles.User.ToString(), pagedList.Items.FirstOrDefault()?.Role); + Assert.Equal("Message 6", pagedList.Items.FirstOrDefault()?.Text); + Assert.Equal(MessageRoles.Application.ToString(), pagedList.Items.FirstOrDefault()?.Role); } [Fact] - public async Task StartPersonalWishlistAsync_InvalidWishlist_ReturnsErrors() + public async Task AddProductToPersonalWishlist_ValidProduct_ReturnsNewProduct() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { + addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { + url, name, description, rating, imagesUrls, wasOpened + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + dto = new + { + url = "https://www.amazon.com/url", + name = "Generic name", + description = "Generic description", + rating = 4.8, + imagesUrls = new string[] + { + "https://www.amazon.com/image-url-1", + "https://www.amazon.com/image-url-2" + }, + wasOpened = false + } + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var product = (ProductDto?) jsonObject?.data?.addProductToPersonalWishlist?.ToObject(); + + Assert.NotNull(product); + Assert.Equal("https://www.amazon.com/url", product.Url); + Assert.Equal("Generic name", product.Name); + Assert.Equal("Generic description", product.Description); + Assert.Equal(4.8, product.Rating); + Assert.Equal("https://www.amazon.com/image-url-1", product.ImagesUrls[0]); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_ValidPageNumberAndSizeValidWishlistIdOrAuthorizedAccess_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 1, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.productsPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Equal("Samsung 970 EVO Plus SSD 2TB NVMe M.2 Internal Solid State Hard Drive, V-NAND Technology, Storage and Memory Expansion for Gaming, Graphics w/ Heat Control, Max Speed, MZ-V7S2T0B/AM ", pagedList.Items.LastOrDefault()?.Name); + Assert.Equal(TestingValidWishlistId, pagedList.Items.LastOrDefault()?.WishlistId); + } + + [Fact] + public async Task DeletePersonalWishlist_ValidWishlistIdOrAuthorizedAccess_ReturnsWishlist() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation deletePersonalWishlist($wishlistId: String!) { + deletePersonalWishlist (wishlistId: $wishlistId) { + createdById, id, name, type + } + }", + variables = new + { + wishlistId = TestingValidWishlistId + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var wishlist = (WishlistDto?) jsonObject?.data?.deletePersonalWishlist?.ToObject(); + + Assert.NotNull(wishlist); + Assert.Equal(TestingValidWishlistId, wishlist.Id); + Assert.Equal(TestingValidWishlistName, wishlist.Name); + Assert.Equal(TestingValidWishlistType.ToString(), wishlist.Type); + Assert.Equal(TestingUserId, wishlist.CreatedById); + } + + [Fact] + public async Task StartPersonalWishlist_InvalidWishlist_ReturnsErrors() { await LoginAsync(TestingUserEmail, TestingUserPassword); var mutation = new @@ -203,7 +315,113 @@ public class WishlistsTests : TestsBase } [Fact] - public async Task GetPersonalWishlist_InvalidWishlistId_ReturnsErrors() + public async Task GetPersonalWishlistsPage_PageNumberGreaterThanAvailablePages_ReturnsEmptyList() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query personalWishlistsPage($pageNumber: Int!, $pageSize: Int!) { + personalWishlistsPage(pageNumber: $pageNumber, pageSize: $pageSize) { + items { createdById, id, name, type } + } + }", + variables = new + { + pageNumber = 100, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.personalWishlistsPage?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Empty(pagedList.Items); + } + + [Fact] + public async Task GetPersonalWishlistsPage_PageNumberLessThan1_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query personalWishlistsPage($pageNumber: Int!, $pageSize: Int!) { + personalWishlistsPage(pageNumber: $pageNumber, pageSize: $pageSize) { + items { createdById, id, name, type } + } + }", + variables = new + { + pageNumber = 0, + pageSize = 1 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetPersonalWishlistsPage_PageSizeGreaterThanAvailableEntities_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query personalWishlistsPage($pageNumber: Int!, $pageSize: Int!) { + personalWishlistsPage(pageNumber: $pageNumber, pageSize: $pageSize) { + items { createdById, id, name, type } + } + }", + variables = new + { + pageNumber = 1, + pageSize = 100 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.personalWishlistsPage?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.NotEmpty(pagedList.Items); + Assert.Equal(TestingUserId, pagedList.Items.FirstOrDefault()?.CreatedById); + } + + [Fact] + public async Task GetPersonalWishlistsPage_PageSizeLessThan0_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query personalWishlistsPage($pageNumber: Int!, $pageSize: Int!) { + personalWishlistsPage(pageNumber: $pageNumber, pageSize: $pageSize) { + items { createdById, id, name, type } + } + }", + variables = new + { + pageNumber = 1, + pageSize = -1 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.personalWishlistsPage?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.NotEmpty(pagedList.Items); + Assert.Equal(TestingUserId, pagedList.Items.FirstOrDefault()?.CreatedById); + } + + [Fact] + public async Task GetPersonalWishlist_NotExistingWishlistId_ReturnsErrors() { await LoginAsync(TestingUserEmail, TestingUserPassword); var query = new @@ -216,7 +434,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = "1234567890abcdef12345678" // Invalid wishlistId + wishlistId = TestingNotExistingWishlistId } }; @@ -228,7 +446,397 @@ public class WishlistsTests : TestsBase } [Fact] - public async Task GetPersonalWishlist_UnauthorizedAccess_ReturnsErrors() + public async Task GetMessagesPageFromPersonalWishlist_PageNumberGreaterThanAvailablePages_ReturnsEmptyList() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 100, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.messagesPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Empty(pagedList.Items); + } + + [Fact] + public async Task GetMessagesPageFromPersonalWishlist_PageNumberLessThan1_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 0, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetMessagesPageFromPersonalWishlist_PageSizeGreaterThanAvailableEntities_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 1, + pageSize = 10 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.messagesPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Equal("Message 6", pagedList.Items.FirstOrDefault()?.Text); + Assert.Equal(MessageRoles.Application.ToString(), pagedList.Items.FirstOrDefault()?.Role); + } + + [Fact] + public async Task GetMessagesPageFromPersonalWishlist_PageSizeLessThan0_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 1, + pageSize = -2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.messagesPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Equal("Message 6", pagedList.Items.FirstOrDefault()?.Text); + Assert.Equal(MessageRoles.Application.ToString(), pagedList.Items.FirstOrDefault()?.Role); + } + + [Fact] + public async Task GetMessagesPageFromPersonalWishlist_NotExistingWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingNotExistingWishlistId, + pageNumber = 1, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetMessagesPageFromPersonalWishlist_OtherUserWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, text, role, createdById }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingUnauthorizedWishlistId, + pageNumber = 1, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task AddProductToPersonalWishlist_NotExistingWishlistId_RturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { + addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { + url, name, description, rating, imagesUrls, wasOpened + } + }", + variables = new + { + wishlistId = TestingNotExistingWishlistId, + dto = new + { + url = "https://www.amazon.com/url", + name = "Generic name", + description = "Generic description", + rating = 4.8, + imagesUrls = new string[] + { + "https://www.amazon.com/image-url-1", + "https://www.amazon.com/image-url-2" + }, + wasOpened = false + } + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task AddProductToPersonalWishlist_OtherUserWishlistId_RturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { + addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { + url, name, description, rating, imagesUrls, wasOpened + } + }", + variables = new + { + wishlistId = TestingUnauthorizedWishlistId, + dto = new + { + url = "https://www.amazon.com/url", + name = "Generic name", + description = "Generic description", + rating = 4.8, + imagesUrls = new string[] + { + "https://www.amazon.com/image-url-1", + "https://www.amazon.com/image-url-2" + }, + wasOpened = false + } + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_PageNumberGreaterThanAvailablePages_ReturnsEmptyList() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 100, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.productsPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + Assert.Empty(pagedList.Items); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_PageNumberLessThan1_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 0, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_PageSizeGreaterThanAvailableEntities_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = "query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, pageNumber, pageSize, totalItems, totalPages } }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 1, + pageSize = 100 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.productsPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + + Assert.Equal("Samsung 970 EVO Plus SSD 2TB NVMe M.2 Internal Solid State Hard Drive, V-NAND Technology, Storage and Memory Expansion for Gaming, Graphics w/ Heat Control, Max Speed, MZ-V7S2T0B/AM ", pagedList.Items.ToList()[1].Name); + Assert.Equal(TestingValidWishlistId, pagedList.Items.ToList()[1].WishlistId); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_PageSizeLessThan0_ReturnsPage() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = "query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, pageNumber, pageSize, totalItems, totalPages } }", + variables = new + { + wishlistId = TestingValidWishlistId, + pageNumber = 1, + pageSize = -2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var pagedList = (PagedList?) jsonObject?.data?.productsPageFromPersonalWishlist?.ToObject>(); + + Assert.NotNull(pagedList); + + Assert.Equal("Samsung 970 EVO Plus SSD 2TB NVMe M.2 Internal Solid State Hard Drive, V-NAND Technology, Storage and Memory Expansion for Gaming, Graphics w/ Heat Control, Max Speed, MZ-V7S2T0B/AM ", pagedList.Items.ToList()[1].Name); + Assert.Equal(TestingValidWishlistId, pagedList.Items.ToList()[1].WishlistId); + } + + [Fact] + public async Task GetPersonalWishlist_OtherUserWishlistId_ReturnsErrors() { await LoginAsync(TestingUserEmail, TestingUserPassword); var query = new @@ -241,7 +849,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = "ab6c2c2d9edf39abcd1ef9ab" // Other user's wishlist + wishlistId = TestingUnauthorizedWishlistId } }; @@ -252,6 +860,122 @@ public class WishlistsTests : TestsBase Assert.True(errors.Count > 0); } + [Fact] + public async Task GetProductsPageFromPersonalWishlist_NotExistingWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingNotExistingWishlistId, + pageNumber = 0, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task GetProductsPageFromPersonalWishlist_OtherUserWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + query productsPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + productsPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { + hasNextPage, + hasPreviousPage, + items { id, url, name, description, rating, imagesUrls, wasOpened, wishlistId }, + pageNumber, + pageSize, + totalItems, + totalPages + } + }", + variables = new + { + wishlistId = TestingUnauthorizedWishlistId, + pageNumber = 0, + pageSize = 2 + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task DeletePersonalWishlist_NotExistingWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation deletePersonalWishlist($wishlistId: String!) { + deletePersonalWishlist (wishlistId: $wishlistId) { + createdById, id, name, type + } + }", + variables = new + { + wishlistId = TestingNotExistingWishlistId + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + + [Fact] + public async Task DeletePersonalWishlist_OtherUserWishlistId_ReturnsError() + { + await LoginAsync(TestingUserEmail, TestingUserPassword); + var mutation = new + { + query = @" + mutation deletePersonalWishlist($wishlistId: String!) { + deletePersonalWishlist (wishlistId: $wishlistId) { + createdById, id, name, type + } + }", + variables = new + { + wishlistId = TestingUnauthorizedWishlistId + } + }; + + var jsonObject = await SendGraphQlRequestAsync(mutation); + var errors = (JArray?) jsonObject?.errors; + + Assert.NotNull(errors); + Assert.True(errors.Count > 0); + } + [Fact] public async Task AddMessageToPersonalWishlist_InvalidMessage_ReturnsErrors() { @@ -266,7 +990,7 @@ public class WishlistsTests : TestsBase }", variables = new { - wishlistId = "8125jad7g12", // Invalid wishlistId + wishlistId = TestingNotExistingWishlistId, dto = new { text = "random text", @@ -280,4 +1004,4 @@ public class WishlistsTests : TestsBase Assert.NotNull(errors); Assert.True(errors.Count > 0); } -} \ No newline at end of file +}