Merge branch 'delete_uploaded_files_feature' into master
This commit is contained in:
commit
12bb2f9843
16 changed files with 587 additions and 8 deletions
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue