// 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" "net/http/httptest" "testing" "github.com/drone/drone/handler/api/errors" "github.com/drone/drone/handler/api/request" "github.com/drone/drone/mock" "github.com/go-chi/chi" "github.com/golang/mock/gomock" "github.com/google/go-cmp/cmp" ) func TestPurge(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().Purge(gomock.Any(), mockRepo.ID, int64(50)).Return(nil) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") w := httptest.NewRecorder() r := httptest.NewRequest("DELETE", "/?before=50", nil) r = r.WithContext( context.WithValue(request.WithUser(r.Context(), mockUser), chi.RouteCtxKey, c), ) HandlePurge(repos, builds)(w, r) if got, want := w.Code, http.StatusNoContent; want != got { t.Errorf("Want response code %d, got %d", want, got) } } // The test verifies that a 404 Not Found error is returned // if the repository store returns an error. func TestPurge_NotFound(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") w := httptest.NewRecorder() r := httptest.NewRequest("DELETE", "/?before=50", nil) r = r.WithContext( context.WithValue(request.WithUser(r.Context(), mockUser), chi.RouteCtxKey, c), ) HandlePurge(repos, 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) } } // The test verifies that a 400 Bad Request error is returned // if the user provides an invalid ?before query parameter // that cannot be parsed. func TestPurge_BadRequest(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") w := httptest.NewRecorder() r := httptest.NewRequest("DELETE", "/?before=XLII", nil) r = r.WithContext( context.WithValue(request.WithUser(r.Context(), mockUser), chi.RouteCtxKey, c), ) HandlePurge(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 "XLII": invalid syntax`, } json.NewDecoder(w.Body).Decode(got) if diff := cmp.Diff(got, want); len(diff) != 0 { t.Errorf(diff) } } // The test verifies that a 500 Internal server error is // returned if the database purge transaction fails. func TestPurge_InternalError(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().Purge(gomock.Any(), mockRepo.ID, int64(50)).Return(errors.ErrNotFound) c := new(chi.Context) c.URLParams.Add("owner", "octocat") c.URLParams.Add("name", "hello-world") w := httptest.NewRecorder() r := httptest.NewRequest("DELETE", "/?before=50", nil) r = r.WithContext( context.WithValue(request.WithUser(r.Context(), mockUser), chi.RouteCtxKey, c), ) HandlePurge(repos, builds)(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) } }