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"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
2020-02-07 17:19:32 +00:00
|
|
|
"github.com/drone/drone/mock"
|
2019-02-19 23:56:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
// var (
|
|
|
|
// mockUser = &core.User{
|
|
|
|
// Login: "octocat",
|
|
|
|
// }
|
|
|
|
|
|
|
|
// mockUsers = []*core.User{
|
|
|
|
// {
|
|
|
|
// Login: "octocat",
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // mockNotFound = &Error{
|
|
|
|
// // Message: "sql: no rows in result set",
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // mockBadRequest = &Error{
|
|
|
|
// // Message: "EOF",
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // mockInternalError = &Error{
|
|
|
|
// // Message: "database/sql: connection is already closed",
|
|
|
|
// // }
|
|
|
|
// )
|
|
|
|
|
|
|
|
func TestUserFind(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
users := mock.NewMockUserStore(controller)
|
|
|
|
users.EXPECT().FindLogin(gomock.Any(), mockUser.Login).Return(mockUser, nil)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "octocat")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleFind(users)(w, r)
|
|
|
|
if got, want := w.Code, 200; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want := &core.User{}, mockUser
|
|
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
|
|
t.Errorf(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:19:32 +00:00
|
|
|
func TestUserFindID(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
users := mock.NewMockUserStore(controller)
|
|
|
|
users.EXPECT().FindLogin(gomock.Any(), "1").Return(nil, sql.ErrNoRows)
|
|
|
|
users.EXPECT().Find(gomock.Any(), mockUser.ID).Return(mockUser, nil)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "1")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleFind(users)(w, r)
|
|
|
|
if got, want := w.Code, 200; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, want := &core.User{}, mockUser
|
|
|
|
json.NewDecoder(w.Body).Decode(got)
|
|
|
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
|
|
|
t.Errorf(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
func TestUserFindErr(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)
|
|
|
|
|
|
|
|
c := new(chi.Context)
|
|
|
|
c.URLParams.Add("user", "octocat")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r = r.WithContext(
|
|
|
|
context.WithValue(context.Background(), chi.RouteCtxKey, c),
|
|
|
|
)
|
|
|
|
|
|
|
|
HandleFind(users)(w, r)
|
|
|
|
if got, want := w.Code, 404; want != got {
|
|
|
|
t.Errorf("Want response code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|