Skip to content

Commit

Permalink
[ci] increased coverage (#695)
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart authored Jan 5, 2025
1 parent 098fa71 commit 8f9a581
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 37 deletions.
66 changes: 66 additions & 0 deletions cmd/accountusercontextparams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"github.com/nats-io/cliprompts/v2"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"testing"
)

type tp struct {
AccountUserContextParams
}

func (p *tp) SetDefaults(ctx ActionCtx) error {
return p.AccountUserContextParams.SetDefaults(ctx)
}

func (p *tp) Validate(ctx ActionCtx) error {
return p.AccountUserContextParams.Validate(ctx)
}

func (p *tp) Load(ctx ActionCtx) error {
return nil
}

func (p *tp) Run(ctx ActionCtx) (store.Status, error) {
return nil, nil
}

func (p *tp) PreInteractive(ctx ActionCtx) error {
return p.AccountUserContextParams.Edit(ctx)
}

func (p *tp) PostInteractive(ctx ActionCtx) error {
return nil
}

func Test_AccountUserContextParams(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "a")
ts.AddAccount(t, "B")
ts.AddUser(t, "B", "b")

var params tp
cmd := &cobra.Command{
Use: "ucp",
SilenceUsage: true,

RunE: func(cmd *cobra.Command, args []string) error {
return RunAction(cmd, args, &params)
},
}

cliprompts.LogFn = t.Log
// A, a
inputs := []interface{}{0, 0}

_, _, err := ExecuteInteractiveCmd(cmd, inputs)
require.NoError(t, err)
require.Equal(t, "A", params.AccountContextParams.Name)
require.Equal(t, "a", params.UserContextParams.Name)
}
10 changes: 5 additions & 5 deletions cmd/deleteimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Test_DeleteImport(t *testing.T) {
ts.AddExport(t, "A", jwt.Stream, "bar", 0, false)

ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", "bar", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "bar", "B")

tests := CmdTests{
{createDeleteImportCmd(), []string{"delete", "import", "--account", "A"}, nil, []string{"account \"A\" doesn't have imports"}, true},
Expand All @@ -51,7 +51,7 @@ func Test_DeleteImportAccountRequired(t *testing.T) {
ts.AddAccount(t, "A")
ts.AddExport(t, "A", jwt.Stream, "foo", 0, false)
ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")

GetConfig().SetAccount("")
_, _, err := ExecuteCmd(createDeleteImportCmd(), "--subject", "A")
Expand All @@ -68,8 +68,8 @@ func Test_DeleteImportInteractive(t *testing.T) {
ts.AddExport(t, "A", jwt.Stream, "bar", 0, false)

ts.AddAccount(t, "B")
ts.AddImport(t, "A", "foo", "B")
ts.AddImport(t, "A", "bar", "B")
ts.AddImport(t, "A", jwt.Stream, "foo", "B")
ts.AddImport(t, "A", jwt.Stream, "bar", "B")

input := []interface{}{1, 0, 0}
cmd := createDeleteImportCmd()
Expand Down
86 changes: 86 additions & 0 deletions cmd/describeaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/nats-io/nkeys"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/nats-io/jwt/v2"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -309,3 +311,87 @@ func TestDescribeAccount_Exports(t *testing.T) {
require.Contains(t, out, "| Account Token Position |")
require.Contains(t, out, "foo.bar.*.> | 3")
}

func TestDescribeAccountMore(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("running in windows - looking at output hangs")
}
ts := NewTestStore(t, "O")
defer ts.Done(t)
ts.AddAccount(t, "A")
ac, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)
ac.Description = "hello"
ac.InfoURL = "https://example.com"
_, signingKey, _ := CreateAccountKey(t)
ac.SigningKeys.Add(signingKey)

_, issuer, _ := CreateAccountKey(t)
scope := jwt.NewUserScope()
scope.Key = issuer
scope.Role = "nothing"
scope.Description = "no permissions"
scope.Template = jwt.UserPermissionLimits{
Permissions: jwt.Permissions{
Sub: jwt.Permission{Deny: []string{">"}},
Pub: jwt.Permission{Deny: []string{">"}},
},
}
ac.SigningKeys.AddScopedSigner(scope)

ac.Limits.JetStreamLimits = jwt.JetStreamLimits{DiskStorage: -1, MemoryStorage: -1}
ac.Limits.LeafNodeConn = 1

_, user, _ := CreateUserKey(t)
ac.Revocations = jwt.RevocationList{}
ac.Revocations.Revoke(user, time.Now())

ac.Trace = &jwt.MsgTrace{
Destination: "foo",
Sampling: 100,
}

ekp, err := nkeys.CreateUser()
require.NoError(t, err)
a, err := ekp.PublicKey()
require.NoError(t, err)
ac.Imports.Add(&jwt.Import{
Name: "hello",
Subject: "bar.>",
LocalSubject: "fromA.>",
Type: jwt.Stream,
AllowTrace: true,
Share: true,
Account: a,
})

ac.Mappings = make(map[jwt.Subject][]jwt.WeightedMapping)
ac.Mappings["mapfoo"] = []jwt.WeightedMapping{jwt.WeightedMapping{Subject: "map.>", Weight: 20, Cluster: "a"}}

token, err := ac.Encode(ts.OperatorKey)
require.NoError(t, err)
_, err = ts.Store.StoreClaim([]byte(token))
require.NoError(t, err)

out, _, err := ExecuteCmd(rootCmd, "describe", "account", "-n", "A")
require.NoError(t, err)

out = StripMultipleSpaces(out)
t.Log(out)
require.Contains(t, out, "| Description | hello")
require.Contains(t, out, "| Info Url | https://example.com")
// order of the key may be unexpected, just find the key
require.Contains(t, out, signingKey)
require.Contains(t, out, "| Max Disk Storage | Unlimited")
require.Contains(t, out, "| Max Mem Storage | Unlimited")
require.Contains(t, out, "| Max Leaf Node Connections | 1")
require.Contains(t, out, "| Revocations | 1")
require.Contains(t, out, "| Subject | foo")
require.Contains(t, out, "| Sampling | 100%")
require.Contains(t, out, "| hello | Stream | bar.> | fromA.>")
require.Contains(t, out, "| mapfoo | map.> | 20")

require.Contains(t, out, "| Key | "+issuer)
require.Contains(t, out, "| Role | nothing")
require.Contains(t, out, "| Description | no permissions")
}
1 change: 0 additions & 1 deletion cmd/describeuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"testing"

"github.com/nats-io/jwt/v2"

"github.com/stretchr/testify/require"
)

Expand Down
49 changes: 37 additions & 12 deletions cmd/fixenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ package cmd

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/stretchr/testify/require"
"path/filepath"
"testing"
"time"
)

func Test_FixRequiresInArg(t *testing.T) {
Expand Down Expand Up @@ -62,13 +61,6 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o.jwt"), []byte(otok)))

// and another with a tag
oc.Tags.Add("test")
time.Sleep(time.Second)
otok2, err := oc.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o2.jwt"), []byte(otok2)))

ask, apk, akp := CreateAccountKey(t)
require.NoError(t, Write(filepath.Join(in, "apk.nk"), ask))
ac := jwt.NewAccountClaims(apk)
Expand All @@ -85,8 +77,35 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u.jwt"), []byte(utok)))

usk2, upk2, _ := CreateUserKey(t)
uc2 := jwt.NewUserClaims(upk2)
uc2.Name = "U2"
u2tok, err := uc2.Encode(akp)
require.NoError(t, err)
creds, err := jwt.FormatUserConfig(u2tok, usk2)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u2.creds"), creds))

// and copy of operator with a tag (and newer date)
time.Sleep(time.Second)

oc.Tags.Add("test")
otok2, err := oc.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "o2.jwt"), []byte(otok2)))

ac.Tags.Add("test")
atok, err = ac.Encode(okp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "a2.jwt"), []byte(atok)))

uc.Tags.Add("test")
utok, err = uc.Encode(akp)
require.NoError(t, err)
require.NoError(t, Write(filepath.Join(in, "u2.jwt"), []byte(utok)))

ofp := filepath.Join(ts.Dir, "out")
_, _, err = ExecuteCmd(createFixCmd(), "--in", in, "--out", ofp)
_, _, err = ExecuteCmd(createFixCmd(), "--creds", "--in", in, "--out", ofp)
require.NoError(t, err)

s, err := store.LoadStore(filepath.Join(ofp, "operators", "O"))
Expand All @@ -102,11 +121,17 @@ func Test_FixBasics(t *testing.T) {
require.NoError(t, err)
require.Equal(t, apk, aac.Subject)
require.Equal(t, "A", aac.Name)
require.True(t, aac.Tags.Contains("test"))

uuc, err := s.ReadUserClaim("A", "U")
require.NoError(t, err)
require.Equal(t, upk, uuc.Subject)
require.Equal(t, "U", uuc.Name)
require.True(t, uuc.Tags.Contains("test"))

uuc2, err := s.ReadUserClaim("A", "U2")
require.NoError(t, err)
require.Equal(t, upk2, uuc2.Subject)

okf := filepath.Join(ofp, "keys", "keys", opk[:1], opk[1:3], fmt.Sprintf("%s.nk", opk))
require.FileExists(t, okf)
Expand Down
41 changes: 29 additions & 12 deletions cmd/generatediagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ import (
"github.com/spf13/cobra"
)

var accDetail bool
var outputFile string
var users, showKeys, detail bool

func init() {
func createDiagramCmd() *cobra.Command {
diagram := &cobra.Command{
Use: "diagram",
Short: "Generate diagrams for this store",
Args: MaxArgs(0),
SilenceUsage: true,
}
accDetail := false
comp := &cobra.Command{
return diagram
}

func createComponentDiagreamCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "component",
Short: "Generate a plantuml component diagram for this store",
Args: MaxArgs(0),
Expand All @@ -47,10 +52,14 @@ func init() {
return componentDiagram(accDetail)
},
}
comp.Flags().BoolVarP(&accDetail, "detail", "", false, "Include account descriptions")
diagram.AddCommand(comp)
showKeys, detail, users := false, false, false
object := &cobra.Command{
cmd.Flags().BoolVarP(&accDetail, "detail", "", false, "Include account descriptions")
cmd.Flags().StringVarP(&outputFile, "output-file", "o", "--", "output file, '--' is stdout")

return cmd
}

func createObjectDiagramCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "object",
Short: "Generate a plantuml object diagram for this store",
Args: MaxArgs(0),
Expand All @@ -60,12 +69,20 @@ func init() {
return objectDiagram(users, showKeys, detail)
},
}
object.Flags().BoolVarP(&showKeys, "show-keys", "", false, "Include keys in diagram")
object.Flags().BoolVarP(&users, "users", "", false, "Include User")
object.Flags().BoolVarP(&detail, "detail", "", false, "Include empty/unlimited values")
diagram.AddCommand(object)
diagram.PersistentFlags().StringVarP(&outputFile, "output-file", "o", "--", "output file, '--' is stdout")

cmd.Flags().BoolVarP(&showKeys, "show-keys", "", false, "Include keys in diagram")
cmd.Flags().BoolVarP(&users, "users", "", false, "Include User")
cmd.Flags().BoolVarP(&detail, "detail", "", false, "Include empty/unlimited values")
cmd.Flags().StringVarP(&outputFile, "output-file", "o", "--", "output file, '--' is stdout")

return cmd
}

func init() {
diagram := createDiagramCmd()
generateCmd.AddCommand(diagram)
diagram.AddCommand(createComponentDiagreamCmd())
diagram.AddCommand(createObjectDiagramCmd())
}

const rename = "<&resize-width>"
Expand Down
Loading

0 comments on commit 8f9a581

Please sign in to comment.