-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/quality of life improvements (#21)
This bundles up a handful of small changes that makes the application easier to build and run. * Warning hunt to clean up some existing warnings that have crept in. * Adds AdminList configuration option, a comma-separated list of emails that are admin accounts * This partially implements #4 but the actual admin-y pages need to be added to complete it. * Adds unit tests for AdminList related functions * Extraneous forward slashes are dropped from the end of URL lookups, making #6 easier to implement * Fix the dockerfile to use the right sdk image for building. * Fix the default okta API urls.
- Loading branch information
Showing
15 changed files
with
256 additions
and
25 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
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
30 changes: 30 additions & 0 deletions
30
NetGoLynx.Tests/UnitTests/Models/Configuration/AccountSettingsTest.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,30 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NetGoLynx.Models; | ||
using NetGoLynx.Models.Configuration; | ||
|
||
namespace NetGoLynx.Tests.UnitTests.Models.Configuration | ||
{ | ||
[TestClass] | ||
public class AccountSettingsTest | ||
{ | ||
[TestMethod] | ||
public void AdminUsernameReturnsTrue() | ||
{ | ||
var goodAccount = new Account() | ||
{ | ||
Name = "someaccount@example.com" | ||
}; | ||
var badAccount = new Account() | ||
{ | ||
Name = "somerando@example.com" | ||
}; | ||
var settings = new AccountSettings | ||
{ | ||
AdminList = goodAccount.Name | ||
}; | ||
|
||
Assert.IsTrue(settings.IsAdmin(goodAccount)); | ||
Assert.IsFalse(settings.IsAdmin(badAccount)); | ||
} | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
NetGoLynx.Tests/UnitTests/Models/Services/AccountServiceTests.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,112 @@ | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NetGoLynx.Data; | ||
using NetGoLynx.Models; | ||
using NetGoLynx.Services; | ||
|
||
namespace NetGoLynx.Tests.UnitTests.Models.Services | ||
{ | ||
[TestClass] | ||
public class AccountServiceTests | ||
{ | ||
private static readonly DbContextOptions<RedirectContext> s_dbOps = | ||
new DbContextOptionsBuilder<RedirectContext>() | ||
.UseInMemoryDatabase(databaseName: "account_service_tests") | ||
.Options; | ||
|
||
private static int s_accountCount; | ||
|
||
private static int AccountCount | ||
{ | ||
get | ||
{ | ||
s_accountCount++; | ||
return s_accountCount; | ||
} | ||
} | ||
|
||
private static Account GetValidAccount() | ||
{ | ||
var count = AccountCount; | ||
return new Account() | ||
{ | ||
Access = AccessType.Default, | ||
AccountId = count, | ||
Name = $"test_account_{count}@example.com", | ||
}; | ||
} | ||
|
||
[ClassInitialize] | ||
public static void ClassInitialize(TestContext testContext) | ||
{ | ||
testContext.WriteLine("Adding default database entries"); | ||
// Add some reference stuff | ||
using var context = new RedirectContext(s_dbOps); | ||
context.Add(GetValidAccount()); | ||
context.Add(GetValidAccount()); | ||
context.Add(new Account() | ||
{ | ||
AccountId = 12345, | ||
Access = AccessType.Default, | ||
Name = "GoodAccount@example.com" | ||
}); | ||
context.SaveChanges(); | ||
} | ||
|
||
[TestMethod] | ||
public void WhitelistedAccountReturnsAsAdminViaAnyMethod() | ||
{ | ||
using var context = new RedirectContext(s_dbOps); | ||
var configDict = new Dictionary<string, string> | ||
{ | ||
{"AccountSettings:AdminList", "GoodAccount@example.com,OtherGoodAccount@example.com,moregood@example.com"} | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(configDict) | ||
.Build(); | ||
var accountService = new AccountService(context, null, config); | ||
|
||
// Standard get methods | ||
Assert.AreEqual(accountService.Get("GoodAccount@example.com").Result.Access, AccessType.Admin); | ||
Assert.AreEqual(accountService.Get(12345).Result.Access, AccessType.Admin); | ||
|
||
// ClaimsPrincipal overload | ||
var claims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] | ||
{ | ||
new Claim(ClaimTypes.Email, "GoodAccount@example.com") | ||
}, "mock")); | ||
Assert.AreEqual(accountService.Get(claims).Result.Access, AccessType.Admin); | ||
|
||
// Create operation | ||
var newAccount = new Account() | ||
{ | ||
Access = AccessType.Default, | ||
Name = "OtherGoodAccount@example.com" | ||
}; | ||
var (account, created) = accountService.Create(newAccount).Result; | ||
Assert.IsTrue(created); | ||
Assert.AreEqual(account.Access, AccessType.Admin); | ||
|
||
Assert.AreEqual(accountService.GetOrCreate("moregood@example.com").Result.Access, AccessType.Admin); | ||
} | ||
|
||
[TestMethod] | ||
public void RegularAccountDoesntReturnAsAdmin() | ||
{ | ||
using var context = new RedirectContext(s_dbOps); | ||
var configDict = new Dictionary<string, string> | ||
{ | ||
{"AccountSettings:AdminList", "GoodAccount@example.com"} | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(configDict) | ||
.Build(); | ||
var accountService = new AccountService(context, null, config); | ||
|
||
Assert.AreNotEqual(accountService.Get("test_account_1@example.com").Result.Access, AccessType.Admin); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace NetGoLynx.Models.Configuration | ||
{ | ||
/// <summary> | ||
/// Model for Account-related settings. | ||
/// </summary> | ||
public class AccountSettings | ||
{ | ||
private HashSet<string> _adminList; | ||
|
||
private string _whitelist; | ||
|
||
/// <summary> | ||
/// Gets or sets the comma-separated list of accounts that have admin. | ||
/// </summary> | ||
public string AdminList | ||
{ | ||
get => _whitelist; | ||
set | ||
{ | ||
_whitelist = value; | ||
_adminList = new HashSet<string>(_whitelist.Split(',')); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determine if an account is an admin. | ||
/// </summary> | ||
/// <param name="account">The account to check</param> | ||
/// <returns>True if the username exactly matches an entry in the whitelist.</returns> | ||
public bool IsAdmin(Account account) | ||
{ | ||
return account != null && _adminList.Contains(account.Name); | ||
} | ||
} | ||
} |
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.