auth
46:c6ace3d27c6f Browse Files
Add simple tests for logins. Test for login adding, removal, listing, and updating, to ensure they work as intended across all our ProfileStores.
1.1 --- a/profile_test.go Sat Sep 27 20:51:19 2014 -0400 1.2 +++ b/profile_test.go Sat Sep 27 21:41:39 2014 -0400 1.3 @@ -63,6 +63,25 @@ 1.4 return true, "", nil, nil 1.5 } 1.6 1.7 +func compareLogins(login1, login2 Login) (success bool, field string, val1, val2 interface{}) { 1.8 + if login1.Type != login2.Type { 1.9 + return false, "Type", login1.Type, login2.Type 1.10 + } 1.11 + if login1.Value != login2.Value { 1.12 + return false, "Value", login1.Value, login2.Value 1.13 + } 1.14 + if !login1.ProfileID.Equal(login2.ProfileID) { 1.15 + return false, "ProfileID", login1.ProfileID, login2.ProfileID 1.16 + } 1.17 + if !login1.Created.Equal(login2.Created) { 1.18 + return false, "Created", login1.Created, login2.Created 1.19 + } 1.20 + if !login1.LastUsed.Equal(login2.LastUsed) { 1.21 + return false, "LastUsed", login1.LastUsed, login2.LastUsed 1.22 + } 1.23 + return true, "", nil, nil 1.24 +} 1.25 + 1.26 func TestProfileStoreSuccess(t *testing.T) { 1.27 t.Parallel() 1.28 profile := Profile{ 1.29 @@ -281,3 +300,64 @@ 1.30 } 1.31 } 1.32 } 1.33 + 1.34 +func TestProfileStoreLoginSuccess(t *testing.T) { 1.35 + t.Parallel() 1.36 + login := Login{ 1.37 + Type: "type", 1.38 + Value: "value", 1.39 + ProfileID: uuid.NewID(), 1.40 + Created: time.Now().Add(-1 * time.Hour), 1.41 + LastUsed: time.Now().Add(-1 * time.Minute), 1.42 + } 1.43 + for _, store := range profileStores { 1.44 + err := store.AddLogin(login) 1.45 + if err != nil { 1.46 + t.Errorf("Error adding login to %T: %s", store, err) 1.47 + } 1.48 + err = store.AddLogin(login) 1.49 + if err != ErrLoginAlreadyExists { 1.50 + t.Errorf("Expected ErrLoginAlreadyExists from %T, got %+v", store, err) 1.51 + } 1.52 + retrieved, err := store.ListLogins(login.ProfileID, 10, 0) 1.53 + if err != nil { 1.54 + t.Errorf("Error retrieving logins from %T: %s", store, err) 1.55 + } 1.56 + if len(retrieved) != 1 { 1.57 + t.Errorf("Expected 1 login result from %T, got %d", store, len(retrieved)) 1.58 + } 1.59 + match, field, expectation, result := compareLogins(login, retrieved[0]) 1.60 + if !match { 1.61 + t.Errorf("Expected `%v` in the `%s` field of login retrieved from %T, got `%v`", expectation, field, store, result) 1.62 + } 1.63 + lastUsed := time.Now() 1.64 + err = store.RecordLoginUse(login.Type, login.Value, lastUsed) 1.65 + if err != nil { 1.66 + t.Errorf("Error recording use of login to %T: %s", store, err) 1.67 + } 1.68 + login.LastUsed = lastUsed 1.69 + retrieved, err = store.ListLogins(login.ProfileID, 10, 0) 1.70 + if err != nil { 1.71 + t.Errorf("Error retrieving logins from %T: %s", store, err) 1.72 + } 1.73 + if len(retrieved) != 1 { 1.74 + t.Errorf("Expected 1 login result from %T, got %d", store, len(retrieved)) 1.75 + } 1.76 + match, field, expectation, result = compareLogins(login, retrieved[0]) 1.77 + if !match { 1.78 + t.Errorf("Expected `%v` in the `%s` field of login retrieved from %T, got `%v`", expectation, field, store, result) 1.79 + } 1.80 + err = store.RemoveLogin(login.Type, login.Value, login.ProfileID) 1.81 + if err != nil { 1.82 + t.Errorf("Error removing login from %T: %s", store, err) 1.83 + } 1.84 + retrieved, err = store.ListLogins(login.ProfileID, 10, 0) 1.85 + if len(retrieved) != 0 { 1.86 + t.Errorf("Expected 0 login results from %T, got %d: %+v", store, len(retrieved), retrieved) 1.87 + } 1.88 + err = store.RemoveLogin(login.Type, login.Value, login.ProfileID) 1.89 + if err != ErrLoginNotFound { 1.90 + t.Errorf("Expected ErrLoginNotFound from %T, got %+v", store, err) 1.91 + } 1.92 + } 1.93 +}