-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
486 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using Api.Controllers.Models; | ||
using Api.Database.Models; | ||
using Api.Test.Mocks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
namespace Api.Test | ||
{ | ||
[Collection("Database collection")] | ||
public class RoleAccessTests : IClassFixture<TestWebApplicationFactory<Program>> | ||
{ | ||
private readonly HttpClient _client; | ||
private readonly MockHttpContextAccessor _httpContextAccessor; | ||
private readonly JsonSerializerOptions _serializerOptions = | ||
new() | ||
{ | ||
Converters = | ||
{ | ||
new JsonStringEnumConverter() | ||
}, | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
public RoleAccessTests(TestWebApplicationFactory<Program> factory) | ||
{ | ||
_httpContextAccessor = (MockHttpContextAccessor)factory.Services.GetService<IHttpContextAccessor>()!; | ||
_httpContextAccessor.SetHttpContextRoles(["Role.Admin"]); | ||
//var x = new HttpContextAccessor(); | ||
_client = factory.CreateClient(new WebApplicationFactoryClientOptions | ||
{ | ||
AllowAutoRedirect = false, | ||
BaseAddress = new Uri("https://localhost:8000") | ||
}); | ||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( | ||
TestAuthHandler.AuthenticationScheme | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task AuthorisedPostPlantTest_NotFound() | ||
{ | ||
// Arrange | ||
_httpContextAccessor.SetHttpContextRoles(["User.TestInstallationAreaTest_Wrong"]); | ||
|
||
string testInstallation = "AuthorisedPostPlantTest_NotFoundInstallation"; | ||
var installationQuery = new CreateInstallationQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
Name = testInstallation | ||
}; | ||
|
||
string testPlant = "AuthorisedPostPlantTest_NotFoundPlant"; | ||
var plantQuery = new CreatePlantQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
Name = testPlant | ||
}; | ||
var installationContent = new StringContent( | ||
JsonSerializer.Serialize(installationQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var plantContent = new StringContent( | ||
JsonSerializer.Serialize(plantQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
// Act | ||
string installationUrl = "/installations"; | ||
var installationResponse = await _client.PostAsync(installationUrl, installationContent); | ||
string plantUrl = "/plants"; | ||
var plantResponse = await _client.PostAsync(plantUrl, plantContent); | ||
|
||
// Assert | ||
Assert.True(installationResponse.IsSuccessStatusCode); | ||
Assert.False(plantResponse.IsSuccessStatusCode); | ||
Assert.Equal("NotFound", plantResponse.StatusCode.ToString()); | ||
} | ||
|
||
[Fact] | ||
public async Task ExplicitlyAuthorisedPostPlantTest() | ||
{ | ||
// Arrange | ||
var accessRoleQuery = new CreateAccessRoleQuery | ||
{ | ||
InstallationCode = "ExplicitlyAuthorisedPostPlantTestInstallation", | ||
RoleName = "User.ExplicitlyAuthorisedPostPlantTestInstallation", | ||
AccessLevel = RoleAccessLevel.USER | ||
}; | ||
var accessRoleContent = new StringContent( | ||
JsonSerializer.Serialize(accessRoleQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var testPose = new Pose | ||
{ | ||
Position = new Position | ||
{ | ||
X = 1, | ||
Y = 2, | ||
Z = 2 | ||
}, | ||
Orientation = new Orientation | ||
{ | ||
X = 0, | ||
Y = 0, | ||
Z = 0, | ||
W = 1 | ||
} | ||
}; | ||
|
||
string testInstallation = "ExplicitlyAuthorisedPostPlantTestInstallation"; | ||
var installationQuery = new CreateInstallationQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
Name = testInstallation | ||
}; | ||
|
||
string testPlant = "ExplicitlyAuthorisedPostPlantTestPlant"; | ||
var plantQuery = new CreatePlantQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
Name = testPlant | ||
}; | ||
|
||
string testDeck = "ExplicitlyAuthorisedPostPlantTestDeck"; | ||
var deckQuery = new CreateDeckQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
Name = testDeck | ||
}; | ||
|
||
string testArea = "ExplicitlyAuthorisedPostPlantTestArea"; | ||
var areaQuery = new CreateAreaQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
DeckName = testDeck, | ||
AreaName = testArea, | ||
DefaultLocalizationPose = testPose | ||
}; | ||
|
||
var installationContent = new StringContent( | ||
JsonSerializer.Serialize(installationQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var plantContent = new StringContent( | ||
JsonSerializer.Serialize(plantQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var deckContent = new StringContent( | ||
JsonSerializer.Serialize(deckQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var areaContent = new StringContent( | ||
JsonSerializer.Serialize(areaQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
// Act | ||
string installationUrl = "/installations"; | ||
var installationResponse = await _client.PostAsync(installationUrl, installationContent); | ||
string accessRoleUrl = "/access-roles"; | ||
var accessRoleResponse = await _client.PostAsync(accessRoleUrl, accessRoleContent); | ||
|
||
// Only restrict ourselves to non-admin role after adding a role | ||
_httpContextAccessor.SetHttpContextRoles(["User.ExplicitlyAuthorisedPostPlantTestInstallation"]); | ||
|
||
string plantUrl = "/plants"; | ||
var plantResponse = await _client.PostAsync(plantUrl, plantContent); | ||
string deckUrl = "/decks"; | ||
var deckResponse = await _client.PostAsync(deckUrl, deckContent); | ||
string areaUrl = "/areas"; | ||
var areaResponse = await _client.PostAsync(areaUrl, areaContent); | ||
|
||
// Assert | ||
Assert.True(accessRoleResponse.IsSuccessStatusCode); | ||
Assert.True(installationResponse.IsSuccessStatusCode); | ||
Assert.True(plantResponse.IsSuccessStatusCode); | ||
Assert.True(deckResponse.IsSuccessStatusCode); | ||
Assert.True(areaResponse.IsSuccessStatusCode); | ||
var area = await areaResponse.Content.ReadFromJsonAsync<AreaResponse>(_serializerOptions); | ||
Assert.True(area != null); | ||
} | ||
|
||
[Fact] | ||
public async Task AdminAuthorisedPostPlantTest() | ||
{ | ||
// Arrange | ||
var testPose = new Pose | ||
{ | ||
Position = new Position | ||
{ | ||
X = 1, | ||
Y = 2, | ||
Z = 2 | ||
}, | ||
Orientation = new Orientation | ||
{ | ||
X = 0, | ||
Y = 0, | ||
Z = 0, | ||
W = 1 | ||
} | ||
}; | ||
|
||
string testInstallation = "AdminAuthorisedPostPlantTestInstallation"; | ||
var installationQuery = new CreateInstallationQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
Name = testInstallation | ||
}; | ||
|
||
string testPlant = "AdminAuthorisedPostPlantTestPlant"; | ||
var plantQuery = new CreatePlantQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
Name = testPlant | ||
}; | ||
|
||
string testDeck = "AdminAuthorisedPostPlantTestDeck"; | ||
var deckQuery = new CreateDeckQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
Name = testDeck | ||
}; | ||
|
||
string testArea = "AdminAuthorisedPostPlantTestArea"; | ||
var areaQuery = new CreateAreaQuery | ||
{ | ||
InstallationCode = testInstallation, | ||
PlantCode = testPlant, | ||
DeckName = testDeck, | ||
AreaName = testArea, | ||
DefaultLocalizationPose = testPose | ||
}; | ||
|
||
var installationContent = new StringContent( | ||
JsonSerializer.Serialize(installationQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var plantContent = new StringContent( | ||
JsonSerializer.Serialize(plantQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var deckContent = new StringContent( | ||
JsonSerializer.Serialize(deckQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
var areaContent = new StringContent( | ||
JsonSerializer.Serialize(areaQuery), | ||
null, | ||
"application/json" | ||
); | ||
|
||
// Act | ||
string installationUrl = "/installations"; | ||
var installationResponse = await _client.PostAsync(installationUrl, installationContent); | ||
string plantUrl = "/plants"; | ||
var plantResponse = await _client.PostAsync(plantUrl, plantContent); | ||
string deckUrl = "/decks"; | ||
var deckResponse = await _client.PostAsync(deckUrl, deckContent); | ||
string areaUrl = "/areas"; | ||
var areaResponse = await _client.PostAsync(areaUrl, areaContent); | ||
|
||
// Assert | ||
Assert.True(installationResponse.IsSuccessStatusCode); | ||
Assert.True(plantResponse.IsSuccessStatusCode); | ||
Assert.True(deckResponse.IsSuccessStatusCode); | ||
Assert.True(areaResponse.IsSuccessStatusCode); | ||
var area = await areaResponse.Content.ReadFromJsonAsync<AreaResponse>(_serializerOptions); | ||
Assert.True(area != null); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.