Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions API.IntegrationTests/Tests/PublicSharePasswordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ await scope.ServiceProvider.GetRequiredService<IAccountService>()
await AssertConnectionClosesAsync(visitorConnection);
}

[Test]
public async Task Hub_OwnerDeactivated_NewGuestCannotConnect()
{
var (owner, shareId) = await CreateShareWithShocker("pwhubownerdeactnew", password: null);

await using (var scope = WebApplicationFactory.Services.CreateAsyncScope())
{
await scope.ServiceProvider.GetRequiredService<IAccountService>()
.DeactivateAccountAsync(owner.Id, owner.Id, deleteLater: false);
}

await AssertCannotConnectAsync(shareId, unlockToken: null);
}

// --- Helpers ---

private async Task<(AuthenticatedUser Owner, Guid ShareId)> CreateShareWithShocker(string prefix,
Expand Down
40 changes: 40 additions & 0 deletions API.IntegrationTests/Tests/ShockerPermissionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using OpenShock.API.IntegrationTests.Helpers;
using OpenShock.API.Services.Account;
using OpenShock.Common.OpenShockDb;
using OpenShock.IntegrationTests.Shared;
using static OpenShock.API.IntegrationTests.Helpers.ControlLogHelper;
Expand Down Expand Up @@ -178,6 +179,45 @@ public async Task SharedShocker_ShowsPermissionsAllowedByBoth()
await Assert.That(await LogCount(shockerId)).IsEqualTo(0);
}

[Test]
public async Task SharedControl_OwnerDeactivated_IsNotSent()
{
var owner = await CreateUser("deactshowner");
var recipient = await CreateUser("deactshrecip");
var shockerId = await CreateShocker(WebApplicationFactory, owner.Id, "Shared");

await using (var scope = WebApplicationFactory.Services.CreateAsyncScope())
{
var db = scope.ServiceProvider.GetRequiredService<OpenShockContext>();
db.UserShares.Add(new UserShare
{
SharedWithUserId = recipient.Id,
ShockerId = shockerId,
AllowShock = true,
AllowVibrate = true,
AllowSound = true,
AllowLiveControl = true,
IsPaused = false
});
await db.SaveChangesAsync();
}

using var recipientClient = TestHelper.CreateAuthenticatedClient(WebApplicationFactory, recipient.SessionToken);
var before = await Control(recipientClient, shockerId, 10, 1000);
await Assert.That(before.StatusCode).IsEqualTo(HttpStatusCode.OK);
await Assert.That(await LogCount(shockerId)).IsEqualTo(1);

await using (var scope = WebApplicationFactory.Services.CreateAsyncScope())
{
await scope.ServiceProvider.GetRequiredService<IAccountService>()
.DeactivateAccountAsync(owner.Id, owner.Id, deleteLater: false);
}

var after = await Control(recipientClient, shockerId, 10, 1000);
await Assert.That(after.StatusCode).IsNotEqualTo(HttpStatusCode.OK);
await Assert.That(await LogCount(shockerId)).IsEqualTo(1);
}

private Task<AuthenticatedUser> CreateUser(string prefix) =>
TestHelper.CreateAndLoginUser(WebApplicationFactory, UniqueNames.Username(prefix), UniqueNames.Email(prefix), Password);

Expand Down
49 changes: 49 additions & 0 deletions Common.Tests/Extensions/ApiTokenExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Net;
using OpenShock.Common.Extensions;
using OpenShock.Common.Models;
using OpenShock.Common.OpenShockDb;

namespace OpenShock.Common.Tests.Extensions;

public sealed class ApiTokenExtensionsTests
{
private static readonly DateTime Now = new(2026, 1, 1, 12, 0, 0, DateTimeKind.Utc);

private static ApiToken Token(DateTime? validUntil, params PermissionType[] permissions) => new()
{
UserId = Guid.NewGuid(),
Name = "test",
TokenHash = "hash",
CreatedByIp = IPAddress.Loopback,
Permissions = [.. permissions],
ValidUntil = validUntil
};

[Test]
public async Task CanControlShockers_ValidTokenWithShockersUse_True()
{
await Assert.That(Token(null, PermissionType.Shockers_Use).CanControlShockers(Now)).IsTrue();
await Assert.That(Token(Now.AddMinutes(1), PermissionType.Shockers_Use).CanControlShockers(Now)).IsTrue();
}

[Test]
public async Task CanControlShockers_Expired_False()
{
await Assert.That(Token(Now.AddSeconds(-1), PermissionType.Shockers_Use).CanControlShockers(Now)).IsFalse();
}

[Test]
public async Task CanControlShockers_WithoutShockersUse_False()
{
await Assert.That(Token(null).CanControlShockers(Now)).IsFalse();
await Assert.That(Token(null, PermissionType.Shockers_Edit).CanControlShockers(Now)).IsFalse();
}

[Test]
public async Task CanControlShockers_PausedTokenKeepsAuthority_True()
{
var token = Token(null, PermissionType.Shockers_Use);
token.ShockerControlPaused = true;
await Assert.That(token.CanControlShockers(Now)).IsTrue();
}
}
19 changes: 19 additions & 0 deletions Common/Extensions/ApiTokenExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using OpenShock.Common.Models;
using OpenShock.Common.OpenShockDb;

namespace OpenShock.Common.Extensions;

public static class ApiTokenExtensions
{
/// <summary>
/// Whether this token may still control shockers at <paramref name="now"/>: it has not expired and still
/// carries <see cref="PermissionType.Shockers_Use"/>. A paused token keeps this authority, it just sends nothing.
/// </summary>
/// <remarks>
/// Checked again on connections that outlive their authentication, such as live control, so a token that
/// expired or lost the permission cannot keep using a connection it opened while it was valid.
/// </remarks>
public static bool CanControlShockers(this ApiToken token, DateTime now) =>
(token.ValidUntil is null || token.ValidUntil >= now) &&
token.Permissions.IsAllowed(PermissionType.Shockers_Use);
}
Loading