public async Task WithMissingUserIdReturnsBadRequest() { // arrange string userId; var fakeRepository = new FakeBlobRepository(); Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService); IActionResult actionResult = new BadRequestObjectResult(new { error = "Error." }); mockUserAuth.Setup(m => m.GetUserIdAsync(It.IsAny <HttpRequest>(), out userId, out actionResult)) .Returns(Task.FromResult(false)); // act var response = await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var objectResult = (BadRequestObjectResult)response; var addResponse = (dynamic)objectResult.Value; // assert Assert.AreEqual("Error.", addResponse.error); }
public async Task WithSuccessPublishesAudioTranscriptUpdatedEventToEventGrid() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockEventGridAudioCreatedRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService, out Mock <IEventGridSubscriberService> mockEventGridSubscriberService, out Mock <IAudioTranscriptionService> mockAudioTranscriptionService); MockEventGridSubscriberServiceDeconstructResponse(mockEventGridSubscriberService); mockAudioTranscriptionService .Setup(s => s.GetAudioTranscriptFromCognitiveServicesAsync(It.IsAny <System.IO.Stream>())) .ReturnsAsync("my transcript"); // act var response = await sut.UpdateTranscript( mockRequest.Object, mockLogger.Object).ConfigureAwait(false); var objectResult = (OkResult)response; // assert mockEventGridPublisherService.Verify( m => m.PostEventGridEventAsync( AudioEvents.AudioTranscriptUpdated, $"{Mockers.DefaultUserId}/{Mockers.DefaultId}", It.Is <AudioTranscriptUpdatedEventData>(d => d.TranscriptPreview == "my transcript")), Times.Once); }
public async Task WithSuccessUpdatesBlobTranscriptInMetadata() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockEventGridAudioCreatedRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService, out Mock <IEventGridSubscriberService> mockEventGridSubscriberService, out Mock <IAudioTranscriptionService> mockAudioTranscriptionService); MockEventGridSubscriberServiceDeconstructResponse(mockEventGridSubscriberService); mockAudioTranscriptionService .Setup(s => s.GetAudioTranscriptFromCognitiveServicesAsync(It.IsAny <System.IO.Stream>())) .ReturnsAsync("my transcript"); // act var response = await sut.UpdateTranscript( mockRequest.Object, mockLogger.Object).ConfigureAwait(false); var objectResult = (OkResult)response; // assert Assert.IsNotNull(objectResult); Assert.AreEqual("my transcript", fakeBlobRepo.Blobs[0].Properties[Mockers.TranscriptMetadataName]); }
public async Task WithSubscriptionValidationEventReturnsActionResult() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockEventGridAudioCreatedRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService, out Mock <IEventGridSubscriberService> mockEventGridSubscriberService, out Mock <IAudioTranscriptionService> mockAudioTranscriptionService); MockEventGridSubscriberServiceDeconstructResponse(mockEventGridSubscriberService); mockEventGridSubscriberService .Setup(s => s.HandleSubscriptionValidationEvent(It.IsAny <string>(), It.IsAny <StringValues>())) .Returns(new OkResult()); // act var response = await sut.UpdateTranscript( mockRequest.Object, mockLogger.Object).ConfigureAwait(false); var objectResult = (OkResult)response; // assert Assert.IsNotNull(objectResult); }
public async Task WithNoTranscriptReturnsNotFound() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockEventGridAudioCreatedRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService, out Mock <IEventGridSubscriberService> mockEventGridSubscriberService, out Mock <IAudioTranscriptionService> mockAudioTranscriptionService); MockEventGridSubscriberServiceDeconstructResponse(mockEventGridSubscriberService); mockAudioTranscriptionService .Setup(s => s.GetAudioTranscriptFromCognitiveServicesAsync(It.IsAny <System.IO.Stream>())) .ReturnsAsync((string)null); // act var response = await sut.UpdateTranscript( mockRequest.Object, mockLogger.Object).ConfigureAwait(false); var objectResult = (NotFoundResult)response; // assert Assert.IsNotNull(objectResult); }
public async Task WithSuccessUpdatesBlobMetadata() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded(out FakeBlobRepository fakeBlobRepo); // act await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); // assert Assert.AreEqual(Mockers.DefaultCategoryName, fakeBlobRepo.Blobs.Single().Properties[Mockers.CategoryIdMetadataName]); Assert.AreEqual(Mockers.DefaultUserId, fakeBlobRepo.Blobs.Single().Properties[Mockers.UserIdMetadataName]); }
public async Task WithSuccessReturnsNoContentResult() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded(out FakeBlobRepository fakeBlobRepo); // act var response = await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var objectResult = (NoContentResult)response; // assert Assert.IsNotNull(objectResult); }
public async Task WithSuccessDeletesBlob() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = Mockers.MockRequest(null); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded(); // act var response = await sut.Delete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var responseType = (NoContentResult)response; // assert Assert.IsNotNull(responseType); }
public async Task WithAudioFileAlreadyCreatedReturnsAudioAlreadyCreated() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out FakeBlobRepository fakeBlobRepo); fakeBlobRepo.Blobs[0].Properties[Mockers.CategoryIdMetadataName] = Mockers.DefaultCategoryName; // act var response = await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var responseType = (BadRequestObjectResult)response; // assert Assert.IsNotNull(responseType); Assert.AreEqual("Image has already been created.", ((dynamic)responseType.Value).error); }
public async Task WithSuccessPublishesAudioCreatedEventToEventGrid() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService); // act await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); // assert mockEventGridPublisherService.Verify( m => m.PostEventGridEventAsync( AudioEvents.AudioCreated, $"{Mockers.DefaultUserId}/{Mockers.DefaultId}", It.Is <AudioCreatedEventData>(d => d.Category == Mockers.DefaultCategoryName)), Times.Once); }
public async Task WithIncorrectUserIdReturnsAudioNotUploaded() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out FakeBlobRepository fakeBlobRepo); fakeBlobRepo.Blobs.Clear(); fakeBlobRepo.AddFakeBlob(Mockers.AudioContainerName, $"invaliduserid/{Mockers.DefaultId}"); // act var response = await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var responseType = (BadRequestObjectResult)response; // assert Assert.IsNotNull(responseType); Assert.AreEqual("Audio has not yet been uploaded.", ((dynamic)responseType.Value).error); }
public async Task WithMissingAudioFileReturnsAudioNotUploaded() { // arrange Mock <ILogger> mockLogger = new Mock <ILogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out FakeBlobRepository fakeBlobRepo); fakeBlobRepo.Blobs.Clear(); // act var response = await sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId).ConfigureAwait(false); var responseType = (BadRequestObjectResult)response; dynamic responseObject = responseType.Value; // assert Assert.IsNotNull(responseType); Assert.AreEqual("Audio has not yet been uploaded.", responseObject.error); }
public async Task WithThrownExceptionThrowsException() { // arrange string userId; var fakeRepository = new FakeBlobRepository(); Mock <AbstractLogger> mockLogger = new Mock <AbstractLogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventGridPublisherService); IActionResult actionResult = new BadRequestObjectResult(new { error = "Error." }); System.Exception ex = new System.Exception("My error."); mockUserAuth.Setup(m => m.GetUserIdAsync(It.IsAny <HttpRequest>(), out userId, out actionResult)) .ThrowsAsync(ex); // act await Assert.ThrowsExceptionAsync <System.Exception>(() => sut.AddComplete(mockRequest.Object, mockLogger.Object, Mockers.DefaultId)).ConfigureAwait(false); mockLogger.Verify(moc => moc.Log(LogLevel.Error, It.IsAny <System.Exception>(), "Unhandled Exception.")); }
public async Task WithThrownExceptionThrowsException() { // arrange var fakeRepository = new FakeBlobRepository(); Mock <AbstractLogger> mockLogger = new Mock <AbstractLogger>(); var mockRequest = AudioMockers.GetMockAddCompleteRequest(); var sut = AudioMockers.GetApiFunctionsWithBlobUploaded( out Mock <IUserAuthenticationService> mockUserAuth, out FakeBlobRepository fakeBlobRepo, out Mock <IEventGridPublisherService> mockEventPub, out Mock <IEventGridSubscriberService> mockEventSub, out Mock <IAudioTranscriptionService> mockAudioTranscriptService); var ex = new Exception("My error."); mockRequest.Setup(m => m.Headers) .Throws(ex); // act await Assert.ThrowsExceptionAsync <System.Exception>(() => sut.UpdateTranscript(mockRequest.Object, mockLogger.Object)).ConfigureAwait(false); mockLogger.Verify(moc => moc.Log(LogLevel.Error, It.IsAny <System.Exception>(), "Unhandled Exception.")); }