2019-02-19 23:56:41 +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.
|
|
|
|
|
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/mock"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserDelete(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
users := mock.NewMockUserStore(controller)
|
|
|
|
users.EXPECT().FindLogin(gomock.Any(), mockUser.Login).Return(mockUser, nil)
|
|
|
|
users.EXPECT().Delete(gomock.Any(), mockUser).Return(nil)
|
|
|
|
|
2020-06-08 18:32:28 +00:00
|
|
|
transferer := mock.NewMockTransferer(controller)
|
|
|
|
transferer.EXPECT().Transfer(gomock.Any(), mockUser).Return(nil)
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
webhook := mock.NewMockWebhookSender(controller)
|
|
|
|
webhook.EXPECT().Send(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "octocat")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("DELETE", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
2020-06-08 18:32:28 +00:00
|
|
|
HandleDelete(users, transferer, webhook)(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
if got, want := w.Body.Len(), 0; want != got {
|
|
|
|
t.Errorf("Want response body size %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
if got, want := w.Code, 204; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserDelete_NotFound(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
users := mock.NewMockUserStore(controller)
|
|
|
|
users.EXPECT().FindLogin(gomock.Any(), mockUser.Login).Return(nil, sql.ErrNoRows)
|
|
|
|
|
|
|
|
webhook := mock.NewMockWebhookSender(controller)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "octocat")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("DELETE", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
2020-06-08 18:32:28 +00:00
|
|
|
HandleDelete(users, nil, webhook)(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
if got, want := w.Code, 404; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserDelete_InternalError(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
users := mock.NewMockUserStore(controller)
|
|
|
|
users.EXPECT().FindLogin(gomock.Any(), mockUser.Login).Return(mockUser, nil)
|
|
|
|
users.EXPECT().Delete(gomock.Any(), mockUser).Return(sql.ErrConnDone)
|
|
|
|
|
2020-06-08 18:32:28 +00:00
|
|
|
transferer := mock.NewMockTransferer(controller)
|
|
|
|
transferer.EXPECT().Transfer(gomock.Any(), mockUser).Return(nil)
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
webhook := mock.NewMockWebhookSender(controller)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "octocat")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("DELETE", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
2020-06-08 18:32:28 +00:00
|
|
|
HandleDelete(users, transferer, webhook)(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
if got, want := w.Code, http.StatusInternalServerError; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|