From 9432468e7b7357e5d4bf24ae4b6c153f50ef99b9 Mon Sep 17 00:00:00 2001 From: Eric Wollesen Date: Fri, 19 Jul 2024 08:46:39 -0600 Subject: [PATCH] one mock of DeviceTokenRepository is enough As caught by Todd in code review. BACK-2554 --- auth/service/api/v1/devicetokens_test.go | 11 ++--- auth/service/service/client_test.go | 55 +++++----------------- auth/store/test/device_token_repository.go | 9 +++- 3 files changed, 23 insertions(+), 52 deletions(-) diff --git a/auth/service/api/v1/devicetokens_test.go b/auth/service/api/v1/devicetokens_test.go index ef61df746..223208b61 100644 --- a/auth/service/api/v1/devicetokens_test.go +++ b/auth/service/api/v1/devicetokens_test.go @@ -93,15 +93,14 @@ var _ = Describe("Device tokens endpoints", func() { It("may return multiple documents", func() { repo := &storetest.DeviceTokenRepository{ - Documents: []*devicetokens.Document{ - { - DeviceToken: devicetokens.DeviceToken{}, - }, - { - DeviceToken: devicetokens.DeviceToken{}, + Tokens: map[string][]*devicetokens.DeviceToken{ + test.TestUserID1: { + &devicetokens.DeviceToken{}, + &devicetokens.DeviceToken{}, }, }, } + raw := rtr.Service.AuthStore().(*storetest.Store) raw.NewDeviceTokenRepositoryImpl = repo res := test.NewMockRestResponseWriter() diff --git a/auth/service/service/client_test.go b/auth/service/service/client_test.go index 1714e947a..26792ca30 100644 --- a/auth/service/service/client_test.go +++ b/auth/service/service/client_test.go @@ -14,6 +14,7 @@ import ( "github.com/tidepool-org/platform/auth/client" "github.com/tidepool-org/platform/auth/service/service" "github.com/tidepool-org/platform/auth/store" + storetest "github.com/tidepool-org/platform/auth/store/test" platformclient "github.com/tidepool-org/platform/client" "github.com/tidepool-org/platform/devicetokens" logtest "github.com/tidepool-org/platform/log/test" @@ -47,14 +48,14 @@ var _ = Describe("Client", func() { name := "test auth client" logger := logtest.NewLogger() if authStore == nil { + repo := storetest.NewDeviceTokenRepository() + repo.Tokens = map[string][]*devicetokens.DeviceToken{ + testUserID: { + testDeviceToken1, + }} + authStore = &mockAuthStore{ - DeviceTokenRepository: &mockDeviceTokenRepository{ - Tokens: map[string][]*devicetokens.DeviceToken{ - testUserID: { - testDeviceToken1, - }, - }, - }, + DeviceTokenRepository: repo, } } providerFactory := &mockProviderFactory{} @@ -81,10 +82,10 @@ var _ = Describe("Client", func() { ctx := context.Background() server := NewServer() defer server.Close() + repo := storetest.NewDeviceTokenRepository() + repo.Error = fmt.Errorf("test error") authStore := &mockAuthStore{ - DeviceTokenRepository: &mockDeviceTokenRepository{ - Error: fmt.Errorf("test error"), - }, + DeviceTokenRepository: repo, } serviceClient := newTestServiceClient(server.URL(), authStore) @@ -120,37 +121,3 @@ type mockProviderFactory struct{} func (f *mockProviderFactory) Get(typ string, name string) (provider.Provider, error) { return nil, nil } - -type mockDeviceTokenRepository struct { - Error error - Tokens map[string][]*devicetokens.DeviceToken -} - -func (r *mockDeviceTokenRepository) GetAllByUserID(ctx context.Context, userID string) ([]*devicetokens.Document, error) { - if r.Error != nil { - return nil, r.Error - } - - if tokens, ok := r.Tokens[userID]; ok { - docs := make([]*devicetokens.Document, 0, len(tokens)) - for _, token := range tokens { - docs = append(docs, &devicetokens.Document{DeviceToken: *token}) - } - return docs, nil - } - return nil, nil -} - -func (r *mockDeviceTokenRepository) Upsert(ctx context.Context, doc *devicetokens.Document) error { - if r.Error != nil { - return r.Error - } - return nil -} - -func (r *mockDeviceTokenRepository) EnsureIndexes() error { - if r.Error != nil { - return r.Error - } - return nil -} diff --git a/auth/store/test/device_token_repository.go b/auth/store/test/device_token_repository.go index dbb40d920..a15f913af 100644 --- a/auth/store/test/device_token_repository.go +++ b/auth/store/test/device_token_repository.go @@ -10,6 +10,7 @@ import ( type DeviceTokenRepository struct { *authTest.DeviceTokenAccessor Documents []*devicetokens.Document + Tokens map[string][]*devicetokens.DeviceToken Error error } @@ -27,8 +28,12 @@ func (r *DeviceTokenRepository) GetAllByUserID(ctx context.Context, userID strin if r.Error != nil { return nil, r.Error } - if len(r.Documents) > 0 { - return r.Documents, nil + if tokens, ok := r.Tokens[userID]; ok { + docs := make([]*devicetokens.Document, 0, len(tokens)) + for _, token := range tokens { + docs = append(docs, &devicetokens.Document{DeviceToken: *token}) + } + return docs, nil } return nil, nil }