Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase backend test coverage #148

Merged
merged 9 commits into from Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions Makefile
Expand Up @@ -10,9 +10,7 @@ test:
godotenv -f ${ENV_FILE} go test ./... -cover -p=1

coverage:
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 .
Expand Down
6 changes: 3 additions & 3 deletions app/actions/idea.go
Expand Up @@ -21,7 +21,7 @@ func (input *CreateNewIdea) Initialize() interface{} {

// IsAuthorized returns true if current user is authorized to perform this action
func (input *CreateNewIdea) IsAuthorized(user *models.User) bool {
return true
return user != nil
}

// Validate is current model is valid
Expand Down Expand Up @@ -52,7 +52,7 @@ func (input *AddNewComment) Initialize() interface{} {

// IsAuthorized returns true if current user is authorized to perform this action
func (input *AddNewComment) IsAuthorized(user *models.User) bool {
return true
return user != nil
}

// Validate is current model is valid
Expand All @@ -79,7 +79,7 @@ func (input *SetResponse) Initialize() interface{} {

// IsAuthorized returns true if current user is authorized to perform this action
func (input *SetResponse) IsAuthorized(user *models.User) bool {
return true
return user != nil && user.IsStaff()
}

// Validate is current model is valid
Expand Down
60 changes: 60 additions & 0 deletions 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))
}
9 changes: 0 additions & 9 deletions app/handlers/idea.go
Expand Up @@ -80,9 +80,6 @@ func IdeaDetails() web.HandlerFunc {
ideas := c.Services().Ideas
idea, err := ideas.GetByNumber(number)
if err != nil {
if err == app.ErrNotFound {
return c.NotFound()
}
return c.Failure(err)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
63 changes: 53 additions & 10 deletions app/handlers/idea_test.go
@@ -1,9 +1,12 @@
package handlers_test

import (
"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"
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -202,11 +205,51 @@ 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)

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))
}
16 changes: 15 additions & 1 deletion app/handlers/signin_test.go
Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions app/handlers/signup_test.go
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
49 changes: 49 additions & 0 deletions app/middlewares/jwt_test.go
Expand Up @@ -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)

Expand Down