// 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 crons import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "testing" "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" ) func TestHandleUpdate(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() mockCron := new(core.Cron) *mockCron = *dummyCron mockCron.Disabled = false mockCron.Branch = "develop" mockCron.Target = "staging" repos := mock.NewMockRepositoryStore(controller) repos.EXPECT().FindName(gomock.Any(), dummyCronRepo.Namespace, dummyCronRepo.Name).Return(dummyCronRepo, nil) crons := mock.NewMockCronStore(controller) crons.EXPECT().FindName(gomock.Any(), dummyCronRepo.ID, mockCron.Name).Return(mockCron, nil) crons.EXPECT().Update(gomock.Any(), mockCron).Return(nil) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") c.URLParams.Add("cron", "nightly") in := new(bytes.Buffer) json.NewEncoder(in).Encode(mockCron) w := httptest.NewRecorder() r := httptest.NewRequest("POST", "/", in) r = r.WithContext( context.WithValue(context.Background(), chi.RouteCtxKey, c), ) HandleUpdate(repos, crons).ServeHTTP(w, r) if got, want := w.Code, http.StatusOK; want != got { t.Errorf("Want response code %d, got %d", want, got) } got, want := &core.Cron{}, mockCron json.NewDecoder(w.Body).Decode(got) if diff := cmp.Diff(got, want); len(diff) != 0 { t.Errorf(diff) } } func TestHandleUpdate_RepoNotFound(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() repos := mock.NewMockRepositoryStore(controller) repos.EXPECT().FindName(gomock.Any(), dummyCronRepo.Namespace, dummyCronRepo.Name).Return(nil, errors.ErrNotFound) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") c.URLParams.Add("cron", "nightly") w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", nil) r = r.WithContext( context.WithValue(context.Background(), chi.RouteCtxKey, c), ) HandleUpdate(repos, nil).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) } } func TestHandleUpdate_CronNotFound(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() repos := mock.NewMockRepositoryStore(controller) repos.EXPECT().FindName(gomock.Any(), dummyCronRepo.Namespace, dummyCronRepo.Name).Return(dummyCronRepo, nil) crons := mock.NewMockCronStore(controller) crons.EXPECT().FindName(gomock.Any(), dummyCronRepo.ID, dummyCron.Name).Return(nil, errors.ErrNotFound) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") c.URLParams.Add("cron", "nightly") w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", nil) r = r.WithContext( context.WithValue(context.Background(), chi.RouteCtxKey, c), ) HandleUpdate(repos, crons).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) } } func TestHandleUpdate_UpdateError(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() repos := mock.NewMockRepositoryStore(controller) repos.EXPECT().FindName(gomock.Any(), dummyCronRepo.Namespace, dummyCronRepo.Name).Return(dummyCronRepo, nil) crons := mock.NewMockCronStore(controller) crons.EXPECT().FindName(gomock.Any(), dummyCronRepo.ID, dummyCron.Name).Return(dummyCron, nil) crons.EXPECT().Update(gomock.Any(), dummyCron).Return(errors.ErrNotFound) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") c.URLParams.Add("cron", "nightly") w := httptest.NewRecorder() r := httptest.NewRequest("POST", "/", nil) r = r.WithContext( context.WithValue(context.Background(), chi.RouteCtxKey, c), ) HandleUpdate(repos, crons).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) } }