scopes
2015-12-13
scopes/scope_test.go
Port over Postgres storage and tests. Do the minimum possible port of the code from auth to get Postgres working and make the tests run and pass again. This leaves a bug where the ErrScopeAlreadyExists type, when populatd from Postgres, contains the entire error string returned from the database, instead of parsing the ID itself out. Which is a thing we should do and add a test for.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/scope_test.go Sun Dec 13 20:42:48 2015 -0800 1.3 @@ -0,0 +1,167 @@ 1.4 +package scopes 1.5 + 1.6 +import ( 1.7 + "os" 1.8 + 1.9 + "golang.org/x/net/context" 1.10 + 1.11 + "code.secondbit.org/scopes.hg/types" 1.12 +) 1.13 +import "testing" 1.14 + 1.15 +func init() { 1.16 + if os.Getenv("PG_TEST_DB") != "" { 1.17 + p, err := NewPostgres(os.Getenv("PG_TEST_DB")) 1.18 + if err != nil { 1.19 + panic(err) 1.20 + } 1.21 + storers = append(storers, &p) 1.22 + } 1.23 +} 1.24 + 1.25 +var storers = []Storer{NewMemstore()} 1.26 + 1.27 +func compareScopes(scope1, scope2 scopeTypes.Scope) (success bool, field string, val1, val2 interface{}) { 1.28 + if scope1.ID != scope2.ID { 1.29 + return false, "ID", scope1.ID, scope2.ID 1.30 + } 1.31 + if scope1.Name != scope2.Name { 1.32 + return false, "Name", scope1.Name, scope2.Name 1.33 + } 1.34 + if scope1.Description != scope2.Description { 1.35 + return false, "Description", scope1.Description, scope2.Description 1.36 + } 1.37 + return true, "", nil, nil 1.38 +} 1.39 + 1.40 +func TestScopeInScopeStore(t *testing.T) { 1.41 + scope := scopeTypes.Scope{ 1.42 + ID: "testscope", 1.43 + Name: "Test Scope", 1.44 + Description: "Access to testing data.", 1.45 + } 1.46 + scope2 := scopeTypes.Scope{ 1.47 + ID: "testscope2", 1.48 + Name: "Test Scope 2", 1.49 + Description: "Access to minions.", 1.50 + } 1.51 + scope3 := scopeTypes.Scope{ 1.52 + ID: "testscope3", 1.53 + Name: "Test Scope 3", 1.54 + Description: "Access to bananas.", 1.55 + } 1.56 + updatedName := "Updated Scope" 1.57 + updatedDescription := "An updated scope." 1.58 + update := scopeTypes.ScopeChange{ 1.59 + ScopeID: scope.ID, 1.60 + Name: &updatedName, 1.61 + } 1.62 + update2 := scopeTypes.ScopeChange{ 1.63 + ScopeID: scope2.ID, 1.64 + Description: &updatedDescription, 1.65 + } 1.66 + update3 := scopeTypes.ScopeChange{ 1.67 + ScopeID: scope3.ID, 1.68 + Name: &updatedName, 1.69 + Description: &updatedDescription, 1.70 + } 1.71 + for _, store := range storers { 1.72 + ctx := context.Background() 1.73 + retrieved, err := store.GetScopes([]string{scope.ID}, ctx) 1.74 + if len(retrieved) != 0 { 1.75 + t.Logf("%+v", retrieved) 1.76 + t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store) 1.77 + } 1.78 + err = store.CreateScopes([]scopeTypes.Scope{scope}, ctx) 1.79 + if err != nil { 1.80 + t.Errorf("Error saving scope to %T: %s", store, err) 1.81 + } 1.82 + err = store.CreateScopes([]scopeTypes.Scope{scope}, ctx) 1.83 + if _, ok := err.(ErrScopeAlreadyExists); !ok { 1.84 + t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store) 1.85 + } 1.86 + retrieved, err = store.GetScopes([]string{scope.ID}, ctx) 1.87 + if err != nil { 1.88 + t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) 1.89 + } 1.90 + if len(retrieved) != 1 { 1.91 + t.Logf("%+v", retrieved) 1.92 + t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store) 1.93 + } 1.94 + if _, ok := retrieved[scope.ID]; !ok { 1.95 + t.Logf("%+v", retrieved) 1.96 + t.Errorf("Expected %s to be in results from %T, wasn't", scope.ID, store) 1.97 + } 1.98 + success, field, val1, val2 := compareScopes(scope, retrieved[scope.ID]) 1.99 + if !success { 1.100 + t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store) 1.101 + } 1.102 + err = store.CreateScopes([]scopeTypes.Scope{scope2, scope3}, ctx) 1.103 + if err != nil { 1.104 + t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err) 1.105 + } 1.106 + retrieved, err = store.GetScopes([]string{scope.ID, scope2.ID, scope3.ID}, ctx) 1.107 + if err != nil { 1.108 + t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) 1.109 + } 1.110 + if len(retrieved) != 3 { 1.111 + t.Logf("%+v", retrieved) 1.112 + t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store) 1.113 + } 1.114 + for _, s := range []scopeTypes.Scope{scope, scope2, scope3} { 1.115 + if _, ok := retrieved[s.ID]; !ok { 1.116 + t.Logf("%+v", retrieved) 1.117 + t.Errorf("Expected %s to be in results from %T, wasn't", s.ID, store) 1.118 + } 1.119 + success, field, val1, val2 = compareScopes(s, retrieved[s.ID]) 1.120 + if !success { 1.121 + t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) 1.122 + } 1.123 + } 1.124 + err = store.UpdateScope(update, ctx) 1.125 + if err != nil { 1.126 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 1.127 + } 1.128 + scope = scopeTypes.ApplyChange(update, scope) 1.129 + err = store.UpdateScope(update2, ctx) 1.130 + if err != nil { 1.131 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 1.132 + } 1.133 + scope2 = scopeTypes.ApplyChange(update2, scope2) 1.134 + err = store.UpdateScope(update3, ctx) 1.135 + if err != nil { 1.136 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 1.137 + } 1.138 + scope3 = scopeTypes.ApplyChange(update3, scope3) 1.139 + results, err := store.ListScopes(ctx) 1.140 + if err != nil { 1.141 + t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) 1.142 + } 1.143 + if len(results) != 3 { 1.144 + t.Logf("%+v", results) 1.145 + t.Errorf("Expected %d results, got %d from %T", 3, len(results), store) 1.146 + } 1.147 + for pos, s := range []scopeTypes.Scope{scope, scope2, scope3} { 1.148 + success, field, val1, val2 = compareScopes(s, results[pos]) 1.149 + if !success { 1.150 + t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) 1.151 + } 1.152 + } 1.153 + err = store.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID}, ctx) 1.154 + if err != nil { 1.155 + t.Errorf("Unexpected error removing scopes from %T: %+v", store, err) 1.156 + } 1.157 + results, err = store.ListScopes(ctx) 1.158 + if err != nil { 1.159 + t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) 1.160 + } 1.161 + if len(results) != 0 { 1.162 + t.Logf("%+v", results) 1.163 + t.Errorf("Expected %d results, got %d from %T", 0, len(results), store) 1.164 + } 1.165 + err = store.UpdateScope(update, ctx) 1.166 + if err != ErrScopeNotFound { 1.167 + t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err) 1.168 + } 1.169 + } 1.170 +}