diff --git a/core/template.go b/core/template.go index d29fb227..803fa0d3 100644 --- a/core/template.go +++ b/core/template.go @@ -22,8 +22,6 @@ import ( var ( errTemplateNameInvalid = errors.New("No Template Name Provided") errTemplateDataInvalid = errors.New("No Template Data Provided") - //errTemplateCreatedInvalid = errors.New("Invalid Template Created Value") - //errTemplateUpdatedInvalid = errors.New("Invalid Template Updated Value") ) type ( @@ -36,7 +34,7 @@ type ( Template struct { Id int64 `json:"id,omitempty"` Name string `json:"name,omitempty"` - Data string `json:"data,omitempty"` + Data []byte `json:"data,omitempty"` Created int64 `json:"created,omitempty"` Updated int64 `json:"updated,omitempty"` } @@ -70,10 +68,6 @@ func (s *Template) Validate() error { return errTemplateNameInvalid case len(s.Data) == 0: return errTemplateDataInvalid - //case s.Created == 0: - // return errTemplateCreatedInvalid - //case s.Updated == 0: - // return errTemplateUpdatedInvalid default: return nil } diff --git a/handler/api/template/create.go b/handler/api/template/create.go index 369a1116..34a81645 100644 --- a/handler/api/template/create.go +++ b/handler/api/template/create.go @@ -14,10 +14,8 @@ import ( ) type templateInput struct { - Name string `json:"name"` - Data string `json:"data"` - Created int64 `json:"created"` - Updated int64 `json:"updated"` + Name string `json:"name"` + Data []byte `json:"data"` } // HandleCreate returns an http.HandlerFunc that processes http @@ -32,10 +30,8 @@ func HandleCreate(secrets core.TemplateStore) http.HandlerFunc { } t := &core.Template{ - Name: in.Name, - Data: in.Data, - Created: in.Created, - Updated: in.Updated, + Name: in.Name, + Data: in.Data, } err = t.Validate() diff --git a/handler/api/template/create_test.go b/handler/api/template/create_test.go index 1a27e1a8..0266154b 100644 --- a/handler/api/template/create_test.go +++ b/handler/api/template/create_test.go @@ -51,7 +51,7 @@ func TestHandleCreate_ValidationErrorName(t *testing.T) { c := new(chi.Context) in := new(bytes.Buffer) - json.NewEncoder(in).Encode(&core.Template{Name: "", Data: "my_data"}) + json.NewEncoder(in).Encode(&core.Template{Name: "", Data: []byte("my_data")}) w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", in) @@ -77,7 +77,7 @@ func TestHandleCreate_ValidationErrorData(t *testing.T) { c := new(chi.Context) in := new(bytes.Buffer) - json.NewEncoder(in).Encode(&core.Template{Name: "my_template", Data: ""}) + json.NewEncoder(in).Encode(&core.Template{Name: "my_template", Data: nil}) w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", in) diff --git a/handler/api/template/list_test.go b/handler/api/template/list_test.go index 79a05e75..6cb55628 100644 --- a/handler/api/template/list_test.go +++ b/handler/api/template/list_test.go @@ -6,12 +6,24 @@ package template -import "github.com/drone/drone/core" +import ( + "context" + "encoding/json" + "github.com/drone/drone/core" + "github.com/drone/drone/handler/api/errors" + "github.com/drone/drone/mock" + "github.com/go-chi/chi" + "github.com/golang/mock/gomock" + "github.com/google/go-cmp/cmp" + "net/http" + "net/http/httptest" + "testing" +) var ( dummyTemplate = &core.Template{ Name: "my_template", - Data: "my_data", + Data: []byte("my_data"), Created: 1, Updated: 2, } @@ -19,3 +31,51 @@ var ( dummyTemplate, } ) + +func TestHandleList(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + + templates := mock.NewMockTemplateStore(controller) + templates.EXPECT().ListAll(gomock.Any()).Return(dummyTemplateList, nil) + + c := new(chi.Context) + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + r = r.WithContext( + context.WithValue(context.Background(), chi.RouteCtxKey, c), + ) + + HandleList(templates).ServeHTTP(w, r) + if got, want := w.Code, http.StatusOK; want != got { + t.Errorf("Want response code %d, got %d", want, got) + } +} + +func TestHandleList_TemplateListErr(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + + templates := mock.NewMockTemplateStore(controller) + templates.EXPECT().ListAll(gomock.Any()).Return(nil, errors.ErrNotFound) + + c := new(chi.Context) + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + r = r.WithContext( + context.WithValue(context.Background(), chi.RouteCtxKey, c), + ) + + HandleList(templates).ServeHTTP(w, r) + if got, want := w.Code, http.StatusNotFound; 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) + } +} diff --git a/handler/api/template/update.go b/handler/api/template/update.go index 43795ef8..f1879783 100644 --- a/handler/api/template/update.go +++ b/handler/api/template/update.go @@ -13,7 +13,7 @@ import ( ) type templateUpdate struct { - Data *string `json:"data"` + Data *[]byte `json:"data"` Updated *int64 `json:"Updated"` } diff --git a/handler/api/template/update_test.go b/handler/api/template/update_test.go index 6036eb8c..3396400b 100644 --- a/handler/api/template/update_test.go +++ b/handler/api/template/update_test.go @@ -121,7 +121,7 @@ func TestHandleUpdate_UpdateError(t *testing.T) { c.URLParams.Add("name", "my_template") in := new(bytes.Buffer) - json.NewEncoder(in).Encode(&core.Template{Data: "my_data"}) + json.NewEncoder(in).Encode(&core.Template{Data: []byte("my_data")}) w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", in) diff --git a/store/shared/migrate/sqlite/files/015_create_template_table.sql b/store/shared/migrate/sqlite/files/015_create_template_table.sql index b9b88315..85262dcf 100644 --- a/store/shared/migrate/sqlite/files/015_create_template_table.sql +++ b/store/shared/migrate/sqlite/files/015_create_template_table.sql @@ -1,7 +1,7 @@ -- name: create-table-template CREATE TABLE IF NOT EXISTS template ( - template_id INTEGER PRIMARY KEY AUTOINCREMENT + template_id INTEGER PRIMARY KEY AUTOINCREMENT ,template_name TEXT UNIQUE ,template_data BLOB ,template_created INTEGER diff --git a/store/template/template.go b/store/template/template.go index c6c1f2fd..969314de 100644 --- a/store/template/template.go +++ b/store/template/template.go @@ -8,7 +8,6 @@ package template import ( "context" - "github.com/drone/drone/core" "github.com/drone/drone/store/shared/db" ) @@ -168,14 +167,12 @@ ORDER BY template_name const stmtInsert = ` INSERT INTO template ( - template_id -,template_name + template_name ,template_data ,template_created ,template_updated ) VALUES ( - :template_id -,:template_name + :template_name ,:template_data ,:template_created ,:template_updated diff --git a/store/template/template_test.go b/store/template/template_test.go index 41d8abd4..790571e8 100644 --- a/store/template/template_test.go +++ b/store/template/template_test.go @@ -7,6 +7,7 @@ package template import ( + "bytes" "context" "database/sql" "github.com/drone/drone/core" @@ -36,7 +37,7 @@ func testTemplateCreate(store *templateStore) func(t *testing.T) { item := &core.Template{ Id: 1, Name: "my_template", - Data: "some_template_data", + Data: []byte("some_template_data"), Created: 1, Updated: 2, } @@ -83,7 +84,7 @@ func testTemplate(item *core.Template) func(t *testing.T) { if got, want := item.Name, "my_template"; got != want { t.Errorf("Want template name %q, got %q", want, got) } - if got, want := item.Data, "some_template_data"; got != want { + if got, want := item.Data, []byte("some_template_data"); bytes.Compare(got, want) != 0 { t.Errorf("Want template data %q, got %q", want, got) } }