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