-
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 integration tests for ContextTypes
- Loading branch information
Showing
11 changed files
with
350 additions
and
58 deletions.
There are no files selected for viewing
17 changes: 15 additions & 2 deletions
17
backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/Data/ContextTypeData.cs
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,13 +1,26 @@ | ||
using Equinor.ProjectExecutionPortal.Domain.Entities; | ||
using Fusion.Integration; | ||
|
||
namespace Equinor.ProjectExecutionPortal.Tests.WebApi.Data | ||
{ | ||
internal class ContextTypeData | ||
{ | ||
public class InitialSeedData | ||
{ | ||
public static ContextType ContextType1 = new("ProjectMaster"); | ||
public static ContextType ContextType2 = new("Facility"); | ||
public static ContextType ContextType1 = new(ValidData.ProjectMasterContextTypeKey); | ||
public static ContextType ContextType2 = new(ValidData.FacilityContextTypeKey); | ||
} | ||
|
||
public class ValidData | ||
{ | ||
public static string ProjectMasterContextTypeKey = "ProjectMaster"; | ||
public static string FacilityContextTypeKey = "Facility"; | ||
public static string ContractContextTypeKey = "Contract"; | ||
} | ||
|
||
public class InvalidData | ||
{ | ||
public static string InvalidContextTypeKey = "SuperContext"; | ||
} | ||
} | ||
} |
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
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
270 changes: 270 additions & 0 deletions
270
...quinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/ContextTypeControllerTests.cs
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,270 @@ | ||
using System.Net; | ||
using System.Text; | ||
using Equinor.ProjectExecutionPortal.Tests.WebApi.Data; | ||
using Equinor.ProjectExecutionPortal.Tests.WebApi.Setup; | ||
using Equinor.ProjectExecutionPortal.WebApi.ViewModels.ContextType; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Newtonsoft.Json; | ||
|
||
namespace Equinor.ProjectExecutionPortal.Tests.WebApi.IntegrationTests | ||
{ | ||
[TestClass] | ||
public class ContextTypeControllerTests : TestBase | ||
{ | ||
private const string Route = "api/context-types"; | ||
|
||
[TestMethod] | ||
public async Task Get_ContextTypes_AsAuthenticatedUser_ShouldReturnOk() | ||
{ | ||
// Act | ||
var contextTypes = await AssertGetAllContextTypes(UserType.Authenticated, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.IsTrue(contextTypes.Count == 2); | ||
|
||
foreach (var contextType in contextTypes) | ||
{ | ||
AssertHelpers.AssertContextTypeValues(contextType); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task Get_ContextTypes_AsAdministratorUser_ShouldReturnOk() | ||
{ | ||
// Act | ||
var contextTypes = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.IsTrue(contextTypes.Count == 2); | ||
|
||
foreach (var contextType in contextTypes) | ||
{ | ||
AssertHelpers.AssertContextTypeValues(contextType); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task Get_ContextTypes_AsAnonymous_ShouldReturnUnauthorized() | ||
{ | ||
// Act | ||
var contextTypes = await AssertGetAllContextTypes(UserType.Anonymous, HttpStatusCode.Unauthorized); | ||
|
||
// Assert | ||
Assert.IsNull(contextTypes); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_Valid_ContextType_AsAdministratorUser_ShouldReturnCreated() | ||
{ | ||
// Arrange | ||
var getAllBeforeAdded = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
var payload = new ApiAddContextTypeRequest | ||
{ | ||
Type = ContextTypeData.ValidData.ContractContextTypeKey | ||
}; | ||
|
||
// Act | ||
var response = await AddContextType(UserType.Administrator, payload); | ||
|
||
// Assert | ||
var getAllAfterAdded = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); | ||
Assert.IsNotNull(getAllBeforeAdded); | ||
Assert.IsNotNull(getAllAfterAdded); | ||
Assert.AreEqual(getAllBeforeAdded.Count + 1, getAllAfterAdded.Count); | ||
Assert.AreEqual(payload.Type, getAllAfterAdded.Last().Type); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_Valid_ContextType_AsAuthenticatedUser_ShouldReturnForbidden() | ||
{ | ||
// Arrange | ||
var payload = new ApiAddContextTypeRequest | ||
{ | ||
Type = ContextTypeData.ValidData.ContractContextTypeKey | ||
}; | ||
|
||
// Act | ||
var response = await AddContextType(UserType.Authenticated, payload); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_Valid_ContextType_AsAnonymousUser_ShouldReturnUnauthorized() | ||
{ | ||
// Arrange | ||
var payload = new ApiAddContextTypeRequest | ||
{ | ||
Type = ContextTypeData.ValidData.ContractContextTypeKey | ||
}; | ||
|
||
// Act | ||
var response = await AddContextType(UserType.Anonymous, payload); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_NonExistent_ContextType_AsAdministratorUser_ShouldReturnNotFound() | ||
{ | ||
// Arrange | ||
var payload = new ApiAddContextTypeRequest | ||
{ | ||
Type = ContextTypeData.InvalidData.InvalidContextTypeKey | ||
}; | ||
|
||
// Act | ||
var response = await AddContextType(UserType.Administrator, payload); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_Duplicate_ContextType_AsAdministratorUser_ShouldReturnConflict() | ||
{ | ||
// Arrange | ||
var payload = new ApiAddContextTypeRequest | ||
{ | ||
Type = ContextTypeData.InitialSeedData.ContextType1.ContextTypeKey | ||
}; | ||
|
||
// Act | ||
var addDuplicateResponse = await AddContextType(UserType.Administrator, payload); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.Conflict, addDuplicateResponse.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_ContextType_WithoutPortals_AsAdministratorUser_ShouldReturnOk() | ||
{ | ||
// Arrange | ||
var payload = ContextTypeData.InitialSeedData.ContextType2.ContextTypeKey; | ||
|
||
// Act | ||
var getAllBefore = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
var removeResponse = await RemoveContextType(UserType.Administrator, payload); | ||
|
||
var getAllAfterRemoval = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.OK, removeResponse.StatusCode); | ||
Assert.IsNotNull(getAllBefore.Count); | ||
Assert.IsNotNull(getAllAfterRemoval.Count); | ||
Assert.AreEqual(getAllBefore.Count - 1, getAllAfterRemoval.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_ContextType_WithPortals_AsAdministratorUser_ShouldReturnBadRequest() | ||
{ | ||
// Arrange | ||
var payload = ContextTypeData.InitialSeedData.ContextType1.ContextTypeKey; | ||
|
||
// Act | ||
var getAllBefore = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
var removeResponse = await RemoveContextType(UserType.Administrator, payload); | ||
|
||
var getAllAfter = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.BadRequest, removeResponse.StatusCode); | ||
Assert.AreEqual(getAllBefore.Count, getAllAfter.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_ContextType_AsAuthenticatedUser_ShouldReturnForbidden() | ||
{ | ||
// Arrange | ||
var existingContextTypeKey = ContextTypeData.InitialSeedData.ContextType1.ContextTypeKey; | ||
|
||
// Act | ||
var getAllBefore = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
var removeResponse = await RemoveContextType(UserType.Authenticated, existingContextTypeKey); | ||
|
||
var getAllAfter = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.Forbidden, removeResponse.StatusCode); | ||
Assert.AreEqual(getAllBefore.Count, getAllAfter.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_ContextType_AsAnonymousUser_ShouldReturnUnauthorized() | ||
{ | ||
// Arrange | ||
var existingContextTypeKey = ContextTypeData.InitialSeedData.ContextType1.ContextTypeKey; | ||
|
||
// Act | ||
var getAllBefore = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
var removeResponse = await RemoveContextType(UserType.Anonymous, existingContextTypeKey); | ||
|
||
var getAllAfter = await AssertGetAllContextTypes(UserType.Administrator, HttpStatusCode.OK); | ||
|
||
// Assert | ||
Assert.AreEqual(HttpStatusCode.Unauthorized, removeResponse.StatusCode); | ||
Assert.AreEqual(getAllBefore.Count, getAllAfter.Count); | ||
} | ||
|
||
#region Helpers | ||
|
||
private static async Task<IList<ApiContextType>> AssertGetAllContextTypes(UserType userType, HttpStatusCode expectedStatusCode) | ||
{ | ||
// Act | ||
var response = await GetAllContextTypes(userType); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
var contextTypes = JsonConvert.DeserializeObject<IList<ApiContextType>>(content); | ||
|
||
// Assert | ||
Assert.AreEqual(expectedStatusCode, response.StatusCode); | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
{ | ||
return contextTypes; | ||
} | ||
|
||
Assert.IsNotNull(content); | ||
Assert.IsNotNull(contextTypes); | ||
|
||
return contextTypes; | ||
} | ||
|
||
private static async Task<HttpResponseMessage> AddContextType(UserType userType, ApiAddContextTypeRequest contextType) | ||
{ | ||
var serializePayload = JsonConvert.SerializeObject(contextType); | ||
var content = new StringContent(serializePayload, Encoding.UTF8, "application/json"); | ||
|
||
var client = TestFactory.Instance.GetHttpClient(userType); | ||
var response = await client.PostAsync($"{Route}", content); | ||
|
||
return response; | ||
} | ||
|
||
private static async Task<HttpResponseMessage> GetAllContextTypes(UserType userType) | ||
{ | ||
var client = TestFactory.Instance.GetHttpClient(userType); | ||
var response = await client.GetAsync($"{Route}"); | ||
|
||
return response; | ||
} | ||
|
||
private static async Task<HttpResponseMessage> RemoveContextType(UserType userType, string contextTypeKey) | ||
{ | ||
var client = TestFactory.Instance.GetHttpClient(userType); | ||
var response = await client.DeleteAsync($"{Route}/{contextTypeKey}"); | ||
|
||
return response; | ||
} | ||
|
||
#endregion Helpers | ||
} | ||
} |
Oops, something went wrong.