Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into the-purge-2
Browse files Browse the repository at this point in the history
  • Loading branch information
fassadlr committed Jan 30, 2020
2 parents 04751c1 + fcf257f commit f5a0b0d
Show file tree
Hide file tree
Showing 51 changed files with 291 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using NBitcoin;
using Stratis.Feature.PoA.Tokenless;

namespace CertificateAuthority.Tests.FullProjectTests.Helpers
namespace CertificateAuthority.Tests
{
public static class TestsHelper
public static class CaTestHelper
{
public const string AdminPassword = "4815162342";
public static string BaseAddress = "http://localhost:5050";
public const string CaMnemonic = "young shoe immense usual faculty edge habit misery swarm tape viable toddler";
public const string CaMnemonicPassword = "node";

private static Random random = new Random();

public static string GenerateRandomString(int length = 10)
Expand All @@ -29,7 +35,7 @@ public static CredentialsModel CreateAccount(TestServer server, AccountAccessFla
string password = GenerateRandomString();
string passHash = DataHelper.ComputeSha256Hash(password);

var adminCredentials = new CredentialsModel(1, "4815162342");
var adminCredentials = new CredentialsModel(Settings.AdminAccountId, AdminPassword);

var accountsController = (AccountsController)server.Host.Services.GetService(typeof(AccountsController));

Expand All @@ -39,6 +45,15 @@ public static CredentialsModel CreateAccount(TestServer server, AccountAccessFla
return new CredentialsModel(id, password);
}

internal static void InitializeCa(TestServer server)
{
var network = new TokenlessNetwork();

var certificatesController = (CertificatesController)server.Host.Services.GetService(typeof(CertificatesController));
var model = new InitializeCertificateAuthorityModel(CaMnemonic, CaMnemonicPassword, network.Consensus.CoinType, network.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS][0], AdminPassword);
certificatesController.InitializeCertificateAuthority(model);
}

public static IWebHostBuilder CreateWebHostBuilder([CallerMemberName] string callingMethod = null)
{
// Create a datafolder path for the CA settings to use
Expand Down
5 changes: 2 additions & 3 deletions src/CertificateAuthority.Tests/DataHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DataHelperTests
[Fact]
public void ComputeSha256HashTests()
{
Dictionary<string, string> inputToHashDictionary = new Dictionary<string, string>()
var inputToHashDictionary = new Dictionary<string, string>()
{
{"65y56h456gergerg", "4e0e7b3189d7cb3a4c6b7bca5844b30d83653eef364af449c6f1e7bf224701fd" },
{"rg54g4g45g45g", "1eacd796aa227beea7112f5a8524264aec1bb7d72f6fefba9b047c3bd1173c96" },
Expand Down Expand Up @@ -46,7 +46,7 @@ public void GetCertificateRequestLinesSuccess_Test()
[Fact]
public void GetCertificateRequestLinesInvalidData_Test()
{
List<string> invalidData = new List<string>()
var invalidData = new List<string>()
{
"-----BEGIN g45g54g REQUEST----- MIIE1jQH 2IpYg1u4ox 79pteewn -----END CERTIFICATE REQUEST-----",
"-----BEGIN CERTIFICATE REQUEST----- MIIE1jQH 2IpYg1u4ox 79pteewn -----END CERTIFICATE REQaaaEST-----",
Expand Down Expand Up @@ -76,7 +76,6 @@ public AccessTestData(AccountAccessFlags a, AccountAccessFlags b, bool expectedR
public bool ExpectedResult { get; set; }
}


[Fact]
public void IsCreatorHasGreaterOrEqualAccessTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using CertificateAuthority.Controllers;
using CertificateAuthority.Database;
using CertificateAuthority.Models;
using CertificateAuthority.Tests.FullProjectTests.Helpers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.TestHost;
Expand All @@ -21,34 +20,36 @@ public sealed class AccountsControllerTests

public AccountsControllerTests()
{
IWebHostBuilder builder = TestsHelper.CreateWebHostBuilder();
IWebHostBuilder builder = CaTestHelper.CreateWebHostBuilder();
this.server = new TestServer(builder);

this.adminCredentials = new CredentialsModel(1, "4815162342");
this.adminCredentials = new CredentialsModel(Settings.AdminAccountId, CaTestHelper.AdminPassword);
this.accountsController = (AccountsController)this.server.Host.Services.GetService(typeof(AccountsController));
this.dataCacheLayer = (DataCacheLayer)this.server.Host.Services.GetService(typeof(DataCacheLayer));

CaTestHelper.InitializeCa(this.server);
}

[Fact]
private void TestAccountsControllerMethods()
{
// Just admin on start.
Assert.Single(TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials)));
Assert.Single(CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials)));

AccountAccessFlags credentials1Access = AccountAccessFlags.AccessAccountInfo | AccountAccessFlags.BasicAccess | AccountAccessFlags.IssueCertificates;
CredentialsModel credentials1 = TestsHelper.CreateAccount(this.server, credentials1Access);
CredentialsModel credentials2 = TestsHelper.CreateAccount(this.server, AccountAccessFlags.DeleteAccounts);
CredentialsModel accToDelete = TestsHelper.CreateAccount(this.server);
CredentialsModel credentials1 = CaTestHelper.CreateAccount(this.server, credentials1Access);
CredentialsModel credentials2 = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.DeleteAccounts);
CredentialsModel accToDelete = CaTestHelper.CreateAccount(this.server);

// GetAccountInfoById
{
// Admin can access new user's data
AccountInfo info = TestsHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(credentials1.AccountId, this.adminCredentials.AccountId, this.adminCredentials.Password)));
AccountInfo info = CaTestHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(credentials1.AccountId, this.adminCredentials.AccountId, this.adminCredentials.Password)));
Assert.Equal(credentials1Access, info.AccessInfo);
Assert.Equal(this.adminCredentials.AccountId, info.CreatorId);

// First user can access admin's data'
AccountInfo info2 = TestsHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(this.adminCredentials.AccountId, credentials1.AccountId, credentials1.Password)));
AccountInfo info2 = CaTestHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(this.adminCredentials.AccountId, credentials1.AccountId, credentials1.Password)));
Assert.Equal(this.adminCredentials.AccountId, info2.CreatorId);
Assert.Equal(Settings.AdminName, info2.Name);

Expand All @@ -58,13 +59,13 @@ private void TestAccountsControllerMethods()
}

// GetAllAccounts
List<AccountModel> allAccounts = TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials));
List<AccountModel> allAccounts = CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials));
Assert.Equal(4, allAccounts.Count);

// DeleteAccountByAccountId
{
this.accountsController.DeleteAccountByAccountId(new CredentialsModelWithTargetId(accToDelete.AccountId, credentials2.AccountId, credentials2.Password));
Assert.Equal(3, TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials)).Count);
Assert.Equal(3, CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(this.adminCredentials)).Count);

IActionResult result = this.accountsController.DeleteAccountByAccountId(new CredentialsModelWithTargetId(credentials2.AccountId, credentials1.AccountId, credentials1.Password));
Assert.True(((ObjectResult)result).StatusCode == 403);
Expand All @@ -74,15 +75,15 @@ private void TestAccountsControllerMethods()
int newFlag = 8 + 16 + 2 + 64;
this.accountsController.ChangeAccountAccessLevel(new ChangeAccountAccessLevel(newFlag, credentials1.AccountId, this.adminCredentials.AccountId, this.adminCredentials.Password));

int newAccessInfo = (int)TestsHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(credentials1.AccountId, this.adminCredentials.AccountId, this.adminCredentials.Password))).AccessInfo;
int newAccessInfo = (int)CaTestHelper.GetValue<AccountInfo>(this.accountsController.GetAccountInfoById(new CredentialsModelWithTargetId(credentials1.AccountId, this.adminCredentials.AccountId, this.adminCredentials.Password))).AccessInfo;
Assert.Equal(newFlag, newAccessInfo);

// GetCertIdsIssuedByAccountId
{
int issuerId = credentials1.AccountId;

string print1 = TestsHelper.GenerateRandomString(20);
string print2 = TestsHelper.GenerateRandomString(20);
string print1 = CaTestHelper.GenerateRandomString(20);
string print2 = CaTestHelper.GenerateRandomString(20);
byte[] blockSignPubKey1 = (new Key()).PubKey.ToBytes();
byte[] blockSignPubKey2 = (new Key()).PubKey.ToBytes();
byte[] txSignPubKeyHash1 = (new Key()).PubKey.Hash.ToBytes();
Expand All @@ -109,7 +110,7 @@ private void TestAccountsControllerMethods()
TransactionSigningPubKeyHash = txSignPubKeyHash2
});

List<CertificateInfoModel> certs = TestsHelper.GetValue<List<CertificateInfoModel>>(this.accountsController.GetCertificatesIssuedByAccountId(new CredentialsModelWithTargetId(issuerId, this.adminCredentials.AccountId, this.adminCredentials.Password)));
List<CertificateInfoModel> certs = CaTestHelper.GetValue<List<CertificateInfoModel>>(this.accountsController.GetCertificatesIssuedByAccountId(new CredentialsModelWithTargetId(issuerId, this.adminCredentials.AccountId, this.adminCredentials.Password)));

Assert.Equal(2, certs.Count);
Assert.Equal(50, certs[0].CertificateContentDer.Length);
Expand All @@ -123,56 +124,56 @@ private void TestAccountsControllerMethods()
[Fact]
public void ChangeAccountPassword_CurrentUser_Pass()
{
CredentialsModel credentials = TestsHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel credentials = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);

var model = new ChangeAccountPasswordModel(credentials.AccountId, credentials.AccountId, credentials.Password, "newpassword");
this.accountsController.ChangeAccountPassword(model);

var adminCredentialsModel = new CredentialsModel(this.adminCredentials.AccountId, this.adminCredentials.Password);
List<AccountModel> accounts = TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
List<AccountModel> accounts = CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
AccountModel account = accounts.FirstOrDefault(a => a.Id == credentials.AccountId);
Assert.True(account.VerifyPassword("newpassword"));
}

[Fact]
public void ChangeAccountPassword_CurrentUser_WrongPassword_Fail()
{
CredentialsModel credentials = TestsHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel credentials = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);

var model = new ChangeAccountPasswordModel(credentials.AccountId, credentials.AccountId, "wrongpassword", "newpassword");
this.accountsController.ChangeAccountPassword(model);

var adminCredentialsModel = new CredentialsModel(this.adminCredentials.AccountId, this.adminCredentials.Password);
List<AccountModel> accounts = TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
List<AccountModel> accounts = CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
AccountModel account = accounts.FirstOrDefault(a => a.Id == credentials.AccountId);
Assert.False(account.VerifyPassword("newpassword"));
}

[Fact]
public void ChangeAccountPassword_AdminUser_Pass()
{
CredentialsModel userA_Credentials = TestsHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel userA_Credentials = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);

var changePasswordModel = new ChangeAccountPasswordModel(this.adminCredentials.AccountId, userA_Credentials.AccountId, this.adminCredentials.Password, "newpassword");
this.accountsController.ChangeAccountPassword(changePasswordModel);

var adminCredentialsModel = new CredentialsModel(this.adminCredentials.AccountId, this.adminCredentials.Password);
List<AccountModel> accounts = TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
List<AccountModel> accounts = CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
AccountModel account = accounts.FirstOrDefault(a => a.Id == userA_Credentials.AccountId);
Assert.True(account.VerifyPassword("newpassword"));
}

[Fact]
public void ChangeAccountPassword_DifferentUser_Fail()
{
CredentialsModel userA_Credentials = TestsHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel userB_Credentials = TestsHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel userA_Credentials = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);
CredentialsModel userB_Credentials = CaTestHelper.CreateAccount(this.server, AccountAccessFlags.BasicAccess);

var model = new ChangeAccountPasswordModel(userA_Credentials.AccountId, userB_Credentials.AccountId, userA_Credentials.Password, "newpassword");
this.accountsController.ChangeAccountPassword(model);

var adminCredentialsModel = new CredentialsModel(this.adminCredentials.AccountId, this.adminCredentials.Password);
List<AccountModel> accounts = TestsHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
List<AccountModel> accounts = CaTestHelper.GetValue<List<AccountModel>>(this.accountsController.GetAllAccounts(adminCredentialsModel));
AccountModel userB_Account = accounts.FirstOrDefault(a => a.Id == userB_Credentials.AccountId);
Assert.False(userB_Account.VerifyPassword("newpassword"));
Assert.True(userB_Account.VerifyPassword(userB_Credentials.Password));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using CertificateAuthority.Models;
using CertificateAuthority.Tests.FullProjectTests.Helpers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using NBitcoin;
Expand All @@ -13,13 +12,8 @@

namespace CertificateAuthority.Tests.FullProjectTests
{
public class CertificateAuthorityIntegrationTests
public sealed class CertificateAuthorityIntegrationTests
{
public const int TestAccountId = 1;
public const string TestPassword = "4815162342";
public const string CaMnemonic = "young shoe immense usual faculty edge habit misery swarm tape viable toddler";
public const string CaMnemonicPassword = "node";

private readonly Network network;

public CertificateAuthorityIntegrationTests()
Expand All @@ -30,10 +24,12 @@ public CertificateAuthorityIntegrationTests()
[Fact]
public void CertificateAuthorityTestServerStartsUp()
{
IWebHostBuilder builder = TestsHelper.CreateWebHostBuilder();
IWebHostBuilder builder = CaTestHelper.CreateWebHostBuilder();

var server = new TestServer(builder);
var client = new CaClient(server.BaseAddress, server.CreateClient(), TestAccountId, TestPassword);
var client = new CaClient(server.BaseAddress, server.CreateClient(), Settings.AdminAccountId, CaTestHelper.AdminPassword);

CaTestHelper.InitializeCa(server);

List<CertificateInfoModel> response = client.GetAllCertificates();

Expand All @@ -45,25 +41,25 @@ public void CertificateAuthorityTestServerStartsUp()
[Fact]
public void CertificateAuthorityTestServerGetsInitialized()
{
IWebHostBuilder builder = TestsHelper.CreateWebHostBuilder();
IWebHostBuilder builder = CaTestHelper.CreateWebHostBuilder();

var server = new TestServer(builder);
var client = new CaClient(server.BaseAddress, server.CreateClient(), TestAccountId, TestPassword);
var client = new CaClient(server.BaseAddress, server.CreateClient(), Settings.AdminAccountId, CaTestHelper.AdminPassword);

Assert.True(client.InitializeCertificateAuthority(CaMnemonic, CaMnemonicPassword, this.network));
Assert.True(client.InitializeCertificateAuthority(CaTestHelper.CaMnemonic, CaTestHelper.CaMnemonicPassword, this.network));

server.Dispose();
}

[Fact]
public void CertificateAuthorityCanGenerateCertificateSigningRequest()
{
IWebHostBuilder builder = TestsHelper.CreateWebHostBuilder();
IWebHostBuilder builder = CaTestHelper.CreateWebHostBuilder();

var server = new TestServer(builder);
var client = new CaClient(server.BaseAddress, server.CreateClient(), TestAccountId, TestPassword);
var client = new CaClient(server.BaseAddress, server.CreateClient(), Settings.AdminAccountId, CaTestHelper.AdminPassword);

Assert.True(client.InitializeCertificateAuthority(CaMnemonic, CaMnemonicPassword, this.network));
Assert.True(client.InitializeCertificateAuthority(CaTestHelper.CaMnemonic, CaTestHelper.CaMnemonicPassword, this.network));

var privateKey = new Key();
PubKey pubKey = privateKey.PubKey;
Expand All @@ -85,12 +81,12 @@ public void CertificateAuthorityCanGenerateCertificateSigningRequest()
[Fact]
public void CertificateAuthorityCanIssueCertificate()
{
IWebHostBuilder builder = TestsHelper.CreateWebHostBuilder();
IWebHostBuilder builder = CaTestHelper.CreateWebHostBuilder();

var server = new TestServer(builder);
var client = new CaClient(server.BaseAddress, server.CreateClient(), TestAccountId, TestPassword);
var client = new CaClient(server.BaseAddress, server.CreateClient(), Settings.AdminAccountId, CaTestHelper.AdminPassword);

Assert.True(client.InitializeCertificateAuthority(CaMnemonic, CaMnemonicPassword, this.network));
Assert.True(client.InitializeCertificateAuthority(CaTestHelper.CaMnemonic, CaTestHelper.CaMnemonicPassword, this.network));

var privateKey = new Key();

Expand Down
Loading

0 comments on commit f5a0b0d

Please sign in to comment.