harness-drone/handler/api/builds/builds_test.go

76 lines
1.8 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
2019-03-13 21:47:47 +00:00
// +build !oss
2019-02-19 23:56:41 +00:00
package builds
import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"
2019-03-13 21:47:47 +00:00
"github.com/drone/drone/core"
2019-02-19 23:56:41 +00:00
"github.com/drone/drone/handler/api/errors"
"github.com/drone/drone/mock"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/sirupsen/logrus"
)
func init() {
logrus.SetOutput(ioutil.Discard)
}
func TestHandleBuilds(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
want := []*core.Repository{
{ID: 1, Slug: "octocat/hello-world"},
{ID: 2, Slug: "octocat/spoon-fork"},
}
repos := mock.NewMockRepositoryStore(controller)
repos.EXPECT().ListIncomplete(gomock.Any()).Return(want, nil)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
HandleIncomplete(repos)(w, r)
if got, want := w.Code, 200; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
got := []*core.Repository{}
json.NewDecoder(w.Body).Decode(&got)
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Errorf(diff)
}
}
func TestHandleBuilds_Error(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
repos := mock.NewMockRepositoryStore(controller)
repos.EXPECT().ListIncomplete(gomock.Any()).Return(nil, errors.ErrNotFound)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
HandleIncomplete(repos)(w, r)
if got, want := w.Code, 500; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
got, want := &errors.Error{}, errors.ErrNotFound
json.NewDecoder(w.Body).Decode(got)
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Errorf(diff)
}
}