The great documentation and exported interface cleanup.
Modify all our *Store interfaces to be unexported, as there's no real good
reason they need to be exported, especially as they can be implemented without
being exported. The interfaces shouldn't matter to 99% of users of the package,
so let's not pollute our package API.
Further, all methods of the interfaces are now unexported, for pretty much the
same reasoning.
Add a doc.go file with documentation explaining the choices the package is
making and what it provides.
Implement documentation on all our exported types and methods and functions,
which makes golint happy. The only remaining golint warning is about
NewMemstore, which will stay the way it is. The memstore type is useful outside
tests for things like standing up a server quickly when we don't care about the
storage, and because the type is unexported, we _need_ a New function to create
an instance that can be passed to the Context.
7 "code.secondbit.org/uuid"
10 var tokenStores = []tokenStore{NewMemstore()}
12 func compareTokens(token1, token2 Token) (success bool, field string, val1, val2 interface{}) {
13 if token1.AccessToken != token2.AccessToken {
14 return false, "access token", token1.AccessToken, token2.AccessToken
16 if token1.RefreshToken != token2.RefreshToken {
17 return false, "refresh token", token1.RefreshToken, token2.RefreshToken
19 if !token1.Created.Equal(token2.Created) {
20 return false, "created", token1.Created, token2.Created
22 if token1.ExpiresIn != token2.ExpiresIn {
23 return false, "expires in", token1.ExpiresIn, token2.ExpiresIn
25 if token1.TokenType != token2.TokenType {
26 return false, "token type", token1.TokenType, token2.TokenType
28 if token1.Scope != token2.Scope {
29 return false, "scope", token1.Scope, token2.Scope
31 if !token1.ProfileID.Equal(token2.ProfileID) {
32 return false, "profile ID", token1.ProfileID, token2.ProfileID
34 return true, "", nil, nil
37 func TestTokenStoreSuccess(t *testing.T) {
40 AccessToken: "access",
41 RefreshToken: "refresh",
46 ProfileID: uuid.NewID(),
48 for _, store := range tokenStores {
49 err := store.saveToken(token)
51 t.Errorf("Error saving token to %T: %s", store, err)
53 err = store.saveToken(token)
54 if err != ErrTokenAlreadyExists {
55 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err)
57 retrievedAccess, err := store.getToken(token.AccessToken, false)
59 t.Errorf("Error retrieving token from %T: %s", store, err)
61 success, field, expectation, result := compareTokens(token, retrievedAccess)
63 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
65 retrievedRefresh, err := store.getToken(token.RefreshToken, true)
67 t.Errorf("Error retrieving refresh token from %T: %s", store, err)
69 success, field, expectation, result = compareTokens(token, retrievedRefresh)
71 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
73 retrievedProfile, err := store.getTokensByProfileID(token.ProfileID, 25, 0)
75 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
77 if len(retrievedProfile) != 1 {
78 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile)
80 success, field, expectation, result = compareTokens(token, retrievedProfile[0])
82 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
84 err = store.removeToken(token.AccessToken)
86 t.Errorf("Error removing token from %T: %s", store, err)
88 _, err = store.getToken(token.AccessToken, false)
89 if err != ErrTokenNotFound {
90 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
92 _, err = store.getToken(token.RefreshToken, true)
93 if err != ErrTokenNotFound {
94 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
96 retrievedProfile, err = store.getTokensByProfileID(token.ProfileID, 25, 0)
98 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
100 if len(retrievedProfile) != 0 {
101 t.Errorf("Expected list of 0 tokens from %T, got %+v", store, retrievedProfile)
103 err = store.removeToken(token.AccessToken)
104 if err != ErrTokenNotFound {
105 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)