59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using GameAuthApi.Controllers;
|
|
using GameAuthApi.Models;
|
|
using GameAuthApi.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Security.Claims;
|
|
|
|
public class ChatControllerTests
|
|
{
|
|
private static IConfiguration cfg = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string,string>
|
|
{
|
|
{"MongoDB:ConnectionString","mongodb://localhost"},
|
|
{"MongoDB:DatabaseName","test"}
|
|
}).Build();
|
|
|
|
private static ControllerContext ContextWithUser(string id)
|
|
{
|
|
var ctx = new DefaultHttpContext();
|
|
ctx.User = new ClaimsPrincipal(new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier,id)
|
|
}, "mock"));
|
|
return new ControllerContext { HttpContext = ctx };
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Send_EmptyContent_ReturnsBadRequest()
|
|
{
|
|
var svc = new Mock<ChatService>(cfg);
|
|
var controller = new ChatController(svc.Object)
|
|
{
|
|
ControllerContext = ContextWithUser("u")
|
|
};
|
|
|
|
var result = await controller.Send(new ChatMessageRequest { Content = "" });
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Recent_ReturnsOk()
|
|
{
|
|
var list = new List<ChatMessage>();
|
|
var svc = new Mock<ChatService>(cfg);
|
|
svc.Setup(s => s.GetRecentAsync(5)).ReturnsAsync(list);
|
|
var controller = new ChatController(svc.Object)
|
|
{
|
|
ControllerContext = ContextWithUser("u")
|
|
};
|
|
|
|
var result = await controller.Recent(5);
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(list, ok.Value);
|
|
}
|
|
}
|