mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
[issue #139] client lookup and stats api
This commit is contained in:
203
Tests/ApplicationTests/ApiTests.cs
Normal file
203
Tests/ApplicationTests/ApiTests.cs
Normal file
@ -0,0 +1,203 @@
|
||||
using ApplicationTests.Fixtures;
|
||||
using FakeItEasy;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Helpers;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using Stats.Dtos;
|
||||
using StatsWeb.Dtos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WebfrontCore.Controllers.API;
|
||||
using WebfrontCore.Controllers.API.Dtos;
|
||||
using WebfrontCore.Controllers.API.Validation;
|
||||
|
||||
namespace ApplicationTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class ApiTests
|
||||
{
|
||||
private IServiceProvider serviceProvider;
|
||||
private IDatabaseContextFactory contextFactory;
|
||||
private ClientController clientController;
|
||||
private StatsWeb.API.StatsController statsController;
|
||||
private IResourceQueryHelper<FindClientRequest, FindClientResult> fakeClientQueryHelper;
|
||||
private IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> fakeStatsQueryHelper;
|
||||
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var collection = new ServiceCollection();
|
||||
|
||||
collection.AddMvc()
|
||||
.AddFluentValidation();
|
||||
|
||||
serviceProvider = collection.AddSingleton<ClientController>()
|
||||
.AddSingleton<StatsWeb.API.StatsController>()
|
||||
.AddSingleton(A.Fake<IResourceQueryHelper<FindClientRequest, FindClientResult>>())
|
||||
.AddSingleton(A.Fake<IResourceQueryHelper<StatsInfoRequest, StatsInfoResult>>())
|
||||
.AddTransient<IValidator<FindClientRequest>, FindClientRequestValidator>()
|
||||
.BuildBase()
|
||||
.BuildServiceProvider();
|
||||
|
||||
clientController = serviceProvider.GetRequiredService<ClientController>();
|
||||
statsController = serviceProvider.GetRequiredService<StatsWeb.API.StatsController>();
|
||||
contextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
||||
fakeClientQueryHelper = serviceProvider.GetRequiredService<IResourceQueryHelper<FindClientRequest, FindClientResult>>();
|
||||
fakeStatsQueryHelper = serviceProvider.GetRequiredService<IResourceQueryHelper<StatsInfoRequest, StatsInfoResult>>();
|
||||
}
|
||||
|
||||
#region CLIENT_CONTROLLER
|
||||
[Test]
|
||||
public async Task Test_ClientController_FindAsync_Happy()
|
||||
{
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Name = "test"
|
||||
};
|
||||
|
||||
int expectedClientId = 123;
|
||||
|
||||
A.CallTo(() => fakeClientQueryHelper.QueryResource(A<FindClientRequest>.Ignored))
|
||||
.Returns(Task.FromResult(new ResourceQueryHelperResult<FindClientResult>()
|
||||
{
|
||||
Results = new[]
|
||||
{
|
||||
new FindClientResult()
|
||||
{
|
||||
ClientId = expectedClientId
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
var result = await clientController.FindAsync(query);
|
||||
Assert.IsInstanceOf<OkObjectResult>(result);
|
||||
|
||||
var viewResult = (result as OkObjectResult).Value as FindClientResponse;
|
||||
Assert.NotNull(viewResult);
|
||||
Assert.AreEqual(expectedClientId, viewResult.Clients.First().ClientId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_ClientController_FindAsync_InvalidModelState()
|
||||
{
|
||||
var query = new FindClientRequest();
|
||||
|
||||
clientController.ModelState.AddModelError("test", "test");
|
||||
var result = await clientController.FindAsync(query);
|
||||
Assert.IsInstanceOf<BadRequestObjectResult>(result);
|
||||
clientController.ModelState.Clear();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_ClientController_FindAsync_Exception()
|
||||
{
|
||||
string expectedExceptionMessage = "failure";
|
||||
int expectedStatusCode = 500;
|
||||
var query = new FindClientRequest();
|
||||
A.CallTo(() => fakeClientQueryHelper.QueryResource(A<FindClientRequest>.Ignored))
|
||||
.Throws(new Exception(expectedExceptionMessage));
|
||||
|
||||
var result = await clientController.FindAsync(query);
|
||||
Assert.IsInstanceOf<ObjectResult>(result);
|
||||
|
||||
var statusResult = (result as ObjectResult);
|
||||
Assert.AreEqual(expectedStatusCode, statusResult.StatusCode);
|
||||
//Assert.IsTrue((statusResult.Value as ErrorResponse).Messages.Contains(expectedExceptionMessage));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region STATS_CONTROLLER
|
||||
[Test]
|
||||
public async Task Test_StatsController_ClientStats_Happy()
|
||||
{
|
||||
var client = ClientGenerators.CreateBasicClient(null);
|
||||
|
||||
var query = new StatsInfoRequest
|
||||
{
|
||||
ClientId = client.ClientId
|
||||
};
|
||||
|
||||
var queryResult = new ResourceQueryHelperResult<StatsInfoResult>()
|
||||
{
|
||||
Results = new[]
|
||||
{
|
||||
new StatsInfoResult
|
||||
{
|
||||
Deaths = 1,
|
||||
Kills = 1,
|
||||
LastPlayed = DateTime.Now,
|
||||
Performance = 100,
|
||||
Ranking = 10,
|
||||
ScorePerMinute = 500,
|
||||
ServerGame = "IW4",
|
||||
ServerId = 123,
|
||||
ServerName = "IW4Host",
|
||||
TotalSecondsPlayed = 100
|
||||
}
|
||||
},
|
||||
TotalResultCount = 1,
|
||||
RetrievedResultCount = 1
|
||||
};
|
||||
|
||||
A.CallTo(() => fakeStatsQueryHelper.QueryResource(A<StatsInfoRequest>.Ignored))
|
||||
.Returns(Task.FromResult(queryResult));
|
||||
|
||||
var result = await statsController.ClientStats(query.ClientId.Value);
|
||||
Assert.IsInstanceOf<OkObjectResult>(result);
|
||||
|
||||
var viewResult = (result as OkObjectResult).Value as IEnumerable<StatsInfoResult>;
|
||||
Assert.NotNull(viewResult);
|
||||
Assert.AreEqual(queryResult.Results, viewResult);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_StatsController_ClientStats_InvalidModelState()
|
||||
{
|
||||
statsController.ModelState.AddModelError("test", "test");
|
||||
var result = await statsController.ClientStats(1);
|
||||
Assert.IsInstanceOf<BadRequestObjectResult>(result);
|
||||
statsController.ModelState.Clear();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_StatsController_ClientStats_Exception()
|
||||
{
|
||||
string expectedExceptionMessage = "failure";
|
||||
int expectedStatusCode = 500;
|
||||
|
||||
A.CallTo(() => fakeStatsQueryHelper.QueryResource(A<StatsInfoRequest>.Ignored))
|
||||
.Throws(new Exception(expectedExceptionMessage));
|
||||
|
||||
var result = await statsController.ClientStats(1);
|
||||
Assert.IsInstanceOf<ObjectResult>(result);
|
||||
|
||||
var statusResult = (result as ObjectResult);
|
||||
Assert.AreEqual(expectedStatusCode, statusResult.StatusCode);
|
||||
Assert.IsTrue((statusResult.Value as ErrorResponse).Messages.Contains(expectedExceptionMessage));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_StatsController_ClientStats_NotFound()
|
||||
{
|
||||
var queryResult = new ResourceQueryHelperResult<StatsInfoResult>()
|
||||
{
|
||||
Results = new List<StatsInfoResult>()
|
||||
};
|
||||
|
||||
A.CallTo(() => fakeStatsQueryHelper.QueryResource(A<StatsInfoRequest>.Ignored))
|
||||
.Returns(Task.FromResult(queryResult));
|
||||
|
||||
var result = await statsController.ClientStats(1);
|
||||
Assert.IsInstanceOf<NotFoundResult>(result);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -37,6 +37,7 @@ namespace ApplicationTests
|
||||
|
||||
serviceProvider = new ServiceCollection()
|
||||
.BuildBase(new EventHandlerMock(true))
|
||||
.AddSingleton(A.Fake<ClientService>())
|
||||
.BuildServiceProvider()
|
||||
.SetupTestHooks();
|
||||
|
||||
|
@ -42,7 +42,6 @@ namespace ApplicationTests
|
||||
.AddSingleton(A.Fake<IRConParser>())
|
||||
.AddSingleton(A.Fake<IParserRegexFactory>())
|
||||
.AddSingleton<DataFileLoader>()
|
||||
.AddSingleton(A.Fake<ClientService>())
|
||||
.AddSingleton(A.Fake<IGameLogReaderFactory>())
|
||||
.AddSingleton(eventHandler)
|
||||
.AddSingleton(ConfigurationGenerators.CreateApplicationConfiguration())
|
||||
|
@ -31,6 +31,7 @@ namespace ApplicationTests
|
||||
public void Setup()
|
||||
{
|
||||
serviceProvider = new ServiceCollection().BuildBase()
|
||||
.AddSingleton(A.Fake<ClientService>())
|
||||
.AddSingleton<IScriptCommandFactory, ScriptCommandFactory>()
|
||||
.BuildServiceProvider();
|
||||
fakeManager = serviceProvider.GetRequiredService<IManager>();
|
||||
|
173
Tests/ApplicationTests/ServiceTests.cs
Normal file
173
Tests/ApplicationTests/ServiceTests.cs
Normal file
@ -0,0 +1,173 @@
|
||||
using ApplicationTests.Fixtures;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using SharedLibraryCore.Dtos;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using SharedLibraryCore.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ApplicationTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class ServiceTests
|
||||
{
|
||||
private IServiceProvider serviceProvider;
|
||||
private IDatabaseContextFactory contextFactory;
|
||||
private ClientService clientService;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
serviceProvider = new ServiceCollection()
|
||||
.AddSingleton<ClientService>()
|
||||
.BuildBase()
|
||||
|
||||
.BuildServiceProvider();
|
||||
|
||||
contextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
||||
clientService = serviceProvider.GetRequiredService<ClientService>();
|
||||
}
|
||||
|
||||
#region QUERY_RESOURCE
|
||||
[Test]
|
||||
public async Task Test_QueryClientResource_Xuid()
|
||||
{
|
||||
var client = ClientGenerators.CreateBasicClient(null);
|
||||
client.NetworkId = -1;
|
||||
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Xuid = client.NetworkId.ToString("X")
|
||||
};
|
||||
|
||||
using var context = contextFactory.CreateContext();
|
||||
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.AreEqual(query.Xuid, result.Results.First().Xuid);
|
||||
|
||||
context.Clients.Remove(client);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_QueryClientResource_NameExactMatch()
|
||||
{
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Name = "test"
|
||||
};
|
||||
|
||||
using var context = contextFactory.CreateContext();
|
||||
var client = ClientGenerators.CreateBasicClient(null);
|
||||
client.Name = query.Name;
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.AreEqual(query.Name, result.Results.First().Name);
|
||||
|
||||
context.Clients.Remove(client);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_QueryClientResource_NameCaseInsensitivePartial()
|
||||
{
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Name = "TEST"
|
||||
};
|
||||
|
||||
using var context = contextFactory.CreateContext();
|
||||
var client = ClientGenerators.CreateBasicClient(null);
|
||||
client.Name = "atesticle";
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.IsTrue(result.Results.First().Name.ToUpper().Contains(query.Name));
|
||||
|
||||
context.Clients.Remove(client);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_QueryClientResource_SortDirection()
|
||||
{
|
||||
var firstClient = ClientGenerators.CreateBasicClient(null);
|
||||
firstClient.ClientId = 0;
|
||||
firstClient.NetworkId = -1;
|
||||
firstClient.LastConnection = DateTime.Now.AddHours(-1);
|
||||
firstClient.Name = "test";
|
||||
var secondClient = ClientGenerators.CreateBasicClient(null);
|
||||
secondClient.ClientId = 0;
|
||||
secondClient.NetworkId = -2;
|
||||
secondClient.LastConnection = DateTime.Now;
|
||||
secondClient.Name = firstClient.Name;
|
||||
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Name = firstClient.Name
|
||||
};
|
||||
|
||||
using var context = contextFactory.CreateContext();
|
||||
|
||||
context.Clients.Add(firstClient);
|
||||
context.Clients.Add(secondClient);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.AreEqual(secondClient.NetworkId.ToString("X"), result.Results.First().Xuid);
|
||||
Assert.AreEqual(firstClient.NetworkId.ToString("X"), result.Results.Last().Xuid);
|
||||
|
||||
query.Direction = SortDirection.Ascending;
|
||||
result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.AreEqual(firstClient.NetworkId.ToString("X"), result.Results.First().Xuid);
|
||||
Assert.AreEqual(secondClient.NetworkId.ToString("X"), result.Results.Last().Xuid);
|
||||
|
||||
context.Clients.Remove(firstClient);
|
||||
context.Clients.Remove(secondClient);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Test_QueryClientResource_NoMatch()
|
||||
{
|
||||
var query = new FindClientRequest()
|
||||
{
|
||||
Name = "test"
|
||||
};
|
||||
|
||||
using var context = contextFactory.CreateContext();
|
||||
var client = ClientGenerators.CreateBasicClient(null);
|
||||
client.Name = "client";
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await clientService.QueryResource(query);
|
||||
|
||||
Assert.IsEmpty(result.Results);
|
||||
|
||||
context.Clients.Remove(client);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -14,6 +14,8 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using IW4MAdmin.Plugins.Stats.Helpers;
|
||||
using ApplicationTests.Fixtures;
|
||||
using System.Threading.Tasks;
|
||||
using Stats.Helpers;
|
||||
using Stats.Dtos;
|
||||
|
||||
namespace ApplicationTests
|
||||
{
|
||||
@ -23,6 +25,7 @@ namespace ApplicationTests
|
||||
ILogger logger;
|
||||
private IServiceProvider serviceProvider;
|
||||
private IConfigurationHandlerFactory handlerFactory;
|
||||
private IDatabaseContextFactory contextFactory;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
@ -31,10 +34,13 @@ namespace ApplicationTests
|
||||
handlerFactory = A.Fake<IConfigurationHandlerFactory>();
|
||||
|
||||
serviceProvider = new ServiceCollection()
|
||||
.AddSingleton<StatsResourceQueryHelper>()
|
||||
.BuildBase()
|
||||
.AddSingleton<IW4MAdmin.Plugins.Stats.Plugin>()
|
||||
.BuildServiceProvider();
|
||||
|
||||
contextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
||||
|
||||
void testLog(string msg) => Console.WriteLine(msg);
|
||||
|
||||
A.CallTo(() => logger.WriteError(A<string>.Ignored)).Invokes((string msg) => testLog(msg));
|
||||
@ -155,5 +161,55 @@ namespace ApplicationTests
|
||||
|
||||
await mgr.UpdateStatHistory(target, stats);
|
||||
}
|
||||
|
||||
#region QUERY_HELPER
|
||||
[Test]
|
||||
public async Task Test_StatsQueryHelper_Get()
|
||||
{
|
||||
var queryHelper = serviceProvider.GetRequiredService<StatsResourceQueryHelper>();
|
||||
using var context = contextFactory.CreateContext();
|
||||
|
||||
var server = new EFServer() { ServerId = 1 };
|
||||
var stats = new EFClientStatistics()
|
||||
{
|
||||
Client = ClientGenerators.CreateBasicClient(null),
|
||||
SPM = 100,
|
||||
Server = server
|
||||
};
|
||||
|
||||
var ratingHistory = new EFClientRatingHistory()
|
||||
{
|
||||
Client = stats.Client,
|
||||
Ratings = new[]
|
||||
{
|
||||
new EFRating()
|
||||
{
|
||||
Ranking = 100,
|
||||
Server = server,
|
||||
Newest = true
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
context.Set<EFClientStatistics>().Add(stats);
|
||||
context.Set<EFClientRatingHistory>().Add(ratingHistory);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var query = new StatsInfoRequest()
|
||||
{
|
||||
ClientId = stats.Client.ClientId
|
||||
};
|
||||
var result = await queryHelper.QueryResource(query);
|
||||
|
||||
Assert.IsNotEmpty(result.Results);
|
||||
Assert.AreEqual(stats.SPM, result.Results.First().ScorePerMinute);
|
||||
Assert.AreEqual(ratingHistory.Ratings.First().Ranking, result.Results.First().Ranking);
|
||||
|
||||
context.Set<EFClientStatistics>().Remove(stats);
|
||||
context.Set<EFClientRatingHistory>().Remove(ratingHistory);
|
||||
context.Set<EFServer>().Remove(server);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user