public void ReturnsForbiddenIfSpecifiedUserIsNotReviewAuthor() { // Arrange var iters = CreateService(); var iter = new Iteration() { Review = new Review() { UserId = 42 } }; iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); // Act Assert.Equal(DatabaseActionOutcome.Forbidden, iters.AddDiffToIteration(iter.Id, "diff", 24).Outcome); }
public void AddsFilesInDiffToIterationIfSuccessful() { // Arrange var iters = CreateService(); var iter = new Iteration() { Review = new Review() { UserId = 42 }, Files = new List<FileChange>() }; iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); var expectedChanges = new List<FileChange>() { new FileAddition() }; iters.MockDiff.Setup(d => d.CreateFromGitDiff(It.Is<TextReader>(tr => tr.ReadToEnd() == "thediff"))) .Returns(expectedChanges) .Verifiable(); // Act var result = iters.AddDiffToIteration(iter.Id, "thediff", 42); // Assert Assert.Equal(DatabaseActionOutcome.Success, result); Assert.Equal(1, iter.Files.Count); Assert.Same(iter.Files.Single(), expectedChanges.Single()); iters.MockDiff.Verify(); }
public void ReturnsIterationMatchingId() { // Arrange var iters = CreateService(); var iter = new Iteration(); iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); // Act/Assert Assert.Same(iter, iters.GetIteration(iter.Id)); }
public void ReturnsSuccessAndRemovesIterationIfUserCorrectAndIterationExists() { // Arrange var iters = CreateService(); var iter1 = new Iteration() { Review = new Review() { UserId = 42 } }; var iter2 = new Iteration() { Review = new Review() { UserId = 42 } }; iters.MockData.Iterations.Add(iter1); iters.MockData.Iterations.Add(iter2); iters.MockData.SaveChanges(); // Act var result = iters.DeleteIteration(iter1.Id, 42); // Assert Assert.Equal(DatabaseActionOutcome.Success, result); Assert.DoesNotContain(iter1, iters.MockData.Iterations); Assert.Contains(iter2, iters.MockData.Iterations); }
public virtual DatabaseActionResult<Iteration> AddIteration(int reviewId, int currentUserId) { Requires.InRange(reviewId >= 0, "reviewId"); Requires.InRange(currentUserId >= 0, "currentUserId"); Review review = Data.Reviews .Include("Iterations") .Where(r => r.Id == reviewId) .FirstOrDefault(); if (review == null) { return DatabaseActionResult<Iteration>.NotFound(); } else if (review.UserId != currentUserId) { return DatabaseActionResult<Iteration>.Forbidden(); } Iteration i = new Iteration() { Published = false, StartedOn = DateTime.UtcNow, }; review.Iterations.Add(i); Data.SaveChanges(); return DatabaseActionResult<Iteration>.Success(i); }
public void Returns204AndRemovesIterationIfIterationExistsAndCurrentUserIsOwner() { // Arrange var c = CreateController(); var iter = new Iteration() { Description = "blorg", Review = new Review() { UserId = ApiTestData.LoggedInUser.Id } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Delete(iter.Id); // Assert Assert.Equal(HttpStatusCode.NoContent, result.StatusCode); Assert.False(c.MockData.Iterations.Any()); }
public void Returns404IfNoIterationWithSpecifiedId() { // Arrange var c = CreateController(); var iter = new Iteration() { Description = "blorg" }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Delete(iter.Id + 42); // Assert Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); Assert.Equal("blorg", c.MockData.Iterations.Single().Description); }
public void Returns200WithModifiedIterationAndModifiesIterationIfExistsAndUserIsOwner() { // Arrange var c = CreateController(); var iter = new Iteration() { Published = false, Review = new Review() { UserId = ApiTestData.LoggedInUser.Id } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.PutPublished(iter.Id, published: true); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.True(c.MockData.Iterations.Single().Published); }
public void Returns403IfCurrentUserNotOwnerOfSpecifiedReview() { // Arrange var c = CreateController(); var iter = new Iteration() { Files = new List<FileChange>(), Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.PutDiff(iter.Id, "glarb"); // Assert Assert.Equal(HttpStatusCode.Forbidden, result.StatusCode); Assert.False(c.MockData.Iterations.Single().Files.Any()); }
public void Returns404IfNoIterationWithSpecifiedId() { // Arrange var c = CreateController(); var iter = new Iteration() { Files = new List<FileChange>() }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.PutDiff(iter.Id + 42, "glarb"); // Assert Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); Assert.False(c.MockData.Iterations.Single().Files.Any()); }
public void ReturnsOkIfUserNotCreatorAndIterationPublished() { // Arrange var c = CreateController(); var iter = new Iteration() { Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id }, Published = true, Files = new List<FileChange>() }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Get(iter.Id); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
public void ReturnsForbiddenIfUserNotCreatorAndIterationNotPublished() { // Arrange var c = CreateController(); var iter = new Iteration() { Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id }, Published = false }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Get(iter.Id); // Assert Assert.Equal(HttpStatusCode.Forbidden, result.StatusCode); Assert.Null(result.Content); }
public void ReturnsFilesGroupedByFolder() { // Arrange var c = CreateController(); var iter = new Iteration() { Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id }, Published = true, Files = new List<FileChange>() { new FileAddition() { Id = 1, FileName = "Foo/Bar/Baz" }, new FileRemoval() { Id = 2, FileName = "Foo/Bar/Boz" }, new FileAddition() { Id = 3, FileName = "Biz/Qux" }, new FileRemoval() { Id = 4, FileName = "Foo/Tux" }, new FileAddition() { Id = 5, FileName = "Box" } } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Get(iter.Id); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal(new[] { new FolderModel { Name = "Foo/Bar", Files = new [] { new FileModel { Id = 1, FileName = "Baz", FullPath = "Foo/Bar/Baz", ChangeType = FileChangeType.Added }, new FileModel { Id = 2, FileName = "Boz", FullPath = "Foo/Bar/Boz", ChangeType = FileChangeType.Removed } } }, new FolderModel { Name = "Biz", Files = new [] { new FileModel { Id = 3, FileName = "Qux", FullPath = "Biz/Qux", ChangeType = FileChangeType.Added }, } }, new FolderModel { Name = "Foo", Files = new [] { new FileModel { Id = 4, FileName = "Tux", FullPath = "Foo/Tux", ChangeType = FileChangeType.Removed }, } }, new FolderModel { Name = "/", Files = new [] { new FileModel { Id = 5, FileName = "Box", FullPath = "Box", ChangeType = FileChangeType.Added }, } }, }, result.GetObjectContent(), new PropertyEqualityComparer(typeEquality: false)); }
public void ReturnsNullIfNoIterationWithId() { // Arrange var iters = CreateService(); var iter = new Iteration(); iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); // Act/Assert Assert.Null(iters.GetIteration(iter.Id + 42)); }
public void Returns403IfCurrentUserNotOwnerOfSpecifiedReview() { // Arrange var c = CreateController(); var iter = new Iteration() { Published = false, Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.PutPublished(iter.Id, published: true); // Assert Assert.Equal(HttpStatusCode.Forbidden, result.StatusCode); Assert.False(c.MockData.Iterations.Single().Published); }
public void SetsIterationToSpecifiedPublishStateIfSuccessful() { // Arrange var iters = CreateService(); var iter = new Iteration() { Review = new Review() { UserId = 42 } }; iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); // Act var result = iters.SetIterationPublished(iter.Id, true, 42); // Assert Assert.Equal(DatabaseActionOutcome.Success, result.Outcome); Assert.Same(iter, result.Object); Assert.True(iter.Published); }
public void Returns404IfNoIterationWithSpecifiedId() { // Arrange var c = CreateController(); var iter = new Iteration() { Description = "blorg", Published = false }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.PutPublished(iter.Id + 42, published: true); // Assert Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); Assert.False(c.MockData.Iterations.Single().Published); }
public void ReturnsNotFoundIfNoIterationWithSpecifiedId() { // Arrange var iters = CreateService(); var iter = new Iteration(); iters.MockData.Iterations.Add(iter); iters.MockData.SaveChanges(); // Act Assert.Equal(DatabaseActionOutcome.ObjectNotFound, iters.AddDiffToIteration(iter.Id + 42, "diff", 42).Outcome); }
public void Returns403IfCurrentUserNotOwnerOfSpecifiedReview() { // Arrange var c = CreateController(); var iter = new Iteration() { Description = "blorg", Review = new Review() { UserId = ApiTestData.NotLoggedInUser.Id } }; c.MockData.Iterations.Add(iter); c.MockData.SaveChanges(); // Act var result = c.Delete(iter.Id); // Assert Assert.Equal(HttpStatusCode.Forbidden, result.StatusCode); Assert.Equal("blorg", c.MockData.Iterations.Single().Description); }