unit testing
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using isn;
|
||||
using isnd.Data;
|
||||
using isnd.Entities;
|
||||
@ -31,6 +32,10 @@ namespace isnd.tests
|
||||
private IsndSettings siteSettings;
|
||||
|
||||
public IDataProtector DataProtector { get; private set; }
|
||||
|
||||
public ApplicationDbContext dbContext { get; private set; }
|
||||
|
||||
public string TestingUserName { get; private set; }
|
||||
public string ProtectedTestingApiKey { get; internal set; }
|
||||
public ApplicationUser TestingUser { get; private set; }
|
||||
|
||||
@ -49,19 +54,19 @@ namespace isnd.tests
|
||||
{
|
||||
var builder = WebHost.CreateDefaultBuilder(new string[0]);
|
||||
|
||||
// .UseContentRoot("../../../../../src/isnd")
|
||||
builder.UseStartup(typeof(Startup))
|
||||
.ConfigureAppConfiguration((builderContext, config) =>
|
||||
{
|
||||
config.AddJsonFile("appsettings.json", true);
|
||||
config.AddJsonFile("appsettings.Development.json", false);
|
||||
});
|
||||
// .UseContentRoot("../../../../../src/isnd")
|
||||
builder.UseStartup(typeof(Startup))
|
||||
.ConfigureAppConfiguration((builderContext, config) =>
|
||||
{
|
||||
config.AddJsonFile("appsettings.json", true);
|
||||
config.AddJsonFile("appsettings.Development.json", false);
|
||||
});
|
||||
|
||||
Host = builder.Build();
|
||||
|
||||
var logFactory = Host.Services.GetRequiredService<ILoggerFactory>();
|
||||
Logger = logFactory.CreateLogger<WebServerFixture>();
|
||||
|
||||
|
||||
Host.Start(); //Starts listening on the configured addresses.
|
||||
var server = Host.Services.GetRequiredService<IServer>();
|
||||
|
||||
@ -76,38 +81,47 @@ namespace isnd.tests
|
||||
DataProtector = Host.Services.GetRequiredService<IDataProtectionProvider>()
|
||||
.CreateProtector(siteSettings.ProtectionTitle);
|
||||
|
||||
var dbContext = Host.Services.GetRequiredService<ApplicationDbContext>();
|
||||
string testingUserName = "Tester";
|
||||
TestingUser = dbContext.Users.FirstOrDefault(u=>u.UserName==testingUserName);
|
||||
if (TestingUser==null)
|
||||
{
|
||||
var userManager = Host.Services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
TestingUser = new ApplicationUser
|
||||
{
|
||||
UserName=testingUserName
|
||||
};
|
||||
|
||||
var result = userManager.CreateAsync(TestingUser).Result;
|
||||
|
||||
Assert.True(result.Succeeded);
|
||||
TestingUser = dbContext.Users.FirstOrDefault(u=>u.UserName==testingUserName);
|
||||
}
|
||||
var testKey = dbContext.ApiKeys.FirstOrDefault(k=>k.UserId==TestingUser.Id);
|
||||
dbContext = Host.Services.GetRequiredService<ApplicationDbContext>();
|
||||
TestingUserName = "Tester";
|
||||
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == TestingUserName);
|
||||
EnsureUser(TestingUserName);
|
||||
var testKey = dbContext.ApiKeys.FirstOrDefault(k => k.UserId == TestingUser.Id);
|
||||
if (testKey == null)
|
||||
{
|
||||
var keyProvider = Host.Services.GetService<IApiKeyProvider>();
|
||||
var apiKeyQuery = new Data.ApiKeys.CreateModel
|
||||
{
|
||||
Name = "Testing Key",
|
||||
UserId = TestingUser.Id,
|
||||
ValidityPeriodInDays = 1
|
||||
{
|
||||
Name = "Testing Key",
|
||||
UserId = TestingUser.Id,
|
||||
ValidityPeriodInDays = 1
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
testKey = keyProvider.CreateApiKeyAsync(apiKeyQuery).Result;
|
||||
|
||||
}
|
||||
ProtectedTestingApiKey = DataProtector.Protect(testKey.Id);
|
||||
|
||||
ServicePointManager.ServerCertificateValidationCallback =
|
||||
(sender, cert, chain, sslPolicyErrors) => true;
|
||||
|
||||
}
|
||||
|
||||
public void EnsureUser(string testingUserName)
|
||||
{
|
||||
if (TestingUser == null)
|
||||
{
|
||||
var userManager = Host.Services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
TestingUser = new ApplicationUser
|
||||
{
|
||||
UserName = testingUserName
|
||||
};
|
||||
|
||||
var result = userManager.CreateAsync(TestingUser).Result;
|
||||
|
||||
Assert.True(result.Succeeded);
|
||||
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == testingUserName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user