-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* AccountManager and StreamWrapper Changes to ensure they are aware of the movedFrom property added for server migration * test(core): [CNX-9138] Unit tests for AccountManager server migration (#3215) * Unit tests * teardown * More test cases * polish * removing dupes removing duplicate accounts from those returned from GetAccounts(), where the account ID matches but is essentially the same as the account coming from the upgraded account. * Updated summary comment Updated summary comment * TrimEnd('/'); Added .TrimEnd('/') when setting the ServerInfo.url * Fixed account test (#3218) Account unit tests --------- Co-authored-by: Jedd Morgan <[email protected]>
- Loading branch information
Showing
5 changed files
with
150 additions
and
39 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
87 changes: 87 additions & 0 deletions
87
Core/Tests/Speckle.Core.Tests.Unit/Credentials/AccountServerMigrationTests.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,87 @@ | ||
using NUnit.Framework; | ||
using Speckle.Core.Api; | ||
using Speckle.Core.Credentials; | ||
|
||
namespace Speckle.Core.Tests.Unit.Credentials; | ||
|
||
public class AccountServerMigrationTests | ||
{ | ||
private readonly List<Account> _accountsToCleanUp = new(); | ||
|
||
public static IEnumerable<TestCaseData> MigrationTestCase() | ||
{ | ||
const string OLD_URL = "https://old.example.com"; | ||
const string NEW_URL = "https://new.example.com"; | ||
const string OTHER_URL = "https://other.example.com"; | ||
Account oldAccount = CreateTestAccount(OLD_URL, null, new(NEW_URL)); | ||
Account newAccount = CreateTestAccount(NEW_URL, new(OLD_URL), null); | ||
Account otherAccount = CreateTestAccount(OTHER_URL, null, null); | ||
|
||
List<Account> givenAccounts = new() { oldAccount, newAccount, otherAccount }; | ||
|
||
yield return new TestCaseData(givenAccounts, NEW_URL, new[] { newAccount }) | ||
.SetName("Get New") | ||
.SetDescription("When requesting for new account, ensure only this account is returned"); | ||
|
||
yield return new TestCaseData(givenAccounts, OLD_URL, new[] { newAccount }) | ||
.SetName("Get New via Old") | ||
.SetDescription("When requesting for old account, ensure migrated account is returned first"); | ||
|
||
var reversed = Enumerable.Reverse(givenAccounts).ToList(); | ||
|
||
yield return new TestCaseData(reversed, OLD_URL, new[] { newAccount }) | ||
.SetName("Get New via Old (Reversed order)") | ||
.SetDescription("Account order shouldn't matter"); | ||
} | ||
|
||
[Test] | ||
[TestCaseSource(nameof(MigrationTestCase))] | ||
public void TestServerMigration(IList<Account> accounts, string requestedUrl, IList<Account> expectedSequence) | ||
{ | ||
AddAccounts(accounts); | ||
|
||
var result = AccountManager.GetAccounts(requestedUrl).ToList(); | ||
|
||
Assert.That(result, Is.EquivalentTo(expectedSequence)); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
//Clean up any of the test accounts we made | ||
foreach (var acc in _accountsToCleanUp) | ||
{ | ||
Fixtures.DeleteLocalAccount(acc.id); | ||
} | ||
_accountsToCleanUp.Clear(); | ||
} | ||
|
||
private static Account CreateTestAccount(string url, Uri movedFrom, Uri movedTo) | ||
{ | ||
return new Account | ||
{ | ||
token = "myToken", | ||
serverInfo = new ServerInfo | ||
{ | ||
url = url, | ||
name = "myServer", | ||
migration = new ServerMigration { movedTo = movedTo, movedFrom = movedFrom } | ||
}, | ||
userInfo = new UserInfo | ||
{ | ||
id = new Guid().ToString(), | ||
email = "[email protected]", | ||
name = "user" | ||
} | ||
}; | ||
} | ||
|
||
private void AddAccounts(IEnumerable<Account> accounts) | ||
{ | ||
foreach (Account account in accounts) | ||
{ | ||
_accountsToCleanUp.Add(account); | ||
Fixtures.UpdateOrSaveAccount(account); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,29 +7,29 @@ namespace Speckle.Core.Tests.Unit.Credentials; | |
[TestFixture] | ||
public class CredentialInfrastructure | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
[OneTimeSetUp] | ||
public static void SetUp() | ||
{ | ||
_testAccount1 = new Account | ||
s_testAccount1 = new Account | ||
{ | ||
refreshToken = "bla", | ||
token = "bla", | ||
serverInfo = new ServerInfo { url = "bla", company = "bla" }, | ||
serverInfo = new ServerInfo { url = "https://bla.example.com", company = "bla" }, | ||
userInfo = new UserInfo { email = "[email protected]" } | ||
}; | ||
|
||
_testAccount2 = new Account | ||
s_testAccount2 = new Account | ||
{ | ||
refreshToken = "foo", | ||
token = "bar", | ||
serverInfo = new ServerInfo { url = "baz", company = "qux" }, | ||
serverInfo = new ServerInfo { url = "https://baz.example.com", company = "qux" }, | ||
userInfo = new UserInfo { email = "[email protected]" } | ||
}; | ||
|
||
_testAccount3 = new Account | ||
s_testAccount3 = new Account | ||
{ | ||
token = "secret", | ||
serverInfo = new ServerInfo { url = "https://sample.com", name = "qux" }, | ||
serverInfo = new ServerInfo { url = "https://example.com", name = "qux" }, | ||
userInfo = new UserInfo | ||
{ | ||
email = "[email protected]", | ||
|
@@ -38,22 +38,22 @@ public void SetUp() | |
} | ||
}; | ||
|
||
Fixtures.UpdateOrSaveAccount(_testAccount1); | ||
Fixtures.UpdateOrSaveAccount(_testAccount2); | ||
Fixtures.SaveLocalAccount(_testAccount3); | ||
Fixtures.UpdateOrSaveAccount(s_testAccount1); | ||
Fixtures.UpdateOrSaveAccount(s_testAccount2); | ||
Fixtures.SaveLocalAccount(s_testAccount3); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
[OneTimeTearDown] | ||
public static void TearDown() | ||
{ | ||
Fixtures.DeleteLocalAccount(_testAccount1.id); | ||
Fixtures.DeleteLocalAccount(_testAccount2.id); | ||
Fixtures.DeleteLocalAccount(s_testAccount1.id); | ||
Fixtures.DeleteLocalAccount(s_testAccount2.id); | ||
Fixtures.DeleteLocalAccountFile(); | ||
} | ||
|
||
private Account _testAccount1, | ||
_testAccount2, | ||
_testAccount3; | ||
private static Account s_testAccount1, | ||
s_testAccount2, | ||
s_testAccount3; | ||
|
||
[Test] | ||
public void GetAllAccounts() | ||
|
@@ -62,24 +62,27 @@ public void GetAllAccounts() | |
Assert.That(accs, Has.Count.GreaterThanOrEqualTo(3)); // Tests are adding three accounts, you might have extra accounts on your machine when testing :D | ||
} | ||
|
||
[Test] | ||
public void GetAccountsForServer() | ||
public static IEnumerable<Account> TestCases() | ||
{ | ||
var accs = AccountManager.GetAccounts("baz").ToList(); | ||
|
||
Assert.That(accs, Has.Count.EqualTo(1)); | ||
Assert.That(accs[0].serverInfo.company, Is.EqualTo("qux")); | ||
Assert.That(accs[0].serverInfo.url, Is.EqualTo("baz")); | ||
Assert.That(accs[0].refreshToken, Is.EqualTo("foo")); | ||
SetUp(); | ||
return new[] { s_testAccount1, s_testAccount2, s_testAccount3 }; | ||
} | ||
|
||
[Test] | ||
public void GetLocalAccount() | ||
[TestCaseSource(nameof(TestCases))] | ||
public void GetAccountsForServer(Account target) | ||
{ | ||
var acc = AccountManager.GetAccounts().FirstOrDefault(x => x.userInfo.id == "123345"); | ||
var accs = AccountManager.GetAccounts(target.serverInfo.url).ToList(); | ||
|
||
Assert.That(accs, Has.Count.EqualTo(1)); | ||
|
||
var acc = accs[0]; | ||
|
||
Assert.That(acc.serverInfo.url, Is.EqualTo("https://sample.com")); | ||
Assert.That(acc.token, Is.EqualTo("secret")); | ||
Assert.That(acc, Is.Not.SameAs(target), "We expect new objects (no reference equality)"); | ||
Assert.That(acc.serverInfo.company, Is.EqualTo(target.serverInfo.company)); | ||
Assert.That(acc.serverInfo.url, Is.EqualTo(target.serverInfo.url)); | ||
Assert.That(acc.refreshToken, Is.EqualTo(target.refreshToken)); | ||
Assert.That(acc.token, Is.EqualTo(target.token)); | ||
} | ||
|
||
[Test] | ||
|
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