Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

add some tests

goenninga28623109eda
diff --git a/app/context.go b/app/context.go
index 8cfe94cf8..cc78cbfef 100644
--- a/app/context.go
+++ b/app/context.go
@@ -37,7 +37,12 @@ func (ctx *Context) IsAuthenticated() bool {
return ctx.Get(claimsContextKey) != nil
}
-//Failure returns a 500 response
+//NotFound returns a 404 page
+func (ctx *Context) NotFound() error {
+ return echo.NewHTTPError(http.StatusNotFound)
+}
+
+//Failure returns a 500 page
func (ctx *Context) Failure(err error) error {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
diff --git a/app/feedback/handler.go b/app/feedback/handler.go
index 15ab7c587..f59ad8a1f 100644
--- a/app/feedback/handler.go
+++ b/app/feedback/handler.go
@@ -3,22 +3,24 @@ package feedback
import (
"strings"
+ "fmt"
+
"github.com/WeCanHearYou/wechy/app"
"github.com/labstack/echo"
)
-// IndexHandlder contains multiple feedback HTTP handlers
-type IndexHandlder struct {
+// AllHandlers contains multiple feedback HTTP handlers
+type AllHandlers struct {
ideaService IdeaService
}
-// Index handles initial page
-func Index(ideaService IdeaService) IndexHandlder {
- return IndexHandlder{ideaService}
+// Handlers handles feedback based page
+func Handlers(ideaService IdeaService) AllHandlers {
+ return AllHandlers{ideaService}
}
// List all tenant ideas
-func (h IndexHandlder) List() app.HandlerFunc {
+func (h AllHandlers) List() app.HandlerFunc {
return func(c app.Context) error {
ideas, err := h.ideaService.GetAll(c.Tenant().ID)
if err != nil {
@@ -36,14 +38,16 @@ type newIdeaInput struct {
Description string `json:"description"`
}
-// Post creates a new idea on current tenant
-func (h IndexHandlder) Post() app.HandlerFunc {
+// PostIdea creates a new idea on current tenant
+func (h AllHandlers) PostIdea() app.HandlerFunc {
return func(c app.Context) error {
input := new(newIdeaInput)
if err := c.Bind(input); err != nil {
return c.Failure(err)
}
+ fmt.Println(input)
+
if strings.Trim(input.Title, " ") == "" {
return c.JSON(400, echo.Map{
"message": "Idea title is required.",
@@ -62,7 +66,7 @@ func (h IndexHandlder) Post() app.HandlerFunc {
}
// Details shows details of given Idea by id
-func (h IndexHandlder) Details() app.HandlerFunc {
+func (h AllHandlers) Details() app.HandlerFunc {
return func(c app.Context) error {
tenant := c.Tenant()
@@ -73,6 +77,9 @@ func (h IndexHandlder) Details() app.HandlerFunc {
idea, err := h.ideaService.GetByID(tenant.ID, ideaID)
if err != nil {
+ if err == app.ErrNotFound {
+ return c.NotFound()
+ }
return c.Failure(err)
}
@@ -93,7 +100,7 @@ type newCommentInput struct {
}
// PostComment creates a new comment on given idea
-func (h IndexHandlder) PostComment() app.HandlerFunc {
+func (h AllHandlers) PostComment() app.HandlerFunc {
return func(c app.Context) error {
input := new(newCommentInput)
if err := c.Bind(input); err != nil {
diff --git a/app/feedback/handler_test.go b/app/feedback/handler_test.go
index 138ab246e..6f33a61cc 100644
--- a/app/feedback/handler_test.go
+++ b/app/feedback/handler_test.go
@@ -9,14 +9,30 @@ import (
. "github.com/onsi/gomega"
)
-type mockIdeaService struct{}
+type mockIdeaService struct {
+ ideas []*feedback.Idea
+}
+
+func NewMockIdeaService() *mockIdeaService {
+ return &mockIdeaService{
+ ideas: []*feedback.Idea{
+ &feedback.Idea{ID: 1, Title: "Idea #1"},
+ &feedback.Idea{ID: 2, Title: "Idea #2"},
+ },
+ }
+}
func (svc mockIdeaService) GetByID(tenantID, ideaID int) (*feedback.Idea, error) {
- return new(feedback.Idea), nil
+ for _, idea := range svc.ideas {
+ if idea.ID == ideaID {
+ return idea, nil
+ }
+ }
+ return nil, app.ErrNotFound
}
func (svc mockIdeaService) GetAll(tenantID int) ([]*feedback.Idea, error) {
- return make([]*feedback.Idea, 0), nil
+ return svc.ideas, nil
}
func (svc mockIdeaService) GetCommentsByIdeaID(tenantID, ideaID int) ([]*feedback.Comment, error) {
@@ -31,12 +47,90 @@ func (svc mockIdeaService) AddComment(userID, ideaID int, content string) (int,
return 0, nil
}
-func TestIndexHandler(t *testing.T) {
+func TestListHandler(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).List())
+
+ Expect(code).To(Equal(200))
+}
+
+func TestDetailsHandler(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ server.Context.SetParamNames("id")
+ server.Context.SetParamValues("1")
+
+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).Details())
+
+ Expect(code).To(Equal(200))
+}
+
+func TestDetailsHandler_NotFound(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ server.Context.SetParamNames("id")
+ server.Context.SetParamValues("99")
+
+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).Details())
+
+ Expect(code).To(Equal(404))
+}
+
+func TestDetailsHandler_AddIdea(t *testing.T) {
RegisterTestingT(t)
server := mock.NewServer()
server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
- code, _ := server.Execute(feedback.Index(&mockIdeaService{}).List())
+ server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})
+ handler := feedback.Handlers(NewMockIdeaService()).PostIdea()
+ code, _ := server.ExecutePost(handler, `{ "title": "My newest idea :)" }`)
Expect(code).To(Equal(200))
}
+
+func TestDetailsHandler_AddIdea_WithoutTitle(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})
+ handler := feedback.Handlers(NewMockIdeaService()).PostIdea()
+ code, _ := server.ExecutePost(handler, `{ "title": "" }`)
+
+ Expect(code).To(Equal(400))
+}
+
+func TestDetailsHandler_AddComment(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})
+ server.Context.SetParamNames("id")
+ server.Context.SetParamValues("1")
+ handler := feedback.Handlers(NewMockIdeaService()).PostComment()
+ code, _ := server.ExecutePost(handler, `{ "content": "This is a comment!" }`)
+
+ Expect(code).To(Equal(200))
+}
+
+func TestDetailsHandler_AddComment_WithoutContent(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})
+ server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})
+ server.Context.SetParamNames("id")
+ server.Context.SetParamValues("1")
+ handler := feedback.Handlers(NewMockIdeaService()).PostComment()
+ code, _ := server.ExecutePost(handler, `{ "content": "" }`)
+
+ Expect(code).To(Equal(400))
+}
diff --git a/app/mock/server.go b/app/mock/server.go
index 2d767b385..d23da856f 100644
--- a/app/mock/server.go
+++ b/app/mock/server.go
@@ -7,6 +7,8 @@ import (
"strings"
+ "fmt"
+
"github.com/WeCanHearYou/wechy/app"
"github.com/jmoiron/jsonq"
"github.com/labstack/echo"
@@ -24,6 +26,10 @@ type Server struct {
func NewServer() *Server {
engine := echo.New()
engine.Renderer = app.NewHTMLRenderer(engine.Logger)
+ engine.HTTPErrorHandler = func(e error, c echo.Context) {
+ fmt.Println(e)
+ c.NoContent(e.(*echo.HTTPError).Code)
+ }
request, _ := http.NewRequest(echo.GET, "/", nil)
recorder := httptest.NewRecorder()
@@ -45,7 +51,28 @@ func (s *Server) NewContext(req *http.Request, w http.ResponseWriter) app.Contex
// Execute given handler and return response as JSON
func (s *Server) Execute(handler app.HandlerFunc) (int, *jsonq.JsonQuery) {
ctx := app.Context{Context: s.Context}
- handler(ctx)
+ if err := handler(ctx); err != nil {
+ s.engine.HTTPErrorHandler(err, ctx)
+ }
+
+ return parseJSONBody(s)
+}
+
+// ExecutePost executes given handler with posted JSON
+func (s *Server) ExecutePost(handler app.HandlerFunc, body string) (int, *jsonq.JsonQuery) {
+ ctx := app.Context{Context: s.Context}
+
+ req, _ := http.NewRequest("POST", "/", strings.NewReader(body))
+ req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationJSON)
+ ctx.SetRequest(req)
+
+ if err := handler(ctx); err != nil {
+ s.engine.HTTPErrorHandler(err, ctx)
+ }
+ return parseJSONBody(s)
+}
+
+func parseJSONBody(s *Server) (int, *jsonq.JsonQuery) {
if s.recorder.Code == 200 && hasJSON(s.recorder) {
var data interface{}
@@ -61,7 +88,9 @@ func (s *Server) Execute(handler app.HandlerFunc) (int, *jsonq.JsonQuery) {
// ExecuteRaw executes given handler and return raw response
func (s *Server) ExecuteRaw(handler app.HandlerFunc) (int, *http.Response) {
ctx := app.Context{Context: s.Context}
- handler(ctx)
+ if err := handler(ctx); err != nil {
+ s.engine.HTTPErrorHandler(err, ctx)
+ }
return s.recorder.Code, s.recorder.Result()
}
diff --git a/app/postgres/feedback_test.go b/app/postgres/feedback_test.go
index 6f71c9c32..a5bebe6aa 100644
--- a/app/postgres/feedback_test.go
+++ b/app/postgres/feedback_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"time"
+ "github.com/WeCanHearYou/wechy/app"
"github.com/WeCanHearYou/wechy/app/postgres"
. "github.com/onsi/gomega"
)
@@ -68,7 +69,7 @@ func TestIdeaService_GetInvalid(t *testing.T) {
svc := &postgres.IdeaService{DB: db}
dbIdea, err := svc.GetByID(1, 1)
- Expect(err).ToNot(BeNil())
+ Expect(err).To(Equal(app.ErrNotFound))
Expect(dbIdea).To(BeNil())
}
diff --git a/app/renderer.go b/app/renderer.go
index 720ad868c..acf6c6fb5 100644
--- a/app/renderer.go
+++ b/app/renderer.go
@@ -73,11 +73,7 @@ func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo
}
}
- files, err := ioutil.ReadDir("dist/js")
- if err != nil {
- return err
- }
-
+ files, _ := ioutil.ReadDir("dist/js")
if len(files) > 0 {
m["JavaScriptBundle"] = files[0].Name()
}
diff --git a/router.go b/router.go
index 3e0b8b7a4..7938128fa 100644
--- a/router.go
+++ b/router.go
@@ -110,13 +110,13 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
use(appGroup, identity.JwtSetter())
use(appGroup, identity.MultiTenant(ctx.Tenant))
- get(appGroup, "/", feedback.Index(ctx.Idea).List())
- get(appGroup, "/ideas/:id", feedback.Index(ctx.Idea).Details())
+ get(appGroup, "/", feedback.Handlers(ctx.Idea).List())
+ get(appGroup, "/ideas/:id", feedback.Handlers(ctx.Idea).Details())
get(appGroup, "/logout", identity.OAuth(ctx.OAuth, ctx.User).Logout())
get(appGroup, "/api/status", infra.Status(ctx.Health, ctx.Settings))
- post(appGroup, "/api/ideas", feedback.Index(ctx.Idea).Post())
- post(appGroup, "/api/ideas/:id/comments", feedback.Index(ctx.Idea).PostComment())
+ post(appGroup, "/api/ideas", feedback.Handlers(ctx.Idea).PostIdea())
+ post(appGroup, "/api/ideas/:id/comments", feedback.Handlers(ctx.Idea).PostComment())
}
return router