Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions cmd/set/site/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package site

import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to run goimports and let it order the imports by

  1. Standard packages
  2. External packages
  3. Private packages

"encoding/json"
"fmt"
"github.com/loginradius/lr-cli/api"
"github.com/spf13/cobra"
"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) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spurious newline

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",
"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 {
t.Run(tt.name, func(t *testing.T) {

appid = tt.args["appId"].(int64)

createSiteInfo(tt, baseFileName)

createToken(tt, baseFileName)

output := captureOutput(tt.args["function"])
assert.Equal(t, tt.want, output)

removeFile(baseFileName)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spurious newline

})
}
}

func captureOutput(fun interface{}) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

switch f := fun.(type) {
case func() *cobra.Command:
f().Execute()
case func() error:
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))

dest, err := os.Create(fileName)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better style to stick the line where err is instantiated with the line where the error is checked

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
}

func removeFile(baseFileName string) {
err := os.RemoveAll(baseFileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your indentation is inconsistent, you might want to run gofmt

if err != nil {
fmt.Println("os.Create:", err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.3.0 is kinda old, the latest version is 1.7.0

github.com/tklauser/go-sysconf v0.3.5 // indirect
)

Expand Down