Skip to content

Commit

Permalink
New login configuration flow (#7)
Browse files Browse the repository at this point in the history
- go formatted cmd directory & make profile public
- remove configure command moved under stripe auth --interactive
- Create login command
- Break cli configuration into own file (config.go)
- test files

with @jmuia-stripe
  • Loading branch information
brandonl-stripe authored and ob-stripe committed Jul 6, 2019
1 parent 999b591 commit 4016f72
Show file tree
Hide file tree
Showing 18 changed files with 512 additions and 190 deletions.
153 changes: 0 additions & 153 deletions cmd/configure.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/delete.go
Expand Up @@ -14,7 +14,7 @@ func newDeleteCmd() *deleteCmd {
gc := &deleteCmd{}

gc.reqs.Method = "DELETE"
gc.reqs.Profile = profile
gc.reqs.Profile = Profile
gc.reqs.Cmd = &cobra.Command{
Use: "delete",
Args: validators.ExactArgs(1),
Expand Down
2 changes: 1 addition & 1 deletion cmd/get.go
Expand Up @@ -14,7 +14,7 @@ func newGetCmd() *getCmd {
gc := &getCmd{}

gc.reqs.Method = "GET"
gc.reqs.Profile = profile
gc.reqs.Profile = Profile
gc.reqs.Cmd = &cobra.Command{
Use: "get",
Args: validators.ExactArgs(1),
Expand Down
6 changes: 3 additions & 3 deletions cmd/listen.go
Expand Up @@ -77,14 +77,14 @@ $ stripe listen --events charge.created,charge.updated --forward-to localhost:90
// Normally, this function would be listed alphabetically with the others declared in this file,
// but since it's acting as the core functionality for the cmd above, I'm keeping it close.
func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {
deviceName, err := profile.GetDeviceName()
deviceName, err := Profile.GetDeviceName()
if err != nil {
return err
}

endpointsMap := make(map[string][]string)

key, err := profile.GetSecretKey()
key, err := Profile.GetSecretKey()
if err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {

func (lc *listenCmd) getEndpointsFromAPI(secretKey string) requests.WebhookEndpointList {
examples := requests.Examples{
Profile: profile,
Profile: Profile,
APIVersion: "2019-03-14",
SecretKey: secretKey,
}
Expand Down
38 changes: 38 additions & 0 deletions cmd/login.go
@@ -0,0 +1,38 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/stripe/stripe-cli/login"
"github.com/stripe/stripe-cli/validators"
)

type loginCmd struct {
cmd *cobra.Command
interactive bool
url string
}

func newLoginCmd() *loginCmd {
lc := &loginCmd{}

lc.cmd = &cobra.Command{
Use: "login",
Args: validators.NoArgs,
Short: "Log into your Stripe account",
Long: `Log into your Stripe account to write your configuration file`,
RunE: lc.runLoginCmd,
}
lc.cmd.Flags().BoolVarP(&lc.interactive, "interactive", "i", false, "interactive configuration mode")
lc.cmd.Flags().StringVarP(&lc.url, "url", "u", "", "Testing URL for login ")
lc.cmd.Flags().MarkHidden("url")

return lc
}


func (lc *loginCmd) runLoginCmd(cmd *cobra.Command, args []string) error {
if lc.interactive {
return login.InteractiveLogin(Profile)
}
return login.Login(lc.url, Profile)
}
66 changes: 66 additions & 0 deletions cmd/login_test.go
@@ -0,0 +1,66 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stripe/stripe-cli/login"
"github.com/stripe/stripe-cli/profile"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

)

func TestLogin(t *testing.T) {
configFile := filepath.Join(os.TempDir(), "stripe", "config.toml")
p := profile.Profile{
Color: "auto",
ConfigFile: configFile,
LogLevel: "info",
ProfileName: "tests",
DeviceName: "st-testing",
}



var pollURL string
var browserURL string

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "auth") {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
expectedLinks := login.Links{
BrowserURL: browserURL,
PollURL: pollURL,
VerificationCode: "dinosaur-pineapple-polkadot",
}
json.NewEncoder(w).Encode(expectedLinks)
}
if strings.Contains(r.URL.Path,"browser") {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
w.Write([]byte("<HTML></HTML>"))

}
if strings.Contains(r.URL.Path,"poll") {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
data := []byte(`{"redeemed": true, "account_id": "acct_123", "testmode_key_secret": "sk_test_1234"}`)
fmt.Println(string(data))
w.Write(data)
}
}))
defer ts.Close()

authURL := fmt.Sprintf( "%s%s", ts.URL, "/auth")
pollURL = fmt.Sprintf( "%s%s", ts.URL, "/poll")
browserURL = fmt.Sprintf( "%s%s", ts.URL, "/browser")

err := login.Login(authURL, p)
assert.NoError(t, err)
}
3 changes: 2 additions & 1 deletion cmd/post.go
Expand Up @@ -14,7 +14,7 @@ func newPostCmd() *postCmd {
gc := &postCmd{}

gc.reqs.Method = "POST"
gc.reqs.Profile = profile
gc.reqs.Profile = Profile
gc.reqs.Cmd = &cobra.Command{
Use: "post",
Args: validators.ExactArgs(1),
Expand All @@ -32,6 +32,7 @@ $ stripe post /payment_intents -d amount=2000 -d currency=usd -d payment_method_
RunE: gc.reqs.RunRequestsCmd,
}


gc.reqs.InitFlags()

return gc
Expand Down
16 changes: 9 additions & 7 deletions cmd/root.go
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/stripe/stripe-cli/version"
)

var profile prof.Profile
// Profile is the cli configuration for the user
var Profile prof.Profile

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -82,20 +83,21 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
}

func init() {
cobra.OnInitialize(profile.InitConfig)
cobra.OnInitialize(Profile.InitConfig)

rootCmd.PersistentFlags().String("api-key", "", "Your test mode API secret key to use for the command")
rootCmd.PersistentFlags().StringVar(&profile.Color, "color", "auto", "turn on/off color output (on, off, auto)")
rootCmd.PersistentFlags().StringVar(&profile.ConfigFile, "config", "", "config file (default is $HOME/.config/stripe/config.toml)")
rootCmd.PersistentFlags().StringVar(&profile.ProfileName, "project-name", "default", "the project name to read from for config")
rootCmd.PersistentFlags().StringVar(&profile.LogLevel, "log-level", "info", "log level (debug, info, warn, error)")
rootCmd.PersistentFlags().StringVar(&Profile.Color, "color", "auto", "turn on/off color output (on, off, auto)")
rootCmd.PersistentFlags().StringVar(&Profile.ConfigFile, "config", "", "config file (default is $HOME/.config/stripe/config.toml)")
rootCmd.PersistentFlags().StringVar(&Profile.ProfileName, "project-name", "default", "the project name to read from for config")
rootCmd.PersistentFlags().StringVar(&Profile.LogLevel, "log-level", "info", "log level (debug, info, warn, error)")
rootCmd.PersistentFlags().StringVar(&Profile.DeviceName, "device-name", "", "device name")
viper.BindPFlag("secret_key", rootCmd.PersistentFlags().Lookup("api-key")) // #nosec G104

viper.SetEnvPrefix("stripe")
viper.AutomaticEnv() // read in environment variables that match

rootCmd.AddCommand(newCompletionCmd().cmd)
rootCmd.AddCommand(newConfigureCmd().cmd)
rootCmd.AddCommand(newLoginCmd().cmd)
rootCmd.AddCommand(newDeleteCmd().reqs.Cmd)
rootCmd.AddCommand(newGetCmd().reqs.Cmd)
rootCmd.AddCommand(newListenCmd().cmd)
Expand Down
6 changes: 3 additions & 3 deletions cmd/root_test.go
Expand Up @@ -3,12 +3,12 @@ package cmd
import (
"testing"

homedir "github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/assert"
)

func TestGetPathNoXDG(t *testing.T) {
actual := profile.GetConfigFolder("")
actual := Profile.GetConfigFolder("")
expected, err := homedir.Dir()
expected += "/.config/stripe"

Expand All @@ -17,7 +17,7 @@ func TestGetPathNoXDG(t *testing.T) {
}

func TestGetPathXDG(t *testing.T) {
actual := profile.GetConfigFolder("/some/xdg/path")
actual := Profile.GetConfigFolder("/some/xdg/path")
expected := "/some/xdg/path/stripe"

assert.Equal(t, actual, expected)
Expand Down

0 comments on commit 4016f72

Please sign in to comment.