auth
153:3e8964a914ef Browse Files
Fix tests for scopeStore. Update all our tests to use the PG_TEST_DB environment variable if set, and use that to control whether or not the postgres tests get run. testing.Short() just wasn't working. Update ErrScopeNotFound and ErrScopeAlreadyExists to be variables instead of types, because PostgreSQL (annoyingly) offers no way to determine which specific row insertion caused the problem, and I anticipate this being a problem that is ongoing. So it's really just not worth it. Stop getScopes from returning an ErrScopeNotFound. Let's return what we find, and let the absence of what we didn't find speak for itself. Fix an error with generating the SQL for the postgres.createScopes call. We used to generate it in a way that was invalid (not joining values with ",") when more than one set of values was supplied. Hooray, testing! Update the postgres scopeStore to return ErrScopeNotFound and ErrScopeAlreadyExists errors, as appropriate. Update our tests to reflect that ErrScopeNotFound and ErrScopeAlreadyExists are now variables, not types.
client_test.go oauth2.go profile_test.go scope.go scope_postgres.go scope_test.go
1.1 --- a/client_test.go Tue Mar 24 20:39:03 2015 -0400 1.2 +++ b/client_test.go Tue Mar 24 21:50:42 2015 -0400 1.3 @@ -9,6 +9,7 @@ 1.4 "net/http" 1.5 "net/http/httptest" 1.6 "net/url" 1.7 + "os" 1.8 "sort" 1.9 "strings" 1.10 "testing" 1.11 @@ -26,11 +27,11 @@ 1.12 ) 1.13 1.14 func init() { 1.15 - p, err := NewPostgres("dbname=testdb sslmode=disable") 1.16 - if err != nil { 1.17 - panic(err) 1.18 - } 1.19 - if !testing.Short() { 1.20 + if os.Getenv("PG_TEST_DB") != "" { 1.21 + p, err := NewPostgres(os.Getenv("PG_TEST_DB")) 1.22 + if err != nil { 1.23 + panic(err) 1.24 + } 1.25 clientStores = append(clientStores, &p) 1.26 } 1.27 }
2.1 --- a/oauth2.go Tue Mar 24 20:39:03 2015 -0400 2.2 +++ b/oauth2.go Tue Mar 24 21:50:42 2015 -0400 2.3 @@ -281,7 +281,7 @@ 2.4 scopeParams := strings.Split(r.URL.Query().Get("scope"), " ") 2.5 scopes, err := context.GetScopes(scopeParams) 2.6 if err != nil { 2.7 - if _, ok := err.(ErrScopeNotFound); ok { 2.8 + if err == ErrScopeNotFound { 2.9 q.Add("error", "invalid_scope") 2.10 redirectURL.RawQuery = q.Encode() 2.11 http.Redirect(w, r, redirectURL.String(), http.StatusFound)
3.1 --- a/profile_test.go Tue Mar 24 20:39:03 2015 -0400 3.2 +++ b/profile_test.go Tue Mar 24 21:50:42 2015 -0400 3.3 @@ -2,6 +2,7 @@ 3.4 3.5 import ( 3.6 "fmt" 3.7 + "os" 3.8 "testing" 3.9 "time" 3.10 3.11 @@ -22,11 +23,11 @@ 3.12 ) 3.13 3.14 func init() { 3.15 - p, err := NewPostgres("dbname=testdb sslmode=disable") 3.16 - if err != nil { 3.17 - panic(err) 3.18 - } 3.19 - if !testing.Short() { 3.20 + if os.Getenv("PG_TEST_DB") != "" { 3.21 + p, err := NewPostgres(os.Getenv("PG_TEST_DB")) 3.22 + if err != nil { 3.23 + panic(err) 3.24 + } 3.25 profileStores = append(profileStores, &p) 3.26 } 3.27 }
4.1 --- a/scope.go Tue Mar 24 20:39:03 2015 -0400 4.2 +++ b/scope.go Tue Mar 24 21:50:42 2015 -0400 4.3 @@ -2,32 +2,15 @@ 4.4 4.5 import ( 4.6 "errors" 4.7 - "fmt" 4.8 "sort" 4.9 ) 4.10 4.11 var ( 4.12 - ErrNoScopeStore = errors.New("scopeStore not set in Context") 4.13 + ErrNoScopeStore = errors.New("scopeStore not set in Context") 4.14 + ErrScopeNotFound = errors.New("scope not found") 4.15 + ErrScopeAlreadyExists = errors.New("scope already exists") 4.16 ) 4.17 4.18 -type ErrScopeNotFound struct { 4.19 - Pos int 4.20 - ID string 4.21 -} 4.22 - 4.23 -func (e ErrScopeNotFound) Error() string { 4.24 - return fmt.Sprintf("scope %s couldn't be found", e.ID) 4.25 -} 4.26 - 4.27 -type ErrScopeAlreadyExists struct { 4.28 - Pos int 4.29 - ID string 4.30 -} 4.31 - 4.32 -func (e ErrScopeAlreadyExists) Error() string { 4.33 - return fmt.Sprintf("scope %s already exists", e.ID) 4.34 -} 4.35 - 4.36 // Scope represents a limit on the access that a grant provides. 4.37 type Scope struct { 4.38 ID string 4.39 @@ -80,9 +63,9 @@ 4.40 m.scopeLock.Lock() 4.41 defer m.scopeLock.Unlock() 4.42 4.43 - for pos, scope := range scopes { 4.44 + for _, scope := range scopes { 4.45 if _, ok := m.scopes[scope.ID]; ok { 4.46 - return ErrScopeAlreadyExists{Pos: pos, ID: scope.ID} 4.47 + return ErrScopeAlreadyExists 4.48 } 4.49 } 4.50 for _, scope := range scopes { 4.51 @@ -96,10 +79,10 @@ 4.52 defer m.scopeLock.RUnlock() 4.53 4.54 scopes := []Scope{} 4.55 - for pos, id := range ids { 4.56 + for _, id := range ids { 4.57 scope, ok := m.scopes[id] 4.58 if !ok { 4.59 - return []Scope{}, ErrScopeNotFound{ID: id, Pos: pos} 4.60 + continue 4.61 } 4.62 scopes = append(scopes, scope) 4.63 } 4.64 @@ -115,7 +98,7 @@ 4.65 4.66 scope, ok := m.scopes[id] 4.67 if !ok { 4.68 - return ErrScopeNotFound{Pos: 0, ID: id} 4.69 + return ErrScopeNotFound 4.70 } 4.71 scope.ApplyChange(change) 4.72 m.scopes[id] = scope 4.73 @@ -126,9 +109,9 @@ 4.74 m.scopeLock.Lock() 4.75 defer m.scopeLock.Unlock() 4.76 4.77 - for pos, id := range ids { 4.78 + for _, id := range ids { 4.79 if _, ok := m.scopes[id]; !ok { 4.80 - return ErrScopeNotFound{Pos: pos, ID: id} 4.81 + return ErrScopeNotFound 4.82 } 4.83 } 4.84 for _, id := range ids {
5.1 --- a/scope_postgres.go Tue Mar 24 20:39:03 2015 -0400 5.2 +++ b/scope_postgres.go Tue Mar 24 21:50:42 2015 -0400 5.3 @@ -1,6 +1,7 @@ 5.4 package auth 5.5 5.6 import ( 5.7 + "github.com/lib/pq" 5.8 "github.com/secondbit/pan" 5.9 ) 5.10 5.11 @@ -13,11 +14,12 @@ 5.12 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0])) 5.13 query.Include("(" + pan.QueryList(fields) + ")") 5.14 query.Include("VALUES") 5.15 + query.FlushExpressions(" ") 5.16 for _, scope := range scopes { 5.17 _, values := pan.GetFields(scope) 5.18 query.Include("("+pan.VariableList(len(values))+")", values...) 5.19 } 5.20 - return query.FlushExpressions(" ") 5.21 + return query.FlushExpressions(", ") 5.22 } 5.23 5.24 func (p *postgres) createScopes(scopes []Scope) error { 5.25 @@ -26,6 +28,9 @@ 5.26 } 5.27 query := p.createScopesSQL(scopes) 5.28 _, err := p.db.Exec(query.String(), query.Args...) 5.29 + if e, ok := err.(*pq.Error); ok && e.Constraint == "scopes_pkey" { 5.30 + err = ErrScopeAlreadyExists 5.31 + } 5.32 return err 5.33 } 5.34 5.35 @@ -80,7 +85,17 @@ 5.36 return nil 5.37 } 5.38 query := p.updateScopeSQL(id, change) 5.39 - _, err := p.db.Exec(query.String(), query.Args...) 5.40 + res, err := p.db.Exec(query.String(), query.Args...) 5.41 + if err != nil { 5.42 + return err 5.43 + } 5.44 + rows, err := res.RowsAffected() 5.45 + if err != nil { 5.46 + return err 5.47 + } 5.48 + if rows < 1 { 5.49 + return ErrScopeNotFound 5.50 + } 5.51 return err 5.52 } 5.53 5.54 @@ -99,10 +114,17 @@ 5.55 5.56 func (p *postgres) removeScopes(ids []string) error { 5.57 query := p.removeScopesSQL(ids) 5.58 - _, err := p.db.Exec(query.String(), query.Args...) 5.59 + res, err := p.db.Exec(query.String(), query.Args...) 5.60 if err != nil { 5.61 return err 5.62 } 5.63 + rows, err := res.RowsAffected() 5.64 + if err != nil { 5.65 + return err 5.66 + } 5.67 + if rows < 1 { 5.68 + return ErrScopeNotFound 5.69 + } 5.70 return nil 5.71 } 5.72
6.1 --- a/scope_test.go Tue Mar 24 20:39:03 2015 -0400 6.2 +++ b/scope_test.go Tue Mar 24 21:50:42 2015 -0400 6.3 @@ -1,7 +1,18 @@ 6.4 package auth 6.5 6.6 +import "os" 6.7 import "testing" 6.8 6.9 +func init() { 6.10 + if os.Getenv("PG_TEST_DB") != "" { 6.11 + p, err := NewPostgres(os.Getenv("PG_TEST_DB")) 6.12 + if err != nil { 6.13 + panic(err) 6.14 + } 6.15 + scopeStores = append(scopeStores, &p) 6.16 + } 6.17 +} 6.18 + 6.19 var scopeStores = []scopeStore{NewMemstore()} 6.20 6.21 func compareScopes(scope1, scope2 Scope) (success bool, field string, val1, val2 interface{}) { 6.22 @@ -52,30 +63,13 @@ 6.23 t.Logf("%+v", retrieved) 6.24 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store) 6.25 } 6.26 - if e, ok := err.(ErrScopeNotFound); !ok { 6.27 - t.Errorf("Expected ErrScopeNotFound, got %+v instead for %T", err, store) 6.28 - } else { 6.29 - if e.Pos != 0 { 6.30 - t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store) 6.31 - } 6.32 - if e.ID != scope.ID { 6.33 - t.Errorf("Expected the error to be with scope %s, got %s from %T", scope.ID, e.ID, store) 6.34 - } 6.35 - } 6.36 err = context.CreateScopes([]Scope{scope}) 6.37 if err != nil { 6.38 t.Errorf("Error saving scope to %T: %s", store, err) 6.39 } 6.40 err = context.CreateScopes([]Scope{scope}) 6.41 - if e, ok := err.(ErrScopeAlreadyExists); !ok { 6.42 + if err != ErrScopeAlreadyExists { 6.43 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store) 6.44 - } else { 6.45 - if e.Pos != 0 { 6.46 - t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store) 6.47 - } 6.48 - if e.ID != scope.ID { 6.49 - t.Errorf("Expected the error to be for ID %s, got %s from %T", scope.ID, e.ID, store) 6.50 - } 6.51 } 6.52 retrieved, err = context.GetScopes([]string{scope.ID}) 6.53 if err != nil { 6.54 @@ -152,41 +146,12 @@ 6.55 if err == nil { 6.56 t.Errorf("No error returned removing non-existent scopes from %T", store) 6.57 } 6.58 - if e, ok := err.(ErrScopeNotFound); !ok { 6.59 + if err != ErrScopeNotFound { 6.60 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err) 6.61 - } else { 6.62 - if e.Pos != 0 { 6.63 - t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store) 6.64 - } 6.65 - if e.ID != scope.ID { 6.66 - t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store) 6.67 - } 6.68 } 6.69 err = context.UpdateScope(scope.ID, update) 6.70 - if err == nil { 6.71 - t.Errorf("No error returned updating non-existent scopes from %T", store) 6.72 - } 6.73 - if e, ok := err.(ErrScopeNotFound); !ok { 6.74 + if err != ErrScopeNotFound { 6.75 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err) 6.76 - } else { 6.77 - if e.Pos != 0 { 6.78 - t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store) 6.79 - } 6.80 - if e.ID != scope.ID { 6.81 - t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store) 6.82 - } 6.83 } 6.84 } 6.85 } 6.86 - 6.87 -func TestScopeErrors(t *testing.T) { 6.88 - errors := map[string]error{ 6.89 - "scope test couldn't be found": ErrScopeNotFound{ID: "test", Pos: 0}, 6.90 - "scope test already exists": ErrScopeAlreadyExists{ID: "test", Pos: 0}, 6.91 - } 6.92 - for expectation, e := range errors { 6.93 - if e.Error() != expectation { 6.94 - t.Errorf("Expected %+v to produce '%s', produced '%s'", e, expectation, e.Error()) 6.95 - } 6.96 - } 6.97 -}