auth

Paddy 2014-09-07 Parent:75cf37088852 Child:d89740a34654

33:607708cd8829 Go to Latest

auth/token_test.go

Add more tests for ClientStores. Test that deleting a client that doesn't exist throws an ErrClientNotFound. Test that adding a client twice returns an ErrClientAlreadyExists. Test that clients retrieve have properties that all match the client that was stored. Remove the unused error return from the internal MemoryStore helper for getting a list of clients by their OwnerID. If a profile doesn't exist, return an empty list of clients, as we're technically returning any client that has that OwnerID. There's no guarantee that that OwnerID is actually valid.

History
1 package auth
3 import (
4 "testing"
5 "time"
7 "secondbit.org/uuid"
8 )
10 var tokenStores = []TokenStore{NewMemstore()}
12 func TestTokenStoreSuccess(t *testing.T) {
13 token := Token{
14 AccessToken: "access",
15 RefreshToken: "refresh",
16 Created: time.Now(),
17 ExpiresIn: 3600,
18 TokenType: "bearer",
19 Scope: "scope",
20 ProfileID: uuid.NewID(),
21 }
22 for pos, store := range tokenStores {
23 err := store.SaveToken(token)
24 if err != nil {
25 t.Errorf("Error saving token in TokenStore #%d: %s", pos, err)
26 }
27 retrievedAccess, err := store.GetToken(token.AccessToken, false)
28 if err != nil {
29 t.Errorf("Error retrieving token in TokenStore #%d: %s", pos, err)
30 }
31 t.Log(retrievedAccess)
32 // TODO: compare retrievedAccess to token
33 retrievedRefresh, err := store.GetToken(token.RefreshToken, true)
34 if err != nil {
35 t.Errorf("Error retrieving refresh token in TokenStore #%d: %s", pos, err)
36 }
37 t.Log(retrievedRefresh)
38 // TODO: compare retrievedRefresh to token
39 retrievedProfile, err := store.GetTokensByProfileID(token.ProfileID, 25, 0)
40 if err != nil {
41 t.Errorf("Error retrieving token by profile in TokenStore #%d: %s", pos, err)
42 }
43 if len(retrievedProfile) != 1 {
44 t.Errorf("Expected 1 token retrieved by profile ID from TokenStore #%d, got %+v", pos, retrievedProfile)
45 }
46 // TODO: compare retrievedProfile to token
47 err = store.RemoveToken(token.AccessToken)
48 if err != nil {
49 t.Errorf("Error removing token in TokenStore #%d: %s", pos, err)
50 }
51 _, err = store.GetToken(token.AccessToken, false)
52 if err != ErrTokenNotFound {
53 t.Errorf("Expected ErrTokenNotFound from TokenStore #%d, got %s", pos, err)
54 }
55 _, err = store.GetToken(token.RefreshToken, true)
56 if err != ErrTokenNotFound {
57 t.Errorf("Expected ErrTokenNotFound from TokenStore #%d, got %s", pos, err)
58 }
59 retrievedProfile, err = store.GetTokensByProfileID(token.ProfileID, 25, 0)
60 if err != nil {
61 t.Errorf("Error retrieving token by profile in TokenStore #%d: %s", pos, err)
62 }
63 if len(retrievedProfile) != 0 {
64 t.Errorf("Expected list of 0 tokens from TokenStore #%d, got %+v", pos, retrievedProfile)
65 }
66 }
67 }