Files
speckle-sharp-sdk/tests/Speckle.Sdk.Tests.Unit/Credentials/AccountServerMigrationTests.cs
T
Adam Hathcock 9695ec8c51 SpeckleHttp coverage (#244)
* Show code coverage for dev

* SpeckleHttp coverage and moved base stuff around

* add SpeckleHttpTests
2025-03-07 10:48:01 +00:00

90 lines
2.6 KiB
C#

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Speckle.Sdk.Api.GraphQL.Models;
using Speckle.Sdk.Credentials;
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);
}
}
}