package pact import "testing" // tokenScopeNames is the name table of TestFrontendTokenScopes; cabana's // TestTokenScopeNameParity checks its own validation against the same // names. var tokenScopeNames = []struct { name string ok bool }{ {"read", true}, {"servers:write", true}, {"a_b-c:d", true}, {"", false}, {"Read", false}, {"servers:", false}, {":x", false}, {"a:b:c", false}, {"*", false}, {"with space", false}, } // sampleScopesPlugin declares member token scopes, as an application's // plugin does for E-2. type sampleScopesPlugin struct{} func (sampleScopesPlugin) FrontendTokenScopes() []FrontendTokenScope { return []FrontendTokenScope{{Name: "servers:write", Label: "Report servers", Description: "Lets an agent report server components."}} } // TestFrontendTokenScopes pins plan 07's member token scope seam (E-2, // P-30): ValidTokenScopeName accepts and refuses the table's names, and a // plugin declaring FrontendTokenScopes is discovered through // HasFrontendTokenScopes (and not as an admin HasTokenScopes plugin). func TestFrontendTokenScopes(t *testing.T) { t.Run("ValidTokenScopeName", func(t *testing.T) { for _, c := range tokenScopeNames { if got := ValidTokenScopeName(c.name); got != c.ok { t.Errorf("ValidTokenScopeName(%q) = %v, want %v", c.name, got, c.ok) } } }) t.Run("a sample plugin is discovered", func(t *testing.T) { var plugin any = sampleScopesPlugin{} src, ok := plugin.(HasFrontendTokenScopes) if !ok { t.Fatal("the sample plugin is not a HasFrontendTokenScopes") } scopes := src.FrontendTokenScopes() if len(scopes) != 1 || scopes[0].Name != "servers:write" || !ValidTokenScopeName(scopes[0].Name) { t.Fatalf("scopes = %+v", scopes) } if _, admin := plugin.(HasTokenScopes); admin { t.Fatal("a member token scope plugin is also an admin token scope plugin") } }) }