Skip to content

Commit 151a297

Browse files
committed
feat(DX-3908): obtain auth token at runtime for integration tests.
- Run Login once in AssemblyInitialize and inject token into shared client - Add GetLoginCredentials() (env CONTENTSTACK_USERNAME/PASSWORD then config) - Add SetRuntimeAuthToken() and use it when building Contentstack.Client - Login tests use GetLoginCredentials(); remove redundant Login in DeliveryTokenTest - Contentstack:Authtoken in appSettings.json no longer required
1 parent 4880712 commit 151a297

4 files changed

Lines changed: 69 additions & 29 deletions

File tree

Contentstack.Management.Core.Tests/Contentstack.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
55
using System.Net;
66
using System.Reflection;
7+
using Contentstack.Management.Core;
78
using Contentstack.Management.Core.Tests.Model;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.Options;
@@ -14,11 +15,39 @@ namespace Contentstack.Management.Core.Tests
1415
{
1516
public class Contentstack
1617
{
18+
/// <summary>
19+
/// Runtime token set by integration test assembly init (Login once, inject for shared client).
20+
/// When set, overrides Authtoken from config when building Contentstack.Client.
21+
/// </summary>
22+
private static string _runtimeAuthToken;
23+
24+
/// <summary>
25+
/// Sets the runtime auth token used when building the shared Client. Used by integration test assembly init.
26+
/// </summary>
27+
public static void SetRuntimeAuthToken(string token)
28+
{
29+
_runtimeAuthToken = token;
30+
}
31+
32+
/// <summary>
33+
/// Returns credentials for Login: environment variables CONTENTSTACK_USERNAME and CONTENTSTACK_PASSWORD first, then config.
34+
/// </summary>
35+
public static NetworkCredential GetLoginCredentials()
36+
{
37+
var envUser = Environment.GetEnvironmentVariable("CONTENTSTACK_USERNAME");
38+
var envPass = Environment.GetEnvironmentVariable("CONTENTSTACK_PASSWORD");
39+
if (!string.IsNullOrEmpty(envUser) && !string.IsNullOrEmpty(envPass))
40+
return new NetworkCredential(envUser, envPass);
41+
return Config.GetSection("Contentstack:Credentials").Get<NetworkCredential>();
42+
}
43+
1744
private static readonly Lazy<ContentstackClient>
1845
client =
1946
new Lazy<ContentstackClient>(() =>
2047
{
2148
ContentstackClientOptions options = Config.GetSection("Contentstack").Get<ContentstackClientOptions>();
49+
if (!string.IsNullOrEmpty(_runtimeAuthToken))
50+
options.Authtoken = _runtimeAuthToken;
2251
return new ContentstackClient(new OptionsWrapper<ContentstackClientOptions>(options));
2352
});
2453

Contentstack.Management.Core.Tests/IntegrationTest/Contentstack001_LoginTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Net;
33
using Contentstack.Management.Core.Exceptions;
44
using Contentstack.Management.Core.Models;
@@ -65,7 +65,7 @@ public async System.Threading.Tasks.Task Test003_Should_Return_Success_On_Async_
6565

6666
try
6767
{
68-
ContentstackResponse contentstackResponse = await client.LoginAsync(Contentstack.Credential);
68+
ContentstackResponse contentstackResponse = await client.LoginAsync(Contentstack.GetLoginCredentials());
6969
string loginResponse = contentstackResponse.OpenResponse();
7070

7171
Assert.IsNotNull(client.contentstackOptions.Authtoken);
@@ -87,7 +87,7 @@ public void Test004_Should_Return_Success_On_Login()
8787
{
8888
ContentstackClient client = new ContentstackClient();
8989

90-
ContentstackResponse contentstackResponse = client.Login(Contentstack.Credential);
90+
ContentstackResponse contentstackResponse = client.Login(Contentstack.GetLoginCredentials());
9191
string loginResponse = contentstackResponse.OpenResponse();
9292

9393
Assert.IsNotNull(client.contentstackOptions.Authtoken);
@@ -107,7 +107,7 @@ public void Test005_Should_Return_Loggedin_User()
107107
{
108108
ContentstackClient client = new ContentstackClient();
109109

110-
client.Login(Contentstack.Credential);
110+
client.Login(Contentstack.GetLoginCredentials());
111111

112112
ContentstackResponse response = client.GetUser();
113113

@@ -130,7 +130,7 @@ public async System.Threading.Tasks.Task Test006_Should_Return_Loggedin_User_Asy
130130
{
131131
ContentstackClient client = new ContentstackClient();
132132

133-
await client.LoginAsync(Contentstack.Credential);
133+
await client.LoginAsync(Contentstack.GetLoginCredentials());
134134

135135
ContentstackResponse response = await client.GetUserAsync();
136136

@@ -159,7 +159,7 @@ public void Test007_Should_Return_Loggedin_User_With_Organizations_detail()
159159

160160
ContentstackClient client = new ContentstackClient();
161161

162-
client.Login(Contentstack.Credential);
162+
client.Login(Contentstack.GetLoginCredentials());
163163

164164
ContentstackResponse response = client.GetUser(collection);
165165

Contentstack.Management.Core.Tests/IntegrationTest/Contentstack016_DeliveryTokenTest.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,6 @@ public async Task Initialize()
2424
{
2525
try
2626
{
27-
// First, ensure the client is logged in
28-
try
29-
{
30-
ContentstackResponse loginResponse = Contentstack.Client.Login(Contentstack.Credential);
31-
if (!loginResponse.IsSuccessStatusCode)
32-
{
33-
Assert.Fail($"Login failed: {loginResponse.OpenResponse()}");
34-
}
35-
}
36-
catch (Exception loginEx)
37-
{
38-
// If already logged in, that's fine - continue with the test
39-
if (loginEx.Message.Contains("already logged in"))
40-
{
41-
Console.WriteLine("Client already logged in, continuing with test");
42-
}
43-
else
44-
{
45-
throw; // Re-throw if it's a different error
46-
}
47-
}
48-
4927
StackResponse response = StackResponse.getStack(Contentstack.Client.serializer);
5028
_stack = Contentstack.Client.Stack(response.Stack.APIKey);
5129

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Contentstack.Management.Core;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Contentstack.Management.Core.Tests.IntegrationTest
6+
{
7+
[TestClass]
8+
public class IntegrationTestAssembly
9+
{
10+
[AssemblyInitialize]
11+
public static void Init(TestContext context)
12+
{
13+
try
14+
{
15+
var client = new ContentstackClient();
16+
var credentials = Contentstack.GetLoginCredentials();
17+
var response = client.Login(credentials);
18+
if (!response.IsSuccessStatusCode)
19+
{
20+
throw new InvalidOperationException(
21+
$"Assembly init Login failed: {response.OpenResponse()}");
22+
}
23+
Contentstack.SetRuntimeAuthToken(client.contentstackOptions.Authtoken);
24+
}
25+
catch (Exception ex)
26+
{
27+
throw new InvalidOperationException(
28+
"Integration test assembly init failed (Login to get runtime token). Set CONTENTSTACK_USERNAME and CONTENTSTACK_PASSWORD or Contentstack:Credentials in appsettings.json.",
29+
ex);
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)