Skip to content

Commit

Permalink
Add tests for post creation with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercier Mateo committed Dec 10, 2024
1 parent 7d4c70a commit 1a4712c
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .test.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ JWT_SECRET=SOME_RANDOM_TOKEN_JUST_FOR_TESTING

METRICS_ENABLED=true

POST_CREATION_WITH_TAGS_ENABLED=true

BLOB_STORAGE=s3
BLOB_STORAGE_S3_ENDPOINT_URL=http://localhost:9000
BLOB_STORAGE_S3_REGION=us-east-1
Expand Down Expand Up @@ -37,4 +39,4 @@ EMAIL_MAILGUN_API=mys3cr3tk3y
EMAIL_MAILGUN_DOMAIN=mydomain.com

USER_LIST_ENABLED=true
USER_LIST_APIKEY=abcdefg
USER_LIST_APIKEY=abcdefg
180 changes: 180 additions & 0 deletions app/handlers/apiv1/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/getfider/fider/app/handlers/apiv1"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/mock"
)

Expand Down Expand Up @@ -63,6 +64,185 @@ func TestCreatePostHandler_WithoutTitle(t *testing.T) {
Expect(code).Equals(http.StatusBadRequest)
}

func TestCreatePostHandler_WithInexistentTag(t *testing.T) {
if env.Config.PostCreationWithTagsEnabled {
RegisterT(t)

bus.AddHandler(func(ctx context.Context, q *query.GetTagBySlug) error {
return app.ErrNotFound
})
bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error {
return app.ErrNotFound
})

code, _ := mock.NewServer().
OnTenant(mock.DemoTenant).
AsUser(mock.JonSnow).
ExecutePost(apiv1.CreatePost(), `{ "title": "My newest post :)", "tags": ["inexistent_tag"]}`)

Expect(code).Equals(http.StatusBadRequest)
}
}

func TestCreatePostHandler_WithPrivateTagAsVisitor(t *testing.T) {
if env.Config.PostCreationWithTagsEnabled {
RegisterT(t)

privateTag := &entity.Tag{
ID: 1,
Name: "private_tag",
Slug: "private_tag",
Color: "blue",
IsPublic: false,
}
bus.AddHandler(func(ctx context.Context, q *query.GetTagBySlug) error {
if q.Slug == "private_tag" {
q.Result = privateTag
return nil
}
return app.ErrNotFound
})

bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error {
return app.ErrNotFound
})

code, _ := mock.NewServer().
OnTenant(mock.DemoTenant).
AsUser(mock.AryaStark).
ExecutePost(apiv1.CreatePost(), `{ "title": "My newest post :)", "tags": ["private_tag"]}`)

Expect(code).Equals(http.StatusForbidden)
}
}

func TestCreatePostHandler_WithPublicTagAsVisitor(t *testing.T) {
if env.Config.PostCreationWithTagsEnabled {
RegisterT(t)

var newPost *cmd.AddNewPost
bus.AddHandler(func(ctx context.Context, c *cmd.AddNewPost) error {
newPost = c
c.Result = &entity.Post{
ID: 1,
Title: c.Title,
Description: c.Description,
}
return nil
})

publicTag := &entity.Tag{
ID: 1,
Name: "public_tag",
Slug: "public_tag",
Color: "red",
IsPublic: true,
}
bus.AddHandler(func(ctx context.Context, q *query.GetTagBySlug) error {
if q.Slug == "public_tag" {
q.Result = publicTag
return nil
}
return app.ErrNotFound
})

var tagAssignment *cmd.AssignTag
bus.AddHandler(func(ctx context.Context, c *cmd.AssignTag) error {
tagAssignment = c
return nil
})

bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error {
return app.ErrNotFound
})

bus.AddHandler(func(ctx context.Context, c *cmd.SetAttachments) error { return nil })
bus.AddHandler(func(ctx context.Context, c *cmd.AddVote) error { return nil })
bus.AddHandler(func(ctx context.Context, c *cmd.UploadImages) error { return nil })

code, _ := mock.NewServer().
OnTenant(mock.DemoTenant).
AsUser(mock.AryaStark).
ExecutePost(apiv1.CreatePost(), `{ "title": "My newest post :)", "tags": ["public_tag"]}`)

Expect(code).Equals(http.StatusOK)
Expect(tagAssignment.Tag).Equals(publicTag)
Expect(tagAssignment.Post).Equals(newPost.Result)
}
}

func TestCreatePostHandler_WithPublicTagAndPrivateTagAsCollaborator(t *testing.T) {
if env.Config.PostCreationWithTagsEnabled {
RegisterT(t)

var newPost *cmd.AddNewPost
bus.AddHandler(func(ctx context.Context, c *cmd.AddNewPost) error {
newPost = c
c.Result = &entity.Post{
ID: 1,
Title: c.Title,
Description: c.Description,
}
return nil
})

publicTag := &entity.Tag{
ID: 1,
Name: "public_tag",
Slug: "public_tag",
Color: "red",
IsPublic: true,
}
privateTag := &entity.Tag{
ID: 1,
Name: "private_tag",
Slug: "private_tag",
Color: "blue",
IsPublic: false,
}
bus.AddHandler(func(ctx context.Context, q *query.GetTagBySlug) error {
if q.Slug == "public_tag" {
q.Result = publicTag
return nil
}
if q.Slug == "private_tag" {
q.Result = privateTag
return nil
}
return app.ErrNotFound
})

tagAssignments := make([]*cmd.AssignTag, 2)
bus.AddHandler(func(ctx context.Context, c *cmd.AssignTag) error {
if c.Tag.Slug == "public_tag" {
tagAssignments[0] = c
} else if c.Tag.Slug == "private_tag" {
tagAssignments[1] = c
}
return nil
})

bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error {
return app.ErrNotFound
})

bus.AddHandler(func(ctx context.Context, c *cmd.SetAttachments) error { return nil })
bus.AddHandler(func(ctx context.Context, c *cmd.AddVote) error { return nil })
bus.AddHandler(func(ctx context.Context, c *cmd.UploadImages) error { return nil })

code, _ := mock.NewServer().
OnTenant(mock.DemoTenant).
AsUser(mock.JonSnow).
ExecutePost(apiv1.CreatePost(), `{ "title": "My newest post :)", "tags": ["public_tag", "private_tag"]}`)

Expect(code).Equals(http.StatusOK)
Expect(tagAssignments[0].Tag).Equals(publicTag)
Expect(tagAssignments[1].Tag).Equals(privateTag)
Expect(tagAssignments[0].Post).Equals(newPost.Result)
Expect(tagAssignments[1].Post).Equals(newPost.Result)
}
}

func TestGetPostHandler(t *testing.T) {
RegisterT(t)

Expand Down

0 comments on commit 1a4712c

Please sign in to comment.