Skip to content

Commit

Permalink
TestData rewrite for LocalTest (#9342)
Browse files Browse the repository at this point in the history
* Consolidate reading of TestData in Localtest This is in preparation of adding multiple data sources for registry data. Also enable <ImplicitUsings> for localtest and gradually enable nullable reference types on single files while I touch them.
* Add indentation to json responses from LocalTest
* Add new simplified test data model, and a few debug endpoints on HomeController
* Add functionality to fetch an alternate user database from the app itself it is stored in `app/wwwroot/testData.json`
  • Loading branch information
ivarne authored Jan 2, 2023
1 parent 7365424 commit 8435b87
Show file tree
Hide file tree
Showing 23 changed files with 666 additions and 259 deletions.
80 changes: 40 additions & 40 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Security.Claims;
using System.Xml;

Expand All @@ -27,8 +21,8 @@
using LocalTest.Services.Profile.Interface;
using LocalTest.Services.LocalApp.Interface;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.Cookies;
using LocalTest.Services.TestData;

namespace LocalTest.Controllers
{
Expand All @@ -41,6 +35,7 @@ public class HomeController : Controller
private readonly IApplicationRepository _applicationRepository;
private readonly IClaims _claimsService;
private readonly ILocalApp _localApp;
private readonly TestDataService _testDataService;

public HomeController(
IOptions<GeneralSettings> generalSettings,
Expand All @@ -49,7 +44,8 @@ public HomeController(
IAuthentication authenticationService,
IApplicationRepository applicationRepository,
IClaims claimsService,
ILocalApp localApp)
ILocalApp localApp,
TestDataService testDataService)
{
_generalSettings = generalSettings.Value;
_localPlatformSettings = localPlatformSettings.Value;
Expand All @@ -58,6 +54,33 @@ public HomeController(
_applicationRepository = applicationRepository;
_claimsService = claimsService;
_localApp = localApp;
_testDataService = testDataService;
}

[AllowAnonymous]
public async Task<IActionResult> LocalTestUsersRaw()
{
var localData = await TestDataDiskReader.ReadFromDisk(_localPlatformSettings.LocalTestingStaticTestDataPath);

return Json(localData);
}

[AllowAnonymous]
public async Task<IActionResult> LocalTestUsers()
{
var localData = await TestDataDiskReader.ReadFromDisk(_localPlatformSettings.LocalTestingStaticTestDataPath);
var constructedAppData = AppTestDataModel.FromTestDataModel(localData);

return Json(constructedAppData);
}

[AllowAnonymous]
public async Task<IActionResult> LocalTestUsersRoundTrip()
{
var localData = await TestDataDiskReader.ReadFromDisk(_localPlatformSettings.LocalTestingStaticTestDataPath);
var constructedAppData = AppTestDataModel.FromTestDataModel(localData);

return Json(constructedAppData.GetTestDataModel());
}

[AllowAnonymous]
Expand All @@ -67,19 +90,19 @@ public async Task<IActionResult> Index()
try
{
model.TestApps = await GetAppsList();
model.TestUsers = await GetTestUsersForList();
var defaultAuthLevel = _localPlatformSettings.LocalAppMode == "http" ? await GetAppAuthLevel(model.TestApps) : 2;
model.AuthenticationLevels = GetAuthenticationLevels(defaultAuthLevel);
}
catch (HttpRequestException e)
{
model.HttpException = e;
}

model.TestUsers = await GetTestUsersForList();
model.AppPath = _localPlatformSettings.AppRepositoryBasePath;
model.StaticTestDataPath = _localPlatformSettings.LocalTestingStaticTestDataPath;
model.LocalAppUrl = _localPlatformSettings.LocalAppUrl;
var defaultAuthLevel = _localPlatformSettings.LocalAppMode == "http" ? await GetAppAuthLevel(model.TestApps) : 2;
model.AppModeIsHttp = _localPlatformSettings.LocalAppMode == "http";
model.AuthenticationLevels = GetAuthenticationLevels(defaultAuthLevel);
model.LocalFrontendUrl = HttpContext.Request.Cookies[FRONTEND_URL_COOKIE_NAME];

if (!model.TestApps?.Any() ?? true)
Expand Down Expand Up @@ -270,49 +293,26 @@ public ActionResult FrontendVersion(FrontendVersion frontendVersion)
return RedirectToAction("Index");
}


private async Task<List<UserProfile>> GetTestUsers()
{
List<UserProfile> users = new List<UserProfile>();
string path = this._localPlatformSettings.LocalTestingStaticTestDataPath + "Profile/User/";

if (!Directory.Exists(path))
{
return users;
}

string[] files = Directory.GetFiles(path, "*.json");

foreach (string file in files)
{
if (int.TryParse(Path.GetFileNameWithoutExtension(file), out int userId))
{
users.Add(await _userProfileService.GetUser(userId));
}
}

return users;
}

private async Task<IEnumerable<SelectListItem>> GetTestUsersForList()
{
List<UserProfile> users = await GetTestUsers();

var data = await _testDataService.GetTestData();
List<SelectListItem> userItems = new List<SelectListItem>();

foreach (UserProfile profile in users)
foreach (UserProfile profile in data.Profile.User.Values)
{
var properProfile = await _userProfileService.GetUser(profile.UserId);
SelectListItem item = new SelectListItem()
{
Value = profile.UserId.ToString(),
Text = profile.Party.Person.Name
Value = properProfile.UserId.ToString(),
Text = properProfile.Party.Person.Name
};

userItems.Add(item);
}

return userItems;
}

private async Task<int> GetAppAuthLevel(IEnumerable<SelectListItem> testApps)
{
try
Expand Down
1 change: 1 addition & 0 deletions src/LocalTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>56f36ce2-b44b-415e-a8a5-f399a76e35b9</UserSecretsId>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand Down
36 changes: 10 additions & 26 deletions src/Services/Authorization/Implementation/ClaimsService.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
#nullable enable
using System.Security.Claims;
using System.Threading.Tasks;
using Altinn.Platform.Authentication.Model;
using Altinn.Platform.Authorization.Services.Interface;
using LocalTest.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using LocalTest.Services.TestData;

namespace LocalTest.Services.Authorization.Implementation
{
public class ClaimsService : IClaims
{
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly TestDataService _testDataService;

public ClaimsService(IOptions<LocalPlatformSettings> localPlatformSettings)
public ClaimsService(TestDataService testDataService)
{
_localPlatformSettings = localPlatformSettings.Value;
_testDataService = testDataService;
}

public Task<List<Claim>> GetCustomClaims(int userId, string issuer)
public async Task<List<Claim>> GetCustomClaims(int userId, string issuer)
{
var path = GetCustomClaimsPath(userId);

if (File.Exists(path))
var data = await _testDataService.GetTestData();
if(data.Authorization.Claims.TryGetValue(userId.ToString(), out var customClaims))
{
var content = File.ReadAllText(path);
var claims = JsonConvert.DeserializeObject<List<CustomClaim>>(content) ?? new List<CustomClaim>();
return Task.FromResult(claims.Select(c => new Claim(c.Type, c.Value, c.ValueType, issuer)).ToList());
return customClaims.Select(c => new Claim(c.Type, c.Value, c.ValueType, issuer)).ToList();
}

return Task.FromResult(new List<Claim>());
}

private string GetCustomClaimsPath(int userId)
{
return _localPlatformSettings.LocalTestingStaticTestDataPath +
_localPlatformSettings.AuthorizationDataFolder + _localPlatformSettings.ClaimsFolder + userId +
".json";
return new List<Claim>();
}
}
}
35 changes: 8 additions & 27 deletions src/Services/Authorization/Implementation/PartiesService.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
#nullable enable
using Altinn.Platform.Authorization.Services.Interface;
using Altinn.Platform.Register.Models;
using LocalTest.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using LocalTest.Services.TestData;

namespace LocalTest.Services.Authorization.Implementation
{
public class PartiesService : IParties
{
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly TestDataService _testDataService;

public PartiesService(IOptions<LocalPlatformSettings> localPlatformSettings)
public PartiesService(TestDataService testDataService)
{
_localPlatformSettings = localPlatformSettings.Value;
_testDataService = testDataService;
}

public Task<List<Party>> GetParties(int userId)
public async Task<List<Party>?> GetParties(int userId)
{
string path = GetPartyListPath(userId);

if (File.Exists(path))
{
string content = System.IO.File.ReadAllText(path);
List<Party> instance = (List<Party>)JsonConvert.DeserializeObject(content, typeof(List<Party>));
return Task.FromResult(instance);
}

return null;
var data = await _testDataService.GetTestData();
return data.Authorization.PartyList.TryGetValue(userId.ToString(), out var result) ? result : null;
}

public Task<bool> ValidateSelectedParty(int userId, int partyId)
{
return Task.FromResult(true);
}


private string GetPartyListPath(int userId)
{
return _localPlatformSettings.LocalTestingStaticTestDataPath + _localPlatformSettings.AuthorizationDataFolder + _localPlatformSettings.PartyListFolder + userId + ".json";
}
}
}
44 changes: 13 additions & 31 deletions src/Services/Authorization/Implementation/RolesWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
#nullable enable
using Altinn.Platform.Authorization.Services.Interface;
using Authorization.Interface.Models;
using LocalTest.Configuration;
using Microsoft.Extensions.Logging;
using LocalTest.Services.TestData;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

Expand All @@ -16,44 +12,30 @@ namespace Altinn.Platform.Authorization.Services.Implementation
/// </summary>
public class RolesWrapper : IRoles
{
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly TestDataService _testDataService;

/// <summary>
/// Initializes a new instance of the <see cref="RolesWrapper"/> class
/// </summary>
/// <param name="rolesClient">the client handler for roles api</param>
public RolesWrapper(IOptions<LocalPlatformSettings> localPlatformSettings)
/// <param name="testDataService">Service to fetch test data</param>
public RolesWrapper(TestDataService testDataService)
{
this._localPlatformSettings = localPlatformSettings.Value;
this._testDataService = testDataService;
}

/// <inheritdoc />
public async Task<List<Role>> GetDecisionPointRolesForUser(int coveredByUserId, int offeredByPartyId)
{
string rolesPath = GetRolesPath(coveredByUserId, offeredByPartyId);

List<Role> roles = new List<Role>();

if (File.Exists(rolesPath))
var data = await _testDataService.GetTestData();
if(data.Authorization.Roles.TryGetValue(coveredByUserId.ToString(), out var user))
{
string content = System.IO.File.ReadAllText(rolesPath);
roles = (List<Role>)JsonConvert.DeserializeObject(content, typeof(List<Role>));
if(user.TryGetValue(offeredByPartyId.ToString(), out var roles))
{
return roles;
}
}

return await Task.FromResult(roles);
}

private string GetRolesPath(int userId, int resourcePartyId)
{
string[] pathArray = new string[] {
this._localPlatformSettings.LocalTestingStaticTestDataPath,
this._localPlatformSettings.AuthorizationDataFolder,
this._localPlatformSettings.RolesFolder,
$"User_{userId}/",
$"party_{resourcePartyId}/",
"roles.json"
};
return Path.Combine(pathArray);
return new List<Role>();
}
}
}
3 changes: 1 addition & 2 deletions src/Services/Authorization/Interface/IClaims.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
#nullable enable
using System.Security.Claims;
using System.Threading.Tasks;

namespace Altinn.Platform.Authorization.Services.Interface
{
Expand Down
5 changes: 2 additions & 3 deletions src/Services/Authorization/Interface/IParties.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable
using Altinn.Platform.Register.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Altinn.Platform.Authorization.Services.Interface
{
Expand All @@ -14,7 +13,7 @@ public interface IParties
/// </summary>
/// <param name="userId">The user id</param>
/// <returns>list of parties that the logged in user can represent</returns>
Task<List<Party>> GetParties(int userId);
Task<List<Party>?> GetParties(int userId);

/// <summary>
/// Verifies that the selected party is contained in the user's party list
Expand Down
Loading

0 comments on commit 8435b87

Please sign in to comment.