2021-05-20 12:55:26 +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.
|
|
|
|
|
|
|
|
// +build !oss
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-06-02 13:11:18 +00:00
|
|
|
|
2021-05-20 12:55:26 +00:00
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/handler/api/errors"
|
|
|
|
"github.com/drone/drone/mock"
|
2021-06-02 13:11:18 +00:00
|
|
|
|
2021-05-20 12:55:26 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2021-06-02 13:17:42 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-05-20 12:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleCreate(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
|
|
|
templates.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
|
|
|
|
in := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(in).Encode(dummyTemplate)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", in)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleCreate(templates).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusOK; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:51:39 +00:00
|
|
|
func TestHandleCreate_NotValidTemplateExtensionName(t *testing.T) {
|
2021-05-20 12:55:26 +00:00
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
in := new(bytes.Buffer)
|
2021-08-31 14:51:39 +00:00
|
|
|
json.NewEncoder(in).Encode(&core.Template{Name: "my_template", Data: "my_data"})
|
2021-05-20 12:55:26 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", in)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleCreate(nil).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusBadRequest; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:51:39 +00:00
|
|
|
got, want := &errors.Error{}, &errors.Error{Message: "Template extension invalid. Must be yaml, starlark or jsonnet"}
|
2021-05-20 12:55:26 +00:00
|
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
|
|
t.Errorf(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleCreate_ValidationErrorData(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
in := new(bytes.Buffer)
|
2021-08-31 14:51:39 +00:00
|
|
|
json.NewEncoder(in).Encode(&core.Template{Name: "my_template.yml", Data: ""})
|
2021-05-20 12:55:26 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", in)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleCreate(nil).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusBadRequest; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want := &errors.Error{}, &errors.Error{Message: "No Template Data Provided"}
|
|
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
|
|
t.Errorf(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleCreate_BadRequest(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleCreate(nil).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusBadRequest; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want := &errors.Error{}, &errors.Error{Message: "EOF"}
|
|
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
|
|
t.Errorf(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleCreate_CreateError(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
|
|
|
templates.EXPECT().Create(gomock.Any(), gomock.Any()).Return(errors.ErrNotFound)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
in := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(in).Encode(dummyTemplate)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", in)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleCreate(templates).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusInternalServerError; 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)
|
|
|
|
}
|
|
|
|
}
|