From b3b26c0484b1e5cec8069c4610f15f2209a49dee Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 18 Jun 2018 23:41:08 -0500 Subject: [PATCH] add avatar proxy mock to test provider --- app/rest/auth/provider.go | 2 +- app/rest/auth/provider_test.go | 21 +++++++--- app/rest/proxy/avatar_store.go | 2 + app/rest/proxy/avatar_store_mock.go | 61 +++++++++++++++++++++++++++++ app/rest/proxy/avatar_store_test.go | 2 +- app/store/engine/bolt_accessor.go | 3 +- 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 app/rest/proxy/avatar_store_mock.go diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index fa55f23a..02698904 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -189,7 +189,7 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, &u) } -// alterUser sets fileds not handled by provider's MapUser, things like avatar, admin, verified +// alterUser sets fields not handled by provider's MapUser, things like avatar, admin, verified func (p Provider) alterUser(u store.User, oauthClaims *CustomClaims) store.User { if p.AvatarProxy != nil { if avatarURL, e := p.AvatarProxy.Put(u); e == nil { diff --git a/app/rest/auth/provider_test.go b/app/rest/auth/provider_test.go index ae42865b..ba347e9f 100644 --- a/app/rest/auth/provider_test.go +++ b/app/rest/auth/provider_test.go @@ -12,9 +12,11 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "golang.org/x/oauth2" + "github.com/umputun/remark/app/rest/proxy" "github.com/umputun/remark/app/store" ) @@ -49,7 +51,7 @@ func TestLogin(t *testing.T) { u := store.User{} err = json.Unmarshal(body, &u) assert.Nil(t, err) - assert.Equal(t, store.User{Name: "blah", ID: "mock_myuser1", Picture: "http://exmple.com/pic1.png", + assert.Equal(t, store.User{Name: "blah", ID: "mock_myuser1", Picture: "/v1/avatar/23/pic1.png", Admin: false, Blocked: false, IP: ""}, u) // check admin user @@ -60,7 +62,7 @@ func TestLogin(t *testing.T) { assert.Nil(t, err) err = json.Unmarshal(body, &u) assert.Nil(t, err) - assert.Equal(t, store.User{Name: "blah", ID: "mock_myuser2", Picture: "http://exmple.com/pic1.png", + assert.Equal(t, store.User{Name: "blah", ID: "mock_myuser2", Picture: "/v1/avatar/23/pic1.png", Admin: true, Blocked: false, IP: "", Verified: true}, u) } @@ -158,8 +160,13 @@ func mockProvider(t *testing.T, loginPort, authPort int) (*http.Server, *http.Se return userInfo }, } + + mockAvatarStore := proxy.MockAvatarStore{} + mockAvatarStore.On("Put", mock.Anything, mock.Anything).Return("23/pic1.png", nil) + params := Params{RemarkURL: "url", SecretKey: "123456", Cid: "cid", Csecret: "csecret", JwtService: NewJWT("12345", false, time.Hour), Admins: []string{"mock_myuser2"}, + AvatarProxy: &proxy.Avatar{Store: &mockAvatarStore, RoutePath: "/v1/avatar"}, IsVerifiedFn: func(siteID, userID string) bool { return userID == "mock_myuser2" }} provider = initProvider(params, provider) @@ -189,7 +196,8 @@ func mockProvider(t *testing.T, loginPort, authPort int) (*http.Server, *http.Se }` w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(200) - w.Write([]byte(res)) + _, err := w.Write([]byte(res)) + assert.NoError(t, err) case strings.HasPrefix(r.URL.Path, "/user"): res := fmt.Sprintf(`{ "id": "%s", @@ -199,15 +207,16 @@ func mockProvider(t *testing.T, loginPort, authPort int) (*http.Server, *http.Se count++ w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(200) - w.Write([]byte(res)) + _, err := w.Write([]byte(res)) + assert.NoError(t, err) default: t.Fatalf("unexpected oauth request %s %s", r.Method, r.URL) } }), } - go oauth.ListenAndServe() - go ts.ListenAndServe() + go func() { _ = oauth.ListenAndServe() }() + go func() { _ = ts.ListenAndServe() }() time.Sleep(time.Millisecond * 100) // let them start return ts, oauth diff --git a/app/rest/proxy/avatar_store.go b/app/rest/proxy/avatar_store.go index 23653e40..22b3bc63 100644 --- a/app/rest/proxy/avatar_store.go +++ b/app/rest/proxy/avatar_store.go @@ -1,5 +1,7 @@ package proxy +//go:generate sh -c "mockery -inpkg -name AvatarStore -print > /tmp/mock.tmp && mv /tmp/mock.tmp avatar_store_mock.go" + import ( "bytes" "fmt" diff --git a/app/rest/proxy/avatar_store_mock.go b/app/rest/proxy/avatar_store_mock.go new file mode 100644 index 00000000..c12762fb --- /dev/null +++ b/app/rest/proxy/avatar_store_mock.go @@ -0,0 +1,61 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. +package proxy + +import io "io" +import mock "github.com/stretchr/testify/mock" + +// MockAvatarStore is an autogenerated mock type for the AvatarStore type +type MockAvatarStore struct { + mock.Mock +} + +// Get provides a mock function with given fields: avatar +func (_m *MockAvatarStore) Get(avatar string) (io.ReadCloser, int, error) { + ret := _m.Called(avatar) + + var r0 io.ReadCloser + if rf, ok := ret.Get(0).(func(string) io.ReadCloser); ok { + r0 = rf(avatar) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(io.ReadCloser) + } + } + + var r1 int + if rf, ok := ret.Get(1).(func(string) int); ok { + r1 = rf(avatar) + } else { + r1 = ret.Get(1).(int) + } + + var r2 error + if rf, ok := ret.Get(2).(func(string) error); ok { + r2 = rf(avatar) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// Put provides a mock function with given fields: userID, reader +func (_m *MockAvatarStore) Put(userID string, reader io.Reader) (string, error) { + ret := _m.Called(userID, reader) + + var r0 string + if rf, ok := ret.Get(0).(func(string, io.Reader) string); ok { + r0 = rf(userID, reader) + } else { + r0 = ret.Get(0).(string) + } + + var r1 error + if rf, ok := ret.Get(1).(func(string, io.Reader) error); ok { + r1 = rf(userID, reader) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} diff --git a/app/rest/proxy/avatar_store_test.go b/app/rest/proxy/avatar_store_test.go index 3d1f39e4..702c881b 100644 --- a/app/rest/proxy/avatar_store_test.go +++ b/app/rest/proxy/avatar_store_test.go @@ -116,7 +116,7 @@ func TestAvatarStore_resize(t *testing.T) { img, err := ioutil.ReadFile(c.file) require.Nil(t, err, "can't open test file %s", c.file) - // No need for resize, avatar dimentions are smaller than resize limit. + // No need for resize, avatar dimensions are smaller than resize limit. resizedR = resize(bytes.NewReader(img), 800) assert.NotNilf(t, resizedR, "file %s", c.file) checkC(t, resizedR, img) diff --git a/app/store/engine/bolt_accessor.go b/app/store/engine/bolt_accessor.go index 890b639e..70b210bd 100644 --- a/app/store/engine/bolt_accessor.go +++ b/app/store/engine/bolt_accessor.go @@ -55,7 +55,6 @@ func NewBoltDB(options bolt.Options, sites ...BoltSite) (*BoltDB, error) { log.Printf("[INFO] bolt store for sites %+v", sites) result := BoltDB{dbs: make(map[string]*bolt.DB)} for _, site := range sites { - db, err := bolt.Open(site.FileName, 0600, &options) // bolt.Options{Timeout: 30 * time.Second} if err != nil { return nil, errors.Wrapf(err, "failed to make boltdb for %s", site.FileName) @@ -349,7 +348,7 @@ func (b *BoltDB) User(siteID, userID string, limit, skip int) (comments []store. return comments, err } -// UserCount returns number of comments for user TODO: this can be slow, but userIDBkt just refs +// UserCount returns number of comments for user func (b *BoltDB) UserCount(siteID, userID string) (int, error) { bdb, err := b.db(siteID) if err != nil {