forked from Access4Learning/sif3-framework-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from nsip/release-21
Enabled session tokens to be stored in a database
- Loading branch information
Showing
77 changed files
with
2,848 additions
and
1,196 deletions.
There are no files selected for viewing
Binary file modified
BIN
+8 KB
(170%)
Code/Sif.Framework.Tests/Sif.Framework.EntityFramework.Tests/EntityFrameworkTest.db
Binary file not shown.
672 changes: 672 additions & 0 deletions
672
Code/Sif.Framework.Tests/Sif.Framework.EntityFramework.Tests/SessionTest.cs
Large diffs are not rendered by default.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
Code/Sif.Framework.Tests/Sif.Framework.EntityFramework.Tests/SetUp/UnitTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2021 Systemic Pty Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Sif.Framework.Model.Sessions; | ||
using System; | ||
using Tardigrade.Framework.Patterns.DependencyInjection; | ||
using Tardigrade.Framework.Persistence; | ||
|
||
namespace Sif.Framework.EntityFramework.Tests.SetUp | ||
{ | ||
/// <summary> | ||
/// <a href="https://stackoverflow.com/questions/50921675/dependency-injection-in-xunit-project">Dependency injection in XUnit project</a> | ||
/// </summary> | ||
public class UnitTestFixture : IDisposable | ||
{ | ||
private readonly IRepository<Session, Guid> repository; | ||
|
||
public IServiceContainer Container { get; } | ||
|
||
public Session Reference { get; } | ||
|
||
public UnitTestFixture() | ||
{ | ||
Container = new UnitTestServiceContainer(); | ||
|
||
// Create a reference session for testing. | ||
Reference = new Session | ||
{ | ||
ApplicationKey = "Sif3DemoConsumer", | ||
EnvironmentUrl = "http://localhost:62921/api/environments/bca76787-48ae-4532-b851-fd7099a4098b", | ||
Id = Guid.NewGuid(), | ||
QueueId = Guid.NewGuid().ToString(), | ||
SessionToken = "U2lmM0RlbW9Db25zdW1lcjo6OlNpZjNGcmFtZXdvcms=", | ||
SolutionId = "Sif3Framework", | ||
SubscriptionId = Guid.NewGuid().ToString() | ||
}; | ||
|
||
repository = Container.GetService<IRepository<Session, Guid>>(); | ||
_ = repository.Create(Reference); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Delete the reference user. | ||
repository.Delete(Reference); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...Sif.Framework.Tests/Sif.Framework.EntityFramework.Tests/SetUp/UnitTestServiceContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2021 Systemic Pty Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Sif.Framework.EntityFramework.Data; | ||
using Sif.Framework.EntityFramework.Services.Sessions; | ||
using Sif.Framework.Model.Sessions; | ||
using Sif.Framework.Service.Sessions; | ||
using System; | ||
using System.Data.Entity; | ||
using Tardigrade.Framework.EntityFramework; | ||
using Tardigrade.Framework.Patterns.DependencyInjection; | ||
using Tardigrade.Framework.Persistence; | ||
using Tardigrade.Framework.Services; | ||
|
||
namespace Sif.Framework.EntityFramework.Tests.SetUp | ||
{ | ||
internal class UnitTestServiceContainer : MicrosoftServiceContainer | ||
{ | ||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
// Inject business services. | ||
services.AddScoped<DbContext>(_ => new SessionDbContext("name=SettingsDb")); | ||
services.AddScoped<IRepository<Session, Guid>, Repository<Session, Guid>>(); | ||
services.AddScoped<IObjectService<Session, Guid>, ObjectService<Session, Guid>>(); | ||
services.AddScoped<ISessionService, SessionService>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Code/Sif.Framework.Tests/Sif.Framework.Shared/DataFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2021 Systemic Pty Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Bogus; | ||
using Sif.Framework.Model.Sessions; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
namespace Sif.Framework.Shared | ||
{ | ||
public class DataFactory | ||
{ | ||
private const char LowercaseZ = '\x7a'; | ||
private const char Zero = '\x30'; | ||
|
||
private static readonly string[] SifType = {"Consumer", "Provider"}; | ||
|
||
private static readonly Faker<Session> FakeSessions = new Faker<Session>() | ||
.RuleFor( | ||
u => u.ApplicationKey, | ||
f => $"{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(f.Lorem.Word())}{f.PickRandom(SifType)}") | ||
.RuleFor( | ||
u => u.EnvironmentUrl, | ||
f => $"{f.Internet.Protocol()}://{f.Internet.DomainName()}/api/environments/{f.Random.Guid()}") | ||
.RuleFor(u => u.Id, f => f.Random.Guid()) | ||
.RuleFor(u => u.QueueId, f => f.Random.Guid().ToString()) | ||
.RuleFor(u => u.SessionToken, f => f.Random.String(44, Zero, LowercaseZ)) | ||
.RuleFor(u => u.SolutionId, f => f.Lorem.Word()) | ||
.RuleFor(u => u.SubscriptionId, f => f.Random.Guid().ToString()); | ||
|
||
public static Session CreateSession() => FakeSessions.Generate(); | ||
|
||
public static IList<Session> CreateSessions(uint count) => FakeSessions.Generate((int) count); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Code/Sif.Framework.Tests/Sif.Framework.Shared/Sif.Framework.Shared.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Bogus" Version="32.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Sif3Framework\Sif.Framework\Sif.Framework.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Code/Sif3Framework/Sif.Framework.EntityFramework/Data/SessionDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2021 Systemic Pty Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Sif.Framework.Model.Sessions; | ||
using System.Data.Entity; | ||
|
||
namespace Sif.Framework.EntityFramework.Data | ||
{ | ||
/// <summary> | ||
/// Entity Framework database context for use with session data stored in a database. | ||
/// </summary> | ||
public class SessionDbContext : DbContext | ||
{ | ||
/// <summary> | ||
/// Create an instance of this class. | ||
/// </summary> | ||
/// <param name="nameOrConnectionString">Name of the database connection or database connection string.</param> | ||
public SessionDbContext(string nameOrConnectionString) : base(nameOrConnectionString) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Sessions. | ||
/// </summary> | ||
public DbSet<Session> Sessions { get; set; } | ||
} | ||
} |
Oops, something went wrong.