Tests are working again

This commit is contained in:
luke-hagar-sp
2023-05-17 18:49:52 -05:00
parent 2cdc756291
commit d2d4c823d6
6 changed files with 37 additions and 48 deletions

View File

@@ -2,20 +2,22 @@
package transform
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/charmbracelet/log"
sailpointbetasdk "github.com/sailpoint-oss/golang-sdk/beta"
"github.com/sailpoint-oss/sailpoint-cli/internal/config"
"github.com/sailpoint-oss/sailpoint-cli/internal/sdk"
"github.com/sailpoint-oss/sailpoint-cli/internal/util"
"github.com/spf13/cobra"
)
func newCreateCmd() *cobra.Command {
var filepath string
cmd := &cobra.Command{
Use: "create",
Short: "Create an IdentityNow Transform from a file",
@@ -25,44 +27,44 @@ func newCreateCmd() *cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var transform sailpointbetasdk.Transform
var decoder *json.Decoder
filepath := cmd.Flags().Lookup("file").Value.String()
if filepath != "" {
file, err := os.Open(filepath)
if err != nil {
return err
}
defer file.Close()
err = json.NewDecoder(file).Decode(&transform)
if err != nil {
return err
}
decoder = json.NewDecoder(bufio.NewReader(file))
} else {
err := json.NewDecoder(os.Stdin).Decode(&transform)
if err != nil {
decoder = json.NewDecoder(bufio.NewReader(os.Stdin))
}
if err := decoder.Decode(&transform); err != nil {
return err
}
}
log.Debug("Transform", "transform", util.PrettyPrint(transform))
log.Debug("Filepath", "path", filepath)
if transform.Name == "" {
log.Debug("Transform", "transform", transform)
if transform.GetName() == "" {
return fmt.Errorf("the transform must have a name")
}
if transform.Id != nil {
if transform.GetId() != "" {
return fmt.Errorf("the transform cannot have an ID")
}
createTransform := sailpointbetasdk.NewTransform(transform.Name, transform.Type, transform.Attributes)
apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
transformObj, resp, err := apiClient.Beta.TransformsApi.CreateTransform(context.TODO()).Transform(*createTransform).Execute()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
transformObj, resp, err := apiClient.Beta.TransformsApi.CreateTransform(ctx).Transform(transform).Execute()
if err != nil {
return sdk.HandleSDKError(resp, err)
}
@@ -75,7 +77,7 @@ func newCreateCmd() *cobra.Command {
},
}
cmd.Flags().StringP("file", "f", "", "The path to the transform file")
cmd.Flags().StringVarP(&filepath, "file", "f", "", "The path to the transform file")
return cmd
}

View File

@@ -7,13 +7,12 @@ import (
"encoding/json"
"math/rand"
"os"
"path/filepath"
PATH "path/filepath"
"testing"
"time"
"github.com/charmbracelet/log"
"github.com/golang/mock/gomock"
sailpointsdk "github.com/sailpoint-oss/golang-sdk/v3"
"github.com/sailpoint-oss/sailpoint-cli/internal/config"
"github.com/sailpoint-oss/sailpoint-cli/internal/util"
@@ -47,15 +46,14 @@ func randSeq(n int) string {
return string(b)
}
func SaveTransform(filePath string, transform map[string]interface{}) error {
// Make sure to create the files if they dont exist
file, err := os.OpenFile((PATH.Join(path, filePath)), os.O_RDWR|os.O_CREATE, 0777)
func SaveTransform(fileName string, transform map[string]interface{}) error {
file, err := os.OpenFile((PATH.Join(path, fileName)), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
createString, err := json.MarshalIndent(transform, "", " ")
createString, err := json.Marshal(transform)
if err != nil {
return err
}
@@ -71,9 +69,9 @@ func SaveTransform(filePath string, transform map[string]interface{}) error {
if err != nil {
return err
}
return nil
}
func TestNewCRUDCmd(t *testing.T) {
var transform sailpointsdk.Transform
@@ -83,7 +81,7 @@ func TestNewCRUDCmd(t *testing.T) {
t.Fatalf("Error unmarshalling template: %v", err)
}
transformName := randSeq(6)
transformName := randSeq(16)
createTransform := make(map[string]interface{})
createTransform["name"] = transformName
@@ -103,14 +101,14 @@ func TestNewCRUDCmd(t *testing.T) {
t.Fatalf("Unable to save test data: %v", err)
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()
createCMD := newCreateCmd()
createBuffer := new(bytes.Buffer)
createCMD.SetOut(createBuffer)
createCMD.Flags().Set("file", filepath.Join(path, createFile))
createCMD.Flags().Set("file", PATH.Join(path, createFile))
err = createCMD.Execute()
if err != nil {
@@ -120,12 +118,12 @@ func TestNewCRUDCmd(t *testing.T) {
transformID := string(createBuffer.String())
t.Log(transformID)
Attributes := make(map[string]interface{})
Attributes := make(map[string]string)
Attributes["substring"] = randSeq(24)
updateTransform := make(map[string]interface{})
updateTransform["attributes"] = Attributes
updateTransform["name"] = transform.Name
updateTransform["name"] = transformName
updateTransform["type"] = transform.Type
updateTransform["id"] = transformID
@@ -138,7 +136,7 @@ func TestNewCRUDCmd(t *testing.T) {
cmd := newUpdateCmd()
cmd.Flags().Set("file", filepath.Join(path, updateFile))
cmd.Flags().Set("file", PATH.Join(path, updateFile))
err = cmd.Execute()
if err != nil {

8
cmd/transform/test_data/test_create.json Executable file → Normal file
View File

@@ -1,7 +1 @@
{
"attributes": {
"substring": "admin_"
},
"name": "LgQqTz",
"type": "indexOf"
}
{"attributes":{"substring":"admin_"},"name":"hvmqNSbKOnZjCjsL","type":"indexOf"}

9
cmd/transform/test_data/test_update.json Executable file → Normal file
View File

@@ -1,8 +1 @@
{
"attributes": {
"substring": "xLoHZwUplSEJoYVYsQJpIpQO"
},
"id": "21d0bcf9-ae8f-4c98-9dbf-24ea8a12b339",
"name": "Test Index Of Transform",
"type": "indexOf"
}
{"attributes":{"substring":"vWUmzobJBFMbfToJfrBFIHjc"},"id":"e73acbf8-20bf-496a-85bf-21425cb7e111","name":"hvmqNSbKOnZjCjsL","type":"indexOf"}

2
go.mod
View File

@@ -15,7 +15,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/sftp v1.13.5
github.com/sailpoint-oss/golang-sdk v1.0.4
github.com/sailpoint-oss/golang-sdk v1.0.5
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5

2
go.sum
View File

@@ -254,6 +254,8 @@ github.com/sailpoint-oss/golang-sdk v1.0.3 h1:uJzKJ2+gOdVY7sbpjpvm4O4c3woAjBSx9A
github.com/sailpoint-oss/golang-sdk v1.0.3/go.mod h1:k8tO4zw0wmivf5NjrPE2tF+ToCr6AJUV9BJnyGW4/rA=
github.com/sailpoint-oss/golang-sdk v1.0.4 h1:NLdExj3bb7NWKhHnzReP0ZiSUNR+Nlqd58HjHJtue+I=
github.com/sailpoint-oss/golang-sdk v1.0.4/go.mod h1:k8tO4zw0wmivf5NjrPE2tF+ToCr6AJUV9BJnyGW4/rA=
github.com/sailpoint-oss/golang-sdk v1.0.5 h1:eu0JGrpfzgWDXrdSXkY2sjs0vZ+2iGRN7zcntEDAE7U=
github.com/sailpoint-oss/golang-sdk v1.0.5/go.mod h1:k8tO4zw0wmivf5NjrPE2tF+ToCr6AJUV9BJnyGW4/rA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=