Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
using Microsoft.AspNet.Identity;
using Microsoft.AspNetCore.Identity;
using Moq;
using NUnit.Framework;
using SimpleTrader.Domain.Exceptions;
using SimpleTrader.Domain.Models;
using SimpleTrader.Domain.Services;
using SimpleTrader.Domain.Services.AuthenticationServices;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTrader.Domain.Tests.Services.AuthenticationServices
{
[TestFixture]
public class AuthenticationServiceTests
{
private Mock<IPasswordHasher> _mockPasswordHasher;
private Mock<IPasswordHasher<string>> _mockPasswordHasher;
private Mock<IAccountService> _mockAccountService;
private AuthenticationService _authenticationService;

[SetUp]
public void SetUp()
{
_mockPasswordHasher = new Mock<IPasswordHasher>();
_mockPasswordHasher = new Mock<IPasswordHasher<string>>();
_mockAccountService = new Mock<IAccountService>();
_authenticationService = new AuthenticationService(_mockAccountService.Object, _mockPasswordHasher.Object);
}
Expand All @@ -33,7 +30,7 @@ public async Task Login_WithCorrectPasswordForExistingUsername_ReturnsAccountFor
string expectedUsername = "testuser";
string password = "testpassword";
_mockAccountService.Setup(s => s.GetByUsername(expectedUsername)).ReturnsAsync(new Account() { AccountHolder = new User() { Username = expectedUsername } });
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Success);
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(expectedUsername, It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Success);

Account account = await _authenticationService.Login(expectedUsername, password);

Expand All @@ -47,7 +44,7 @@ public void Login_WithIncorrectPasswordForExistingUsername_ThrowsInvalidPassword
string expectedUsername = "testuser";
string password = "testpassword";
_mockAccountService.Setup(s => s.GetByUsername(expectedUsername)).ReturnsAsync(new Account() { AccountHolder = new User() { Username = expectedUsername } });
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Failed);
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(expectedUsername, It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Failed);

InvalidPasswordException exception = Assert.ThrowsAsync<InvalidPasswordException>(() => _authenticationService.Login(expectedUsername, password));

Expand All @@ -60,7 +57,7 @@ public void Login_WithNonExistingUsername_ThrowsInvalidPasswordExceptionForUsern
{
string expectedUsername = "testuser";
string password = "testpassword";
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Failed);
_mockPasswordHasher.Setup(s => s.VerifyHashedPassword(expectedUsername, It.IsAny<string>(), password)).Returns(PasswordVerificationResult.Failed);

UserNotFoundException exception = Assert.ThrowsAsync<UserNotFoundException>(() => _authenticationService.Login(expectedUsername, password));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using Microsoft.AspNet.Identity;
using Microsoft.AspNetCore.Identity;
using SimpleTrader.Domain.Exceptions;
using SimpleTrader.Domain.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTrader.Domain.Services.AuthenticationServices
{
public class AuthenticationService : IAuthenticationService
{
private readonly IAccountService _accountService;
private readonly IPasswordHasher _passwordHasher;
private readonly IPasswordHasher<string> _passwordHasher;

public AuthenticationService(IAccountService accountService, IPasswordHasher passwordHasher)
public AuthenticationService(IAccountService accountService, IPasswordHasher<string> passwordHasher)
{
_accountService = accountService;
_passwordHasher = passwordHasher;
Expand All @@ -28,7 +26,7 @@ public async Task<Account> Login(string username, string password)
throw new UserNotFoundException(username);
}

PasswordVerificationResult passwordResult = _passwordHasher.VerifyHashedPassword(storedAccount.AccountHolder.PasswordHash, password);
var passwordResult = _passwordHasher.VerifyHashedPassword(username, storedAccount.AccountHolder.PasswordHash, password);

if(passwordResult != PasswordVerificationResult.Success)
{
Expand Down Expand Up @@ -61,7 +59,7 @@ public async Task<RegistrationResult> Register(string email, string username, st

if(result == RegistrationResult.Success)
{
string hashedPassword = _passwordHasher.HashPassword(password);
string hashedPassword = _passwordHasher.HashPassword(username, password);

User user = new User()
{
Expand Down
4 changes: 2 additions & 2 deletions SimpleTrader/SimpleTrader.Domain/SimpleTrader.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.3" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,85 +1,60 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Internal;
using SimpleTrader.Domain.Models;
using SimpleTrader.Domain.Services;
using SimpleTrader.EntityFramework.Services.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTrader.EntityFramework.Services
{
public class AccountDataService : IAccountService
public class AccountDataService : NonQueryDataService<Account>, IAccountService
{
private readonly SimpleTraderDbContextFactory _contextFactory;
private readonly NonQueryDataService<Account> _nonQueryDataService;

public AccountDataService(SimpleTraderDbContextFactory contextFactory)
public AccountDataService(SimpleTraderDbContextFactory contextFactory) : base(contextFactory)
{
_contextFactory = contextFactory;
_nonQueryDataService = new NonQueryDataService<Account>(contextFactory);
}

public async Task<Account> Create(Account entity)
{
return await _nonQueryDataService.Create(entity);
}

public async Task<bool> Delete(int id)
{
return await _nonQueryDataService.Delete(id);
}

public async Task<Account> Get(int id)
{
using (SimpleTraderDbContext context = _contextFactory.CreateDbContext())
{
Account entity = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync((e) => e.Id == id);
return entity;
}
using SimpleTraderDbContext context = _contextFactory.CreateDbContext();
var entity = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync((e) => e.Id == id);
return entity;
}

public async Task<IEnumerable<Account>> GetAll()
{
using (SimpleTraderDbContext context = _contextFactory.CreateDbContext())
{
IEnumerable<Account> entities = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.ToListAsync();
return entities;
}
using SimpleTraderDbContext context = _contextFactory.CreateDbContext();
var entities = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.ToListAsync();
return entities;
}

public async Task<Account> GetByEmail(string email)
{
using (SimpleTraderDbContext context = _contextFactory.CreateDbContext())
{
return await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync(a => a.AccountHolder.Email == email);
}
using SimpleTraderDbContext context = _contextFactory.CreateDbContext();
var account = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync(a => a.AccountHolder.Email == email);
return account;
}

public async Task<Account> GetByUsername(string username)
{
using (SimpleTraderDbContext context = _contextFactory.CreateDbContext())
{
return await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync(a => a.AccountHolder.Username == username);
}
}

public async Task<Account> Update(int id, Account entity)
{
return await _nonQueryDataService.Update(id, entity);
using SimpleTraderDbContext context = _contextFactory.CreateDbContext();
var account = await context.Accounts
.Include(a => a.AccountHolder)
.Include(a => a.AssetTransactions)
.FirstOrDefaultAsync(a => a.AccountHolder.Username == username);
return account;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,10 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNet.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SimpleTrader.Domain.Models;
Expand All @@ -7,9 +7,6 @@
using SimpleTrader.Domain.Services.TransactionServices;
using SimpleTrader.EntityFramework.Services;
using SimpleTrader.FinancialModelingPrepAPI.Services;
using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleTrader.WPF.HostBuilders
{
Expand All @@ -19,8 +16,7 @@ public static IHostBuilder AddServices(this IHostBuilder host)
{
host.ConfigureServices(services =>
{
services.AddSingleton<IPasswordHasher, PasswordHasher>();

services.AddSingleton<IPasswordHasher<string>, PasswordHasher<string>>();
services.AddSingleton<IAuthenticationService, AuthenticationService>();
services.AddSingleton<IDataService<Account>, AccountDataService>();
services.AddSingleton<IAccountService, AccountDataService>();
Expand Down
8 changes: 4 additions & 4 deletions SimpleTrader/SimpleTrader.WPF/SimpleTrader.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand All @@ -12,12 +12,12 @@

<ItemGroup>
<PackageReference Include="LoadingSpinner.WPF" Version="1.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="PasswordBoxMVVM" Version="1.0.5" />
</ItemGroup>

Expand Down