Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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]);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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]);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
        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."));
        }
Ejemplo n.º 14
0
        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."));
        }