Skip to content

Commit

Permalink
tests fixed + removed race condition in file download
Browse files Browse the repository at this point in the history
  • Loading branch information
joao4all committed Apr 17, 2024
1 parent 6e6ef7d commit 0014073
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,18 @@ public void VerifyUpdateThings()
var clone = element.Clone(false);
clone.Name = "Satellite";
thingsToUpdate.Add(clone);
Assert.DoesNotThrowAsync(async() => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate));
Assert.That(async () => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate), Throws.Nothing);

var filesToUpload = new List<string> { this.uri.LocalPath };

Assert.Multiple(() =>
{
Assert.DoesNotThrowAsync(async () => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate, filesToUpload));
Assert.That(async () => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate, filesToUpload), Throws.Nothing);
this.session.Verify(x => x.Write(It.IsAny<OperationContainer>(), It.IsAny<IEnumerable<string>>()), Times.Once);
});

this.session.Setup(x => x.Write(It.IsAny<OperationContainer>(), It.IsAny<IEnumerable<string>>())).Throws(new DalWriteException());
Assert.DoesNotThrowAsync(async () => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate, filesToUpload));
Assert.That(async () => await this.sessionService.UpdateThings(this.iteration, thingsToUpdate, filesToUpload), Throws.Nothing);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ public void SetUp()
[Test]
public async Task VerifyFileDownloadUtility()
{
Assert.ThrowsAsync<ArgumentNullException>(async () => await this.service.DownloadFileFromStreamAsync(null, null));
Assert.ThrowsAsync<ArgumentNullException>(async () => await this.service.DownloadFileFromStreamAsync(new MemoryStream(), null));

Assert.Multiple(() =>
{
Assert.That(async () => await this.service.DownloadFileFromStreamAsync(null, null), Throws.ArgumentNullException);
Assert.That(async () => await this.service.DownloadFileFromStreamAsync(new MemoryStream(), null), Throws.ArgumentNullException);
});

await this.service.DownloadFileFromStreamAsync(new MemoryStream(), "fileTest");
Assert.That(this.jsRuntimeMock.Invocations.Count, Is.EqualTo(1));
}
Expand Down
3 changes: 1 addition & 2 deletions COMETwebapp/Extensions/FolderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public static class FolderExtensions
/// <returns>The path</returns>
public static string GetFolderPath(this Folder folder)
{
var path = $"{folder.Path}/{folder.Name}/";
return path;
return Path.Combine(folder.Path, folder.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public void InitializeViewModel(File file, FileStore fileStore)
this.CurrentFileStore = fileStore;
this.FileTypes = this.SessionService.GetSiteDirectory().AvailableReferenceDataLibraries().SelectMany(x => x.FileType);
this.ErrorMessage = string.Empty;

this.UploadsDirectory = $"wwwroot/uploads/{Guid.NewGuid()}";
this.UploadsDirectory = Path.Combine("wwwroot", "uploads", Guid.NewGuid().ToString());

if (!Directory.Exists(this.UploadsDirectory))
{
Expand Down
24 changes: 11 additions & 13 deletions COMETwebapp/wwwroot/Scripts/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
* @param {string} fileName
* @param {any} contentStreamReference
*/
function DownloadFileFromStream(fileName, contentStreamReference) {
(async () => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: 'text/plain;charset=utf-8' });
async function DownloadFileFromStream(fileName, contentStreamReference) {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: 'text/plain;charset=utf-8' });

const url = URL.createObjectURL(blob);
const url = URL.createObjectURL(blob);

const anchorElement = document.createElement('a');
const anchorElement = document.createElement('a');

anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.target = "_blank";
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
})();
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.target = "_blank";
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}

0 comments on commit 0014073

Please sign in to comment.