auth
auth/client_test.go
Allow executing tests in parallel. Add t.Parallel() to tests so they can execute in parallel.
| 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@36 | 37 t.Parallel() |
| paddy@31 | 38 client := Client{ |
| paddy@31 | 39 ID: uuid.NewID(), |
| paddy@31 | 40 Secret: "secret", |
| paddy@31 | 41 RedirectURI: "redirectURI", |
| paddy@31 | 42 OwnerID: uuid.NewID(), |
| paddy@31 | 43 Name: "name", |
| paddy@31 | 44 Logo: "logo", |
| paddy@31 | 45 Website: "website", |
| paddy@31 | 46 } |
| paddy@31 | 47 for _, store := range clientStores { |
| paddy@31 | 48 err := store.SaveClient(client) |
| paddy@31 | 49 if err != nil { |
| paddy@31 | 50 t.Errorf("Error saving client to %T: %s", store, err) |
| paddy@31 | 51 } |
| paddy@33 | 52 err = store.SaveClient(client) |
| paddy@33 | 53 if err != ErrClientAlreadyExists { |
| paddy@33 | 54 t.Errorf("Expected ErrClientAlreadyExists, got %v from %T", err, store) |
| paddy@33 | 55 } |
| paddy@31 | 56 retrieved, err := store.GetClient(client.ID) |
| paddy@31 | 57 if err != nil { |
| paddy@31 | 58 t.Errorf("Error retrieving client from %T: %s", store, err) |
| paddy@31 | 59 } |
| paddy@33 | 60 success, field, expectation, result := compareClients(client, retrieved) |
| paddy@33 | 61 if !success { |
| paddy@33 | 62 t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result) |
| paddy@33 | 63 } |
| paddy@31 | 64 clients, err := store.ListClientsByOwner(client.OwnerID, 25, 0) |
| paddy@31 | 65 if err != nil { |
| paddy@31 | 66 t.Errorf("Error retrieving clients by owner from %T: %s", store, err) |
| paddy@31 | 67 } |
| paddy@31 | 68 if len(clients) != 1 { |
| paddy@31 | 69 t.Errorf("Expected 1 client in response from %T, got %+v", store, clients) |
| paddy@31 | 70 } |
| paddy@33 | 71 success, field, expectation, result = compareClients(client, clients[0]) |
| paddy@33 | 72 if !success { |
| paddy@33 | 73 t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result) |
| paddy@33 | 74 } |
| paddy@31 | 75 err = store.DeleteClient(client.ID) |
| paddy@31 | 76 if err != nil { |
| paddy@31 | 77 t.Errorf("Error deleting client from %T: %s", store, err) |
| paddy@31 | 78 } |
| paddy@33 | 79 err = store.DeleteClient(client.ID) |
| paddy@33 | 80 if err != ErrClientNotFound { |
| paddy@33 | 81 t.Errorf("Expected ErrClientNotFound, got %s from %T", err, store) |
| paddy@33 | 82 } |
| paddy@31 | 83 retrieved, err = store.GetClient(client.ID) |
| paddy@31 | 84 if err != ErrClientNotFound { |
| paddy@31 | 85 t.Errorf("Expected ErrClientNotFound from %T, got %+v and %s", store, retrieved, err) |
| paddy@31 | 86 } |
| paddy@31 | 87 clients, err = store.ListClientsByOwner(client.OwnerID, 25, 0) |
| paddy@31 | 88 if err != nil { |
| paddy@31 | 89 t.Errorf("Error listing clients by owner from %T: %s", store, err) |
| paddy@31 | 90 } |
| paddy@31 | 91 if len(clients) != 0 { |
| paddy@31 | 92 t.Errorf("Expected 0 clients in response from %T, got %+v", store, clients) |
| paddy@31 | 93 } |
| paddy@31 | 94 } |
| paddy@31 | 95 } |