auth

Paddy 2014-09-07 Parent:607708cd8829 Child:bd274615ce72

34:14599a5c7819 Go to Latest

auth/client_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 package auth
3 import (
4 "testing"
6 "secondbit.org/uuid"
7 )
9 var clientStores = []ClientStore{NewMemstore()}
11 func compareClients(client1, client2 Client) (success bool, field string, val1, val2 interface{}) {
12 if !client1.ID.Equal(client2.ID) {
13 return false, "ID", client1.ID, client2.ID
14 }
15 if client1.Secret != client2.Secret {
16 return false, "secret", client1.Secret, client2.Secret
17 }
18 if client1.RedirectURI != client2.RedirectURI {
19 return false, "redirect URI", client1.RedirectURI, client2.RedirectURI
20 }
21 if !client1.OwnerID.Equal(client2.OwnerID) {
22 return false, "owner ID", client1.OwnerID, client2.OwnerID
23 }
24 if client1.Name != client2.Name {
25 return false, "name", client1.Name, client2.Name
26 }
27 if client1.Logo != client2.Logo {
28 return false, "logo", client1.Logo, client2.Logo
29 }
30 if client1.Website != client2.Website {
31 return false, "website", client1.Website, client2.Website
32 }
33 return true, "", nil, nil
34 }
36 func TestClientStoreSuccess(t *testing.T) {
37 client := Client{
38 ID: uuid.NewID(),
39 Secret: "secret",
40 RedirectURI: "redirectURI",
41 OwnerID: uuid.NewID(),
42 Name: "name",
43 Logo: "logo",
44 Website: "website",
45 }
46 for _, store := range clientStores {
47 err := store.SaveClient(client)
48 if err != nil {
49 t.Errorf("Error saving client to %T: %s", store, err)
50 }
51 err = store.SaveClient(client)
52 if err != ErrClientAlreadyExists {
53 t.Errorf("Expected ErrClientAlreadyExists, got %v from %T", err, store)
54 }
55 retrieved, err := store.GetClient(client.ID)
56 if err != nil {
57 t.Errorf("Error retrieving client from %T: %s", store, err)
58 }
59 success, field, expectation, result := compareClients(client, retrieved)
60 if !success {
61 t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result)
62 }
63 clients, err := store.ListClientsByOwner(client.OwnerID, 25, 0)
64 if err != nil {
65 t.Errorf("Error retrieving clients by owner from %T: %s", store, err)
66 }
67 if len(clients) != 1 {
68 t.Errorf("Expected 1 client in response from %T, got %+v", store, clients)
69 }
70 success, field, expectation, result = compareClients(client, clients[0])
71 if !success {
72 t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result)
73 }
74 err = store.DeleteClient(client.ID)
75 if err != nil {
76 t.Errorf("Error deleting client from %T: %s", store, err)
77 }
78 err = store.DeleteClient(client.ID)
79 if err != ErrClientNotFound {
80 t.Errorf("Expected ErrClientNotFound, got %s from %T", err, store)
81 }
82 retrieved, err = store.GetClient(client.ID)
83 if err != ErrClientNotFound {
84 t.Errorf("Expected ErrClientNotFound from %T, got %+v and %s", store, retrieved, err)
85 }
86 clients, err = store.ListClientsByOwner(client.OwnerID, 25, 0)
87 if err != nil {
88 t.Errorf("Error listing clients by owner from %T: %s", store, err)
89 }
90 if len(clients) != 0 {
91 t.Errorf("Expected 0 clients in response from %T, got %+v", store, clients)
92 }
93 }
94 }