Repository navigation
feat: close hub connections when their session or API token is revoked #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bfa72d5
feat: close hub connections when their session or API token is revoked
hhvrc 0ba8199
Merge remote-tracking branch 'origin/develop' into feature/hub-connec…
LucHeart e36716c
fix: admin deactivate, reactivate and delete user failed on every call
LucHeart 788cf7e
test: cover session expiry, account deletion, token edits and public …
LucHeart 6761ce3
fix: address review on hub revalidation
LucHeart 3c680b0
test: assert the specific rejection when deleting another admin
LucHeart 4d2dab1
refactor: drop the generation guard from hub revalidation
LucHeart 09a5a17
refactor: close API token connections on any token change
LucHeart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System.Net; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using OpenShock.API.IntegrationTests.Helpers; | ||
| using OpenShock.Common.Models; | ||
| using OpenShock.Common.OpenShockDb; | ||
|
|
||
| namespace OpenShock.API.IntegrationTests.Tests; | ||
|
|
||
| public sealed class AdminUserLifecycleTests | ||
| { | ||
| [ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerTestSession)] | ||
| public required WebApplicationFactory WebApplicationFactory { get; init; } | ||
|
|
||
| [Test] | ||
| public async Task Admin_CanDeactivateAndReactivateAUser() | ||
| { | ||
| using var admin = await CreateAdminClient("adminlcdeact"); | ||
| var userId = await TestHelper.CreateUserInDb(WebApplicationFactory, TestHelper.UniqueUsername("adminlcdeactu"), | ||
| TestHelper.UniqueEmail("adminlcdeactu"), "SecurePassword123#"); | ||
|
|
||
| var deactivate = | ||
| await admin.PutAsync($"/1/admin/users/{userId}/deactivate?deleteLater=false&reason=test", null); | ||
| await Assert.That(deactivate.StatusCode).IsEqualTo(HttpStatusCode.OK); | ||
| await Assert.That(await IsDeactivated(userId)).IsTrue(); | ||
|
|
||
| var reactivate = await admin.PutAsync($"/1/admin/users/{userId}/reactivate?reason=test", null); | ||
| await Assert.That(reactivate.StatusCode).IsEqualTo(HttpStatusCode.OK); | ||
| await Assert.That(await IsDeactivated(userId)).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Admin_CanDeleteAUser() | ||
| { | ||
| using var admin = await CreateAdminClient("adminlcdelete"); | ||
| var userId = await TestHelper.CreateUserInDb(WebApplicationFactory, TestHelper.UniqueUsername("adminlcdeleteu"), | ||
| TestHelper.UniqueEmail("adminlcdeleteu"), "SecurePassword123#"); | ||
|
|
||
| var response = await admin.DeleteAsync($"/1/admin/users/{userId}?reason=test"); | ||
| await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); | ||
|
|
||
| await using var scope = WebApplicationFactory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<OpenShockContext>(); | ||
| await Assert.That(await db.Users.AnyAsync(u => u.Id == userId)).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Admin_CannotDeleteAnotherAdmin() | ||
| { | ||
| using var admin = await CreateAdminClient("adminlcpriv"); | ||
| var otherAdmin = await TestHelper.CreateAndLoginUser(WebApplicationFactory, | ||
| TestHelper.UniqueUsername("adminlcprivo"), TestHelper.UniqueEmail("adminlcprivo"), "SecurePassword123#"); | ||
| await SetRoles(otherAdmin.Id, [RoleType.Admin]); | ||
|
|
||
| var response = await admin.DeleteAsync($"/1/admin/users/{otherAdmin.Id}"); | ||
| await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.Forbidden); | ||
| await Assert.That(await response.Content.ReadAsStringAsync()).Contains("Account.Deactivate.DeniedPrivileged"); | ||
|
|
||
| await using var scope = WebApplicationFactory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<OpenShockContext>(); | ||
| await Assert.That(await db.Users.AnyAsync(u => u.Id == otherAdmin.Id)).IsTrue(); | ||
| } | ||
|
|
||
| private async Task<HttpClient> CreateAdminClient(string prefix) | ||
| { | ||
| var admin = await TestHelper.CreateAndLoginUser(WebApplicationFactory, TestHelper.UniqueUsername(prefix), | ||
| TestHelper.UniqueEmail(prefix), "SecurePassword123#"); | ||
| await SetRoles(admin.Id, [RoleType.Admin]); | ||
| return TestHelper.CreateAuthenticatedClient(WebApplicationFactory, admin.SessionToken); | ||
| } | ||
|
|
||
| private async Task SetRoles(Guid userId, List<RoleType> roles) | ||
| { | ||
| await using var scope = WebApplicationFactory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<OpenShockContext>(); | ||
| await db.Users.Where(u => u.Id == userId).ExecuteUpdateAsync(s => s.SetProperty(u => u.Roles, roles)); | ||
| } | ||
|
|
||
| private async Task<bool> IsDeactivated(Guid userId) | ||
| { | ||
| await using var scope = WebApplicationFactory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<OpenShockContext>(); | ||
| return await db.UserDeactivations.AnyAsync(d => d.DeactivatedUserId == userId); | ||
| } | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using OpenShock.Common.Hubs; | ||
|
|
||
| namespace OpenShock.Common.Tests.Hubs; | ||
|
|
||
| public sealed class TrackedHubConnectionTests | ||
| { | ||
| private static TrackedHubConnection Create() => new(null!, Guid.NewGuid(), sessionId: null, apiTokenId: Guid.NewGuid()); | ||
|
|
||
| [Test] | ||
| public async Task NewConnection_IsNotValidated() | ||
| { | ||
| var connection = Create(); | ||
|
|
||
| await Assert.That(connection.WasValidatedWithin(TimeSpan.FromMinutes(1))).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task MarkValidated_RecordsTheCheck() | ||
| { | ||
| var connection = Create(); | ||
| connection.MarkValidated(); | ||
|
|
||
| await Assert.That(connection.WasValidatedWithin(TimeSpan.FromMinutes(1))).IsTrue(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not let notification failure interrupt committed account operations.
If
SendUserAuthRevokedfaults at Line 384, deactivation is already committed. The exception skips session deletion andRefreshOwnedDevicesAsync. A retry returnsAccountDeactivationAlreadyInProgress, so it cannot finish those steps.At Line 470, the same failure reports an error after account deletion has committed. A retry returns
NotFound.Handle publication failures separately. Log the failure and preserve the remaining lifecycle steps and successful outcome. Keep publication on the successful path. The periodic validation already provides the stated fallback for missed notifications.
Also applies to: 470-470