예제 #1
0
            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);
            }
예제 #2
0
            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();
            }
예제 #3
0
            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));
            }
예제 #4
0
            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);
            }
예제 #5
0
        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);
        }
예제 #6
0
            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());
            }
예제 #7
0
            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);
            }
예제 #8
0
            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);
            }
예제 #9
0
            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());
            }
예제 #10
0
            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());
            }
예제 #11
0
            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);
            }
예제 #12
0
            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);
            }
예제 #13
0
            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));
            }
예제 #14
0
            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));
            }
예제 #15
0
            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);
            }
예제 #16
0
            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);
            }
예제 #17
0
            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);
            }
예제 #18
0
            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);
            }
예제 #19
0
            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);
            }