Open platform to collect and prioritize feedback
Increase backend test coverage (#148)
Guilherme Oenning481131ea413f
diff --git a/Makefile b/Makefileindex 72b2a06da..2f12bd78f 100644--- a/Makefile+++ b/Makefile@@ -10,9 +10,7 @@ test:godotenv -f ${ENV_FILE} go test ./... -cover -p=1coverage:- echo 'mode: atomic' > cover.out- go list ./... | xargs -n1 -I{} sh -c 'godotenv -f ${ENV_FILE} go test -covermode=atomic -coverpkg=./... -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> cover.out'- rm coverage.tmp+ godotenv -f ${ENV_FILE} courtney -o cover.out $$(go list ./...)build:go build -a -ldflags='-X main.buildtime=${BUILD_TIME}' -o fider .diff --git a/app/actions/idea.go b/app/actions/idea.goindex d84ac3d6f..05099ce5b 100644--- a/app/actions/idea.go+++ b/app/actions/idea.go@@ -21,7 +21,7 @@ func (input *CreateNewIdea) Initialize() interface{} {// IsAuthorized returns true if current user is authorized to perform this actionfunc (input *CreateNewIdea) IsAuthorized(user *models.User) bool {- return true+ return user != nil}// Validate is current model is valid@@ -52,7 +52,7 @@ func (input *AddNewComment) Initialize() interface{} {// IsAuthorized returns true if current user is authorized to perform this actionfunc (input *AddNewComment) IsAuthorized(user *models.User) bool {- return true+ return user != nil}// Validate is current model is valid@@ -79,7 +79,7 @@ func (input *SetResponse) Initialize() interface{} {// IsAuthorized returns true if current user is authorized to perform this actionfunc (input *SetResponse) IsAuthorized(user *models.User) bool {- return true+ return user != nil && user.IsStaff()}// Validate is current model is validdiff --git a/app/handlers/avatar_test.go b/app/handlers/avatar_test.gonew file mode 100644index 000000000..53e95ae28--- /dev/null+++ b/app/handlers/avatar_test.go@@ -0,0 +1,60 @@+package handlers_test++import (+ "io/ioutil"+ "net/http"+ "testing"++ "github.com/getfider/fider/app/pkg/mock"++ "github.com/getfider/fider/app/handlers"+ . "github.com/onsi/gomega"+)++func TestAvatarHandler(t *testing.T) {+ RegisterTestingT(t)++ server, _ := mock.NewServer()+ code, response := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("size", 50).+ AddParam("name", "Jon Snow").+ Execute(handlers.LetterAvatar())++ expectedAvatar := []byte{+ 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 50, 0, 0, 0, 50, 8, 2, 0, 0, 0, 145,+ 93, 31, 230, 0, 0, 2, 150, 73, 68, 65, 84, 120, 156, 236, 152, 91, 79, 226, 64, 20, 128, 167, 115, 233, 141,+ 94, 160, 84, 164, 46, 198, 93, 55, 217, 248, 255, 255, 200, 190, 154, 189, 100, 141, 138, 130, 86, 64, 218,+ 210, 206, 244, 182, 129, 106, 117, 163, 49, 51, 89, 34, 62, 204, 247, 212, 28, 79, 207, 124, 158, 30, 78, 9,+ 248, 251, 233, 47, 240, 241, 128, 187, 22, 120, 29, 169, 37, 130, 212, 18, 65, 106, 137, 32, 181, 68, 144, 90,+ 34, 72, 45, 17, 164, 150, 8, 82, 75, 4, 252, 63, 55, 219, 29, 115, 224, 117, 1, 0, 227, 105, 152, 49, 214, 4,+ 33, 132, 126, 215, 113, 172, 142, 70, 214, 197, 243, 162, 140, 146, 85, 184, 88, 230, 69, 241, 78, 90, 24, 161,+ 142, 174, 53, 42, 77, 132, 16, 124, 252, 105, 168, 17, 210, 230, 32, 21, 234, 170, 235, 185, 246, 217, 213, 52,+ 73, 179, 247, 208, 122, 201, 104, 224, 107, 132, 212, 0, 220, 220, 45, 238, 147, 164, 174, 129, 169, 107, 67, 191,+ 71, 16, 58, 10, 246, 79, 207, 46, 170, 170, 226, 169, 179, 205, 217, 66, 8, 218, 166, 1, 0, 88, 44, 227, 233, 108,+ 158, 81, 70, 25, 155, 47, 163, 203, 105, 184, 105, 45, 116, 173, 14, 103, 169, 109, 118, 75, 81, 148, 230, 162,+ 252, 183, 37, 73, 154, 221, 221, 71, 155, 120, 185, 3, 173, 162, 40, 105, 158, 107, 132, 120, 174, 157, 164,+ 217, 125, 156, 52, 241, 170, 170, 198, 55, 161, 80, 169, 45, 47, 136, 241, 205, 93, 93, 3, 168, 40, 71, 193,+ 224, 219, 209, 104, 224, 117, 213, 103, 227, 191, 51, 173, 120, 149, 254, 190, 188, 74, 233, 122, 89, 232,+ 42, 25, 246, 123, 39, 159, 71, 199, 163, 192, 212, 245, 93, 106, 1, 0, 86, 25, 253, 121, 62, 254, 51, 158,+ 204, 163, 184, 25, 50, 203, 208, 191, 30, 6, 125, 215, 225, 47, 178, 229, 5, 209, 18, 173, 210, 104, 149,+ 66, 69, 113, 237, 78, 224, 123, 24, 161, 131, 65, 63, 78, 51, 250, 184, 117, 223, 134, 183, 91, 16, 66,+ 140, 16, 70, 232, 121, 80, 121, 186, 172, 1, 0, 134, 166, 117, 109, 171, 107, 91, 237, 71, 178, 170, 235,+ 249, 50, 62, 159, 220, 54, 201, 174, 101, 114, 30, 199, 219, 45, 191, 235, 12, 251, 61, 0, 192, 233, 217,+ 5, 203, 31, 94, 35, 132, 60, 220, 94, 148, 155, 135, 101, 26, 129, 191, 206, 249, 113, 206, 50, 250, 212,+ 149, 246, 26, 65, 238, 46, 112, 230, 165, 143, 165, 219, 17, 33, 24, 123, 142, 221, 188, 245, 88, 158,+ 3, 0, 150, 73, 82, 111, 254, 116, 176, 215, 111, 251, 170, 40, 74, 191, 251, 112, 75, 198, 114, 206,+ 227, 120, 187, 21, 175, 210, 148, 50, 67, 83, 247, 122, 174, 99, 153, 121, 81, 26, 154, 134, 224, 250,+ 97, 93, 135, 179, 38, 135, 178, 124, 18, 206, 2, 223, 179, 12, 253, 228, 203, 97, 70, 89, 89, 85, 154,+ 74, 84, 140, 155, 127, 108, 17, 197, 156, 199, 41, 252, 191, 111, 97, 140, 70, 131, 61, 167, 99, 180,+ 145, 162, 172, 38, 225, 108, 182, 140, 158, 167, 57, 29, 115, 223, 239, 25, 170, 218, 70, 106, 0, 22,+ 81, 124, 125, 59, 43, 74, 222, 45, 47, 160, 213, 160, 18, 172, 107, 42, 84, 96, 94, 20, 105, 70, 171,+ 186, 126, 53, 109, 221, 36, 66, 16, 132, 69, 89, 102, 148, 241, 11, 53, 8, 47, 8, 150, 23, 237, 200,+ 191, 1, 101, 57, 229, 158, 164, 151, 124, 208, 111, 167, 82, 75, 4, 169, 37, 130, 212, 18, 65, 106, 137,+ 32, 181, 68, 144, 90, 34, 72, 45, 17, 254, 6, 0, 0, 255, 255, 195, 184, 4, 70, 45, 232, 84, 206, 0,+ 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}++ Expect(code).To(Equal(http.StatusOK))+ Expect(ioutil.ReadAll(response.Body)).To(Equal(expectedAvatar))+}diff --git a/app/handlers/idea.go b/app/handlers/idea.goindex 6a789a38c..300fc1cfa 100644--- a/app/handlers/idea.go+++ b/app/handlers/idea.go@@ -80,9 +80,6 @@ func IdeaDetails() web.HandlerFunc {ideas := c.Services().Ideasidea, err := ideas.GetByNumber(number)if err != nil {- if err == app.ErrNotFound {- return c.NotFound()- }return c.Failure(err)}@@ -108,9 +105,6 @@ func PostComment() web.HandlerFunc {_, err := c.Services().Ideas.AddComment(input.Model.Number, input.Model.Content, c.User().ID)if err != nil {- if err == app.ErrNotFound {- return c.NotFound()- }return c.Failure(err)}@@ -128,9 +122,6 @@ func SetResponse() web.HandlerFunc {err := c.Services().Ideas.SetResponse(input.Model.Number, input.Model.Text, c.User().ID, input.Model.Status)if err != nil {- if err == app.ErrNotFound {- return c.NotFound()- }return c.Failure(err)}diff --git a/app/handlers/idea_test.go b/app/handlers/idea_test.goindex 991ceaa3a..b9d87908f 100644--- a/app/handlers/idea_test.go+++ b/app/handlers/idea_test.go@@ -1,9 +1,12 @@package handlers_testimport (+ "fmt""net/http""testing"+ "github.com/getfider/fider/app/models"+"github.com/getfider/fider/app/handlers""github.com/getfider/fider/app/pkg/mock". "github.com/onsi/gomega"@@ -27,7 +30,7 @@ func TestDetailsHandler(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).Execute(handlers.IdeaDetails())Expect(code).To(Equal(200))@@ -40,7 +43,7 @@ func TestDetailsHandler_NotFound(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).- WithParam("number", "99").+ AddParam("number", "99").Execute(handlers.IdeaDetails())Expect(code).To(Equal(404))@@ -85,7 +88,7 @@ func TestUpdateIdeaHandler_IdeaOwner(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.AryaStark).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).ExecutePost(handlers.UpdateIdea(), `{ "title": "the new title", "description": "new description" }`)idea, _ = services.Ideas.GetByNumber(idea.Number)@@ -103,7 +106,7 @@ func TestUpdateIdeaHandler_TenantStaff(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).ExecutePost(handlers.UpdateIdea(), `{ "title": "the new title", "description": "new description" }`)idea, _ = services.Ideas.GetByNumber(idea.Number)@@ -121,7 +124,7 @@ func TestUpdateIdeaHandler_NonAuthorized(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.AryaStark).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).ExecutePost(handlers.UpdateIdea(), `{ "title": "the new title", "description": "new description" }`)Expect(code).To(Equal(http.StatusForbidden))@@ -136,7 +139,7 @@ func TestPostCommentHandler(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).ExecutePost(handlers.PostComment(), `{ "content": "This is a comment!" }`)Expect(code).To(Equal(200))@@ -151,7 +154,7 @@ func TestPostCommentHandler_WithoutContent(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).- WithParam("number", idea.Number).+ AddParam("number", idea.Number).ExecutePost(handlers.PostComment(), `{ "content": "" }`)Expect(code).To(Equal(400))@@ -167,7 +170,7 @@ func TestAddSupporterHandler(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.AryaStark).- WithParam("number", second.Number).+ AddParam("number", second.Number).Execute(handlers.AddSupporter())first, _ = services.Ideas.GetByNumber(1)@@ -185,7 +188,7 @@ func TestAddSupporterHandler_InvalidIdea(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.AryaStark).- WithParam("number", 999).+ AddParam("number", 999).Execute(handlers.AddSupporter())Expect(code).To(Equal(404))@@ -202,7 +205,7 @@ func TestRemoveSupporterHandler(t *testing.T) {code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.AryaStark).- WithParam("number", idea.ID).+ AddParam("number", idea.ID).Execute(handlers.RemoveSupporter())idea, _ = services.Ideas.GetByNumber(idea.Number)@@ -210,3 +213,43 @@ func TestRemoveSupporterHandler(t *testing.T) {Expect(code).To(Equal(200))Expect(idea.TotalSupporters).To(Equal(1))}++func TestSetResponseHandler(t *testing.T) {+ RegisterTestingT(t)++ server, services := mock.NewServer()+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)+ services.Ideas.AddSupporter(idea.Number, mock.AryaStark.ID)++ code, _ := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("number", idea.ID).+ ExecutePost(handlers.SetResponse(), fmt.Sprintf(`{ "status": %d, "text": "Done!" }`, models.IdeaCompleted))++ idea, _ = services.Ideas.GetByNumber(idea.Number)++ Expect(code).To(Equal(http.StatusOK))+ Expect(idea.Status).To(Equal(models.IdeaCompleted))+ Expect(idea.Response.Text).To(Equal("Done!"))+ Expect(idea.Response.User.ID).To(Equal(mock.JonSnow.ID))+}++func TestSetResponseHandler_Unauthorized(t *testing.T) {+ RegisterTestingT(t)++ server, services := mock.NewServer()+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)+ services.Ideas.AddSupporter(idea.Number, mock.AryaStark.ID)++ code, _ := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.AryaStark).+ AddParam("number", idea.ID).+ ExecutePost(handlers.SetResponse(), fmt.Sprintf(`{ "status": %d, "text": "Done!" }`, models.IdeaCompleted))++ idea, _ = services.Ideas.GetByNumber(idea.Number)++ Expect(code).To(Equal(http.StatusForbidden))+ Expect(idea.Status).To(Equal(models.IdeaNew))+}diff --git a/app/handlers/signin_test.go b/app/handlers/signin_test.goindex a51a2b64d..c8a1d7127 100644--- a/app/handlers/signin_test.go+++ b/app/handlers/signin_test.go@@ -76,6 +76,18 @@ func TestCallbackHandler_ExistingUserAndProvider(t *testing.T) {Expect(response.Header().Get("Location")).To(Equal("http://demo.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjoxLCJ1c2VyL25hbWUiOiJKb24gU25vdyIsInVzZXIvZW1haWwiOiJqb24uc25vd0Bnb3QuY29tIn0.S7P8zTU0rVovmchNbwamBewYbO96GdJcOygn7tbsikw"))}+func TestCallbackHandler_SignUp(t *testing.T) {+ RegisterTestingT(t)++ server, _ := mock.NewServer()+ code, response := server.+ WithURL("http://demo.test.fider.io/oauth/callback?state=http://demo.test.fider.io/signup&code=123").+ Execute(handlers.OAuthCallback(oauth.FacebookProvider))++ Expect(code).To(Equal(http.StatusTemporaryRedirect))+ Expect(response.Header().Get("Location")).To(Equal("http://demo.test.fider.io/signup?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvYXV0aC9pZCI6IkZCMTIzIiwib2F1dGgvcHJvdmlkZXIiOiJmYWNlYm9vayIsIm9hdXRoL25hbWUiOiJKb24gU25vdyIsIm9hdXRoL2VtYWlsIjoiam9uLnNub3dAZ290LmNvbSJ9.AFMEtMLxd2nAxPzMVeGVrJomn-n56WdhLvgYSmSq008"))+}+func TestCallbackHandler_NewUser(t *testing.T) {RegisterTestingT(t)@@ -283,9 +295,11 @@ func TestVerifySignUpKeyHandler_InactiveTenant(t *testing.T) {WithURL("http://demo.test.fider.io/signup/verify?k=1234567890").Execute(handlers.VerifySignUpKey())+ tenant, _ := services.Tenants.GetByDomain("demo")+Expect(code).To(Equal(http.StatusTemporaryRedirect))+ Expect(tenant.Status).To(Equal(models.TenantActive))Expect(response.Header().Get("Location")).To(Equal("http://demo.test.fider.io"))- //Expect(response.Header().Get("Set-Cookie")).To(ContainSubstring(fmt.Sprintf("%s=%s;", web.CookieAuthName, token)))}func TestCompleteSignInProfileHandler_UnknownKey(t *testing.T) {diff --git a/app/handlers/signup_test.go b/app/handlers/signup_test.goindex 8b0f531a2..2d545bd50 100644--- a/app/handlers/signup_test.go+++ b/app/handlers/signup_test.go@@ -44,7 +44,7 @@ func TestCheckAvailabilityHandler_InvalidSubdomain(t *testing.T) {RegisterTestingT(t)server, _ := mock.NewServer()- code, response := server.WithParam("subdomain", "").ExecuteAsJSON(handlers.CheckAvailability())+ code, response := server.AddParam("subdomain", "").ExecuteAsJSON(handlers.CheckAvailability())Expect(code).To(Equal(200))Expect(response.String("message")).NotTo(BeNil())@@ -54,7 +54,7 @@ func TestCheckAvailabilityHandler_UnavailableSubdomain(t *testing.T) {RegisterTestingT(t)server, _ := mock.NewServer()- code, response := server.WithParam("subdomain", "demo").ExecuteAsJSON(handlers.CheckAvailability())+ code, response := server.AddParam("subdomain", "demo").ExecuteAsJSON(handlers.CheckAvailability())Expect(code).To(Equal(200))Expect(response.String("message")).NotTo(BeNil())@@ -64,7 +64,7 @@ func TestCheckAvailabilityHandler_ValidSubdomain(t *testing.T) {RegisterTestingT(t)server, _ := mock.NewServer()- code, response := server.WithParam("subdomain", "mycompany").ExecuteAsJSON(handlers.CheckAvailability())+ code, response := server.AddParam("subdomain", "mycompany").ExecuteAsJSON(handlers.CheckAvailability())Expect(code).To(Equal(200))Expect(response.Contains("message")).To(BeFalse())diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.goindex ceae05f06..913a70edb 100644--- a/app/middlewares/jwt_test.go+++ b/app/middlewares/jwt_test.go@@ -50,6 +50,55 @@ func TestJwtGetter_WithCookie(t *testing.T) {Expect(response.Body.String()).To(Equal("Jon Snow"))}+func TestJwtGetter_WithCookie_ShouldSetSupportedIdeas(t *testing.T) {+ RegisterTestingT(t)++ server, services := mock.NewServer()+ idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)++ token, _ := jwt.Encode(&models.FiderClaims{+ UserID: mock.JonSnow.ID,+ UserName: mock.JonSnow.Name,+ })++ var ids []int+ server.Use(middlewares.JwtGetter())+ status, _ := server.+ OnTenant(mock.DemoTenant).+ AddCookie(web.CookieAuthName, token).+ Execute(func(c web.Context) error {+ ids = c.RenderVars()["supportedIdeas"].([]int)+ return c.NoContent(http.StatusOK)+ })++ Expect(status).To(Equal(http.StatusOK))+ Expect(ids).To(Equal([]int{idea.ID}))+}++func TestJwtGetter_WithCookie_InvalidUser(t *testing.T) {+ RegisterTestingT(t)++ server, _ := mock.NewServer()+ token, _ := jwt.Encode(&models.FiderClaims{+ UserID: 999,+ UserName: "Unknown",+ })++ server.Use(middlewares.JwtGetter())+ status, response := server.+ OnTenant(mock.OrangeTenant).+ AddCookie(web.CookieAuthName, token).+ Execute(func(c web.Context) error {+ if c.User() == nil {+ return c.NoContent(http.StatusNoContent)+ }+ return c.NoContent(http.StatusOK)+ })++ Expect(status).To(Equal(http.StatusNoContent))+ Expect(response.Header().Get("Set-Cookie")).To(ContainSubstring(web.CookieAuthName + "=;"))+}+func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {RegisterTestingT(t)diff --git a/app/middlewares/setup_test.go b/app/middlewares/setup_test.gonew file mode 100644index 000000000..2cf4cff70--- /dev/null+++ b/app/middlewares/setup_test.go@@ -0,0 +1,61 @@+package middlewares_test++import (+ "errors"+ "net/http"+ "testing"++ "github.com/getfider/fider/app/pkg/email"++ "github.com/getfider/fider/app/middlewares"+ "github.com/getfider/fider/app/pkg/dbx"+ "github.com/getfider/fider/app/pkg/mock"+ "github.com/getfider/fider/app/pkg/web"+ . "github.com/onsi/gomega"+)++func TestSetup(t *testing.T) {+ RegisterTestingT(t)++ db, _ := dbx.New()+ defer db.Close()++ server, _ := mock.NewServer()+ server.Use(middlewares.Setup(db, email.NewNoopSender()))+ status, _ := server.Execute(func(c web.Context) error {+ Expect(c.ActiveTransaction()).NotTo(BeNil())+ return c.NoContent(http.StatusOK)+ })++ Expect(status).To(Equal(http.StatusOK))+}++func TestSetup_Failure(t *testing.T) {+ RegisterTestingT(t)++ db, _ := dbx.New()+ defer db.Close()++ server, _ := mock.NewServer()+ server.Use(middlewares.Setup(db, email.NewNoopSender()))+ status, _ := server.Execute(func(c web.Context) error {+ return c.Failure(errors.New("Something went wrong..."))+ })++ Expect(status).To(Equal(http.StatusInternalServerError))+}++func TestSetup_Panic(t *testing.T) {+ RegisterTestingT(t)++ db, _ := dbx.New()+ defer db.Close()++ server, _ := mock.NewServer()+ server.Use(middlewares.Setup(db, email.NewNoopSender()))+ status, _ := server.Execute(func(c web.Context) error {+ panic("Boom!")+ })++ Expect(status).To(Equal(http.StatusInternalServerError))+}diff --git a/app/pkg/email/email.go b/app/pkg/email/email.goindex f60fb2a29..739d12046 100644--- a/app/pkg/email/email.go+++ b/app/pkg/email/email.go@@ -14,6 +14,11 @@ type Sender interface {type NoopSender struct {}+//NewNoopSender creates a new NoopSender+func NewNoopSender() *NoopSender {+ return &NoopSender{}+}+//Send an e-mailfunc (s *NoopSender) Send(from, to, subject, message string) error {return nildiff --git a/app/pkg/mock/oauth.go b/app/pkg/mock/oauth.goindex 58f099e3c..ea0820984 100644--- a/app/pkg/mock/oauth.go+++ b/app/pkg/mock/oauth.go@@ -44,13 +44,5 @@ func (p *OAuthService) GetProfile(authEndpoint string, provider string, code str}, nil}- if provider == "google" && code == "456" {- return &oauth.UserProfile{- ID: "GO456",- Name: "Bob",- Email: "",- }, nil- }-return nil, nil}diff --git a/app/pkg/mock/server.go b/app/pkg/mock/server.goindex 460245751..6771d65c0 100644--- a/app/pkg/mock/server.go+++ b/app/pkg/mock/server.go@@ -57,9 +57,9 @@ func (s *Server) AsUser(user *models.User) *Server {return s}-// WithParam set current context params-func (s *Server) WithParam(name string, value interface{}) *Server {- s.context.SetParams(web.StringMap{name: fmt.Sprintf("%v", value)})+// AddParam to current context route parameters+func (s *Server) AddParam(name string, value interface{}) *Server {+ s.context.AddParam(name, fmt.Sprintf("%v", value))return s}diff --git a/app/pkg/mock/setup.go b/app/pkg/mock/setup.goindex 7cde8e3c2..577594121 100644--- a/app/pkg/mock/setup.go+++ b/app/pkg/mock/setup.go@@ -42,9 +42,9 @@ func createServices(seed bool) *app.Services {services := &app.Services{Tenants: &inmemory.TenantStorage{},Users: &inmemory.UserStorage{},- Ideas: &inmemory.IdeaStorage{},+ Ideas: inmemory.NewIdeaStorage(),OAuth: &OAuthService{},- Emailer: &email.NoopSender{},+ Emailer: email.NewNoopSender(),}if seed {diff --git a/app/pkg/oauth/http_test.go b/app/pkg/oauth/http_test.goindex 43695c8b1..6c03b9a71 100644--- a/app/pkg/oauth/http_test.go+++ b/app/pkg/oauth/http_test.go@@ -24,3 +24,12 @@ func TestGetAuthURL_Google(t *testing.T) {Expect(authURL).To(Equal("https://accounts.google.com/o/oauth2/auth?client_id=&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2Fgoogle%2Fcallback&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&state="))}++func TestGetAuthURL_GitHub(t *testing.T) {+ RegisterTestingT(t)++ svc := &oauth.HTTPService{}+ authURL := svc.GetAuthURL("http://login.test.fider.io:3000", oauth.GitHubProvider, "")++ Expect(authURL).To(Equal("https://github.com/login/oauth/authorize?client_id=&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2Fgithub%2Fcallback&response_type=code&scope=user%3Aemail&state="))+}diff --git a/app/pkg/validate/validation.go b/app/pkg/validate/validation.goindex e45608875..88e717067 100644--- a/app/pkg/validate/validation.go+++ b/app/pkg/validate/validation.go@@ -20,15 +20,6 @@ type Result struct {Failures map[string][]string}-//AddFailure as a general message-func (r *Result) AddFailure(message string) {- if r.Messages == nil {- r.Messages = []string{}- }- r.Messages = append(r.Messages, message)- r.Ok = false-}-//AddFieldFailure add failure message to specific fieldfunc (r *Result) AddFieldFailure(field string, messages ...string) {if r.Failures == nil {diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 163d93204..acbc0e073 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -116,6 +116,9 @@ func (ctx *Context) NotFound() error {//Failure returns a 500 pagefunc (ctx *Context) Failure(err error) error {+ if err == app.ErrNotFound {+ return ctx.NotFound()+ }ctx.Logger().Errorf("%s", log.Red(err.Error()))ctx.Render(http.StatusInternalServerError, "500.html", Map{})return err@@ -161,13 +164,9 @@ func (ctx *Context) Render(code int, template string, data Map) error {return ctx.Blob(code, HTMLContentType, buf.Bytes())}-//SetParams sets path parameter names and values.-func (ctx *Context) SetParams(dict StringMap) {- if dict == nil {- return- }-- ctx.params = dict+//AddParam add a single param to route parameters list+func (ctx *Context) AddParam(name, value string) {+ ctx.params[name] = value}//User returns authenticated user@@ -342,11 +341,6 @@ func (ctx *Context) String(code int, text string) error {return ctx.Blob(code, PlainContentType, []byte(text))}-// HTML returns a HTML response with status code.-func (ctx *Context) HTML(code int, html string) error {- return ctx.Blob(code, HTMLContentType, []byte(html))-}-// JSON returns a JSON response with status code.func (ctx *Context) JSON(code int, i interface{}) error {b, err := json.Marshal(i)@@ -374,11 +368,6 @@ func (ctx *Context) SetCookie(cookie *http.Cookie) {http.SetCookie(ctx.Response, cookie)}-// Cookies returns the HTTP cookies sent with the request.-func (ctx *Context) Cookies() []*http.Cookie {- return ctx.Request.Cookies()-}-// NoContent sends a response with no body and a status code.func (ctx *Context) NoContent(code int) error {ctx.Response.WriteHeader(code)diff --git a/app/storage/inmemory/idea.go b/app/storage/inmemory/idea.goindex 79b43f0de..80544f6d8 100644--- a/app/storage/inmemory/idea.go+++ b/app/storage/inmemory/idea.go@@ -9,8 +9,17 @@ import (// IdeaStorage contains read and write operations for ideastype IdeaStorage struct {- lastID int- ideas []*models.Idea+ lastID int+ ideas []*models.Idea+ ideasSupportedBy map[int][]int+}++// NewIdeaStorage creates a new IdeaStorage+func NewIdeaStorage() *IdeaStorage {+ return &IdeaStorage{+ ideas: make([]*models.Idea, 0),+ ideasSupportedBy: make(map[int][]int, 0),+ }}// GetByID returns idea by given id@@ -67,6 +76,7 @@ func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea,},}s.ideas = append(s.ideas, idea)+ s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID], idea.ID)return idea, nil}@@ -81,6 +91,7 @@ func (s *IdeaStorage) AddSupporter(number, userID int) error {if err != nil {return err}+ s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID], idea.ID)idea.TotalSupporters = idea.TotalSupporters + 1return nil}@@ -91,6 +102,13 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {if err != nil {return err}++ for i, id := range s.ideasSupportedBy[userID] {+ if id == idea.ID {+ s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID][:i], s.ideasSupportedBy[userID][i+1:]...)+ break+ }+ }idea.TotalSupporters = idea.TotalSupporters - 1return nil}@@ -99,6 +117,7 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) error {for _, idea := range s.ideas {if idea.Number == number {+ idea.Status = statusidea.Response = &models.IdeaResponse{Text: text,User: &models.User{ID: userID},@@ -111,5 +130,5 @@ func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) e// SupportedBy returns a list of Idea ID supported by given userfunc (s *IdeaStorage) SupportedBy(userID int) ([]int, error) {- return make([]int, 0), nil+ return s.ideasSupportedBy[userID], nil}diff --git a/app/storage/postgres/idea_test.go b/app/storage/postgres/idea_test.goindex 2c376a1c4..b0df43247 100644--- a/app/storage/postgres/idea_test.go+++ b/app/storage/postgres/idea_test.go@@ -348,3 +348,23 @@ func TestIdeaStorage_RemoveSupporter_ClosedIdea(t *testing.T) {Expect(err).To(BeNil())Expect(dbIdea.TotalSupporters).To(Equal(1))}++func TestIdeaStorage_ListSupportedIdeas(t *testing.T) {+ RegisterTestingT(t)+ db, _ := dbx.New()+ db.Seed()+ defer db.Close()++ trx, _ := db.Begin()+ defer trx.Rollback()++ tenants := postgres.NewTenantStorage(trx)+ ideas := postgres.NewIdeaStorage(demoTenant(tenants), trx)+ idea1, _ := ideas.Add("My new idea", "with this description", 1)+ idea2, _ := ideas.Add("My other idea", "with better description", 1)+ ideas.AddSupporter(idea1.Number, 2)+ ideas.AddSupporter(idea2.Number, 2)++ Expect(ideas.SupportedBy(1)).To(Equal([]int{}))+ Expect(ideas.SupportedBy(2)).To(Equal([]int{idea1.ID, idea2.ID}))+}diff --git a/wercker.yml b/wercker.ymlindex 1d76379b5..6427904a8 100644--- a/wercker.yml+++ b/wercker.yml@@ -53,6 +53,7 @@ coverage:name: get dependenciescode: |go get github.com/joho/godotenv/cmd/godotenv+ go get -u github.com/dave/courtney# Publish to codecov.io- script: