-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added some sample unit tests in a different assembly
- Loading branch information
Radoslav Radev
committed
Mar 9, 2024
1 parent
e5ee97c
commit 7d9eb15
Showing
10 changed files
with
234 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
Binary file modified
BIN
+0 Bytes
(100%)
Backend/OpenVidStreamer.ManagementNdiscovary/var/raft/raft.db
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
Backend/OpenVidStreamer.ManagementNdiscovary/var/server_metadata.json
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"last_seen_unix":1709995809} | ||
{"last_seen_unix":1709999409} |
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
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,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="xunit" Version="2.5.3"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Account\Account.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="appsettings.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using Account.Model.DTO; | ||
using Account.Repository.EFC; | ||
using Account.Services; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Account.Test; | ||
|
||
public class AccountServiceTests | ||
{ | ||
private readonly DatabaseContext _dbContext; | ||
private readonly IConfiguration _configuration; | ||
private readonly AccountService _accountService; | ||
|
||
public AccountServiceTests() | ||
{ | ||
var serviceProvider = new ServiceCollection() | ||
.AddEntityFrameworkInMemoryDatabase() | ||
.BuildServiceProvider(); | ||
|
||
var options = new DbContextOptionsBuilder<DatabaseContext>() | ||
.UseInMemoryDatabase(databaseName: "TestDatabase") | ||
.UseInternalServiceProvider(serviceProvider) | ||
.Options; | ||
|
||
_dbContext = new DatabaseContext(options); | ||
|
||
// Build configuration from appsettings.json | ||
_configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json") | ||
.Build(); | ||
|
||
_accountService = new AccountService(_dbContext, _configuration); | ||
} | ||
|
||
[Fact] | ||
public async Task Login_ReturnsAccountDto_WhenEmailExists() | ||
{ | ||
// Arrange | ||
var loginRequest = new LoginRequestDTO { email = "[email protected]" }; | ||
_dbContext.Accounts.Add(new Account.Repository.Entities.Account { Email = "[email protected]", PasswordHashed = "hashedPassword" }); | ||
await _dbContext.SaveChangesAsync(); | ||
|
||
// Act | ||
var result = await _accountService.Login(loginRequest); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal("[email protected]", result.Item1.Email); | ||
} | ||
|
||
[Fact] | ||
public async Task Login_ReturnsNull_WhenEmailDoesNotExist() | ||
{ | ||
// Arrange | ||
var loginRequest = new LoginRequestDTO { email = "[email protected]" }; | ||
|
||
// Act | ||
var result = await _accountService.Login(loginRequest); | ||
|
||
// Assert | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public async Task Register_ReturnsAccountDtoAndToken_WhenRegistrationIsSuccessful() | ||
{ | ||
// Arrange | ||
var registerRequest = new RegisterRequestDTO { email = "[email protected]", passwordUnhashed = "password" }; | ||
|
||
// Act | ||
var result = await _accountService.Register(registerRequest); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal("[email protected]", result.Item1.Email); | ||
Assert.NotNull(result.Item2); | ||
} | ||
|
||
[Fact] | ||
public async Task GetBalance_ReturnsBalance_WhenAccountExists() | ||
{ | ||
// Arrange | ||
var accountId = Guid.NewGuid(); | ||
_dbContext.Accounts.Add(new Account.Repository.Entities.Account { AccId = accountId, Balance = 100, Email = "[email protected]", PasswordHashed = "hashedPassword" }); | ||
await _dbContext.SaveChangesAsync(); | ||
|
||
// Act | ||
var result = await _accountService.GetBalance(accountId); | ||
|
||
// Assert | ||
Assert.Equal(100, result); | ||
} | ||
|
||
[Fact] | ||
public async Task GetBalance_ThrowsException_WhenAccountDoesNotExist() | ||
{ | ||
// Arrange | ||
var accountId = Guid.NewGuid(); | ||
|
||
// Act & Assert | ||
await Assert.ThrowsAsync<Exception>(() => _accountService.GetBalance(accountId)); | ||
} | ||
|
||
[Fact] | ||
public async Task ActivateSubscription_ReturnsAccountDto_WhenAccountExistsAndHasSufficientBalance() | ||
{ | ||
// Arrange | ||
var accountId = Guid.NewGuid(); | ||
_dbContext.Accounts.Add(new Account.Repository.Entities.Account | ||
{ | ||
AccId = accountId, | ||
Balance = 100, | ||
Email = "[email protected]", | ||
PasswordHashed = "hashedPassword" | ||
}); | ||
await _dbContext.SaveChangesAsync(); | ||
|
||
// Act | ||
var result = await _accountService.ActivateSubscription(accountId); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal(accountId, result.AccId); | ||
Assert.Equal(85, result.Balance); // Assuming the monthly subscription price is 15 (it is set from the appsettings.json of the assembly) | ||
} | ||
|
||
[Fact] | ||
public async Task ActivateSubscription_ThrowsException_WhenAccountDoesNotExist() | ||
{ | ||
// Arrange | ||
var accountId = Guid.NewGuid(); | ||
|
||
// Act & Assert | ||
await Assert.ThrowsAsync<Exception>(() => _accountService.ActivateSubscription(accountId)); | ||
} | ||
} |
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,30 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"ConnectionStrings": { | ||
"DefaultConnection": "server=account-db-service;uid=admin;pwd=12345;database=accountDB;" | ||
}, | ||
"AllowedHosts": "*", | ||
"consulUri": "http://consul-service:8500", | ||
"POD_IP": "localhost", | ||
"servicePort": "8081", | ||
"serviceName": "OpenVisStreamer.Account", | ||
|
||
"CustomJWT": { | ||
"Issuer": "OpenVidStreamerAccountService", | ||
"Audience": "OpenVidStreamerFE", | ||
"Secret": "rxio0SNqgU2yYEvOyZJ1greSMC75JBU0D6IxBZBxIXm+xzSr2ZZ+ZV/PHoV7sNYg7f9PCHulGu+QHG5qaSNpTQ==", | ||
"ExpirationInHours": 72, | ||
"SignalRsecret" : "qsMC75JBU0D6IxBZBxIXm+xzSr2ZZ+ZV/PHoV7sNYg7f9PCHulGu+QHG5qaSNpTQ==" | ||
}, | ||
"monthlySubscriptionPrice": "15", | ||
"RabbitMQ":{ | ||
"HostAddress": "amqp://rabbitmq-service:5672", | ||
"UserName": "guest", | ||
"Password": "guest" | ||
} | ||
} |
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