diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-01-26 20:10:18 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-01-26 20:25:32 +0300 |
commit | f4abddf5dce3f96b7e25ab76135feffdc9f5f986 (patch) | |
tree | 9efd56700cd12ba2ef80294edddff18756599e87 /internal/handlers | |
parent | ffb818c4aeb5859e243f850fb1482b5fdeba008b (diff) |
test: move mocks to their own package
Diffstat (limited to 'internal/handlers')
-rw-r--r-- | internal/handlers/handlers_test.go | 24 | ||||
-rw-r--r-- | internal/handlers/mock/handler_mock.go | 129 |
2 files changed, 141 insertions, 12 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index dd940251..22027ad0 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -10,19 +10,19 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitlab-pages/internal/mocks" + "gitlab.com/gitlab-org/gitlab-pages/internal/handlers/mock" ) func TestNotHandleArtifactRequestReturnsFalse(t *testing.T) { mockCtrl := gomock.NewController(t) - mockArtifact := mocks.NewMockArtifact(mockCtrl) + mockArtifact := mock.NewMockArtifact(mockCtrl) mockArtifact.EXPECT(). TryMakeRequest(gomock.Any(), gomock.Any(), gomock.Any(), "", gomock.Any()). Return(false). Times(1) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT(). GetTokenIfExists(gomock.Any(), gomock.Any()). Return("", nil). @@ -41,13 +41,13 @@ func TestNotHandleArtifactRequestReturnsFalse(t *testing.T) { func TestHandleArtifactRequestedReturnsTrue(t *testing.T) { mockCtrl := gomock.NewController(t) - mockArtifact := mocks.NewMockArtifact(mockCtrl) + mockArtifact := mock.NewMockArtifact(mockCtrl) mockArtifact.EXPECT(). TryMakeRequest(gomock.Any(), gomock.Any(), gomock.Any(), "", gomock.Any()). Return(true). Times(1) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT(). GetTokenIfExists(gomock.Any(), gomock.Any()). Return("", nil). @@ -64,7 +64,7 @@ func TestHandleArtifactRequestedReturnsTrue(t *testing.T) { func TestNotFoundWithTokenIsNotHandled(t *testing.T) { mockCtrl := gomock.NewController(t) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT().CheckResponseForInvalidToken(gomock.Any(), gomock.Any(), gomock.Any()). Return(false) @@ -101,7 +101,7 @@ func TestForbiddenWithTokenIsNotHandled(t *testing.T) { t.Run(tn, func(t *testing.T) { mockCtrl := gomock.NewController(t) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) if tc.Token == "" { mockAuth.EXPECT().IsAuthSupported().Return(true) mockAuth.EXPECT().RequireAuth(gomock.Any(), gomock.Any()).Return(true) @@ -125,7 +125,7 @@ func TestForbiddenWithTokenIsNotHandled(t *testing.T) { func TestNotFoundWithoutTokenIsNotHandledWhenNotAuthSupport(t *testing.T) { mockCtrl := gomock.NewController(t) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT().IsAuthSupported().Return(false) handlers := New(mockAuth, nil) @@ -140,7 +140,7 @@ func TestNotFoundWithoutTokenIsNotHandledWhenNotAuthSupport(t *testing.T) { func TestNotFoundWithoutTokenIsHandled(t *testing.T) { mockCtrl := gomock.NewController(t) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT().IsAuthSupported().Return(true) mockAuth.EXPECT().RequireAuth(gomock.Any(), gomock.Any()).Times(1).Return(true) @@ -156,7 +156,7 @@ func TestNotFoundWithoutTokenIsHandled(t *testing.T) { func TestInvalidTokenResponseIsHandled(t *testing.T) { mockCtrl := gomock.NewController(t) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT().CheckResponseForInvalidToken(gomock.Any(), gomock.Any(), gomock.Any()). Return(true) @@ -173,12 +173,12 @@ func TestInvalidTokenResponseIsHandled(t *testing.T) { func TestHandleArtifactRequestButGetTokenFails(t *testing.T) { mockCtrl := gomock.NewController(t) - mockArtifact := mocks.NewMockArtifact(mockCtrl) + mockArtifact := mock.NewMockArtifact(mockCtrl) mockArtifact.EXPECT(). TryMakeRequest(gomock.Any(), gomock.Any(), gomock.Any(), "", gomock.Any()). Times(0) - mockAuth := mocks.NewMockAuth(mockCtrl) + mockAuth := mock.NewMockAuth(mockCtrl) mockAuth.EXPECT().GetTokenIfExists(gomock.Any(), gomock.Any()).Return("", errors.New("error when retrieving token")) handlers := New(mockAuth, mockArtifact) diff --git a/internal/handlers/mock/handler_mock.go b/internal/handlers/mock/handler_mock.go new file mode 100644 index 00000000..11548221 --- /dev/null +++ b/internal/handlers/mock/handler_mock.go @@ -0,0 +1,129 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/interface.go + +// Package mock is a generated GoMock package. +package mock + +import ( + http "net/http" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockArtifact is a mock of Artifact interface. +type MockArtifact struct { + ctrl *gomock.Controller + recorder *MockArtifactMockRecorder +} + +// MockArtifactMockRecorder is the mock recorder for MockArtifact. +type MockArtifactMockRecorder struct { + mock *MockArtifact +} + +// NewMockArtifact creates a new mock instance. +func NewMockArtifact(ctrl *gomock.Controller) *MockArtifact { + mock := &MockArtifact{ctrl: ctrl} + mock.recorder = &MockArtifactMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockArtifact) EXPECT() *MockArtifactMockRecorder { + return m.recorder +} + +// TryMakeRequest mocks base method. +func (m *MockArtifact) TryMakeRequest(host string, w http.ResponseWriter, r *http.Request, token string, responseHandler func(*http.Response) bool) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TryMakeRequest", host, w, r, token, responseHandler) + ret0, _ := ret[0].(bool) + return ret0 +} + +// TryMakeRequest indicates an expected call of TryMakeRequest. +func (mr *MockArtifactMockRecorder) TryMakeRequest(host, w, r, token, responseHandler interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TryMakeRequest", reflect.TypeOf((*MockArtifact)(nil).TryMakeRequest), host, w, r, token, responseHandler) +} + +// MockAuth is a mock of Auth interface. +type MockAuth struct { + ctrl *gomock.Controller + recorder *MockAuthMockRecorder +} + +// MockAuthMockRecorder is the mock recorder for MockAuth. +type MockAuthMockRecorder struct { + mock *MockAuth +} + +// NewMockAuth creates a new mock instance. +func NewMockAuth(ctrl *gomock.Controller) *MockAuth { + mock := &MockAuth{ctrl: ctrl} + mock.recorder = &MockAuthMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAuth) EXPECT() *MockAuthMockRecorder { + return m.recorder +} + +// CheckResponseForInvalidToken mocks base method. +func (m *MockAuth) CheckResponseForInvalidToken(w http.ResponseWriter, r *http.Request, resp *http.Response) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CheckResponseForInvalidToken", w, r, resp) + ret0, _ := ret[0].(bool) + return ret0 +} + +// CheckResponseForInvalidToken indicates an expected call of CheckResponseForInvalidToken. +func (mr *MockAuthMockRecorder) CheckResponseForInvalidToken(w, r, resp interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckResponseForInvalidToken", reflect.TypeOf((*MockAuth)(nil).CheckResponseForInvalidToken), w, r, resp) +} + +// GetTokenIfExists mocks base method. +func (m *MockAuth) GetTokenIfExists(w http.ResponseWriter, r *http.Request) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetTokenIfExists", w, r) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetTokenIfExists indicates an expected call of GetTokenIfExists. +func (mr *MockAuthMockRecorder) GetTokenIfExists(w, r interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTokenIfExists", reflect.TypeOf((*MockAuth)(nil).GetTokenIfExists), w, r) +} + +// IsAuthSupported mocks base method. +func (m *MockAuth) IsAuthSupported() bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsAuthSupported") + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsAuthSupported indicates an expected call of IsAuthSupported. +func (mr *MockAuthMockRecorder) IsAuthSupported() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsAuthSupported", reflect.TypeOf((*MockAuth)(nil).IsAuthSupported)) +} + +// RequireAuth mocks base method. +func (m *MockAuth) RequireAuth(w http.ResponseWriter, r *http.Request) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RequireAuth", w, r) + ret0, _ := ret[0].(bool) + return ret0 +} + +// RequireAuth indicates an expected call of RequireAuth. +func (mr *MockAuthMockRecorder) RequireAuth(w, r interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RequireAuth", reflect.TypeOf((*MockAuth)(nil).RequireAuth), w, r) +} |