refact and login

This commit is contained in:
Paul Schneider 2026-03-09 02:07:09 +00:00
commit e042e34bf7
78 changed files with 2492 additions and 50576 deletions

View file

@ -5,6 +5,7 @@ using Xunit.Abstractions;
using Yavsc.Server.Models.IT.SourceCode;
using Microsoft.EntityFrameworkCore;
using isnd.tests;
using Yavsc.Server.Models.IT;
namespace yavscTests
{
@ -21,13 +22,21 @@ namespace yavscTests
this._output = output;
}
[Fact]
// FIXME write a scenario from an empty database [Fact]
public void GitClone()
{
using var scope = _serverFixture.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Assert.NotNull (dbContext.Project);
var firstProject = dbContext.Project.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull(dbContext.Project);
Project yavsc = new Project
{
Name = "Yavsc"
};
dbContext.Project.Add(yavsc);
dbContext.SaveChanges();
var firstProject = dbContext.Project.Include(p => p.Repository).FirstOrDefault(
p => p.Name == "Yavsc"
);
Assert.NotNull (firstProject);
var di = new DirectoryInfo(_serverFixture.SiteSettings.GitRepository);
if (!di.Exists) di.Create();

View file

@ -1,6 +1,9 @@
using isnd.tests;
using Xunit.Abstractions;
using IdentityModel.Client;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace yavscTests
{
@ -14,43 +17,39 @@ namespace yavscTests
}
[Theory]
[MemberData(nameof(GetLoginIntentData))]
public async Task TestUserMayLogin
(
string userName,
string password
)
{
}
[Fact]
public async Task ObtainServiceToken()
{
var serverUrl = _serverFixture.Addresses.FirstOrDefault(
u => u.StartsWith("https:")
);
{
var serverUrl = _serverFixture.Addresses.FirstOrDefault(
u => u.StartsWith("https:")
);
String authority = _serverFixture.SiteSettings.Authority;
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync(authority);
if (disco.IsError) throw new Exception(disco.Error);
String authority = _serverFixture.SiteSettings.Authority;
HttpClient client = NewHttpClient();
var disco = await client.GetDiscoveryDocumentAsync(authority);
if (disco.IsError) throw new Exception(disco.Error);
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = _serverFixture.TestClientId,
ClientSecret = _serverFixture.TestClientSecret,
Scope = "test",
GrantType = "client_credentials"
});
/*"mvc";
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";*/
if (response.IsError) throw new Exception(response.Error);
ClientId = "m2m.client", // _serverFixture.TestClientId,
ClientSecret = "511536EF-F270-4058-80CA-1C89C192F69A" //_serverFixture.TestClientSecret,
});
/*"mvc";
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";*/
if (response.IsError) throw new Exception(response.Error);
}
}
private static HttpClient NewHttpClient()
{
return new HttpClient(new BypassSslValidationHandler());
}
[Fact]
[Fact]
public async Task ObtainResourceOwnerPasswordToken()
{
var serverUrl = _serverFixture.Addresses.FirstOrDefault(
@ -58,7 +57,7 @@ namespace yavscTests
);
String authority = _serverFixture.SiteSettings.Authority;
var client = new HttpClient();
var client = NewHttpClient();
var disco = await client.GetDiscoveryDocumentAsync(authority);
if (disco.IsError) throw new Exception(disco.Error);
@ -66,13 +65,13 @@ namespace yavscTests
{
Address = disco.TokenEndpoint,
ClientId = "m2m.client",
ClientSecret = "511536EF-F270-4058-80CA-1C89C192F69A",
ClientId = _serverFixture.TestClientId,
ClientSecret = _serverFixture.TestClientSecret,
UserName = _serverFixture.TestingUserName,
Password = _serverFixture.TestingUserPassword,
Scope = "scope1",
Scope = "test",
Parameters =
{
@ -90,4 +89,23 @@ namespace yavscTests
}
}
internal class BypassSslValidationHandler : HttpClientHandler
{
public BypassSslValidationHandler()
{
// Override validation for this handler only
ServerCertificateCustomValidationCallback = ValidateCertificate;
}
private bool ValidateCertificate(
HttpRequestMessage request,
X509Certificate2 certificate,
X509Chain chain,
SslPolicyErrors errors)
{
// Accept all certificates (bypass validation)
return true;
}
}
}

View file

@ -16,7 +16,7 @@ namespace yavscTests
this.output = output;
}
[Fact]
public void UniquePathesAfterFileNameCleaning()
public void UniquePathsAfterFileNameCleaning()
{
var name1 = "content:///scanned_files/2020-06-02/00.11.02.JPG";
var name2 = "content:///scanned_files/2020-06-02/00.11.03.JPG";

View file

@ -13,6 +13,9 @@ using Microsoft.EntityFrameworkCore;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using IdentityServer8.EntityFramework.Entities;
using IdentityServer8.Models;
using Client = IdentityServer8.EntityFramework.Entities.Client;
namespace isnd.tests
{
@ -33,12 +36,12 @@ namespace isnd.tests
public IServiceProvider Services { get; private set; }
public string TestingUserName { get; private set; }
public string TestingUserPassword { get; private set; }
public string ProtectedTestingApiKey { get; internal set; }
public ApplicationUser TestingUser { get; private set; }
public bool DbCreated { get; internal set; }
public SiteSettings SiteSettings { get => siteSettings; set => siteSettings = value; }
public string TestClientSecret { get; private set; } = "TestClientSecret";
public string TestClientSecret { get; set; }
public WebServerFixture()
{
@ -47,7 +50,7 @@ namespace isnd.tests
public void Dispose()
{
if (app!=null)
if (app != null)
app.StopAsync().Wait();
}
void ConfigureLogger() => Log.Logger = new LoggerConfiguration()
@ -79,17 +82,20 @@ namespace isnd.tests
this.app = builder.ConfigureWebAppServices();
Services = app.Services;
SiteSettings = app.Services.GetRequiredService<IOptions<SiteSettings>>().Value;
using (var migrationScope = app.Services.CreateScope())
{
var db = migrationScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Database.Migrate();
TestingUserName = "Tester";
TestingUserPassword = "test";
TestingUserPassword = "tesT456+*";
TestClientId = "testClientId";
TestingUser = await db.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName);
TestClientSecret = Guid.CreateVersion7().ToString();
EnsureUser(TestingUserName, TestingUserPassword);
AddAuthorizedClient(TestClientId, TestClientSecret);
TestingUser = await db.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName);
}
await app.ConfigurePipeline();
app.UseSession();
@ -108,8 +114,73 @@ namespace isnd.tests
{
Addresses.Add(address);
}
SiteSettings = app.Services.GetRequiredService<IOptions<SiteSettings>>().Value;
}
private void AddAuthorizedClient(string testClientId, string testClientSecret)
{
using (IServiceScope scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Client testingClient = new Client
{
ClientId = testClientId,
AccessTokenLifetime = 3600000,
AccessTokenType = 1,
BackChannelLogoutUri = SiteSettings.Audience,
ClientName = "Testing client",
Enabled = true
};
db.Clients.Add(testingClient);
db.SaveChanges();
ClientSecret secret = new ClientSecret
{
Value = testClientSecret.Sha256(),
ClientId = testingClient.Id
};
db.ClientSecrets.Add(secret);
var testOrigin = new ClientCorsOrigin
{
ClientId = testingClient.Id,
Origin = SiteSettings.Audience
};
db.ClientCorsOrigins.Add(testOrigin);
db.ClientGrantTypes.Add(new ClientGrantType
{
ClientId = testingClient.Id,
GrantType = "client_credentials"
});
db.ClientGrantTypes.Add(new ClientGrantType
{
ClientId = testingClient.Id,
GrantType = "password"
});
db.ClientGrantTypes.Add(new ClientGrantType
{
ClientId = testingClient.Id,
GrantType = "code"
});
db.ClientScopes.Add(new ClientScope
{
ClientId = testingClient.Id,
Scope = "test"
});
db.ApiScopes.Add(new IdentityServer8.EntityFramework.Entities.ApiScope
{
Name = "test",
Enabled = true
});
db.ClientRedirectUris.Add(new ClientRedirectUri
{
ClientId = testingClient.Id,
RedirectUri = SiteSettings.Audience
});
db.SaveChanges();
}
}
public void EnsureUser(string testingUserName, string password)
@ -118,7 +189,7 @@ namespace isnd.tests
{
using IServiceScope scope = app.Services.CreateScope();
var userManager =
var userManager =
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
TestingUser = new ApplicationUser
@ -128,7 +199,7 @@ namespace isnd.tests
EmailConfirmed = true
};
var result = userManager.CreateAsync(TestingUser,password).Result;
var result = userManager.CreateAsync(TestingUser, password).Result;
Assert.True(result.Succeeded);