diff --git a/api/setup_test.go b/api/setup_test.go index 0822b3e4a..e1042dfaf 100644 --- a/api/setup_test.go +++ b/api/setup_test.go @@ -1,6 +1,8 @@ package api import ( + "fmt" + commonClients "github.com/tidepool-org/go-common/clients" "github.com/tidepool-org/go-common/clients/highwater" "github.com/tidepool-org/go-common/clients/shoreline" @@ -49,41 +51,74 @@ var ( */ mock_NoPermsGatekeeper = commonClients.NewGatekeeperMock(commonClients.Permissions{"upload": commonClients.Permission{"userid": "other-id"}}, nil) - mock_uid1Shoreline = newtestingShorelingMock(testing_uid1) + mock_uid1Shoreline = newtestingShorelineMock(testing_uid1) ) // In an effort to mock shoreline so that we can return the token we wish -type testingShorelingMock struct{ userid string } +type testingShorelineMock struct{ userIDs []string } -func (m *testingShorelingMock) CreateCustodialUserForClinic(clinicId string, userData shoreline.CustodialUserData, token string) (*shoreline.UserData, error) { +func (m *testingShorelineMock) CreateCustodialUserForClinic(clinicId string, userData shoreline.CustodialUserData, token string) (*shoreline.UserData, error) { panic("Not Implemented") } -func (m *testingShorelingMock) DeleteUserSessions(userID, token string) error { +func (m *testingShorelineMock) DeleteUserSessions(userID, token string) error { panic("Not Implemented") } -func newtestingShorelingMock(userid string) *testingShorelingMock { - return &testingShorelingMock{userid: userid} +func newtestingShorelineMock(userIDs ...string) *testingShorelineMock { + return &testingShorelineMock{userIDs: userIDs} } -func (m *testingShorelingMock) Start() error { return nil } -func (m *testingShorelingMock) Close() {} -func (m *testingShorelingMock) Login(username, password string) (*shoreline.UserData, string, error) { - return &shoreline.UserData{UserID: m.userid, Emails: []string{m.userid + "@email.org"}, Username: m.userid + "@email.org"}, "", nil +func (m *testingShorelineMock) Start() error { return nil } +func (m *testingShorelineMock) Close() {} +func (m *testingShorelineMock) Login(userID, password string) (*shoreline.UserData, string, error) { + for _, mockedUserID := range m.userIDs { + if mockedUserID == userID { + return m.mockUser(mockedUserID), "", nil + } + } + return nil, "", fmt.Errorf("userID not mocked: %q", userID) } -func (m *testingShorelingMock) Signup(username, password, email string) (*shoreline.UserData, error) { - return &shoreline.UserData{UserID: m.userid, Emails: []string{m.userid + "@email.org"}, Username: m.userid + "@email.org"}, nil + +func (m *testingShorelineMock) mockUser(userID string) *shoreline.UserData { + return &shoreline.UserData{ + UserID: userID, + Emails: []string{userID + "@email.org"}, + Username: userID + "@email.org", + } +} + +func (m *testingShorelineMock) Signup(userID, password, email string) (*shoreline.UserData, error) { + for _, mockedUserID := range m.userIDs { + if mockedUserID == userID { + return m.mockUser(userID), nil + } + } + return nil, fmt.Errorf("userID not mocked: %q", userID) } -func (m *testingShorelingMock) TokenProvide() string { return testing_token } -func (m *testingShorelingMock) GetUser(userID, token string) (*shoreline.UserData, error) { - return &shoreline.UserData{UserID: m.userid, Emails: []string{m.userid + "@email.org"}, Username: m.userid + "@email.org"}, nil + +func (m *testingShorelineMock) TokenProvide() string { return testing_token } + +func (m *testingShorelineMock) GetUser(userID, token string) (*shoreline.UserData, error) { + for _, mockedUserID := range m.userIDs { + if mockedUserID == userID { + return m.mockUser(userID), nil + } + } + return nil, fmt.Errorf("userID not mocked: %q", userID) } -func (m *testingShorelingMock) UpdateUser(userID string, userUpdate shoreline.UserUpdate, token string) error { + +func (m *testingShorelineMock) UpdateUser(userID string, userUpdate shoreline.UserUpdate, token string) error { return nil } -func (m *testingShorelingMock) CheckToken(token string) *shoreline.TokenData { - return &shoreline.TokenData{UserID: m.userid, IsServer: false} + +func (m *testingShorelineMock) CheckToken(token string) *shoreline.TokenData { + for _, mockedUserID := range m.userIDs { + if mockedUserID == token || (token == testing_token_uid1 && mockedUserID == testing_uid1) { + return &shoreline.TokenData{UserID: mockedUserID, IsServer: false} + } + } + return nil } type (