public async Task GetReturnsNotFoundWhenQueryReturnsNullTest() { var account = Model.Create <Account>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var query = Substitute.For <IExportQuery>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { using (var target = new ExportController(query)) { target.ControllerContext = controllerContext; var actual = await target.Get(tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <ErrorMessageResult>().Which.StatusCode.Should().Be((int)HttpStatusCode.NotFound); } } }
public async Task GetReturnsProfileForSpecifiedIdTest() { var account = Model.Create <Account>(); var profile = Model.Create <ExportProfile>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var query = Substitute.For <IExportQuery>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { query.GetExportProfile(account.Id, tokenSource.Token).Returns(profile); using (var target = new ExportController(query)) { target.ControllerContext = controllerContext; var actual = await target.Get(tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <OkObjectResult>(); var result = actual.As <OkObjectResult>(); result.Value.Should().BeEquivalentTo(profile); } } }
public async Task GetReturnsNotFoundAfterAllRetryAttemptsFailTest() { var account = Model.Create <Account>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var profileQuery = Substitute.For <IProfileQuery>(); var profileCommand = Substitute.For <IProfileCommand>(); var accountCommand = Substitute.For <IAccountCommand>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { using (var target = new AccountProfileController(profileQuery, profileCommand, accountCommand)) { target.ControllerContext = controllerContext; var actual = await target.Get(tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <ErrorMessageResult>().Which.StatusCode.Should().Be((int)HttpStatusCode.NotFound); await profileQuery.Received(4).GetProfile(account.Id, tokenSource.Token).ConfigureAwait(false); } } }
public async Task DeleteReturnsNoContentTest() { var account = Model.Create <Account>(); var profile = Model.Create <Profile>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var profileQuery = Substitute.For <IProfileQuery>(); var profileCommand = Substitute.For <IProfileCommand>(); var accountCommand = Substitute.For <IAccountCommand>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { using (var target = new AccountProfileController(profileQuery, profileCommand, accountCommand)) { target.ControllerContext = controllerContext; var actual = await target.Delete(tokenSource.Token).ConfigureAwait(false); await accountCommand.Received().DeleteAccount(user.Identity.Name, account.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <NoContentResult>(); } } }
public async Task PutProvidesProfileToManagerTest() { var account = Model.Create <Account>(); var expected = Model.Create <UpdatableProfile>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var profileQuery = Substitute.For <IProfileQuery>(); var profileCommand = Substitute.For <IProfileCommand>(); var accountCommand = Substitute.For <IAccountCommand>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { using (var target = new AccountProfileController(profileQuery, profileCommand, accountCommand)) { target.ControllerContext = controllerContext; var actual = await target.Put(expected, tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <NoContentResult>(); await profileCommand.Received().UpdateProfile(account.Id, expected, tokenSource.Token) .ConfigureAwait(false); } } }
public async Task PostCreatesNewPhotoTest() { var account = Model.Create <Account>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var photoDetails = Model.Create <PhotoDetails>(); var command = Substitute.For <IPhotoCommand>(); var model = Substitute.For <IFormFile>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var data = new MemoryStream()) { using (var tokenSource = new CancellationTokenSource()) { model.OpenReadStream().Returns(data); using (var sut = new PhotosController(command)) { sut.ControllerContext = controllerContext; command.CreatePhoto( Arg.Is <Photo>(x => x.ContentType == model.ContentType && x.ProfileId == account.Id && x.Data == data), tokenSource.Token) .Returns(photoDetails); var actual = await sut.Post(model, tokenSource.Token).ConfigureAwait(false); var result = actual.Should().BeOfType <CreatedAtRouteResult>().Which; result.RouteName.Should().Be("ProfilePhoto"); result.RouteValues["profileId"].Should().Be(photoDetails.ProfileId); result.RouteValues["photoId"].Should().Be(photoDetails.Id); var value = result.Value.Should().BeOfType <PhotoDetails>().Which; value.Should().BeEquivalentTo(photoDetails); } } } }
public async Task GetAttemptsToGetProfileMultipleTimesTest() { var account = Model.Create <Account>(); var profile = Model.Create <Profile>(); var user = ClaimsIdentityFactory.BuildPrincipal(account); var profileQuery = Substitute.For <IProfileQuery>(); var profileCommand = Substitute.For <IProfileCommand>(); var accountCommand = Substitute.For <IAccountCommand>(); var httpContext = Substitute.For <HttpContext>(); httpContext.User = user; var routerData = new RouteData(); var actionDescriptor = new ControllerActionDescriptor(); var actionContext = new ActionContext(httpContext, routerData, actionDescriptor); var controllerContext = new ControllerContext(actionContext); using (var tokenSource = new CancellationTokenSource()) { profileQuery.GetProfile(account.Id, tokenSource.Token).Returns(null, null, null, profile); using (var target = new AccountProfileController(profileQuery, profileCommand, accountCommand)) { target.ControllerContext = controllerContext; var actual = await target.Get(tokenSource.Token).ConfigureAwait(false); actual.Should().BeOfType <OkObjectResult>(); var result = actual.As <OkObjectResult>(); result.Value.Should().BeEquivalentTo(profile); await profileQuery.Received(4).GetProfile(account.Id, tokenSource.Token).ConfigureAwait(false); } } }