Skip to content

Commit 36dfb3f

Browse files
Fix tests to use user flows wrt authorizations (#488)
* Fixes * Update dotnet/Tests/Tests.cs Co-authored-by: Scott Phillips <[email protected]>
1 parent c9a5c29 commit 36dfb3f

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

dotnet/Tests/Tests.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,26 @@ namespace Tests;
2727
[SuppressMessage("ReSharper", "MethodHasAsyncOverload")]
2828
public class Tests
2929
{
30+
#if DEBUG
31+
const string DefaultEndpoint = "localhost";
32+
const int DefaultPort = 5000;
33+
const bool DefaultUseTls = false;
34+
#else
35+
const string DefaultEndpoint = "staging-internal.trinsic.cloud";
36+
const int DefaultPort = 443;
37+
const bool DefaultUseTls = true;
38+
#endif
39+
3040
private readonly ITestOutputHelper _testOutputHelper;
3141
private readonly ServiceOptions _options;
3242

3343
public Tests(ITestOutputHelper testOutputHelper) {
3444
_testOutputHelper = testOutputHelper;
3545

3646
_options = new() {
37-
ServerEndpoint = Environment.GetEnvironmentVariable("TEST_SERVER_ENDPOINT") ?? "localhost",
38-
ServerPort = int.TryParse(Environment.GetEnvironmentVariable("TEST_SERVER_PORT"), out var port) ? port : 5000,
39-
ServerUseTls = !bool.TryParse(Environment.GetEnvironmentVariable("TEST_SERVER_USE_TLS"), out var tls) || tls
47+
ServerEndpoint = Environment.GetEnvironmentVariable("TEST_SERVER_ENDPOINT") ?? DefaultEndpoint,
48+
ServerPort = int.TryParse(Environment.GetEnvironmentVariable("TEST_SERVER_PORT"), out var port) ? port : DefaultPort,
49+
ServerUseTls = bool.TryParse(Environment.GetEnvironmentVariable("TEST_SERVER_USE_TLS"), out var tls) ? tls : DefaultUseTls
4050
};
4151

4252
_testOutputHelper.WriteLine($"Testing endpoint: {_options.FormatUrl()}");
@@ -48,13 +58,10 @@ public Tests(ITestOutputHelper testOutputHelper) {
4858
[Fact(DisplayName = "Demo: wallet and credential sample")]
4959
public async Task TestWalletService() {
5060
// createAccountService() {
51-
var providerService = new ProviderService(_options);
52-
var accountService = new AccountService(_options);
53-
var account = await accountService.SignInAsync(new());
54-
55-
providerService.Options.AuthToken = account;
56-
var ecosystem = providerService.CreateEcosystem(new() {Name = $"test-sdk-{Guid.NewGuid():N}"});
57-
var ecosystemId = ecosystem.Ecosystem.Id;
61+
var providerService = new ProviderService(_options.Clone());
62+
var accountService = new AccountService(_options.Clone());
63+
var (ecosystem, _) = providerService.CreateEcosystem(new());
64+
var ecosystemId = ecosystem.Id;
5865
// }
5966

6067
// SETUP ACTORS
@@ -144,9 +151,9 @@ public async Task TestWalletService() {
144151
[Fact(DisplayName = "Demo: trust registries")]
145152
public async Task TestTrustRegistry() {
146153
// setup
147-
var accountService = new AccountService(_options);
148-
var account = await accountService.SignInAsync(new());
149-
var service = new TrustRegistryService(_options.CloneWithAuthToken(account));
154+
var providerService = new ProviderService(_options.Clone());
155+
var (_, authToken) = await providerService.CreateEcosystemAsync(new());
156+
var service = new TrustRegistryService(_options.CloneWithAuthToken(authToken));
150157

151158
// register issuer
152159
var register = service.RegisterIssuerAsync(new() {
@@ -203,15 +210,14 @@ public async Task EcosystemTests() {
203210
var service = new ProviderService(_options.CloneWithAuthToken(account));
204211

205212
// test create ecosystem
206-
var actualCreate = await service.CreateEcosystemAsync(new() {
213+
var (actualCreate, _) = await service.CreateEcosystemAsync(new() {
207214
Description = "My ecosystem",
208-
Name = $"test-sdk-{Guid.NewGuid():N}",
209215
Uri = "https://example.com"
210216
});
211217

212218
actualCreate.Should().NotBeNull();
213-
actualCreate.Ecosystem.Id.Should().NotBeNull();
214-
actualCreate.Ecosystem.Id.Should().StartWith("urn:trinsic:ecosystems:");
219+
actualCreate.Id.Should().NotBeNull();
220+
actualCreate.Id.Should().StartWith("urn:trinsic:ecosystems:");
215221
}
216222

217223
[Fact]
@@ -237,9 +243,8 @@ public async Task TestProtectUnprotectProfile() {
237243

238244
[Fact]
239245
public async Task TestInvitationIdSet() {
240-
var accountService = new AccountService(_options);
241-
var profile = await accountService.SignInAsync(new());
242-
var providerService = new ProviderService(_options.CloneWithAuthToken(profile));
246+
var providerService = new ProviderService(_options.Clone());
247+
_ = await providerService.CreateEcosystemAsync(new());
243248

244249
var invitationResponse = await providerService.InviteParticipantAsync(new());
245250

@@ -272,9 +277,9 @@ public async Task TestGovernanceFrameworkUriParse() {
272277

273278
[Fact(DisplayName = "Demo: template management and credential issuance from template")]
274279
public async Task DemoTemplatesWithIssuance() {
275-
var accountService = new AccountService(_options);
276-
var profile = await accountService.SignInAsync(new());
277-
var options = _options.CloneWithAuthToken(profile);
280+
var providerService = new ProviderService(_options.Clone());
281+
var (_, authToken) = await providerService.CreateEcosystemAsync(new());
282+
var options = _options.CloneWithAuthToken(authToken);
278283

279284
var templateService = new TemplateService(options);
280285
var credentialService = new CredentialsService(options);

dotnet/Trinsic/AccountService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public async Task<string> SignInAsync(SignInRequest request) {
4040
}
4141
var response = await Client.SignInAsync(request);
4242

43-
Options.AuthToken = Convert.ToBase64String(response.Profile.ToByteArray());
44-
return Options.AuthToken;
43+
var authToken = Convert.ToBase64String(response.Profile.ToByteArray());
44+
45+
if (!response.Profile.Protection?.Enabled ?? true) {
46+
Options.AuthToken = authToken;
47+
}
48+
return authToken;
4549
}
4650

4751
/// <summary>

dotnet/Trinsic/ProviderService.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ public InvitationStatusResponse InvitationStatus(InvitationStatusRequest request
6464
/// </summary>
6565
/// <param name="request"></param>
6666
/// <returns></returns>
67-
public async Task<CreateEcosystemResponse> CreateEcosystemAsync(CreateEcosystemRequest request) {
68-
if (string.IsNullOrWhiteSpace(request.Name)) throw new("Field 'name' must be specified");
67+
public async Task<(Ecosystem ecosystem, string authToken)> CreateEcosystemAsync(CreateEcosystemRequest request) {
6968
request.Details ??= new();
7069

71-
return await Client.CreateEcosystemAsync(request);
70+
var response = await Client.CreateEcosystemAsync(request);
71+
72+
var authToken = Convert.ToBase64String(response.Profile.ToByteArray());
73+
74+
if (!response.Profile.Protection?.Enabled ?? false) {
75+
Options.AuthToken = authToken;
76+
}
77+
return (response.Ecosystem, authToken);
7278
}
7379

7480
/// <summary>
@@ -77,13 +83,18 @@ public async Task<CreateEcosystemResponse> CreateEcosystemAsync(CreateEcosystemR
7783
/// <param name="request"></param>
7884
/// <returns></returns>
7985
/// <exception cref="Exception"></exception>
80-
public CreateEcosystemResponse CreateEcosystem(CreateEcosystemRequest request) {
81-
if (string.IsNullOrWhiteSpace(request.Name)) throw new("Field 'name' must be specified");
86+
public (Ecosystem ecosystem, string authToken) CreateEcosystem(CreateEcosystemRequest request) {
8287
request.Details ??= new();
83-
84-
return Client.CreateEcosystem(request);
88+
89+
var response = Client.CreateEcosystem(request);
90+
var authToken = Convert.ToBase64String(response.Profile.ToByteArray());
91+
92+
if (!response.Profile.Protection?.Enabled ?? true) {
93+
Options.AuthToken = authToken;
94+
}
95+
return (response.Ecosystem, authToken);
8596
}
86-
97+
8798
/// <summary>
8899
/// Generates an unprotected authentication token that can be used
89100
/// to configure server side applications
@@ -95,7 +106,7 @@ public async Task<string> GenerateTokenAsync(GenerateTokenRequest request) {
95106

96107
return Convert.ToBase64String(response.ToByteArray());
97108
}
98-
109+
99110
/// <summary>
100111
/// Generates an unprotected authentication token that can be used
101112
/// to configure server side applications

0 commit comments

Comments
 (0)