Open platform to collect and prioritize feedback
Check parent post when fetching a single comment
Matt Robertse91a560fbca9
diff --git a/app/actions/post.go b/app/actions/post.goindex 3d36d39a4..41b3b51a7 100644--- a/app/actions/post.go+++ b/app/actions/post.go@@ -335,8 +335,12 @@ type EditComment struct {// IsAuthorized returns true if current user is authorized to perform this actionfunc (action *EditComment) IsAuthorized(ctx context.Context, user *entity.User) bool {postByNumber := &query.GetPostByNumber{Number: action.PostNumber}- commentByID := &query.GetCommentByID{CommentID: action.ID}- if err := bus.Dispatch(ctx, postByNumber, commentByID); err != nil {+ if err := bus.Dispatch(ctx, postByNumber); err != nil {+ return false+ }++ commentByID := &query.GetCommentByID{CommentID: action.ID, PostID: postByNumber.Result.ID}+ if err := bus.Dispatch(ctx, commentByID); err != nil {return false}@@ -396,7 +400,12 @@ type DeleteComment struct {// IsAuthorized returns true if current user is authorized to perform this actionfunc (action *DeleteComment) IsAuthorized(ctx context.Context, user *entity.User) bool {- commentByID := &query.GetCommentByID{CommentID: action.CommentID}+ postByNumber := &query.GetPostByNumber{Number: action.PostNumber}+ if err := bus.Dispatch(ctx, postByNumber); err != nil {+ return false+ }++ commentByID := &query.GetCommentByID{CommentID: action.CommentID, PostID: postByNumber.Result.ID}if err := bus.Dispatch(ctx, commentByID); err != nil {return false}diff --git a/app/actions/post_test.go b/app/actions/post_test.goindex a131e8485..436c8c10c 100644--- a/app/actions/post_test.go+++ b/app/actions/post_test.go@@ -117,8 +117,18 @@ func TestDeleteComment(t *testing.T) {Content: "Comment #1",}+ post := &entity.Post{ID: 1, Number: 1}+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ if q.Number == post.Number {+ q.Result = post+ return nil+ }+ q.Result = &entity.Post{ID: 2, Number: q.Number}+ return nil+ })+bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {- if q.CommentID == comment.ID {+ if q.CommentID == comment.ID && q.PostID == post.ID {q.Result = commentreturn nil}@@ -126,7 +136,8 @@ func TestDeleteComment(t *testing.T) {})action := &actions.DeleteComment{- CommentID: comment.ID,+ PostNumber: post.Number,+ CommentID: comment.ID,}authorized := action.IsAuthorized(context.Background(), notAuthor)@@ -137,6 +148,11 @@ func TestDeleteComment(t *testing.T) {authorized = action.IsAuthorized(context.Background(), administrator)Expect(authorized).IsTrue()++ // Comment requested through a different post+ action.PostNumber = 2+ Expect(action.IsAuthorized(context.Background(), author)).IsFalse()+ Expect(action.IsAuthorized(context.Background(), administrator)).IsFalse()}func TestAddNewComment_TooLongContent(t *testing.T) {diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex ae0d09ffc..0e29c73b9 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -287,12 +287,23 @@ func ListComments() web.HandlerFunc {// GetComment returns a single comment by its IDfunc GetComment() web.HandlerFunc {return func(c *web.Context) error {+ number, err := c.ParamAsInt("number")+ if err != nil {+ return c.NotFound()+ }+id, err := c.ParamAsInt("id")if err != nil {return c.NotFound()}- commentByID := &query.GetCommentByID{CommentID: id}+ // The parent post must be visible to the caller, and the comment must belong to it+ getPost := &query.GetPostByNumber{Number: number}+ if err := bus.Dispatch(c, getPost); err != nil {+ return c.Failure(err)+ }++ commentByID := &query.GetCommentByID{CommentID: id, PostID: getPost.Result.ID}if err := bus.Dispatch(c, commentByID); err != nil {return c.Failure(err)}@@ -312,7 +323,12 @@ func ToggleReaction() web.HandlerFunc {return c.HandleValidation(result)}- getComment := &query.GetCommentByID{CommentID: action.Comment}+ getPost := &query.GetPostByNumber{Number: action.Number}+ if err := bus.Dispatch(c, getPost); err != nil {+ return c.Failure(err)+ }++ getComment := &query.GetCommentByID{CommentID: action.Comment, PostID: getPost.Result.ID}if err := bus.Dispatch(c, getComment); err != nil {return c.Failure(err)}diff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.goindex c7e9ad1a9..74fb87342 100644--- a/app/handlers/apiv1/post_test.go+++ b/app/handlers/apiv1/post_test.go@@ -1027,6 +1027,11 @@ func TestListCommentHandler(t *testing.T) {func TestCommentReactionToggleHandler(t *testing.T) {RegisterT(t)+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ q.Result = &entity.Post{ID: 1, Number: 1}+ return nil+ })+comment := &entity.Comment{ID: 5, Content: "Old comment text", User: mock.AryaStark}bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {@@ -1070,6 +1075,11 @@ func TestCommentReactionToggleHandler(t *testing.T) {func TestCommentReactionToggleHandler_InvalidEmoji(t *testing.T) {RegisterT(t)+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ q.Result = &entity.Post{ID: 1, Number: 1}+ return nil+ })+comment := &entity.Comment{ID: 5, Content: "Old comment text", User: mock.AryaStark}bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {q.Result = comment@@ -1094,6 +1104,11 @@ func TestCommentReactionToggleHandler_InvalidEmoji(t *testing.T) {func TestCommentReactionToggleHandler_UnAuthorised(t *testing.T) {RegisterT(t)+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ q.Result = &entity.Post{ID: 1, Number: 1}+ return nil+ })+comment := &entity.Comment{ID: 5, Content: "Old comment text", User: mock.AryaStark}bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {q.Result = comment@@ -1117,6 +1132,11 @@ func TestCommentReactionToggleHandler_UnAuthorised(t *testing.T) {func TestCommentReactionToggleHandler_MismatchingTenantAndComment(t *testing.T) {RegisterT(t)+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ q.Result = &entity.Post{ID: 1, Number: 1}+ return nil+ })+bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {return app.ErrNotFound})@@ -1135,3 +1155,166 @@ func TestCommentReactionToggleHandler_MismatchingTenantAndComment(t *testing.T)Expect(code).Equals(http.StatusNotFound)}++// mockCommentOnPost registers a visible post #1 holding comment #5. Any other post number is+// treated as not visible to the caller, as GetPostByNumber does for deleted or unapproved posts.+func mockCommentOnPost() (*entity.Post, *entity.Comment) {+ post := &entity.Post{ID: 1, Number: 1, Title: "The Post #1"}+ other := &entity.Post{ID: 2, Number: 2, Title: "The Post #2"}+ comment := &entity.Comment{ID: 5, Content: "A comment", User: mock.AryaStark}++ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ switch q.Number {+ case post.Number:+ q.Result = post+ case other.Number:+ q.Result = other+ default:+ return app.ErrNotFound+ }+ return nil+ })++ bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {+ if q.CommentID != comment.ID || (q.PostID != 0 && q.PostID != post.ID) {+ return app.ErrNotFound+ }+ q.Result = comment+ return nil+ })++ return post, comment+}++func TestGetCommentHandler(t *testing.T) {+ RegisterT(t)+ post, comment := mockCommentOnPost()++ code, query := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AddParam("number", post.Number).+ AddParam("id", comment.ID).+ ExecuteAsJSON(apiv1.GetComment())++ Expect(code).Equals(http.StatusOK)+ Expect(query.Int32("id")).Equals(comment.ID)+ Expect(query.String("content")).Equals(comment.Content)+}++func TestGetCommentHandler_HiddenPost(t *testing.T) {+ RegisterT(t)+ _, comment := mockCommentOnPost()++ // Post #3 stands in for a deleted, declined or unapproved post+ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AddParam("number", 3).+ AddParam("id", comment.ID).+ ExecuteAsJSON(apiv1.GetComment())++ Expect(code).Equals(http.StatusNotFound)+}++func TestGetCommentHandler_WrongPost(t *testing.T) {+ RegisterT(t)+ _, comment := mockCommentOnPost()++ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AddParam("number", 2).+ AddParam("id", comment.ID).+ ExecuteAsJSON(apiv1.GetComment())++ Expect(code).Equals(http.StatusNotFound)+}++func TestCommentReactionToggleHandler_HiddenOrWrongPost(t *testing.T) {+ RegisterT(t)+ _, comment := mockCommentOnPost()++ toggled := false+ bus.AddHandler(func(ctx context.Context, c *cmd.ToggleCommentReaction) error {+ toggled = true+ return nil+ })++ for _, number := range []int{2, 3} {+ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("number", number).+ AddParam("id", comment.ID).+ AddParam("reaction", "👍").+ ExecutePost(apiv1.ToggleReaction(), ``)++ Expect(code).Equals(http.StatusNotFound)+ }+ Expect(toggled).IsFalse()+}++func TestUpdateCommentHandler_HiddenOrWrongPost(t *testing.T) {+ RegisterT(t)+ _, comment := mockCommentOnPost()++ updated := false+ bus.AddHandler(func(ctx context.Context, c *cmd.UpdateComment) error {+ updated = true+ return nil+ })++ for _, number := range []int{2, 3} {+ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.AryaStark).+ AddParam("number", number).+ AddParam("id", comment.ID).+ ExecutePost(apiv1.UpdateComment(), `{ "content": "Edited" }`)++ Expect(code).Equals(http.StatusForbidden)+ }+ Expect(updated).IsFalse()+}++func TestDeleteCommentHandler_HiddenOrWrongPost(t *testing.T) {+ RegisterT(t)+ _, comment := mockCommentOnPost()++ deleted := false+ bus.AddHandler(func(ctx context.Context, c *cmd.DeleteComment) error {+ deleted = true+ return nil+ })++ for _, number := range []int{2, 3} {+ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.AryaStark).+ AddParam("number", number).+ AddParam("id", comment.ID).+ Execute(apiv1.DeleteComment())++ Expect(code).Equals(http.StatusForbidden)+ }+ Expect(deleted).IsFalse()+}++func TestDeleteCommentHandler_Authorized(t *testing.T) {+ RegisterT(t)+ post, comment := mockCommentOnPost()++ var deleteComment *cmd.DeleteComment+ bus.AddHandler(func(ctx context.Context, c *cmd.DeleteComment) error {+ deleteComment = c+ return nil+ })++ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.AryaStark).+ AddParam("number", post.Number).+ AddParam("id", comment.ID).+ Execute(apiv1.DeleteComment())++ Expect(code).Equals(http.StatusOK)+ Expect(deleteComment.CommentID).Equals(comment.ID)+}diff --git a/app/models/query/comment.go b/app/models/query/comment.goindex 7ae389b0d..49b3e1c13 100644--- a/app/models/query/comment.go+++ b/app/models/query/comment.go@@ -6,6 +6,8 @@ import (type GetCommentByID struct {CommentID int+ // PostID, when set, restricts the lookup to comments on that post+ PostID intResult *entity.Comment}diff --git a/app/services/sqlstore/postgres/comment.go b/app/services/sqlstore/postgres/comment.goindex 2456d4a6f..d9106d47f 100644--- a/app/services/sqlstore/postgres/comment.go+++ b/app/services/sqlstore/postgres/comment.go@@ -123,11 +123,12 @@ func getCommentByID(ctx context.Context, q *query.GetCommentByID) error {AND e.tenant_id = c.tenant_idWHERE c.id = $1AND c.tenant_id = $2- AND c.deleted_at IS NULL%s`, buildApprovalFilter(user))+ AND c.deleted_at IS NULL+ AND ($3 = 0 OR c.post_id = $3)%s`, buildApprovalFilter(user))comment := dbEntities.Comment{}err := trx.Get(&comment,- query, q.CommentID, tenant.ID)+ query, q.CommentID, tenant.ID, q.PostID)if err != nil {return errdiff --git a/app/services/sqlstore/postgres/post_test.go b/app/services/sqlstore/postgres/post_test.goindex 61d0658e4..815a87d76 100644--- a/app/services/sqlstore/postgres/post_test.go+++ b/app/services/sqlstore/postgres/post_test.go@@ -1,6 +1,7 @@package postgres_testimport (+ "context""os""testing""time"@@ -368,6 +369,66 @@ func TestPostStorage_AddDeleteComment(t *testing.T) {Expect(commentByID.Result).IsNil()}+func TestPostStorage_GetCommentByID_WithPostID(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ post1 := &cmd.AddNewPost{Title: "My first post", Description: "with this description"}+ post2 := &cmd.AddNewPost{Title: "My second post", Description: "with this description"}+ bus.MustDispatch(jonSnowCtx, post1, post2)++ addNewComment := &cmd.AddNewComment{Post: post1.Result, Content: "Comment #1"}+ bus.MustDispatch(aryaStarkCtx, addNewComment)++ commentByID := &query.GetCommentByID{CommentID: addNewComment.Result.ID, PostID: post1.Result.ID}+ err := bus.Dispatch(demoTenantCtx, commentByID)+ Expect(err).IsNil()+ Expect(commentByID.Result.ID).Equals(addNewComment.Result.ID)++ commentByID = &query.GetCommentByID{CommentID: addNewComment.Result.ID, PostID: post2.Result.ID}+ err = bus.Dispatch(demoTenantCtx, commentByID)+ Expect(errors.Cause(err)).Equals(app.ErrNotFound)+ Expect(commentByID.Result).IsNil()++ commentByID = &query.GetCommentByID{CommentID: addNewComment.Result.ID}+ err = bus.Dispatch(demoTenantCtx, commentByID)+ Expect(err).IsNil()+ Expect(commentByID.Result.ID).Equals(addNewComment.Result.ID)+}++// Fetching a single comment relies on GetPostByNumber hiding posts that the caller can't see+func TestPostStorage_GetPostByNumber_HiddenPostsForCommentLookup(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ newPost := &cmd.AddNewPost{Title: "My new post", Description: "with this description"}+ bus.MustDispatch(sansaStarkCtx, newPost)+ bus.MustDispatch(sansaStarkCtx, &cmd.AddNewComment{Post: newPost.Result, Content: "Comment #1"})++ _, err := trx.Execute("UPDATE posts SET is_approved = false WHERE id = $1", newPost.Result.ID)+ Expect(err).IsNil()++ for _, ctx := range []context.Context{demoTenantCtx, aryaStarkCtx} {+ postByNumber := &query.GetPostByNumber{Number: newPost.Result.Number}+ err = bus.Dispatch(ctx, postByNumber)+ Expect(errors.Cause(err)).Equals(app.ErrNotFound)+ }++ for _, ctx := range []context.Context{sansaStarkCtx, jonSnowCtx} {+ postByNumber := &query.GetPostByNumber{Number: newPost.Result.Number}+ err = bus.Dispatch(ctx, postByNumber)+ Expect(err).IsNil()+ }++ bus.MustDispatch(jonSnowCtx, &cmd.SetPostResponse{Post: newPost.Result, Text: "Declined", Status: enum.PostDeleted})++ for _, ctx := range []context.Context{demoTenantCtx, aryaStarkCtx, sansaStarkCtx, jonSnowCtx} {+ postByNumber := &query.GetPostByNumber{Number: newPost.Result.Number}+ err = bus.Dispatch(ctx, postByNumber)+ Expect(errors.Cause(err)).Equals(app.ErrNotFound)+ }+}+func TestPostStorage_AddAndGet_DifferentTenants(t *testing.T) {SetupDatabaseTest(t)defer TeardownDatabaseTest()