Skip to content

Commit

Permalink
Merge pull request #43 from nsip/release-21
Browse files Browse the repository at this point in the history
Enabled session tokens to be stored in a database
  • Loading branch information
rafidzal authored May 26, 2021
2 parents 45d3a94 + f0131da commit 8e80f80
Show file tree
Hide file tree
Showing 77 changed files with 2,848 additions and 1,196 deletions.
Binary file not shown.

Large diffs are not rendered by default.

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);
}
}
}
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>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<ItemGroup>
<Compile Include="FrameworkSettingsTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SessionTest.cs" />
<Compile Include="SetUp\UnitTestFixture.cs" />
<Compile Include="SetUp\UnitTestServiceContainer.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions">
Expand Down Expand Up @@ -70,10 +73,19 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Sif3Framework\Sif.Framework.EntityFramework\Sif.Framework.EntityFramework.csproj">
<Project>{983bb129-0fcf-4800-bc80-36da09706a9c}</Project>
<Name>Sif.Framework.EntityFramework</Name>
</ProjectReference>
<ProjectReference Include="..\..\Sif3Framework\Sif.Framework\Sif.Framework.csproj">
<Project>{2b13d050-aa2c-45a6-ba26-8b2706b7e227}</Project>
<Name>Sif.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\Sif.Framework.Shared\Sif.Framework.Shared.csproj">
<Project>{76d0f674-1662-49b6-a2c2-e5a871562628}</Project>
<Name>Sif.Framework.Shared</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
48 changes: 48 additions & 0 deletions Code/Sif.Framework.Tests/Sif.Framework.Shared/DataFactory.cs
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);
}
}
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>
12 changes: 12 additions & 0 deletions Code/Sif.Framework.Tests/Sif.Framework.Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sif.Framework.Tests", "Sif.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sif.Framework", "..\Sif3Framework\Sif.Framework\Sif.Framework.csproj", "{2B13D050-AA2C-45A6-BA26-8B2706B7E227}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sif.Framework.EntityFramework", "..\Sif3Framework\Sif.Framework.EntityFramework\Sif.Framework.EntityFramework.csproj", "{983BB129-0FCF-4800-BC80-36DA09706A9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sif.Framework.Shared", "Sif.Framework.Shared\Sif.Framework.Shared.csproj", "{76D0F674-1662-49B6-A2C2-E5A871562628}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +31,14 @@ Global
{2B13D050-AA2C-45A6-BA26-8B2706B7E227}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B13D050-AA2C-45A6-BA26-8B2706B7E227}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B13D050-AA2C-45A6-BA26-8B2706B7E227}.Release|Any CPU.Build.0 = Release|Any CPU
{983BB129-0FCF-4800-BC80-36DA09706A9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{983BB129-0FCF-4800-BC80-36DA09706A9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{983BB129-0FCF-4800-BC80-36DA09706A9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{983BB129-0FCF-4800-BC80-36DA09706A9C}.Release|Any CPU.Build.0 = Release|Any CPU
{76D0F674-1662-49B6-A2C2-E5A871562628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76D0F674-1662-49B6-A2C2-E5A871562628}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76D0F674-1662-49B6-A2C2-E5A871562628}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76D0F674-1662-49B6-A2C2-E5A871562628}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
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; }
}
}
Loading

0 comments on commit 8e80f80

Please sign in to comment.