auth

Paddy 2014-09-07 Parent:5bf0a5fd1d01 Child:bd274615ce72

34:14599a5c7819 Go to Latest

auth/grant_test.go

Further grant testing. Use the %T format to return the name of the GrantStore that failed the test, rather than just returning the position of the store. Test that storing a grant twice yields an ErrGrantAlreadyExists. Test that retrieving a grant that doesn't exist yields an ErrGrantNotFound. Compare returned grants against expectations.

History
     1.1 --- a/grant_test.go	Sun Sep 07 04:34:17 2014 -0400
     1.2 +++ b/grant_test.go	Sun Sep 07 04:48:06 2014 -0400
     1.3 @@ -9,6 +9,31 @@
     1.4  
     1.5  var grantStores = []GrantStore{NewMemstore()}
     1.6  
     1.7 +func compareGrants(grant1, grant2 Grant) (success bool, field string, grant1val, grant2val interface{}) {
     1.8 +	if grant1.Code != grant2.Code {
     1.9 +		return false, "code", grant1.Code, grant2.Code
    1.10 +	}
    1.11 +	if !grant1.Created.Equal(grant2.Created) {
    1.12 +		return false, "created", grant1.Created, grant2.Created
    1.13 +	}
    1.14 +	if grant1.ExpiresIn != grant2.ExpiresIn {
    1.15 +		return false, "expires in", grant1.ExpiresIn, grant2.ExpiresIn
    1.16 +	}
    1.17 +	if !grant1.ClientID.Equal(grant2.ClientID) {
    1.18 +		return false, "client ID", grant1.ClientID, grant2.ClientID
    1.19 +	}
    1.20 +	if grant1.Scope != grant2.Scope {
    1.21 +		return false, "scope", grant1.Scope, grant2.Scope
    1.22 +	}
    1.23 +	if grant1.RedirectURI != grant2.RedirectURI {
    1.24 +		return false, "redirect URI", grant1.RedirectURI, grant2.RedirectURI
    1.25 +	}
    1.26 +	if grant1.State != grant2.State {
    1.27 +		return false, "state", grant1.State, grant2.State
    1.28 +	}
    1.29 +	return true, "", nil, nil
    1.30 +}
    1.31 +
    1.32  func TestGrantStoreSuccess(t *testing.T) {
    1.33  	grant := Grant{
    1.34  		Code:        "code",
    1.35 @@ -19,24 +44,34 @@
    1.36  		RedirectURI: "redirectURI",
    1.37  		State:       "state",
    1.38  	}
    1.39 -	for pos, store := range grantStores {
    1.40 +	for _, store := range grantStores {
    1.41  		err := store.SaveGrant(grant)
    1.42  		if err != nil {
    1.43 -			t.Errorf("Error saving grant in GrantStore #%d: %s", pos, err)
    1.44 +			t.Errorf("Error saving grant to %T: %s", store, err)
    1.45 +		}
    1.46 +		err = store.SaveGrant(grant)
    1.47 +		if err != ErrGrantAlreadyExists {
    1.48 +			t.Errorf("Expected ErrGrantAlreadyExists from %T, got %+v", store, err)
    1.49  		}
    1.50  		retrieved, err := store.GetGrant(grant.Code)
    1.51  		if err != nil {
    1.52 -			t.Errorf("Error retrieving grant in GrantStore #%d: %s", pos, err)
    1.53 +			t.Errorf("Error retrieving grant from %T: %s", store, err)
    1.54  		}
    1.55 -		t.Log(retrieved)
    1.56 -		// TODO: compare retrieved to grant
    1.57 +		match, field, expectation, result := compareGrants(grant, retrieved)
    1.58 +		if !match {
    1.59 +			t.Errorf("Expected `%v` in the `%s` field of grant retrieved from %T, got `%v`", expectation, field, store, result)
    1.60 +		}
    1.61  		err = store.DeleteGrant(grant.Code)
    1.62  		if err != nil {
    1.63 -			t.Errorf("Error removing grant from GrantStore #%d: %s", pos, err)
    1.64 +			t.Errorf("Error removing grant from %T: %s", store, err)
    1.65  		}
    1.66  		retrieved, err = store.GetGrant(grant.Code)
    1.67  		if err != ErrGrantNotFound {
    1.68 -			t.Errorf("Expected ErrGrantNotFound from GrantStore #%d, got %+v and %+v", pos, retrieved, err)
    1.69 +			t.Errorf("Expected ErrGrantNotFound from %T, got %+v and %+v", store, retrieved, err)
    1.70 +		}
    1.71 +		err = store.DeleteGrant(grant.Code)
    1.72 +		if err != ErrGrantNotFound {
    1.73 +			t.Errorf("Expected ErrGrantNotFound from %T, got %+v", store, err)
    1.74  		}
    1.75  	}
    1.76  }