14d959834f
* xunit unit tests * most pass with formatting * convert objects to xunit * remove nunit * format * merge fixes * switch objects to fluent assertions * update to fluent assertions * more FA * convert all to FA * Format * Fix tests * formatting * hopefully made credential test better * Catch more specific exception * use another more specific exception * Fix tests * update to xunit * update packages
91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Speckle.Sdk.Api.GraphQL.Models;
|
|
using Speckle.Sdk.Credentials;
|
|
using Xunit;
|
|
|
|
namespace Speckle.Sdk.Tests.Unit.Credentials;
|
|
|
|
public class AccountServerMigrationTests : IDisposable
|
|
{
|
|
private readonly List<Account> _accountsToCleanUp = [];
|
|
|
|
public static IEnumerable<object[]> MigrationTestCases()
|
|
{
|
|
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));
|
|
string accountId = oldAccount.userInfo.id; // new account user must match old account user id
|
|
Account newAccount = CreateTestAccount(NEW_URL, new(OLD_URL), null, accountId);
|
|
Account otherAccount = CreateTestAccount(OTHER_URL, null, null);
|
|
|
|
List<Account> givenAccounts = [oldAccount, newAccount, otherAccount];
|
|
|
|
yield return [givenAccounts, NEW_URL, new[] { newAccount }];
|
|
|
|
yield return [givenAccounts, OLD_URL, new[] { newAccount }];
|
|
|
|
var reversed = givenAccounts.AsEnumerable().Reverse().ToList();
|
|
|
|
yield return [reversed, OLD_URL, new[] { newAccount }];
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(MigrationTestCases))]
|
|
public void TestServerMigration(IList<Account> accounts, string requestedUrl, IList<Account> expectedSequence)
|
|
{
|
|
// Add accounts to the local setup
|
|
AddAccounts(accounts);
|
|
|
|
var serviceProvider = TestServiceSetup.GetServiceProvider();
|
|
var result = serviceProvider.GetRequiredService<IAccountManager>().GetAccounts(requestedUrl).ToList();
|
|
|
|
// Assert the result using Shouldly
|
|
result.Should().BeEquivalentTo(expectedSequence);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clean up accounts after each test
|
|
foreach (var acc in _accountsToCleanUp)
|
|
{
|
|
Fixtures.DeleteLocalAccount(acc.id);
|
|
}
|
|
|
|
_accountsToCleanUp.Clear();
|
|
}
|
|
|
|
private static Account CreateTestAccount(string url, Uri? movedFrom, Uri? movedTo, string? id = null)
|
|
{
|
|
id ??= Guid.NewGuid().ToString();
|
|
|
|
return new Account
|
|
{
|
|
token = "myToken",
|
|
serverInfo = new ServerInfo
|
|
{
|
|
url = url,
|
|
name = "myServer",
|
|
migration = new ServerMigration { movedTo = movedTo, movedFrom = movedFrom },
|
|
},
|
|
userInfo = new UserInfo
|
|
{
|
|
id = id,
|
|
email = "user@example.com",
|
|
name = "user",
|
|
},
|
|
};
|
|
}
|
|
|
|
private void AddAccounts(IEnumerable<Account> accounts)
|
|
{
|
|
foreach (var account in accounts)
|
|
{
|
|
_accountsToCleanUp.Add(account);
|
|
Fixtures.UpdateOrSaveAccount(account);
|
|
}
|
|
}
|
|
}
|