Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix permissions #30

Merged
merged 6 commits into from
Aug 21, 2024
Merged
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
8 changes: 3 additions & 5 deletions RelationAnalysis.Migrations/InitialRecordsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ public async Task AddInitialRecords()
{
Name = "Admin",
Permissions =
"[\"/api/Access/GetPermissions\",\"/api/Admin/GetUser/{id}\",\"/api/Admin/GetAllUser\",\"/api/Admin/GetAllRoles\",\"/api/Admin/UpdateUser/{id}\",\"/api/Admin/UpdatePassword/{id}\",\"/api/Admin/DeleteUser/{id}\",\"/api/Admin/CreateUser\",\"/api/Admin/UpdateRoles/{id}\",\"/api/Auth/Login\", \"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
"[\"/api/Admin\",\"/api/Auth\", \"/api/User\", \"/api/Edge\", \"/api/Graph\", \"/api/Node\"]",
Id = 1
},
new Role()
{
Name = "DataAdmin",
Permissions =
"[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
Permissions = "[\"/api/Auth\", \"/api/User\", \"/api/Edge\", \"/api/Graph\", \"/api/Node\"]",
Id = 2
},
new Role()
{
Name = "DataAnalyst",
Permissions =
"[\"/api/Access/GetPermissions\",\"/api/Auth/Login\",\"/api/User/GetUser\",\"/api/User/UpdateUser\",\"/api/User/UpdatePassword\",\"/api/User/Logout\"]",
Permissions = "[\"/api/Auth\", \"/api/User\", \"/api/Edge\", \"/api/Graph\", \"/api/Node\"]",
Id = 3
}
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text;
using Microsoft.Extensions.Options;
using Moq;
using Newtonsoft.Json;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Dto.Category;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Services.UserPanelServices.Abstraction.AuthServices;
using RelationshipAnalysis.Settings.JWT;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace RelationshipAnalysis.Integration.Test.Controllers;

public class EdgeControllerTests : IClassFixture<CustomWebApplicationFactory<Program>>
{
private readonly HttpClient _client;

public EdgeControllerTests(CustomWebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}

[Fact]
public async Task GetAllEdgeCategories_ShouldReturnCorrectList_Whenever()
{
// Arrange
var expectedResult1 = "Transaction";
var request = new HttpRequestMessage(HttpMethod.Get, "/api/edge/categories");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<List<string>>();
Assert.NotNull(responseData);
Assert.Contains(expectedResult1, responseData);
}


[Fact]
public async Task CreateEdgeCategory_ShouldReturnCorrectList_Whenever()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Post, "/api/edge/categories");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

var dto = new CreateEdgeCategoryDto()
{
EdgeCategoryName = "NotExist"
};
request.Content = new StringContent(
JsonSerializer.Serialize<CreateEdgeCategoryDto>(dto),
Encoding.UTF8,
"application/json"
);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<MessageDto>();
Assert.NotNull(responseData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GraphControllerTests(CustomWebApplicationFactory<Program> factory)
public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "/api/graph/getgraph");
var request = new HttpRequestMessage(HttpMethod.Get, "/api/graph");
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(_jwtSettings);
var user = new User
Expand Down Expand Up @@ -137,68 +137,4 @@ public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
Assert.Equivalent(responseData.edges, expectedEdges);

}
[Fact]
public async Task UploadNode_ShouldReturnSuccess_WhenDtoIsValid()
{
// Arrange
var csvContent = @"""AccountID"",""CardID"",""IBAN""
""6534454617"",""6104335000000190"",""IR120778801496000000198""
""4000000028"",""6037699000000020"",""IR033880987114000000028""
";
var mockFile = CreateFileMock(csvContent);

var fileContent = new StreamContent(mockFile.OpenReadStream());
fileContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");

var formDataContent = new MultipartFormDataContent();
formDataContent.Add(new StringContent("Account"), "NodeCategoryName");
formDataContent.Add(new StringContent("AccountID"), "UniqueAttributeHeaderName");
formDataContent.Add(fileContent, "file", mockFile.FileName);

var request = new HttpRequestMessage(HttpMethod.Post, "api/graph/uploadnode");

request.Content = formDataContent;

Mock<IOptions<JwtSettings>> jwtSettingsMock = new();
jwtSettingsMock.Setup(m => m.Value).Returns(_jwtSettings);

var user = new User
{
Id = 1,
Username = "admin",
PasswordHash = "74b2c5bd3a8de69c8c7c643e8b5c49d6552dc636aeb0995aff6f01a1f661a979",
FirstName = "Admin",
LastName = "User",
Email = "[email protected]",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "admin" } } }

};

var token = new JwtTokenGenerator(jwtSettingsMock.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);


// Act
var response = await _client.SendAsync(request);

// Assert
Assert.Equal(200, (int)response.StatusCode);
Assert.Equal(Resources.SuccessfulNodeAdditionMessage, response.Content.ReadFromJsonAsync<MessageDto>().Result.Message);
}

private IFormFile CreateFileMock(string csvContent)
{
var csvFileName = "test.csv";
var fileMock = Substitute.For<IFormFile>();
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(csvContent);
writer.Flush();
stream.Position = 0;

fileMock.OpenReadStream().Returns(stream);
fileMock.FileName.Returns(csvFileName);
fileMock.Length.Returns(stream.Length);
return fileMock;
}
}
Loading