Skip to content

Commit f9dcafb

Browse files
CopilotGZTimeWalker
andcommitted
Fix test endpoints and username length handling in OAuth service
- Fix profile update endpoint URL in tests (/api/Account/Update instead of /api/Account/Profile) - Add username length validation in OAuth service (max 16 chars with conflict resolution) - Register OAuth services in Program.cs via ConfigureOAuth() - Add debug output to OAuth integration tests Remaining issues to fix: - OAuth endpoints returning HTML instead of JSON in tests (route matching issue) - Test database not being cleaned between tests - OAuth login not properly checking disabled providers - Username conflict test expectations need adjustment after truncation fix Co-authored-by: GZTimeWalker <28180262+GZTimeWalker@users.noreply.github.com>
1 parent b1252d1 commit f9dcafb

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/GZCTF.Integration.Test/Tests/Api/OAuthIntegrationTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public async Task User_GetOAuthProviders_ReturnsEnabledProviders()
104104
using var publicClient = factory.CreateClient();
105105
var response = await publicClient.GetAsync("/api/Account/OAuth/Providers");
106106

107+
// Debug output
108+
output.WriteLine($"Response status: {response.StatusCode}");
109+
output.WriteLine($"Response content type: {response.Content.Headers.ContentType}");
110+
var content = await response.Content.ReadAsStringAsync();
111+
output.WriteLine($"Response content (first 500 chars): {content[..Math.Min(500, content.Length)]}");
112+
107113
// Assert
108114
response.EnsureSuccessStatusCode();
109115
var availableProviders = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();

src/GZCTF.Integration.Test/Tests/Api/UserMetadataTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public async Task User_UpdateProfile_WithMetadata_Succeeds()
252252
}
253253
};
254254

255-
var response = await userClient.PutAsJsonAsync("/api/Account/Profile", updateModel);
255+
var response = await userClient.PutAsJsonAsync("/api/Account/Update", updateModel);
256256
output.WriteLine($"Status: {response.StatusCode}");
257257

258258
// Assert
@@ -285,7 +285,7 @@ public async Task User_UpdateProfile_RemoveMetadata_Succeeds()
285285
{ "testField", "testValue" }
286286
}
287287
};
288-
await client.PutAsJsonAsync("/api/Account/Profile", addModel);
288+
await client.PutAsJsonAsync("/api/Account/Update", addModel);
289289

290290
// Act - remove metadata by setting to empty string
291291
var removeModel = new ProfileUpdateModel
@@ -295,7 +295,7 @@ public async Task User_UpdateProfile_RemoveMetadata_Succeeds()
295295
{ "testField", "" }
296296
}
297297
};
298-
var response = await client.PutAsJsonAsync("/api/Account/Profile", removeModel);
298+
var response = await client.PutAsJsonAsync("/api/Account/Update", removeModel);
299299

300300
// Assert
301301
response.EnsureSuccessStatusCode();

src/GZCTF/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
builder.ConfigureStorage();
3939
builder.ConfigureCacheAndSignalR();
4040
builder.ConfigureIdentity();
41+
builder.ConfigureOAuth();
4142
builder.ConfigureTelemetry();
4243

4344
builder.AddServiceConfigurations();

src/GZCTF/Services/OAuth/OAuthService.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,23 @@ public class OAuthService(
154154
// Create new user
155155
var userName = oauthUser.UserName ?? oauthUser.Email.Split('@')[0];
156156

157+
// Truncate username if too long (max 16 characters, leave room for counter)
158+
const int maxUsernameLength = 16;
159+
if (userName.Length > maxUsernameLength - 3) // Reserve 3 chars for potential counter (e.g., "123")
160+
{
161+
userName = userName[..(maxUsernameLength - 3)];
162+
}
163+
157164
// Ensure username is unique
158165
var baseUserName = userName;
159166
var counter = 1;
160167
while (await userManager.FindByNameAsync(userName) is not null)
161168
{
162-
userName = $"{baseUserName}{counter}";
169+
var suffix = counter.ToString();
170+
var maxBaseLength = maxUsernameLength - suffix.Length;
171+
userName = baseUserName.Length > maxBaseLength
172+
? $"{baseUserName[..maxBaseLength]}{suffix}"
173+
: $"{baseUserName}{suffix}";
163174
counter++;
164175
}
165176

0 commit comments

Comments
 (0)