181 lines
5.1 KiB
Go
181 lines
5.1 KiB
Go
// 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.
|
|
|
|
package builds
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/drone/drone/mock"
|
|
"github.com/drone/drone/handler/api/errors"
|
|
|
|
"github.com/go-chi/chi"
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestFind(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
repos := mock.NewMockRepositoryStore(controller)
|
|
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), mockRepo.Name).Return(mockRepo, nil)
|
|
|
|
builds := mock.NewMockBuildStore(controller)
|
|
builds.EXPECT().FindNumber(gomock.Any(), mockRepo.ID, mockBuild.Number).Return(mockBuild, nil)
|
|
|
|
stages := mock.NewMockStageStore(controller)
|
|
stages.EXPECT().ListSteps(gomock.Any(), mockBuild.ID).Return(mockStages, nil)
|
|
|
|
c := new(chi.Context)
|
|
c.URLParams.Add("owner", "octocat")
|
|
c.URLParams.Add("name", "hello-world")
|
|
c.URLParams.Add("number", "1")
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r = r.WithContext(
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
)
|
|
|
|
HandleFind(repos, builds, stages)(w, r)
|
|
|
|
if got, want := w.Code, 200; want != got {
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
}
|
|
|
|
got, want := &buildWithStages{}, &buildWithStages{mockBuild, mockStages}
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
t.Errorf(diff)
|
|
}
|
|
}
|
|
|
|
func TestFind_BadRequest(t *testing.T) {
|
|
c := new(chi.Context)
|
|
c.URLParams.Add("owner", "octocat")
|
|
c.URLParams.Add("name", "hello-world")
|
|
c.URLParams.Add("number", "one")
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r = r.WithContext(
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
)
|
|
|
|
HandleFind(nil, nil, nil)(w, r)
|
|
|
|
if got, want := w.Code, 400; want != got {
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
}
|
|
|
|
got, want := new(errors.Error), &errors.Error{Message: "strconv.ParseInt: parsing \"one\": invalid syntax"}
|
|
json.NewDecoder(w.Body).Decode(&got)
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
t.Errorf(diff)
|
|
}
|
|
}
|
|
|
|
func TestFind_RepoNotFound(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
repos := mock.NewMockRepositoryStore(controller)
|
|
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), mockRepo.Name).Return(nil, errors.ErrNotFound)
|
|
|
|
c := new(chi.Context)
|
|
c.URLParams.Add("owner", "octocat")
|
|
c.URLParams.Add("name", "hello-world")
|
|
c.URLParams.Add("number", "1")
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r = r.WithContext(
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
)
|
|
|
|
HandleFind(repos, nil, nil)(w, r)
|
|
|
|
if got, want := w.Code, 404; want != got {
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
}
|
|
|
|
got, want := new(errors.Error), errors.ErrNotFound
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
t.Errorf(diff)
|
|
}
|
|
}
|
|
|
|
func TestFind_BuildNotFound(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
repos := mock.NewMockRepositoryStore(controller)
|
|
builds := mock.NewMockBuildStore(controller)
|
|
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), mockRepo.Name).Return(mockRepo, nil)
|
|
builds.EXPECT().FindNumber(gomock.Any(), mockRepo.ID, mockBuild.Number).Return(nil, errors.ErrNotFound)
|
|
|
|
c := new(chi.Context)
|
|
c.URLParams.Add("owner", "octocat")
|
|
c.URLParams.Add("name", "hello-world")
|
|
c.URLParams.Add("number", "1")
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r = r.WithContext(
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
)
|
|
|
|
HandleFind(repos, builds, nil)(w, r)
|
|
|
|
if got, want := w.Code, 404; want != got {
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
}
|
|
|
|
got, want := new(errors.Error), errors.ErrNotFound
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
t.Errorf(diff)
|
|
}
|
|
}
|
|
|
|
func TestFind_StagesNotFound(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
repos := mock.NewMockRepositoryStore(controller)
|
|
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), mockRepo.Name).Return(mockRepo, nil)
|
|
|
|
builds := mock.NewMockBuildStore(controller)
|
|
builds.EXPECT().FindNumber(gomock.Any(), mockRepo.ID, mockBuild.Number).Return(mockBuild, nil)
|
|
|
|
stages := mock.NewMockStageStore(controller)
|
|
stages.EXPECT().ListSteps(gomock.Any(), mockBuild.ID).Return(nil, errors.ErrNotFound)
|
|
|
|
c := new(chi.Context)
|
|
c.URLParams.Add("owner", "octocat")
|
|
c.URLParams.Add("name", "hello-world")
|
|
c.URLParams.Add("number", "1")
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r = r.WithContext(
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
)
|
|
|
|
HandleFind(repos, builds, stages)(w, r)
|
|
if got, want := w.Code, 500; want != got {
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
}
|
|
|
|
got, want := new(errors.Error), errors.ErrNotFound
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
t.Errorf(diff)
|
|
}
|
|
}
|