Skip to content

Commit

Permalink
test: cover signalr notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Oct 11, 2020
1 parent 5974a2b commit 5b12f67
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
using Aguacongas.IdentityServer.EntityFramework.Store;
using Aguacongas.IdentityServer.Store;
using Aguacongas.TheIdServer.BlazorApp;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Testing;
using Microsoft.EntityFrameworkCore;
using RichardSzalay.MockHttp;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2020 @Olivier Lefebvre
using Aguacongas.AspNetCore.Authentication;
using Aguacongas.AspNetCore.Authentication.EntityFramework;
using Aguacongas.IdentityServer.Abstractions;
using Aguacongas.IdentityServer.Admin.Http.Store;
using Aguacongas.IdentityServer.Admin.Services;
using Aguacongas.IdentityServer.Store;
using Aguacongas.IdentityServer.Store.Entity;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
Expand All @@ -19,6 +28,20 @@ namespace Aguacongas.TheIdServer.IntegrationTest
{
public class SchemeChangeSubscriberTest
{
[Fact]
public void Constructor_should_throw_on_args_null()
{
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(null, null, null, null, null, null));
var hubConnectionFactory = new HubConnectionFactory(new Mock<IConfiguration>().Object, new Mock<IServiceProvider>().Object, new NullLogger<HubConnectionFactory>());
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, null, null, null, null, null));
var manager = new NoPersistentDynamicManager<SchemeDefinition>(new Mock<IAuthenticationSchemeProvider>().Object, new OptionsMonitorCacheWrapperFactory(new Mock<IServiceProvider>().Object), Array.Empty<Type>());
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, manager, null, null, null, null));
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, manager, new Mock<IDynamicProviderStore<SchemeDefinition>>().Object, null, null, null));
var wrapper1 = new KeyManagerWrapper<Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.IAuthenticatedEncryptorDescriptor>(new Mock<IKeyManager>().Object, new Mock<IDefaultKeyResolver>().Object, new Mock<IProviderClient>().Object);
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, manager, new Mock<IDynamicProviderStore<SchemeDefinition>>().Object, wrapper1, null, null));
var wrapper2 = new KeyManagerWrapper<IdentityServer.KeysRotation.RsaEncryptorDescriptor>(new Mock<IKeyManager>().Object, new Mock<IDefaultKeyResolver>().Object, new Mock<IProviderClient>().Object);
Assert.Throws<ArgumentNullException>(() => new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, manager, new Mock<IDynamicProviderStore<SchemeDefinition>>().Object, wrapper1, wrapper2, null));
}

[Fact]
public async Task Subscribe_should_subcribe_to_hub_events()
Expand All @@ -30,7 +53,7 @@ public async Task Subscribe_should_subcribe_to_hub_events()
["ApiAuthentication:Authority"] = "http://localhost",
["SignalR:HubUrl"] = "http://localhost/providerhub",
["SignalR:UseMessagePack"] = "false",
["Seed"] = "false"
["Seed"] = "true"
};

TestServer server = null;
Expand All @@ -50,6 +73,13 @@ public async Task Subscribe_should_subcribe_to_hub_events()
var store = provider.GetRequiredService<IAdminStore<ExternalProvider>>();
var serializer = provider.GetRequiredService<IAuthenticationSchemeOptionsSerializer>();

var manager = new NoPersistentDynamicManager<SchemeDefinition>(new Mock<IAuthenticationSchemeProvider>().Object, new OptionsMonitorCacheWrapperFactory(new Mock<IServiceProvider>().Object), Array.Empty<Type>());
var wrapper1 = new KeyManagerWrapper<Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.IAuthenticatedEncryptorDescriptor>(new Mock<IKeyManager>().Object, new Mock<IDefaultKeyResolver>().Object, new Mock<IProviderClient>().Object);
var wrapper2 = new KeyManagerWrapper<IdentityServer.KeysRotation.RsaEncryptorDescriptor>(new Mock<IKeyManager>().Object, new Mock<IDefaultKeyResolver>().Object, new Mock<IProviderClient>().Object);
var hubConnectionFactory = new HubConnectionFactory(provider.GetRequiredService<IConfiguration>(), provider, new NullLogger<HubConnectionFactory>());
var subscriber = new SchemeChangeSubscriber<SchemeDefinition>(hubConnectionFactory, manager, new Mock<IDynamicProviderStore<SchemeDefinition>>().Object, wrapper1, wrapper2, new NullLogger<SchemeChangeSubscriber<SchemeDefinition>>());
await subscriber.SubscribeAsync(default).ConfigureAwait(false);

var extProvider = new ExternalProvider
{
DisplayName = "Google",
Expand All @@ -59,13 +89,24 @@ public async Task Subscribe_should_subcribe_to_hub_events()
SerializedOptions = serializer.SerializeOptions(new GoogleOptions(), typeof(GoogleOptions))
};

await Task.Delay(500).ConfigureAwait(false);

await store.CreateAsync(extProvider).ConfigureAwait(false);
await store.UpdateAsync(extProvider).ConfigureAwait(false);
await store.DeleteAsync("google").ConfigureAwait(false);

var providerClient = provider.GetRequiredService<IProviderClient>();

await providerClient.KeyRevokedAsync(nameof(IAuthenticatedEncryptorDescriptor), Guid.NewGuid().ToString()).ConfigureAwait(false);
await providerClient.KeyRevokedAsync(nameof(RsaEncryptorDescriptor), Guid.NewGuid().ToString()).ConfigureAwait(false);

provider.GetRequiredService<IConfiguration>()["SignalR:HubUrl"] = null;

await store.CreateAsync(extProvider).ConfigureAwait(false);
await store.UpdateAsync(extProvider).ConfigureAwait(false);
await store.DeleteAsync("google").ConfigureAwait(false);

await Task.Delay(1000).ConfigureAwait(false);
await providerClient.KeyRevokedAsync("test", "test").ConfigureAwait(false);
}

class MockHttpMessageHandler : DelegatingHandler
Expand Down

0 comments on commit 5b12f67

Please sign in to comment.