From d96e7fe2818072b8d68a5b2d17aa859b7dd7d33c Mon Sep 17 00:00:00 2001 From: Sampriti-Mitra Date: Sun, 10 Oct 2021 14:35:13 +0530 Subject: [PATCH 1/3] added test for site set --- cmd/set/site/set_test.go | 132 +++++++++++++++++++++++++++++++++++++++ go.mod | 1 + 2 files changed, 133 insertions(+) create mode 100644 cmd/set/site/set_test.go diff --git a/cmd/set/site/set_test.go b/cmd/set/site/set_test.go new file mode 100644 index 0000000..c63c7b8 --- /dev/null +++ b/cmd/set/site/set_test.go @@ -0,0 +1,132 @@ +package site + +import ( + "encoding/json" + "fmt" + "github.com/stretchr/testify/assert" + "io/ioutil" + "os" + "os/user" + "path/filepath" + "testing" +) + +type TokenResp struct { + AppName string `json:"app_name"` + XSign string `json:"xsign"` + XToken string `json:"xtoken"` +} + +func TestSetSite(t *testing.T) { + + prevAppId := appid + + user, _ := user.Current() + + baseFileName := filepath.Join(user.HomeDir, ".lrcli") + + defer func() { + appid = prevAppId + }() + + tests := []struct { + name string + args map[string]interface{} + want string + wantErr bool + }{ + { + "invalid app id", + map[string]interface{}{ + "appId": int64(123456), + "pathToSiteInfo": "siteInfo.json", + "pathToToken": "token.json", + }, + "There is no site with this AppID.\n", + false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + appid = tt.args["appId"].(int64) + + createToken(tt, baseFileName) + + createSiteInfo(tt, baseFileName) + + output := captureOutput(setSite) + assert.Equal(t, tt.want, output) + + removeFile(baseFileName) + + }) + } +} + +func captureOutput(f func() error) string { + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + f() + + w.Close() + out, _ := ioutil.ReadAll(r) + os.Stdout = rescueStdout + return string(out) +} + +func createToken(tt struct { + name string + args map[string]interface{} + want string + wantErr bool +}, baseFileName string) error { + token := TokenResp{ + "app_name", + "sign", + "token", + } + + fileName := filepath.Join(baseFileName, tt.args["pathToToken"].(string)) + + dest, err := os.Create(fileName) + if err != nil { + return err + } + + bytes, _ := json.Marshal(token) + + fmt.Fprintf(dest, string(bytes)) + + return nil +} + +func createSiteInfo(tt struct { + name string + args map[string]interface{} + want string + wantErr bool +}, baseFileName string) error { + + + err := os.Mkdir(baseFileName, 0755) + if err != nil { + fmt.Println("os.Create:", err) + } + + fileName := filepath.Join(baseFileName, tt.args["pathToSiteInfo"].(string)) + + _, err = os.Create(fileName) + + return err +} + +func removeFile (baseFileName string){ + err := os.RemoveAll(baseFileName) + if err != nil { + fmt.Println("os.Create:", err) + } +} diff --git a/go.mod b/go.mod index ac8f9f4..b632f05 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/rs/cors v1.7.0 github.com/shirou/gopsutil v3.21.4+incompatible github.com/spf13/cobra v1.1.3 + github.com/stretchr/testify v1.3.0 github.com/tklauser/go-sysconf v0.3.5 // indirect ) From 802c199654d274bbbb234c94c4ef57f3adb1c9c7 Mon Sep 17 00:00:00 2001 From: Sampriti-Mitra Date: Sun, 10 Oct 2021 15:05:00 +0530 Subject: [PATCH 2/3] site test improved --- cmd/set/site/set_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/set/site/set_test.go b/cmd/set/site/set_test.go index c63c7b8..9a9babf 100644 --- a/cmd/set/site/set_test.go +++ b/cmd/set/site/set_test.go @@ -3,6 +3,7 @@ package site import ( "encoding/json" "fmt" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "io/ioutil" "os" @@ -56,7 +57,7 @@ func TestSetSite(t *testing.T) { createSiteInfo(tt, baseFileName) - output := captureOutput(setSite) + output := captureOutput(NewSiteCmd) assert.Equal(t, tt.want, output) removeFile(baseFileName) @@ -65,12 +66,13 @@ func TestSetSite(t *testing.T) { } } -func captureOutput(f func() error) string { +func captureOutput(f func() *cobra.Command) string { rescueStdout := os.Stdout r, w, _ := os.Pipe() os.Stdout = w - f() + a := f() + a.Execute() w.Close() out, _ := ioutil.ReadAll(r) @@ -111,7 +113,6 @@ func createSiteInfo(tt struct { wantErr bool }, baseFileName string) error { - err := os.Mkdir(baseFileName, 0755) if err != nil { fmt.Println("os.Create:", err) @@ -121,10 +122,14 @@ func createSiteInfo(tt struct { _, err = os.Create(fileName) - return err + if err != nil { + return err + } + + return nil } -func removeFile (baseFileName string){ +func removeFile(baseFileName string) { err := os.RemoveAll(baseFileName) if err != nil { fmt.Println("os.Create:", err) From 9b075308d1265de7d3eae3e4e1df13c3be0871a1 Mon Sep 17 00:00:00 2001 From: Sampriti-Mitra Date: Sun, 10 Oct 2021 22:48:42 +0530 Subject: [PATCH 3/3] site test improved --- cmd/set/site/set_test.go | 41 +++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/cmd/set/site/set_test.go b/cmd/set/site/set_test.go index 9a9babf..72fe25b 100644 --- a/cmd/set/site/set_test.go +++ b/cmd/set/site/set_test.go @@ -3,6 +3,7 @@ package site import ( "encoding/json" "fmt" + "github.com/loginradius/lr-cli/api" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "io/ioutil" @@ -42,10 +43,22 @@ func TestSetSite(t *testing.T) { "appId": int64(123456), "pathToSiteInfo": "siteInfo.json", "pathToToken": "token.json", + "function": NewSiteCmd, }, "There is no site with this AppID.\n", false, }, + { + "invalid app id", + map[string]interface{}{ + "appId": int64(0), + "pathToSiteInfo": "siteInfo.json", + "pathToToken": "token.json", + "function": setSite, + }, + "You are already using this site\n", + false, + }, } for _, tt := range tests { @@ -53,11 +66,11 @@ func TestSetSite(t *testing.T) { appid = tt.args["appId"].(int64) - createToken(tt, baseFileName) - createSiteInfo(tt, baseFileName) - output := captureOutput(NewSiteCmd) + createToken(tt, baseFileName) + + output := captureOutput(tt.args["function"]) assert.Equal(t, tt.want, output) removeFile(baseFileName) @@ -66,13 +79,17 @@ func TestSetSite(t *testing.T) { } } -func captureOutput(f func() *cobra.Command) string { +func captureOutput(fun interface{}) string { rescueStdout := os.Stdout r, w, _ := os.Pipe() os.Stdout = w - a := f() - a.Execute() + switch f := fun.(type) { + case func() *cobra.Command: + f().Execute() + case func() error: + f() + } w.Close() out, _ := ioutil.ReadAll(r) @@ -120,12 +137,22 @@ func createSiteInfo(tt struct { fileName := filepath.Join(baseFileName, tt.args["pathToSiteInfo"].(string)) - _, err = os.Create(fileName) + dest, err := os.Create(fileName) if err != nil { return err } + siteInfo := map[int64]api.SitesReponse{ + tt.args["appId"].(int64): { + Appname: "app_name", + }, + } + + bytes, _ := json.Marshal(siteInfo) + + fmt.Fprintf(dest, string(bytes)) + return nil }