Skip to content

Commit 527735e

Browse files
committed
chore: update test code to use OAuth2.0
1 parent 3f0adb8 commit 527735e

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

user-management/tests/Stickerlandia.UserManagement.IntegrationTest/Drivers/AccountDriver.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,36 @@ public AccountDriver(ITestOutputHelper testOutputHelper, HttpClient httpClient,
6363

6464
public async Task<LoginResponse?> Login(string emailAddress, string password)
6565
{
66-
_testOutputHelper.WriteLine("Starting login request");
67-
68-
var requestBody = JsonSerializer.Serialize(new
66+
_testOutputHelper.WriteLine("Starting OAuth2.0 login request");
67+
68+
var requestBody = new List<KeyValuePair<string, string>>
6969
{
70-
emailAddress,
71-
password
72-
});
70+
new("grant_type", "password"),
71+
new("username", emailAddress),
72+
new("password", password),
73+
new("client_id", "user-authentication"), // Replace with your client_id
74+
new("client_secret", "388D45FA-B36B-4988-BA59-B187D329C207") // Replace with your client_secret
75+
};
7376

74-
var loginResult = await _httpClient.PostAsync("api/users/v1/login",
75-
new StringContent(requestBody, Encoding.Default, "application/json"));
77+
var requestContent = new FormUrlEncodedContent(requestBody);
78+
var tokenResult = await _httpClient.PostAsync("api/users/v1/login", requestContent); // Use the original endpoint
7679

77-
_testOutputHelper.WriteLine($"Login status code is {loginResult.StatusCode}");
80+
_testOutputHelper.WriteLine($"OAuth2.0 token status code is {tokenResult.StatusCode}");
7881

79-
var loginResultBody = await loginResult.Content.ReadAsStringAsync();
82+
var tokenResultBody = await tokenResult.Content.ReadAsStringAsync();
83+
if (string.IsNullOrEmpty(tokenResultBody)) return null;
8084

81-
if (string.IsNullOrEmpty(loginResultBody)) return null;
85+
using var doc = JsonDocument.Parse(tokenResultBody);
86+
if (!doc.RootElement.TryGetProperty("access_token", out var accessTokenElement))
87+
return null;
8288

83-
var parsedBody = JsonSerializer.Deserialize<ApiResponse<LoginResponse>>(loginResultBody);
89+
var accessToken = accessTokenElement.GetString();
90+
var expiresIn = doc.RootElement.TryGetProperty("expires_in", out var expiresInElement) ? expiresInElement.GetInt32() : 0;
8491

85-
return loginResult.IsSuccessStatusCode
86-
? parsedBody?.Data
87-
: null;
92+
return new LoginResponse
93+
{
94+
AuthToken = accessToken ?? string.Empty
95+
};
8896
}
8997

9098
public async Task<UserAccountDTO?> GetUserAccount(string authToken)
@@ -110,4 +118,4 @@ public async Task InjectStickerClaimedMessage(string userId, string stickerId)
110118
stickerId = stickerId
111119
});
112120
}
113-
}
121+
}

user-management/tests/Stickerlandia.UserManagement.IntegrationTest/Drivers/TestConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Stickerlandia.UserManagement.IntegrationTest.Drivers;
22

33
public static class TestConstants
44
{
5-
public static string DefaultTestUrl = Environment.GetEnvironmentVariable("API_ENDPOINT") ?? "http://localhost:5139";
5+
public static string DefaultTestUrl = Environment.GetEnvironmentVariable("API_ENDPOINT") ?? "https://localhost:51545";
66

77
public static string DefaultMessagingConnection = Environment.GetEnvironmentVariable("MESSAGING_ENDPOINT") ??
88
"Endpoint=sb://localhost:60001;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";

user-management/tests/Stickerlandia.UserManagement.IntegrationTest/Hooks/TestSetupFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public TestSetupFixture()
5656
else
5757
{
5858
Messaging = new AzureServiceBusMessaging(TestConstants.DefaultMessagingConnection);
59-
HttpClient = new HttpClient();
59+
HttpClient = new HttpClient()
60+
{
61+
BaseAddress = new Uri(TestConstants.DefaultTestUrl)
62+
};
6063
}
6164
}
6265

0 commit comments

Comments
 (0)