Better organized fake store functions for server test
This commit is contained in:
parent
3391f653ac
commit
05505bfebb
2 changed files with 43 additions and 20 deletions
|
@ -37,7 +37,7 @@ func TestServerAuthHandlerSuccess(t *testing.T) {
|
|||
t.Errorf("Expected auth response to contain token: result: %+v err: %+v", string(body), err)
|
||||
}
|
||||
|
||||
if !testStore.SaveTokenCalled {
|
||||
if !testStore.Called.SaveToken {
|
||||
t.Errorf("Expected Store.SaveToken to be called")
|
||||
}
|
||||
}
|
||||
|
@ -50,9 +50,8 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
expectedStatusCode int
|
||||
expectedErrorString string
|
||||
|
||||
authFailLogin bool
|
||||
storeFailures TestStoreFunctions
|
||||
authFailGenToken bool
|
||||
storeFailSave bool
|
||||
}{
|
||||
{
|
||||
name: "bad method",
|
||||
|
@ -90,7 +89,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
expectedStatusCode: http.StatusUnauthorized,
|
||||
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": No match for email and password",
|
||||
|
||||
authFailLogin: true,
|
||||
storeFailures: TestStoreFunctions{GetUserId: true},
|
||||
},
|
||||
{
|
||||
name: "generate token fail",
|
||||
|
@ -108,7 +107,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
expectedStatusCode: http.StatusInternalServerError,
|
||||
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
||||
|
||||
storeFailSave: true,
|
||||
storeFailures: TestStoreFunctions{SaveToken: true},
|
||||
},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
|
@ -116,15 +115,9 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
|||
|
||||
// Set this up to fail according to specification
|
||||
testAuth := TestAuth{TestToken: auth.TokenString("seekrit")}
|
||||
testStore := TestStore{}
|
||||
if tc.authFailLogin {
|
||||
testStore.FailLogin = true
|
||||
} else if tc.authFailGenToken {
|
||||
testStore := TestStore{Failures: tc.storeFailures}
|
||||
if tc.authFailGenToken { // TODO - TestAuth{Failures:authFailures}
|
||||
testAuth.FailGenToken = true
|
||||
} else if tc.storeFailSave {
|
||||
testStore.FailSave = true
|
||||
} else {
|
||||
testAuth.TestToken = auth.TokenString("seekrit")
|
||||
}
|
||||
server := Server{&testAuth, &testStore}
|
||||
|
||||
|
|
|
@ -22,33 +22,53 @@ func (a *TestAuth) NewToken(userId auth.UserId, deviceId auth.DeviceId, scope au
|
|||
return &auth.AuthToken{Token: a.TestToken, UserId: userId, DeviceId: deviceId, Scope: scope}, nil
|
||||
}
|
||||
|
||||
type TestStore struct {
|
||||
FailSave bool
|
||||
FailLogin bool
|
||||
type TestStoreFunctions struct {
|
||||
SaveToken bool
|
||||
GetToken bool
|
||||
GetUserId bool
|
||||
CreateAccount bool
|
||||
SetWallet bool
|
||||
GetWallet bool
|
||||
}
|
||||
|
||||
SaveTokenCalled bool
|
||||
type TestStore struct {
|
||||
// Fake store functions will set these to `true` as they are called
|
||||
Called TestStoreFunctions
|
||||
|
||||
// Fake store functions will fail if these are set to `true` by the test
|
||||
// setup
|
||||
Failures TestStoreFunctions
|
||||
}
|
||||
|
||||
func (s *TestStore) SaveToken(token *auth.AuthToken) error {
|
||||
if s.FailSave {
|
||||
if s.Failures.SaveToken {
|
||||
return fmt.Errorf("TestStore.SaveToken fail")
|
||||
}
|
||||
s.SaveTokenCalled = true
|
||||
s.Called.SaveToken = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) {
|
||||
if s.Failures.GetToken {
|
||||
return nil, fmt.Errorf("TestStore.GetToken fail")
|
||||
}
|
||||
s.Called.GetToken = true
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *TestStore) GetUserId(auth.Email, auth.Password) (auth.UserId, error) {
|
||||
if s.FailLogin {
|
||||
if s.Failures.GetUserId {
|
||||
return 0, store.ErrNoUId
|
||||
}
|
||||
s.Called.GetUserId = true
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (s *TestStore) CreateAccount(auth.Email, auth.Password) error {
|
||||
if s.Failures.CreateAccount {
|
||||
return fmt.Errorf("TestStore.CreateAccount fail")
|
||||
}
|
||||
s.Called.CreateAccount = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -58,10 +78,20 @@ func (s *TestStore) SetWallet(
|
|||
sequence wallet.Sequence,
|
||||
hmac wallet.WalletHmac,
|
||||
) (latestEncryptedWallet wallet.EncryptedWallet, latestSequence wallet.Sequence, latestHmac wallet.WalletHmac, sequenceCorrect bool, err error) {
|
||||
if s.Failures.SetWallet {
|
||||
err = fmt.Errorf("TestStore.SetWallet fail")
|
||||
return
|
||||
}
|
||||
s.Called.SetWallet = true
|
||||
return
|
||||
}
|
||||
|
||||
func (s *TestStore) GetWallet(userId auth.UserId) (encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac, err error) {
|
||||
if s.Failures.GetWallet {
|
||||
err = fmt.Errorf("TestStore.GetWallet fail")
|
||||
return
|
||||
}
|
||||
s.Called.GetWallet = true
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue