Skip to content

Commit

Permalink
one mock of DeviceTokenRepository is enough
Browse files Browse the repository at this point in the history
As caught by Todd in code review.

BACK-2554
  • Loading branch information
ewollesen committed Sep 17, 2024
1 parent 8064160 commit eaa652e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 52 deletions.
11 changes: 5 additions & 6 deletions auth/service/api/v1/devicetokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
55 changes: 11 additions & 44 deletions auth/service/service/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions auth/store/test/device_token_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type DeviceTokenRepository struct {
*authTest.DeviceTokenAccessor
Documents []*devicetokens.Document
Tokens map[string][]*devicetokens.DeviceToken
Error error
}

Expand All @@ -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
}
Expand Down

0 comments on commit eaa652e

Please sign in to comment.