1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/badge_test.go

105 lines
2.9 KiB
Go
Raw Normal View History

2015-04-23 23:56:26 +02:00
package server
import (
2015-05-12 19:06:00 +02:00
"database/sql"
2015-04-28 23:42:09 +02:00
"encoding/xml"
2015-04-23 23:56:26 +02:00
"net/http"
2015-04-28 23:42:09 +02:00
"net/url"
2015-04-23 23:56:26 +02:00
"testing"
"github.com/drone/drone/common"
2015-04-28 23:42:09 +02:00
"github.com/drone/drone/common/ccmenu"
"github.com/drone/drone/pkg/store/mock"
2015-05-01 06:20:59 +02:00
"github.com/drone/drone/server/recorder"
2015-05-12 19:06:00 +02:00
2015-04-23 23:56:26 +02:00
. "github.com/franela/goblin"
"github.com/gin-gonic/gin"
2015-05-12 19:06:00 +02:00
"github.com/stretchr/testify/mock"
2015-04-23 23:56:26 +02:00
)
var badgeTests = []struct {
2015-05-12 19:06:00 +02:00
branch string
badge []byte
state string
activity string
status string
err error
}{
2015-05-12 19:06:00 +02:00
{"", badgeSuccess, common.StateSuccess, "Sleeping", "Success", nil},
{"master", badgeSuccess, common.StateSuccess, "Sleeping", "Success", nil},
{"", badgeStarted, common.StateRunning, "Building", "Unknown", nil},
{"", badgeError, common.StateError, "Sleeping", "Exception", nil},
{"", badgeError, common.StateKilled, "Sleeping", "Exception", nil},
{"", badgeFailure, common.StateFailure, "Sleeping", "Failure", nil},
{"", badgeNone, "", "", "", sql.ErrNoRows},
}
2015-04-23 23:56:26 +02:00
func TestBadges(t *testing.T) {
store := new(mocks.Store)
url_, _ := url.Parse("http://localhost:8080")
2015-04-23 23:56:26 +02:00
g := Goblin(t)
g.Describe("Badges", func() {
2015-04-28 23:42:09 +02:00
2015-05-01 07:48:15 +02:00
g.It("should serve svg badges", func() {
for _, test := range badgeTests {
rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw}
2015-05-12 19:06:00 +02:00
ctx.Request = &http.Request{
Form: url.Values{},
}
if len(test.branch) != 0 {
ctx.Request.Form.Set("branch", test.branch)
2015-04-28 23:42:09 +02:00
}
2015-04-23 23:56:26 +02:00
2015-05-12 19:06:00 +02:00
repo := &common.Repo{FullName: "foo/bar"}
ctx.Set("datastore", store)
ctx.Set("repo", repo)
2015-04-23 23:56:26 +02:00
2015-05-12 19:06:00 +02:00
commit := &common.Commit{State: test.state}
store.On("CommitLast", repo, test.branch).Return(commit, test.err).Once()
GetBadge(ctx)
2015-05-12 19:06:00 +02:00
g.Assert(rw.Code).Equal(200)
g.Assert(rw.Body.Bytes()).Equal(test.badge)
g.Assert(rw.HeaderMap.Get("Content-Type")).Equal("image/svg+xml")
2015-04-28 23:42:09 +02:00
}
})
2015-04-28 23:42:09 +02:00
g.It("should serve ccmenu xml", func() {
2015-04-23 23:56:26 +02:00
for _, test := range badgeTests {
rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw}
ctx.Request = &http.Request{URL: url_}
2015-04-28 23:42:09 +02:00
repo := &common.Repo{FullName: "foo/bar"}
ctx.Set("datastore", store)
ctx.Set("repo", repo)
2015-04-28 23:42:09 +02:00
2015-05-12 19:06:00 +02:00
commits := []*common.Commit{
&common.Commit{State: test.state},
}
store.On("CommitList", repo, mock.AnythingOfType("int"), mock.AnythingOfType("int")).Return(commits, test.err).Once()
GetCC(ctx)
2015-04-28 23:42:09 +02:00
// in an error scenario (ie no build exists) we should
// return a 404 not found error.
if test.err != nil {
g.Assert(rw.Status()).Equal(404)
continue
2015-04-28 23:42:09 +02:00
}
// else parse the CCMenu xml output and verify
// it matches the expected values.
cc := &ccmenu.CCProjects{}
xml.Unmarshal(rw.Body.Bytes(), cc)
2015-05-12 19:06:00 +02:00
g.Assert(rw.Code).Equal(200)
g.Assert(cc.Project.Activity).Equal(test.activity)
g.Assert(cc.Project.LastBuildStatus).Equal(test.status)
g.Assert(rw.HeaderMap.Get("Content-Type")).Equal("application/xml; charset=utf-8")
2015-04-28 23:42:09 +02:00
}
})
2015-04-23 23:56:26 +02:00
})
}