From 521a47ad7adb108cdeba00e6429ec76687b28c3f Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 26 Jan 2021 10:35:03 -0500 Subject: [PATCH] [A0-CLI 17] feat: actions test (#28) --- go.mod | 3 +- go.sum | 7 +-- internal/auth0/actions.go | 8 +++ internal/auth0/auth0.go | 2 + internal/cli/actions.go | 60 +++++++++++++++++++ internal/display/actions.go | 5 ++ internal/display/display.go | 16 +++-- .../gopkg.in/auth0.v5/management/actions.go | 34 +++++++++++ .../auth0.v5/management/management.go | 16 +++-- vendor/modules.txt | 4 +- 10 files changed, 137 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 23dc70c9c..9e5a4615a 100644 --- a/go.mod +++ b/go.mod @@ -22,4 +22,5 @@ require ( ) // replace gopkg.in/auth0.v5 => ../auth0 -replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210125203113-388ed60f4d87 + +replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210126044025-c2f94c36f593 diff --git a/go.sum b/go.sum index 3cee8841c..ad707a2fd 100644 --- a/go.sum +++ b/go.sum @@ -78,8 +78,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-auth0/auth0 v1.3.1-0.20210125203113-388ed60f4d87 h1:u6DcEF5H9NkexI/ebnZkXj6ZtBJcnQBiOPZG0y0Frao= -github.com/go-auth0/auth0 v1.3.1-0.20210125203113-388ed60f4d87/go.mod h1:pbIRmwBulkHNKKsUGGvhyIOI2itMhz2OfwtPaFXBSSQ= +github.com/go-auth0/auth0 v1.3.1-0.20210126044025-c2f94c36f593 h1:Ty0FV0S+59z1ioKa6uM0WW9i39HgZM46oAtFh++rUHQ= +github.com/go-auth0/auth0 v1.3.1-0.20210126044025-c2f94c36f593/go.mod h1:pbIRmwBulkHNKKsUGGvhyIOI2itMhz2OfwtPaFXBSSQ= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -199,9 +199,7 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -358,7 +356,6 @@ golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fq golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/internal/auth0/actions.go b/internal/auth0/actions.go index 8ec27ba61..7b8ae74a4 100644 --- a/internal/auth0/actions.go +++ b/internal/auth0/actions.go @@ -10,3 +10,11 @@ type ActionAPI interface { Delete(id string, opts ...management.RequestOption) error List(opts ...management.RequestOption) (c *management.ActionList, err error) } + +type ActionVersionAPI interface { + Create(actionID string, v *management.ActionVersion) error + Read(actionID string, id string) (*management.ActionVersion, error) + Update(id string, a *management.ActionVersion) error + Delete(actionID string, id string, opts ...management.RequestOption) error + Test(actionID string, id string, payload management.Object) (management.Object, error) +} diff --git a/internal/auth0/auth0.go b/internal/auth0/auth0.go index 732c337e7..9052c1e38 100644 --- a/internal/auth0/auth0.go +++ b/internal/auth0/auth0.go @@ -9,6 +9,7 @@ import ( // the interfaces instead of the concrete structs. type API struct { Action ActionAPI + ActionVersion ActionVersionAPI Client ClientAPI Connection ConnectionAPI Log LogAPI @@ -19,6 +20,7 @@ type API struct { func NewAPI(m *management.Management) *API { return &API{ Action: m.Action, + ActionVersion: m.ActionVersion, Client: m.Client, Connection: m.Connection, Log: m.Log, diff --git a/internal/cli/actions.go b/internal/cli/actions.go index b21c8b00a..67620b035 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -1,6 +1,11 @@ package cli import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth0" "github.com/auth0/auth0-cli/internal/validators" @@ -16,6 +21,7 @@ func actionsCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.AddCommand(listActionsCmd(cli)) + cmd.AddCommand(testActionCmd(cli)) cmd.AddCommand(createActionCmd(cli)) return cmd @@ -44,6 +50,60 @@ Lists your existing actions. To create one try: return cmd } +func testActionCmd(cli *cli) *cobra.Command { + var actionId string + var versionId string + var payloadFile string + var payload = make(management.Object) + + cmd := &cobra.Command{ + Use: "test", + Short: "Test an action draft against a payload", + Long: `$ auth0 actions test --name --file `, + RunE: func(cmd *cobra.Command, args []string) error { + // Open our jsonFile + jsonFile, err := os.Open(payloadFile) + // if we os.Open returns an error then handle it + if err != nil { + return err + } + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + byteValue, err := ioutil.ReadAll(jsonFile) + if err != nil { + return err + } + + if err := json.Unmarshal([]byte(byteValue), &payload); err != nil { + return err + } + + var result management.Object + err = ansi.Spinner(fmt.Sprintf("Testing action: %s, version: %s", actionId, versionId), func() error { + fmt.Println(payload) + result, err = cli.api.ActionVersion.Test(actionId, versionId, payload) + return err + }) + + if err != nil { + return err + } + + cli.renderer.ActionTest(result) + return nil + }, + } + + cmd.Flags().StringVar(&actionId, "name", "", "Action ID to to test") + cmd.Flags().StringVarP(&payloadFile, "file", "f", "", "File containing the payload for the test") + cmd.Flags().StringVarP(&versionId, "version", "v", "draft", "Version ID of the action to test") + + mustRequireFlags(cmd, "name", "file") + + return cmd +} + func createActionCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ Use: "create", diff --git a/internal/display/actions.go b/internal/display/actions.go index e19234012..c94fc3e0e 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -46,6 +46,11 @@ func (r *Renderer) ActionList(actions []*management.Action) { r.Results(res) } +func (r *Renderer) ActionTest(payload management.Object) { + r.Heading(ansi.Bold(r.Tenant), "Actions test result\n") + r.JSONResult(payload) +} + func (r *Renderer) ActionCreate(action *management.Action) { r.Heading(ansi.Bold(r.Tenant), "action created\n") diff --git a/internal/display/display.go b/internal/display/display.go index ea352cd00..c1827633d 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -63,16 +63,20 @@ type View interface { AsTableRow() []string } +func (r *Renderer) JSONResult(data interface{}) { + b, err := json.MarshalIndent(data, "", " ") + if err != nil { + r.Errorf("couldn't marshal results as JSON: %v", err) + return + } + fmt.Fprint(r.ResultWriter, string(b)) +} + func (r *Renderer) Results(data []View) { if len(data) > 0 { switch r.Format { case OutputFormatJSON: - b, err := json.MarshalIndent(data, "", " ") - if err != nil { - r.Errorf("couldn't marshal results as JSON: %v", err) - return - } - fmt.Fprint(r.ResultWriter, string(b)) + r.JSONResult(data) default: rows := make([][]string, len(data)) diff --git a/vendor/gopkg.in/auth0.v5/management/actions.go b/vendor/gopkg.in/auth0.v5/management/actions.go index 9dd2f52aa..aa841c5e9 100644 --- a/vendor/gopkg.in/auth0.v5/management/actions.go +++ b/vendor/gopkg.in/auth0.v5/management/actions.go @@ -72,6 +72,19 @@ type ActionVersionList struct { Versions []*ActionVersion `json:"versions"` } +type ActionBinding struct { + ID *string `json:"id"` + TriggerID *TriggerID `json:"trigger_id,omitempty"` + Action *Action `json:"action,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` + DisplayName *string `json:"display_name,omitempty"` +} +type ActionBindingList struct { + List + Bindings []*ActionBinding `json:"bindings"` +} + type Object map[string]interface{} type ActionManager struct { @@ -129,6 +142,10 @@ type ActionVersionManager struct { *Management } +func newActionVersionManager(m *Management) *ActionVersionManager { + return &ActionVersionManager{m} +} + func (m *ActionVersionManager) Create(actionID string, v *ActionVersion) error { return m.Request("POST", m.URI("actions", "actions", actionID, "versions"), v) } @@ -168,3 +185,20 @@ func (m *ActionVersionManager) Test(actionID, id string, payload Object) (Object err := m.Request("POST", m.URI("actions", "actions", actionID, "versions", id, "test"), &v) return v, err } + +type ActionBindingManager struct { + *Management +} + +func newActionBindingManager(m *Management) *ActionBindingManager { + return &ActionBindingManager{m} +} + +func (m *ActionBindingManager) List(triggerID TriggerID, opts ...RequestOption) (c *ActionBindingList, err error) { + err = m.Request("GET", m.URI("actions", "triggers", string(triggerID), "bindings"), &c, applyActionsListDefaults(opts)) + return +} + +func (m *ActionBindingManager) Update(triggerID TriggerID, v *ActionBindingList) error { + return m.Request("PATCH", m.URI("actions", "triggers", string(triggerID), "bindings"), &v) +} diff --git a/vendor/gopkg.in/auth0.v5/management/management.go b/vendor/gopkg.in/auth0.v5/management/management.go index ececcf771..edd2d5b79 100644 --- a/vendor/gopkg.in/auth0.v5/management/management.go +++ b/vendor/gopkg.in/auth0.v5/management/management.go @@ -86,6 +86,12 @@ type Management struct { // Action manages Auth0 actions Action *ActionManager + // Action manages Auth0 actions bindings + ActionBinding *ActionBindingManager + + // Action manages Auth0 actions versions + ActionVersion *ActionVersionManager + // Client manages Auth0 Client (also known as Application) resources. Client *ClientManager @@ -200,6 +206,8 @@ func New(domain string, options ...ManagementOption) (*Management, error) { client.WithRateLimit()) m.Action = newActionManager(m) + m.ActionBinding = newActionBindingManager(m) + m.ActionVersion = newActionVersionManager(m) m.Client = newClientManager(m) m.ClientGrant = newClientGrantManager(m) m.Connection = newConnectionManager(m) @@ -350,10 +358,10 @@ func (m *managementError) Status() int { // Specific implementations embed this struct, therefore its direct use is not // useful. Rather it has been made public in order to aid documentation. type List struct { - Start int `json:"start"` - Limit int `json:"limit"` - Length int `json:"length"` - Total int `json:"total"` + Start int `json:"start,omitempty"` + Limit int `json:"limit,omitempty"` + Length int `json:"length,omitempty"` + Total int `json:"total,omitempty"` } func (l List) HasNext() bool { diff --git a/vendor/modules.txt b/vendor/modules.txt index 620f603f3..6779b94ae 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -120,7 +120,7 @@ google.golang.org/protobuf/reflect/protoreflect google.golang.org/protobuf/reflect/protoregistry google.golang.org/protobuf/runtime/protoiface google.golang.org/protobuf/runtime/protoimpl -# gopkg.in/auth0.v5 v5.8.0 => github.com/go-auth0/auth0 v1.3.1-0.20210125203113-388ed60f4d87 +# gopkg.in/auth0.v5 v5.8.0 => github.com/go-auth0/auth0 v1.3.1-0.20210126044025-c2f94c36f593 ## explicit gopkg.in/auth0.v5 gopkg.in/auth0.v5/internal/client @@ -131,4 +131,4 @@ gopkg.in/auth0.v5/management # gopkg.in/yaml.v2 v2.2.8 ## explicit gopkg.in/yaml.v2 -# gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210125203113-388ed60f4d87 +# gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210126044025-c2f94c36f593