Merge branch 'master' into p2p_voice_call_turn
This commit is contained in:
commit
fe879781ab
23 changed files with 983 additions and 46 deletions
|
|
@ -321,4 +321,62 @@ public class DatabaseTests : IDisposable
|
|||
await _db.RemovePushSubscriptionByEndpointAsync("https://push.example.org/send/sub_1");
|
||||
Assert.Empty(await _db.GetPushSubscriptionsForUserAsync(bob.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteFileRecord_ShouldDeleteRecordAndAssociatedAttachmentMessages()
|
||||
{
|
||||
var channels = await _db.GetChannelsAsync();
|
||||
var general = channels[0];
|
||||
|
||||
var file = new FileRecord
|
||||
{
|
||||
Id = "file_delete_test_1",
|
||||
ChannelId = general.Id,
|
||||
UploaderId = "user_uploader_1",
|
||||
UploaderUsername = "uploader_user",
|
||||
OriginalFileName = "document_to_delete.pdf",
|
||||
ContentType = "application/pdf",
|
||||
FileSizeBytes = 1234,
|
||||
StoragePath = "/tmp/dummy.pdf",
|
||||
Sha256Checksum = "dummy_sha256",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
await _db.SaveFileRecordAsync(file);
|
||||
|
||||
var msg = new ChatMessage
|
||||
{
|
||||
ChannelId = general.Id,
|
||||
SenderId = "user_uploader_1",
|
||||
SenderUsername = "uploader_user",
|
||||
Content = "Shared file: document_to_delete.pdf",
|
||||
AttachmentId = file.Id,
|
||||
AttachmentName = file.OriginalFileName,
|
||||
AttachmentSizeBytes = file.FileSizeBytes
|
||||
};
|
||||
await _db.SaveMessageAsync(msg);
|
||||
|
||||
// Verify file is returned in recent files and includes UploaderId
|
||||
var recent = await _db.GetRecentFilesAsync();
|
||||
var summary = recent.Find(f => f.Id == file.Id);
|
||||
Assert.NotNull(summary);
|
||||
Assert.Equal("user_uploader_1", summary.UploaderId);
|
||||
Assert.Equal("uploader_user", summary.UploaderUsername);
|
||||
|
||||
// Verify message exists
|
||||
var msgs = await _db.GetRecentMessagesAsync(general.Id);
|
||||
Assert.Contains(msgs, m => m.AttachmentId == file.Id);
|
||||
|
||||
// Delete file record
|
||||
bool deleted = await _db.DeleteFileRecordAsync(file.Id);
|
||||
Assert.True(deleted);
|
||||
|
||||
// Verify file record is gone
|
||||
Assert.Null(await _db.GetFileRecordAsync(file.Id));
|
||||
recent = await _db.GetRecentFilesAsync();
|
||||
Assert.DoesNotContain(recent, f => f.Id == file.Id);
|
||||
|
||||
// Verify message with attachment_id is gone
|
||||
msgs = await _db.GetRecentMessagesAsync(general.Id);
|
||||
Assert.DoesNotContain(msgs, m => m.AttachmentId == file.Id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -234,6 +234,44 @@ public class EndToEndChatTests : IAsyncLifetime
|
|||
return Results.File(stream, record.ContentType, record.OriginalFileName);
|
||||
});
|
||||
|
||||
_app.MapDelete("/api/files/{id}", async (string id, HttpRequest req, IAuthService auth, ChatDatabase chatDb, FileStorageService fileSvc, ChatWebSocketManager ws) =>
|
||||
{
|
||||
string? token = ExtractToken(req);
|
||||
var user = await auth.ValidateSessionTokenAsync(token ?? "");
|
||||
if (user == null) return Results.Unauthorized();
|
||||
|
||||
var record = await fileSvc.GetFileRecordAsync(id);
|
||||
if (record == null) return Results.NotFound(new DeleteFileResponse { Success = false, ErrorMessage = "File not found." });
|
||||
|
||||
bool isUploader = string.Equals(user.Id, record.UploaderId, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(user.Username, record.UploaderUsername, StringComparison.OrdinalIgnoreCase);
|
||||
bool isAdmin = string.Equals(user.Role, UserRoles.Admin, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(user.Role, UserRoles.Owner, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (!isUploader && !isAdmin)
|
||||
{
|
||||
return Results.Json(new DeleteFileResponse
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = "You do not have permission to delete this file. Only the uploader or an admin can delete it."
|
||||
}, statusCode: StatusCodes.Status403Forbidden);
|
||||
}
|
||||
|
||||
bool deleted = await fileSvc.DeleteFileAsync(record.Id);
|
||||
if (!deleted)
|
||||
{
|
||||
return Results.BadRequest(new DeleteFileResponse { Success = false, ErrorMessage = "Failed to delete file." });
|
||||
}
|
||||
|
||||
await ws.BroadcastFileDeletedAsync(record, user.Username);
|
||||
|
||||
return Results.Ok(new DeleteFileResponse
|
||||
{
|
||||
Success = true,
|
||||
FileId = record.Id
|
||||
});
|
||||
});
|
||||
|
||||
_app.MapGet("/ws", async (HttpContext context, IAuthService auth, ChatWebSocketManager ws, ConnectionLimiterService connLimiter, ServerOptions options) =>
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
|
|
@ -390,6 +428,28 @@ public class EndToEndChatTests : IAsyncLifetime
|
|||
Assert.True(File.Exists(downloadedFilePath));
|
||||
string downloadedText = await File.ReadAllTextAsync(downloadedFilePath);
|
||||
Assert.Equal(fileContent, downloadedText);
|
||||
|
||||
// 7. Bob (non-uploader, member) tries to delete Alice's file -> should fail with permission error
|
||||
bool bobDeleteSuccess = await connBob.DeleteFileAsync(sharedFile.Id);
|
||||
Assert.False(bobDeleteSuccess);
|
||||
Assert.Contains("403", stateBob.StatusMessage);
|
||||
|
||||
// 8. Alice (uploader) deletes the file -> succeeds
|
||||
bool aliceDeleteSuccess = await connAlice.DeleteFileAsync(sharedFile.Id);
|
||||
Assert.True(aliceDeleteSuccess);
|
||||
|
||||
// Wait for file list sync to reflect deletion for both Bob and Alice
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (!stateBob.Files.Exists(f => f.Id == sharedFile.Id) && !stateAlice.Files.Exists(f => f.Id == sharedFile.Id))
|
||||
{
|
||||
break;
|
||||
}
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
Assert.DoesNotContain(stateBob.Files, f => f.Id == sharedFile.Id);
|
||||
Assert.DoesNotContain(stateAlice.Files, f => f.Id == sharedFile.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -1002,6 +1062,42 @@ public class EndToEndChatTests : IAsyncLifetime
|
|||
bobItems = stateBob.GetInboxItems();
|
||||
Assert.Contains(bobItems, x => x.Category == InboxCategories.FollowedChannel && x.ContentPreview.Contains("Regular update"));
|
||||
|
||||
// 4b. Bob looks at / joins #general -> all inbox items for #general are auto-marked read!
|
||||
await connBob.JoinChannelAsync(generalId);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (stateBob.UnreadInboxCount == 0) break;
|
||||
await Task.Delay(100);
|
||||
}
|
||||
Assert.Equal(0, stateBob.UnreadInboxCount);
|
||||
bobItems = stateBob.GetInboxItems();
|
||||
Assert.All(bobItems, item => Assert.True(item.IsRead));
|
||||
|
||||
// 4c. Alice sends DM to Bob while Bob is in #general -> Bob receives unread DM inbox item
|
||||
await connAlice.StartDmAsync(stateBob.CurrentUsername, stateBob.CurrentUserId);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (stateAlice.DirectMessages.Count > 0 && stateBob.DirectMessages.Count > 0) break;
|
||||
await Task.Delay(100);
|
||||
}
|
||||
string dmId = stateBob.DirectMessages[0].Id;
|
||||
await connAlice.SendChatMessageAsync(dmId, "Hey Bob, private message here!");
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (stateBob.UnreadInboxCount >= 1) break;
|
||||
await Task.Delay(100);
|
||||
}
|
||||
Assert.Equal(1, stateBob.UnreadInboxCount);
|
||||
|
||||
// When Bob opens/looks at the DM -> DM inbox item is marked read!
|
||||
await connBob.JoinChannelAsync(dmId);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (stateBob.UnreadInboxCount == 0) break;
|
||||
await Task.Delay(100);
|
||||
}
|
||||
Assert.Equal(0, stateBob.UnreadInboxCount);
|
||||
|
||||
// 5. Bob marks all read and clears read items
|
||||
await connBob.SendInboxActionAsync("mark_all_read");
|
||||
for (int i = 0; i < 20; i++)
|
||||
|
|
|
|||
|
|
@ -78,4 +78,83 @@ public class FileStorageServiceTests : IDisposable
|
|||
string readBack = await reader.ReadToEndAsync();
|
||||
Assert.Equal(fileContent, readBack);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteFile_ShouldDeleteFileFromDiskAndDatabase()
|
||||
{
|
||||
var channels = await _db.GetChannelsAsync();
|
||||
var general = channels[0];
|
||||
|
||||
string fileContent = "File to be deleted.";
|
||||
byte[] fileBytes = Encoding.UTF8.GetBytes(fileContent);
|
||||
|
||||
using var memoryStream = new MemoryStream(fileBytes);
|
||||
var (record, message) = await _fileService.StoreFileWithMessageAsync(
|
||||
general.Id,
|
||||
"uploader-1",
|
||||
"Tester",
|
||||
"delete_me.txt",
|
||||
"text/plain",
|
||||
memoryStream,
|
||||
fileBytes.Length
|
||||
);
|
||||
|
||||
Assert.NotNull(record);
|
||||
Assert.True(File.Exists(record.StoragePath));
|
||||
Assert.NotNull(await _fileService.GetFileRecordAsync(record.Id));
|
||||
|
||||
var msgsBefore = await _db.GetRecentMessagesAsync(general.Id);
|
||||
Assert.Contains(msgsBefore, m => m.AttachmentId == record.Id);
|
||||
|
||||
// Delete the file
|
||||
bool deleted = await _fileService.DeleteFileAsync(record.Id);
|
||||
Assert.True(deleted);
|
||||
|
||||
// Verify disk file is gone
|
||||
Assert.False(File.Exists(record.StoragePath));
|
||||
|
||||
// Verify database record is gone
|
||||
Assert.Null(await _fileService.GetFileRecordAsync(record.Id));
|
||||
|
||||
// Verify associated attachment message is deleted
|
||||
var msgsAfter = await _db.GetRecentMessagesAsync(general.Id);
|
||||
Assert.DoesNotContain(msgsAfter, m => m.AttachmentId == record.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteFile_NonExistentId_ShouldReturnFalse()
|
||||
{
|
||||
bool deleted = await _fileService.DeleteFileAsync("non_existent_file_id");
|
||||
Assert.False(deleted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileDeletePermissions_ShouldAllowUploaderAndAdminAndOwnerOnly()
|
||||
{
|
||||
var file = new GsDChat.Core.Models.FileRecord
|
||||
{
|
||||
Id = "f1",
|
||||
UploaderId = "user-alice",
|
||||
UploaderUsername = "alice"
|
||||
};
|
||||
|
||||
static bool CanDelete(GsDChat.Core.Models.User user, GsDChat.Core.Models.FileRecord record)
|
||||
{
|
||||
bool isUploader = string.Equals(user.Id, record.UploaderId, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(user.Username, record.UploaderUsername, StringComparison.OrdinalIgnoreCase);
|
||||
bool isAdmin = string.Equals(user.Role, GsDChat.Core.Models.UserRoles.Admin, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(user.Role, GsDChat.Core.Models.UserRoles.Owner, StringComparison.OrdinalIgnoreCase);
|
||||
return isUploader || isAdmin;
|
||||
}
|
||||
|
||||
var uploaderMember = new GsDChat.Core.Models.User { Id = "user-alice", Username = "alice", Role = GsDChat.Core.Models.UserRoles.Member };
|
||||
var otherMember = new GsDChat.Core.Models.User { Id = "user-bob", Username = "bob", Role = GsDChat.Core.Models.UserRoles.Member };
|
||||
var admin = new GsDChat.Core.Models.User { Id = "user-charlie", Username = "charlie", Role = GsDChat.Core.Models.UserRoles.Admin };
|
||||
var owner = new GsDChat.Core.Models.User { Id = "user-dan", Username = "dan", Role = GsDChat.Core.Models.UserRoles.Owner };
|
||||
|
||||
Assert.True(CanDelete(uploaderMember, file));
|
||||
Assert.False(CanDelete(otherMember, file));
|
||||
Assert.True(CanDelete(admin, file));
|
||||
Assert.True(CanDelete(owner, file));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,6 +109,13 @@ public class WebClientStaticTests : IAsyncLifetime
|
|||
Assert.Contains("id=\"navTabInbox\"", html);
|
||||
Assert.Contains("id=\"inboxUnreadBadge\"", html);
|
||||
Assert.Contains("id=\"toggleWebPushBtn\"", html);
|
||||
Assert.Contains("id=\"navTabInbox\"", html);
|
||||
Assert.Contains("id=\"inboxUnreadBadge\"", html);
|
||||
Assert.Contains("id=\"toggleWebPushBtn\"", html);
|
||||
Assert.Contains("id=\"desktopNotifBanner\"", html);
|
||||
Assert.Contains("id=\"bannerEnablePushBtn\"", html);
|
||||
Assert.Contains("id=\"headerPushBtn\"", html);
|
||||
Assert.Contains("rel=\"icon\"", html);
|
||||
Assert.Contains("id=\"voiceCallBtn\"", html);
|
||||
Assert.Contains("id=\"voiceCallBar\"", html);
|
||||
Assert.Contains("id=\"incomingCallModal\"", html);
|
||||
|
|
@ -128,6 +135,7 @@ public class WebClientStaticTests : IAsyncLifetime
|
|||
Assert.Contains(".conv-security-badge", css);
|
||||
Assert.Contains(".inbox-item", css);
|
||||
Assert.Contains(".inbox-badge", css);
|
||||
Assert.Contains(".notif-banner", css);
|
||||
Assert.Contains(".voice-call-bar", css);
|
||||
Assert.Contains(".voice-participant-chip", css);
|
||||
Assert.Contains(".incoming-call-card", css);
|
||||
|
|
@ -152,6 +160,10 @@ public class WebClientStaticTests : IAsyncLifetime
|
|||
Assert.Contains("INBOX_SYNC", js);
|
||||
Assert.Contains("INBOX_NEW_ITEM", js);
|
||||
Assert.Contains("subscribeWebPush", js);
|
||||
Assert.Contains("updateDesktopNotifBanner", js);
|
||||
Assert.Contains("initServiceWorkerAndPush", js);
|
||||
Assert.Contains("markActiveChannelInboxRead", js);
|
||||
Assert.Contains("deleteFile", js);
|
||||
Assert.Contains("VOICE_CALL_START", js);
|
||||
Assert.Contains("VOICE_CALL_STATE", js);
|
||||
Assert.Contains("VOICE_SIGNAL", js);
|
||||
|
|
@ -170,10 +182,26 @@ public class WebClientStaticTests : IAsyncLifetime
|
|||
string swJs = await swRes.Content.ReadAsStringAsync();
|
||||
Assert.Contains("showNotification", swJs);
|
||||
Assert.Contains("notificationclick", swJs);
|
||||
Assert.Contains("/icon.svg", swJs);
|
||||
Assert.Contains("renotify: true", swJs);
|
||||
|
||||
var manifestRes = await _httpClient.GetAsync("/manifest.json");
|
||||
Assert.True(manifestRes.IsSuccessStatusCode);
|
||||
string manifestJson = await manifestRes.Content.ReadAsStringAsync();
|
||||
Assert.Contains("GsDChat", manifestJson);
|
||||
Assert.Contains("icons", manifestJson);
|
||||
Assert.Contains("/icon.svg", manifestJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetIconSvg_ShouldServeValidSvg()
|
||||
{
|
||||
Assert.NotNull(_httpClient);
|
||||
|
||||
var iconRes = await _httpClient.GetAsync("/icon.svg");
|
||||
Assert.True(iconRes.IsSuccessStatusCode);
|
||||
string svg = await iconRes.Content.ReadAsStringAsync();
|
||||
Assert.Contains("<svg", svg);
|
||||
Assert.Contains("GsDChat", svg);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue