auth
auth/client_test.go
Include client updates and tests. Flesh out updating clients, and include unit tests to ensure that clientstores actually update appropriately. Add TODO comments where functionality is planned but stubbed out.
1.1 --- a/client_test.go Sun Sep 07 19:09:01 2014 -0400 1.2 +++ b/client_test.go Thu Sep 18 19:33:49 2014 -0400 1.3 @@ -1,11 +1,21 @@ 1.4 package auth 1.5 1.6 import ( 1.7 + "fmt" 1.8 "testing" 1.9 1.10 "secondbit.org/uuid" 1.11 ) 1.12 1.13 +const ( 1.14 + clientChangeSecret = 1 << iota 1.15 + clientChangeRedirectURI 1.16 + clientChangeOwnerID 1.17 + clientChangeName 1.18 + clientChangeLogo 1.19 + clientChangeWebsite 1.20 +) 1.21 + 1.22 var clientStores = []ClientStore{NewMemstore()} 1.23 1.24 func compareClients(client1, client2 Client) (success bool, field string, val1, val2 interface{}) { 1.25 @@ -93,3 +103,83 @@ 1.26 } 1.27 } 1.28 } 1.29 + 1.30 +func TestClientUpdates(t *testing.T) { 1.31 + t.Parallel() 1.32 + variations := 1 << 10 1.33 + client := Client{ 1.34 + ID: uuid.NewID(), 1.35 + Secret: "secret", 1.36 + RedirectURI: "redirectURI", 1.37 + OwnerID: uuid.NewID(), 1.38 + Name: "name", 1.39 + Logo: "logo", 1.40 + Website: "website", 1.41 + } 1.42 + for i := 0; i < variations; i++ { 1.43 + var secret, redirectURI, name, logo, website string 1.44 + change := ClientChange{} 1.45 + expectation := client 1.46 + result := client 1.47 + if i&clientChangeSecret != 0 { 1.48 + secret = fmt.Sprintf("secret-%d", i) 1.49 + change.Secret = &secret 1.50 + expectation.Secret = secret 1.51 + } 1.52 + if i&clientChangeRedirectURI != 0 { 1.53 + redirectURI = fmt.Sprintf("redirect-uri-%d", i) 1.54 + change.RedirectURI = &redirectURI 1.55 + expectation.RedirectURI = redirectURI 1.56 + } 1.57 + if i&clientChangeOwnerID != 0 { 1.58 + change.OwnerID = uuid.NewID() 1.59 + expectation.OwnerID = change.OwnerID 1.60 + } 1.61 + if i&clientChangeName != 0 { 1.62 + name = fmt.Sprintf("name-%d", i) 1.63 + change.Name = &name 1.64 + expectation.Name = name 1.65 + } 1.66 + if i&clientChangeLogo != 0 { 1.67 + logo = fmt.Sprintf("logo-%d", i) 1.68 + change.Logo = &logo 1.69 + expectation.Logo = logo 1.70 + } 1.71 + if i&clientChangeWebsite != 0 { 1.72 + website = fmt.Sprintf("website-%d", i) 1.73 + change.Website = &website 1.74 + expectation.Website = website 1.75 + } 1.76 + result.ApplyChange(change) 1.77 + match, field, expected, got := compareClients(expectation, result) 1.78 + if !match { 1.79 + t.Errorf("Expected field `%s` to be `%v`, got `%v`", field, expected, got) 1.80 + } 1.81 + for _, store := range clientStores { 1.82 + err := store.SaveClient(client) 1.83 + if err != nil { 1.84 + t.Errorf("Error saving client in %T: %s", store, err) 1.85 + } 1.86 + err = store.UpdateClient(client.ID, change) 1.87 + if err != nil { 1.88 + t.Errorf("Error updating client in %T: %s", store, err) 1.89 + } 1.90 + retrieved, err := store.GetClient(client.ID) 1.91 + if err != nil { 1.92 + t.Errorf("Error getting profile from %T: %s", store, err) 1.93 + } 1.94 + match, field, expected, got = compareClients(expectation, retrieved) 1.95 + if !match { 1.96 + t.Errorf("Expected field `%s` to be `%v`, got `%v` from %T", field, expected, got, store) 1.97 + } 1.98 + err = store.DeleteClient(client.ID) 1.99 + if err != nil { 1.100 + t.Errorf("Error deleting client from %T: %s", store, err) 1.101 + } 1.102 + err = store.UpdateClient(client.ID, change) 1.103 + if err != ErrClientNotFound { 1.104 + t.Errorf("Expected ErrClientNotFound, got %v from %T", err, store) 1.105 + } 1.106 + } 1.107 + } 1.108 +}