-
Notifications
You must be signed in to change notification settings - Fork 12
added test for site set #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package site | ||
|
|
||
| import ( | ||
| "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) { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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