mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-04 16:49:36 +00:00
add wishlist graphql mutations, data seeding and integrational tests
This commit is contained in:
parent
7c6d36122d
commit
60bc38ee37
@ -16,6 +16,7 @@ public static class GraphQlExtention
|
||||
.AddTypeExtension<AccessMutation>()
|
||||
.AddTypeExtension<UsersMutation>()
|
||||
.AddTypeExtension<RolesMutation>()
|
||||
.AddTypeExtension<WishlistsMutation>()
|
||||
.AddAuthorization()
|
||||
.InitializeOnStartup(keepWarm: true);
|
||||
|
||||
|
17
ShoppingAssistantApi.Api/Mutations/WishlistsMutation.cs
Normal file
17
ShoppingAssistantApi.Api/Mutations/WishlistsMutation.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using ShoppingAssistantApi.Application.IServices;
|
||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||
|
||||
namespace ShoppingAssistantApi.Api.Mutations;
|
||||
|
||||
[ExtendObjectType(OperationTypeNames.Mutation)]
|
||||
public class WishlistsMutation
|
||||
{
|
||||
public Task<WishlistDto> StartPersonalWishlist(WishlistCreateDto dto, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.StartPersonalWishlistAsync(dto, cancellationToken);
|
||||
|
||||
public Task<MessageDto> AddMessageToPersonalWishlist(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
|
||||
}
|
@ -1,41 +1,42 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using ShoppingAssistantApi.Application.GlobalInstances;
|
||||
using ShoppingAssistantApi.Application.IServices;
|
||||
using ShoppingAssistantApi.Application.IServices.Identity;
|
||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||
using ShoppingAssistantApi.Application.Models.Identity;
|
||||
using ShoppingAssistantApi.Domain.Entities;
|
||||
using ShoppingAssistantApi.Domain.Enums;
|
||||
using ShoppingAssistantApi.Persistance.Database;
|
||||
|
||||
namespace ShoppingAssistantApi.Persistance.PersistanceExtentions;
|
||||
|
||||
public class DbInitialaizer
|
||||
{
|
||||
private readonly IUsersService _usersService;
|
||||
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
private readonly IRolesService _rolesService;
|
||||
|
||||
private readonly ITokensService _tokensService;
|
||||
|
||||
private readonly IMongoCollection<Wishlist> _wishlistCollection;
|
||||
|
||||
public IEnumerable<RoleDto> Roles { get; set; }
|
||||
|
||||
public DbInitialaizer(IServiceProvider serviceProvider)
|
||||
{
|
||||
this._usersService = serviceProvider.GetService<IUsersService>();
|
||||
this._rolesService = serviceProvider.GetService<IRolesService>();
|
||||
this._userManager = serviceProvider.GetService<IUserManager>();
|
||||
this._tokensService = serviceProvider.GetService<ITokensService>();
|
||||
_usersService = serviceProvider.GetService<IUsersService>();
|
||||
_rolesService = serviceProvider.GetService<IRolesService>();
|
||||
_userManager = serviceProvider.GetService<IUserManager>();
|
||||
_tokensService = serviceProvider.GetService<ITokensService>();
|
||||
_wishlistCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Wishlist>("Wishlists");
|
||||
}
|
||||
|
||||
public async
|
||||
Task
|
||||
InitialaizeDb(CancellationToken cancellationToken)
|
||||
public async Task InitialaizeDb(CancellationToken cancellationToken)
|
||||
{
|
||||
await this.AddRoles(cancellationToken);
|
||||
await this.AddUsers(cancellationToken);
|
||||
await AddRoles(cancellationToken);
|
||||
await AddUsers(cancellationToken);
|
||||
await AddWishlistsWithMessages(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddUsers(CancellationToken cancellationToken)
|
||||
@ -73,7 +74,7 @@ InitialaizeDb(CancellationToken cancellationToken)
|
||||
_userManager.AccessGuestAsync(guestModel5, cancellationToken)
|
||||
);
|
||||
|
||||
var guests = await this._usersService.GetUsersPageAsync(1, 4, cancellationToken);
|
||||
var guests = await _usersService.GetUsersPageAsync(1, 4, cancellationToken);
|
||||
var guestsResult = guests.Items.ToList();
|
||||
|
||||
var user1 = new UserDto
|
||||
@ -123,7 +124,7 @@ InitialaizeDb(CancellationToken cancellationToken)
|
||||
RefreshToken = _tokensService.GenerateRefreshToken(),
|
||||
RefreshTokenExpiryDate = DateTime.Now.AddDays(7),
|
||||
};
|
||||
|
||||
|
||||
GlobalUser.Id = ObjectId.Parse(user1.Id);
|
||||
await _userManager.UpdateAsync(user1, cancellationToken);
|
||||
|
||||
@ -158,4 +159,54 @@ InitialaizeDb(CancellationToken cancellationToken)
|
||||
var dto2 = await _rolesService.AddRoleAsync(role2, cancellationToken);
|
||||
var dto3 = await _rolesService.AddRoleAsync(role3, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddWishlistsWithMessages(CancellationToken cancellationToken)
|
||||
{
|
||||
var usersPage = await _usersService.GetUsersPageAsync(1, 2, cancellationToken);
|
||||
var userList = usersPage.Items.ToList();
|
||||
|
||||
var wishlists = new Wishlist[]
|
||||
{
|
||||
new Wishlist
|
||||
{
|
||||
Name = "Grandma's Birthday Gift",
|
||||
Type = WishlistTypes.Gift.ToString(),
|
||||
UserId = ObjectId.Parse(userList[0].Id),
|
||||
Messages = new Message[]
|
||||
{
|
||||
new Message
|
||||
{
|
||||
Text = "Prompt",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Answer",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
},
|
||||
}
|
||||
},
|
||||
new Wishlist
|
||||
{
|
||||
Name = "Gaming PC",
|
||||
Type = WishlistTypes.Product.ToString(),
|
||||
UserId = ObjectId.Parse(userList[1].Id),
|
||||
Messages = new Message[]
|
||||
{
|
||||
new Message
|
||||
{
|
||||
Text = "Prompt",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Answer",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await _wishlistCollection.InsertManyAsync(wishlists);
|
||||
}
|
||||
}
|
||||
|
95
ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs
Normal file
95
ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using ShoppingAssistantApi.Tests.TestExtentions;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Xunit;
|
||||
using Newtonsoft.Json;
|
||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||
using ShoppingAssistantApi.Domain.Enums;
|
||||
|
||||
namespace ShoppingAssistantApi.Tests.Tests;
|
||||
|
||||
[Collection("Tests")]
|
||||
public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public WishlistsTests(TestingFactory<Program> factory)
|
||||
{
|
||||
_httpClient = factory.CreateClient();
|
||||
factory.InitialaizeData().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartPersonalWishlistAndAddMessageAsync_ValidWishlistAndMessageModels_ReturnsNewWishlistAndMessageModels()
|
||||
{
|
||||
var tokensModel = await AccessExtention.CreateGuest(new Guid().ToString(), _httpClient);
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
|
||||
var user = await UserExtention.GetCurrentUser(_httpClient);
|
||||
|
||||
var startPersonalWishlistMutation = new
|
||||
{
|
||||
query = "mutation startPersonalWishlist($dto: WishlistCreateDtoInput!) { startPersonalWishlist (dto: $dto) { id, name, type, createdById } }",
|
||||
variables = new
|
||||
{
|
||||
dto = new
|
||||
{
|
||||
firstMessageText = "First message",
|
||||
type = WishlistTypes.Product.ToString()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var jsonPayload = JsonConvert.SerializeObject(startPersonalWishlistMutation);
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
using var startPersonalWishlistResponse = await _httpClient.PostAsync("graphql", content);
|
||||
startPersonalWishlistResponse.EnsureSuccessStatusCode();
|
||||
Assert.Equal(HttpStatusCode.OK, startPersonalWishlistResponse.StatusCode);
|
||||
|
||||
var responseString = await startPersonalWishlistResponse.Content.ReadAsStringAsync();
|
||||
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
|
||||
|
||||
var wishlistId = (string) document.data.startPersonalWishlist.id;
|
||||
var wishlistCreatedById = (string) document.data.startPersonalWishlist.createdById;
|
||||
var wishlistType = (string) document.data.startPersonalWishlist.type;
|
||||
var wishlistName = (string) document.data.startPersonalWishlist.name;
|
||||
|
||||
Assert.Equal(user.Id, wishlistCreatedById);
|
||||
Assert.Equal(WishlistTypes.Product.ToString(), wishlistType);
|
||||
Assert.Equal($"{WishlistTypes.Product} Search", wishlistName);
|
||||
|
||||
const string MESSAGE_TEXT = "Second Message";
|
||||
|
||||
var addMessageToPersonalWishlistMutation = new
|
||||
{
|
||||
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = wishlistId,
|
||||
dto = new
|
||||
{
|
||||
text = MESSAGE_TEXT,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
jsonPayload = JsonConvert.SerializeObject(addMessageToPersonalWishlistMutation);
|
||||
content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
using var addMessageToPersonalWishlistResponse = await _httpClient.PostAsync("graphql", content);
|
||||
addMessageToPersonalWishlistResponse.EnsureSuccessStatusCode();
|
||||
Assert.Equal(HttpStatusCode.OK, addMessageToPersonalWishlistResponse.StatusCode);
|
||||
|
||||
responseString = await addMessageToPersonalWishlistResponse.Content.ReadAsStringAsync();
|
||||
document = JsonConvert.DeserializeObject<dynamic>(responseString);
|
||||
|
||||
var messageRole = (string) document.data.addMessageToPersonalWishlist.role;
|
||||
var messageText = (string) document.data.addMessageToPersonalWishlist.text;
|
||||
var messageCreatedById = (string) document.data.addMessageToPersonalWishlist.createdById;
|
||||
|
||||
Assert.Equal(MessageRoles.User.ToString(), messageRole);
|
||||
Assert.Equal(MESSAGE_TEXT, messageText);
|
||||
Assert.Equal(user.Id, messageCreatedById);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user