public async Task Handle_ByToken_TokenFound_ShouldReturnAttachmentSuccessfully() { // Arrange var attachment = await _wolkDbContext.CreateAndSaveAttachment(); var expirationDateTime = DateTimeOffset.Now.AddSeconds(5); var accessToken = await _wolkDbContext.CreateAndSaveAttachmentAccessToken(attachment, expirationDateTime); var request = new GetAttachmentQuery { Token = accessToken.Token }; var filePath = Path.Combine(_configuration.UploadsPath, attachment.InternalFilename); _mockFileService .Setup(m => m.FileExists(filePath)) .Returns(true); var fileContents = new byte[] { 1, 2, 3, 4 }; _mockFileService .Setup(m => m.ReadAllBytes(filePath)) .Returns(fileContents); // Act var result = await _handler.Handle(request, CancellationToken.None); // Assert ShouldBeEqual(attachment, result); Assert.AreEqual(fileContents, result.Contents); }
public async Task Handle_ById_AttachmentFound_FileFound_ShouldReturnAttachmentCorrectly() { // Arrange var attachment = await _wolkDbContext.CreateAndSaveAttachment(); var request = new GetAttachmentQuery { AttachmentId = attachment.Id }; var filePath = Path.Combine(_configuration.UploadsPath, attachment.InternalFilename); _mockFileService .Setup(m => m.FileExists(filePath)) .Returns(true); var fileContents = new byte[] { 1, 2, 3, 4 }; _mockFileService .Setup(m => m.ReadAllBytes(filePath)) .Returns(fileContents); // Act var result = await _handler.Handle(request, CancellationToken.None); // Assert ShouldBeEqual(attachment, result); Assert.AreEqual(fileContents, result.Contents); }
public async Task <ActionResult> GetAttachmentByAccessToken([FromRoute] string token) { var query = new GetAttachmentQuery { Token = token }; var attachment = await Mediator.Send(query); return(File(attachment.Contents, attachment.MimeType, attachment.Filename)); }
public async Task <ActionResult> GetAttachment([FromRoute] long attachmentId) { var query = new GetAttachmentQuery { AttachmentId = attachmentId }; var attachment = await Mediator.Send(query); return(File(attachment.Contents, attachment.MimeType, attachment.Filename)); }
public async Task Handle_NoIdentifierSet_ShouldThrowInvalidOperationException() { // Arrange var request = new GetAttachmentQuery { Token = null, AttachmentId = null }; // Act / Assert await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => _handler.Handle(request, CancellationToken.None)); }
public async Task Handle_ById_AttachmentNotFound_ShouldThrowNotFoundException() { // Arrange var attachment = await _wolkDbContext.CreateAndSaveAttachment(); var request = new GetAttachmentQuery { AttachmentId = attachment.Id + 1 }; // Act var exception = await Assert.ThrowsExceptionAsync <NotFoundException>(() => _handler.Handle(request, CancellationToken.None)); // Assert Assert.IsTrue(exception.Message.Contains(nameof(Attachment))); }
public async Task <PaginationDto <AttachmentDto> > HandleAsync(GetAttachmentQuery query) { var queryable = _context.Attachments .Where(x => x.Note.UserId == query.RequestBy) .Where(query); var count = await queryable.CountAsync(); var result = await queryable.TransformBy(query).ToListAsync(); var pagination = new PaginationDto <Attachment> { Data = result, TotalCount = count }; return(_mapper.Map <PaginationDto <AttachmentDto> >(pagination)); }
public async Task Handle_ByToken_TokenFoundButIdentifierNotValid_ShouldThrowNotFoundException() { // Arrange var expirationDateTime = DateTimeOffset.Now.AddSeconds(5); var accessToken = await _wolkDbContext.CreateAndSaveAttachmentAccessToken("ID1", expirationDateTime); var request = new GetAttachmentQuery { Token = accessToken.Token }; // Act var exception = await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => _handler.Handle(request, CancellationToken.None)); // Assert Assert.IsTrue(exception.Message.Contains("is not a valid number")); }
private async Task FetchAllAttachmentsAsync(Domain.Synchronization.Synchronization synchronization, NoteMeContext context, CancellationToken cts) { cts.ThrowIfCancellationRequested(); var syncDate = synchronization.LastSynchronization; var hasMore = false; do { var filterBy = $@"{nameof(Attachment.CreatedAt)} > ""{syncDate}"""; var orderBy = $"{nameof(Attachment.CreatedAt)}-desc"; var query = new GetAttachmentQuery() .SetNormalWhere(filterBy) .SetNormalOrderBy(orderBy); var items = await _apiWebService.SendAsync <PaginationDto <AttachmentDto> >(HttpMethod.Get, Endpoints.Attachments._ + query.ToUri()); if (!items.Data.Any()) { return; } hasMore = items.Data.Count == query.PageSize; foreach (var itemDto in items.Data) { var item = await context.Attachments.FirstOrDefaultAsync(x => x.Id == itemDto.Id, cts); if (item != null) { continue; } item = _mapper.MapTo <Attachment>(itemDto); item.StatusSynchronization = SynchronizationStatusEnum.Ok; await context.AddRangeAsync(item); } } while (hasMore); await context.SaveChangesAsync(cts); }
public async Task Handle_ById_AttachmentFound_FileNotFound_ShouldThrowInvalidOperationException() { // Arrange var attachment = await _wolkDbContext.CreateAndSaveAttachment(); var request = new GetAttachmentQuery { AttachmentId = attachment.Id }; var filePath = Path.Combine(_configuration.UploadsPath, attachment.InternalFilename); _mockFileService .Setup(m => m.FileExists(filePath)) .Returns(false); // Act / Assert await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => _handler.Handle(request, CancellationToken.None)); }
public async Task Handle_ByToken_TokenFoundButExpired_ShouldThrowNotFoundException() { // Arrange var attachment = await _wolkDbContext.CreateAndSaveAttachment(); var expirationDateTime = DateTimeOffset.Now.AddSeconds(-1); var accessToken = await _wolkDbContext.CreateAndSaveAttachmentAccessToken(attachment, expirationDateTime); var request = new GetAttachmentQuery { Token = accessToken.Token }; // Act var exception = await Assert.ThrowsExceptionAsync <NotFoundException>(() => _handler.Handle(request, CancellationToken.None)); // Assert Assert.AreEqual("The access token has expired.", exception.Message); }
public async Task <IActionResult> GetAsync(GetAttachmentQuery getAttachmentQuery) { var results = await DispatchQueryAsync <GetAttachmentQuery, PaginationDto <AttachmentDto> >(getAttachmentQuery); return(Ok(results)); }