Skip to content

Commit

Permalink
Add a simple unit test to catch regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 26, 2021
1 parent c786040 commit 62e123f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var requiredScopes = []string{
"create:hooks", "delete:hooks", "read:hooks", "update:hooks",
"create:resource_servers", "delete:resource_servers", "read:resource_servers", "update:resource_servers",
"create:rules", "delete:rules", "read:rules", "update:rules",
"read:logs",
"read:client_keys", "read:logs",
}

type Authenticator struct {
Expand Down
49 changes: 49 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package auth

import "testing"

func TestRequiredScopes(t *testing.T) {
t.Run("verify CRUD", func(t *testing.T) {
crudResources := []string{
"actions",
"clients",
"connections",
"hooks",
"resource_servers",
"rules",
}
crudPrefixes := []string{"create:", "delete:", "read:", "update:"}

for _, resource := range crudResources {
for _, prefix := range crudPrefixes {
scope := prefix + resource

if !strInArray(requiredScopes, scope) {
t.Fatalf("wanted scope: %q, list: %+v", scope, requiredScopes)
}
}
}
})

t.Run("verify special scopes", func(t *testing.T) {
list := []string{
"read:client_keys", "read:logs",
}

for _, v := range list {
if !strInArray(requiredScopes, v) {
t.Fatalf("wanted scope: %q, list: %+v", v, requiredScopes)
}
}
})
}

func strInArray(haystack []string, needle string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}

return false
}

0 comments on commit 62e123f

Please sign in to comment.