add GetPersonalWishlist GraphQL query covered by integrational tests

This commit is contained in:
cuqmbr 2023-10-11 19:57:33 +03:00
parent 4e04a86d6a
commit 5127290365
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
5 changed files with 126 additions and 5 deletions

View File

@ -12,4 +12,9 @@ public class WishlistsQuery
public Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken,
[Service] IWishlistsService wishlistsService)
=> wishlistsService.GetPersonalWishlistsPageAsync(pageNumber, pageSize, cancellationToken);
[Authorize]
public Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken,
[Service] IWishlistsService wishlistsService)
=> wishlistsService.GetPersonalWishlistAsync(wishlistId, cancellationToken);
}

View File

@ -11,4 +11,6 @@ public interface IWishlistsService
Task<MessageDto> AddMessageToPersonalWishlistAsync(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken);
Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
}

View File

@ -86,4 +86,22 @@ public class WishlistsService : IWishlistsService
var count = await _wishlistsRepository.GetTotalCountAsync();
return new PagedList<WishlistDto>(dtos, pageNumber, pageSize, count);
}
public async Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken)
{
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
{
throw new InvalidDataException("Provided id is invalid.");
}
var entity = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistObjectId && x.CreatedById == GlobalUser.Id, cancellationToken);
Console.WriteLine(" WISHLIST: " + entity.CreatedById + " " + GlobalUser.Id);
if (entity == null)
{
throw new UnAuthorizedException<Wishlist>();
}
return _mapper.Map<WishlistDto>(entity);
}
}

View File

@ -169,7 +169,8 @@ public class DbInitialaizer
public async Task AddWishlistsWithMessages(CancellationToken cancellationToken)
{
var user = await (await _userCollection.FindAsync(x => x.Email.Equals("shopping.assistant.team@gmail.com"))).FirstAsync();
var user1 = await (await _userCollection.FindAsync(x => x.Email.Equals("shopping.assistant.team@gmail.com"))).FirstAsync();
var user2 = await (await _userCollection.FindAsync(x => x.Email.Equals("mykhailo.bilodid@nure.ua"))).FirstAsync();
var wishlists = new Wishlist[]
{
@ -178,7 +179,7 @@ public class DbInitialaizer
Id = ObjectId.Parse("ab79cde6f69abcd3efab65cd"),
Name = "Gaming PC",
Type = WishlistTypes.Product.ToString(),
CreatedById = user.Id,
CreatedById = user1.Id,
Messages = new Message[]
{
new Message
@ -192,6 +193,21 @@ public class DbInitialaizer
Role = MessageRoles.Application.ToString(),
},
}
},
new Wishlist
{
Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
Name = "Generic Wishlist Name",
Type = WishlistTypes.Product.ToString(),
CreatedById = user2.Id,
Messages = new Message[]
{
new Message
{
Text = "Prompt",
Role = MessageRoles.User.ToString(),
}
}
}
};

View File

@ -94,14 +94,48 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
var personalWishlistsPageItems = Enumerable.ToList(document.data.personalWishlistsPage.items);
var personalWishlistCreatedById = (string) personalWishlistsPageItems[0].createdById;
Console.WriteLine(personalWishlistsPageItems[0].id);
Console.WriteLine(personalWishlistsPageItems[0].name);
Console.WriteLine(personalWishlistsPageItems[0].type);
Assert.NotEmpty(personalWishlistsPageItems);
Assert.Equal(user.Id, personalWishlistCreatedById);
}
[Fact]
public async Task GetPersonalWishlist_ValidWishlistIdOrAuthorizedAccess_ReturnsWishlistDto()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var query = new
{
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
variables = new
{
wishlistId = TESTING_WISHLIST_ID
}
};
var jsonPayload = JsonConvert.SerializeObject(query);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
response.EnsureSuccessStatusCode();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
var personalWishlistId = (string) document.data.personalWishlist.id;
var personalWishlistName = (string) document.data.personalWishlist.name;
var personalWishlistType = (string) document.data.personalWishlist.type;
var personalWishlistCreatedById = (string) document.data.personalWishlist.createdById;
Assert.Equal(TESTING_WISHLIST_ID, personalWishlistId);
Assert.Equal("Gaming PC", personalWishlistName);
Assert.Equal(WishlistTypes.Product.ToString(), personalWishlistType);
Assert.Equal(user.Id, personalWishlistCreatedById);
}
[Fact]
public async Task AddMessageToPersonalWishlist_ValidMessageModel_ReturnsNewMessageModel()
{
@ -170,6 +204,52 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact]
public async Task GetPersonalWishlist_InvalidWishlistId_ReturnsInternalServerError()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var query = new
{
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
variables = new
{
wishlistId = "1234567890abcdef12345678" // Invalid wishlistId
}
};
var jsonPayload = JsonConvert.SerializeObject(query);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact]
public async Task GetPersonalWishlist_UnAuthorizedAccess_ReturnsInternalServerError()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var query = new
{
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
variables = new
{
wishlistId = "ab6c2c2d9edf39abcd1ef9ab" // Other user's wishlist
}
};
var jsonPayload = JsonConvert.SerializeObject(query);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact]
public async Task AddMessageToPersonalWishlist_InvalidMessageModel_ReturnsInternalServerError()
{