auth

Paddy 2014-09-07 Parent:88523dab00a5 Child:bd274615ce72

33:607708cd8829 Go to Latest

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.

History
     1.1 --- a/client_test.go	Sun Sep 07 04:15:04 2014 -0400
     1.2 +++ b/client_test.go	Sun Sep 07 04:34:17 2014 -0400
     1.3 @@ -8,6 +8,31 @@
     1.4  
     1.5  var clientStores = []ClientStore{NewMemstore()}
     1.6  
     1.7 +func compareClients(client1, client2 Client) (success bool, field string, val1, val2 interface{}) {
     1.8 +	if !client1.ID.Equal(client2.ID) {
     1.9 +		return false, "ID", client1.ID, client2.ID
    1.10 +	}
    1.11 +	if client1.Secret != client2.Secret {
    1.12 +		return false, "secret", client1.Secret, client2.Secret
    1.13 +	}
    1.14 +	if client1.RedirectURI != client2.RedirectURI {
    1.15 +		return false, "redirect URI", client1.RedirectURI, client2.RedirectURI
    1.16 +	}
    1.17 +	if !client1.OwnerID.Equal(client2.OwnerID) {
    1.18 +		return false, "owner ID", client1.OwnerID, client2.OwnerID
    1.19 +	}
    1.20 +	if client1.Name != client2.Name {
    1.21 +		return false, "name", client1.Name, client2.Name
    1.22 +	}
    1.23 +	if client1.Logo != client2.Logo {
    1.24 +		return false, "logo", client1.Logo, client2.Logo
    1.25 +	}
    1.26 +	if client1.Website != client2.Website {
    1.27 +		return false, "website", client1.Website, client2.Website
    1.28 +	}
    1.29 +	return true, "", nil, nil
    1.30 +}
    1.31 +
    1.32  func TestClientStoreSuccess(t *testing.T) {
    1.33  	client := Client{
    1.34  		ID:          uuid.NewID(),
    1.35 @@ -23,12 +48,18 @@
    1.36  		if err != nil {
    1.37  			t.Errorf("Error saving client to %T: %s", store, err)
    1.38  		}
    1.39 +		err = store.SaveClient(client)
    1.40 +		if err != ErrClientAlreadyExists {
    1.41 +			t.Errorf("Expected ErrClientAlreadyExists, got %v from %T", err, store)
    1.42 +		}
    1.43  		retrieved, err := store.GetClient(client.ID)
    1.44  		if err != nil {
    1.45  			t.Errorf("Error retrieving client from %T: %s", store, err)
    1.46  		}
    1.47 -		t.Log(retrieved)
    1.48 -		// TODO: compare retrieved to client
    1.49 +		success, field, expectation, result := compareClients(client, retrieved)
    1.50 +		if !success {
    1.51 +			t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result)
    1.52 +		}
    1.53  		clients, err := store.ListClientsByOwner(client.OwnerID, 25, 0)
    1.54  		if err != nil {
    1.55  			t.Errorf("Error retrieving clients by owner from %T: %s", store, err)
    1.56 @@ -36,11 +67,18 @@
    1.57  		if len(clients) != 1 {
    1.58  			t.Errorf("Expected 1 client in response from %T, got %+v", store, clients)
    1.59  		}
    1.60 -		// TODO: compare clients[0] to client
    1.61 +		success, field, expectation, result = compareClients(client, clients[0])
    1.62 +		if !success {
    1.63 +			t.Errorf("Expected field %s to be %v, but %T returned %v", field, expectation, store, result)
    1.64 +		}
    1.65  		err = store.DeleteClient(client.ID)
    1.66  		if err != nil {
    1.67  			t.Errorf("Error deleting client from %T: %s", store, err)
    1.68  		}
    1.69 +		err = store.DeleteClient(client.ID)
    1.70 +		if err != ErrClientNotFound {
    1.71 +			t.Errorf("Expected ErrClientNotFound, got %s from %T", err, store)
    1.72 +		}
    1.73  		retrieved, err = store.GetClient(client.ID)
    1.74  		if err != ErrClientNotFound {
    1.75  			t.Errorf("Expected ErrClientNotFound from %T, got %+v and %s", store, retrieved, err)