2021-05-20 12:55:45 +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 (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-06-01 09:29:58 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 12:55:45 +00:00
|
|
|
"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:45 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleFind(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
template := mock.NewMockTemplateStore(controller)
|
2021-05-28 15:59:00 +00:00
|
|
|
template.EXPECT().FindName(gomock.Any(), dummyTemplate.Name, dummyTemplate.Namespace).Return(dummyTemplate, nil)
|
2021-05-20 12:55:45 +00:00
|
|
|
|
|
|
|
c := new(chi.Context)
|
2021-08-31 14:51:39 +00:00
|
|
|
c.URLParams.Add("name", "my_template.yml")
|
2021-05-28 15:59:00 +00:00
|
|
|
c.URLParams.Add("namespace", "my_org")
|
2021-05-20 12:55:45 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleFind(template).ServeHTTP(w, r)
|
|
|
|
if got, want := w.Code, http.StatusOK; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleFind_TemplateNotFound(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
template := mock.NewMockTemplateStore(controller)
|
2021-05-28 15:59:00 +00:00
|
|
|
template.EXPECT().FindName(gomock.Any(), dummyTemplate.Name, dummyTemplate.Namespace).Return(nil, errors.ErrNotFound)
|
2021-05-20 12:55:45 +00:00
|
|
|
|
|
|
|
c := new(chi.Context)
|
2021-08-31 14:51:39 +00:00
|
|
|
c.URLParams.Add("name", "my_template.yml")
|
2021-05-28 15:59:00 +00:00
|
|
|
c.URLParams.Add("namespace", "my_org")
|
2021-05-20 12:55:45 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleFind(template).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)
|
|
|
|
}
|
|
|
|
}
|