Skip to content

Commit

Permalink
Remane
Browse files Browse the repository at this point in the history
  • Loading branch information
NotZetka committed Oct 2, 2024
1 parent ab79137 commit 4a259d8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion API/API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "API.csproj", "{32068D16-7CA1-4F9F-95E1-9C71FC411182}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{6ECC12EE-9060-45C9-8ABA-52A7105E2204}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTests", "..\Tests\IntegrationTests.csproj", "{6ECC12EE-9060-45C9-8ABA-52A7105E2204}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using System.Net.Http.Headers;
using System.Text;

namespace Tests.IntegrationTests
namespace IntegrationTests
{
public class AccountsControllerTests(WebApplicationFactory<Program> factory) : BaseControllerTest(factory)
{
Expand All @@ -28,7 +28,7 @@ public async Task UserShouldRegisterSuccessfully()
Gender = "Male",
DateOfBirth = DateTime.Now.AddYears(-20),
};
_context.Users.Where(x=>x.UserName=="Test").ExecuteDelete();
_context.Users.Where(x => x.UserName == "Test").ExecuteDelete();

var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");

Expand Down Expand Up @@ -98,12 +98,12 @@ public async Task UserShouldRetriveListOfUsers()
{
await InitializeUsers();
var scope = _factory.Services.CreateScope();

var userManager = scope.ServiceProvider.GetRequiredService<UserManager<AppUser>>();
var user = await userManager.FindByNameAsync("TestList");
var token = GenerateJwtToken(user.Id.ToString());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

var response = await _client.GetAsync("/Accounts/List");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand All @@ -112,7 +112,7 @@ public async Task UserShouldRetriveListOfUsers()
var accountsList = JsonConvert.DeserializeObject<PagedResult<UserDto>>(stringResponse);

Assert.NotNull(accountsList);
Assert.False(accountsList.Items.Select(x=>x.UserName).Contains("TestList"));
Assert.False(accountsList.Items.Select(x => x.UserName).Contains("TestList"));
}

private async Task InitializeUsers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Security.Claims;
using System.Text;

namespace Tests.IntegrationTests
namespace IntegrationTests
{
public abstract class BaseControllerTest : IClassFixture<WebApplicationFactory<Program>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Net.Http.Headers;
using System.Text;

namespace Tests.IntegrationTests
namespace IntegrationTests
{
public class BodyControllerTests(WebApplicationFactory<Program> factory) : BaseControllerTest(factory)
{
Expand All @@ -20,9 +20,9 @@ public class BodyControllerTests(WebApplicationFactory<Program> factory) : BaseC
public async Task UserShouldSetHeightProperly()
{
await InitializeUsers();
var user = _context.Users.Include(x=>x.BodyWeight).First(x=>x.UserName== "TestBodyWeight");
var user = _context.Users.Include(x => x.BodyWeight).First(x => x.UserName == "TestBodyWeight");
var query = new SetHeightCommand { Height = 110 };
var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");
var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");
var token = GenerateJwtToken(user.Id.ToString());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task UserShouldAddBodyWeightWithOnlyWeight()
var userAfterChanges = GetNewContext()
.Users
.Include(x => x.BodyWeight)
.ThenInclude(x=>x.WeightRecords)
.ThenInclude(x => x.WeightRecords)
.First(x => x.UserName == "TestBodyWeight");

var weightRecord = userAfterChanges.BodyWeight.WeightRecords.Last();
Expand All @@ -64,7 +64,8 @@ public async Task UserShouldAddBodyWeightWithFullData()
{
await InitializeUsers();
var user = _context.Users.Include(x => x.BodyWeight).First(x => x.UserName == "TestBodyWeight");
var query = new AddBodyWeightRecordCommand {
var query = new AddBodyWeightRecordCommand
{
Weight = 110,
Neck = 10,
Chest = 10,
Expand All @@ -74,7 +75,7 @@ public async Task UserShouldAddBodyWeightWithFullData()
Hip = 10,
Thigh = 10,
Calf = 10
};
};
var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");
var token = GenerateJwtToken(user.Id.ToString());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
Expand Down
10 changes: 6 additions & 4 deletions Tests/Tests.csproj → Tests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Compile Remove="UnitTests\**" />
<EmbeddedResource Remove="UnitTests\**" />
<None Remove="UnitTests\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.7" />
Expand All @@ -25,10 +31,6 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<Folder Include="UnitTests\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\API\API.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System.Text;
using static API.Handlers.Plans.AddRecord.AddRecordsCommand;

namespace Tests.IntegrationTests
namespace IntegrationTests
{
public class PlansControllerTests(WebApplicationFactory<Program> factory) : BaseControllerTest(factory)
{
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task UserShouldPublishPlan()
}
}
};
_context.FitnessPlanTemplates.Where(x=>x.Name==query.Name).ExecuteDelete();
_context.FitnessPlanTemplates.Where(x => x.Name == query.Name).ExecuteDelete();
var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");

var response = await _client.PostAsync("/Plans/Publish", content);
Expand All @@ -70,26 +70,26 @@ public async Task UserShouldPublishPlan()
public async Task UserShouldAddPlan()
{
await InitializePlans();
var user = _context.Users.First(x=>x.UserName== "TestUserPlan");
var user = _context.Users.First(x => x.UserName == "TestUserPlan");
var token = GenerateJwtToken(user.Id.ToString());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var planId = _context.FitnessPlanTemplates.Where(x=>x.AuthorId==user.Id).First().Id;
var planId = _context.FitnessPlanTemplates.Where(x => x.AuthorId == user.Id).First().Id;

var response = await _client.GetAsync($"/Plans/Add/{planId}");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var userAfterChanges = GetNewContext().Users.Include(x=>x.FitnessPlans).FirstOrDefault(x=>x.Id == user.Id);
Assert.Equal(2,userAfterChanges.FitnessPlans.Count);
var userAfterChanges = GetNewContext().Users.Include(x => x.FitnessPlans).FirstOrDefault(x => x.Id == user.Id);
Assert.Equal(2, userAfterChanges.FitnessPlans.Count);
}

[Fact]
public async Task UserShouldAddRecord()
{
await InitializePlans();
var user = _context.Users
.Include(x=>x.FitnessPlans)
.ThenInclude(x=>x.Exercises)
.Include(x => x.FitnessPlans)
.ThenInclude(x => x.Exercises)
.First(x => x.UserName == "TestUserPlan");
var token = GenerateJwtToken(user.Id.ToString());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
Expand All @@ -107,7 +107,7 @@ public async Task UserShouldAddRecord()
};
var content = new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");

var response = await _client.PostAsync($"/Plans/Add",content);
var response = await _client.PostAsync($"/Plans/Add", content);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public async Task UserShouldGetSinglePlan()

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task UserShouldArchivePlan()
{
Expand All @@ -157,11 +157,11 @@ public async Task UserShouldArchivePlan()
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var plan = user.FitnessPlans.First();

var response = await _client.PatchAsync($"/Plans/archive/{plan.Id}",null);
var response = await _client.PatchAsync($"/Plans/archive/{plan.Id}", null);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var planAfterChanges = GetNewContext().FitnessPlans.First(x=>x.Id==plan.Id);
var planAfterChanges = GetNewContext().FitnessPlans.First(x => x.Id == plan.Id);
Assert.NotEqual(plan.Archived, planAfterChanges.Archived);
}

Expand Down Expand Up @@ -234,7 +234,7 @@ private async Task InitializePlans()
new()
{
Name = "Test exercise",
Records = new List<API.Data.Record>
Records = new List<API.Data.Record>
{
new()
{
Expand All @@ -247,9 +247,9 @@ private async Task InitializePlans()
}
};
var user1 = _context.Users
.Include(x=>x.FitnessPlansTemplates)
.Include(x=>x.FitnessPlans)
.First(x=>x.UserName=="TestUserPlan");
.Include(x => x.FitnessPlansTemplates)
.Include(x => x.FitnessPlans)
.First(x => x.UserName == "TestUserPlan");
user1.FitnessPlansTemplates.Add(planTemplate);
user1.FitnessPlans.Add(plan);
var planTemplate2 = new FitnessPlanTemplate
Expand Down

0 comments on commit 4a259d8

Please sign in to comment.