Skip to content

Commit

Permalink
[A0-CLI 17] feat: actions test (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
turcottedanny authored Jan 26, 2021
1 parent 5d7c881 commit 521a47a
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 18 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
8 changes: 8 additions & 0 deletions internal/auth0/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions internal/auth0/auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
60 changes: 60 additions & 0 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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 <actionid> --file <payload.json>`,
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",
Expand Down
5 changes: 5 additions & 0 deletions internal/display/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
16 changes: 10 additions & 6 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
34 changes: 34 additions & 0 deletions vendor/gopkg.in/auth0.v5/management/actions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions vendor/gopkg.in/auth0.v5/management/management.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 521a47a

Please sign in to comment.