Skip to content

Commit

Permalink
Fix login test (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jul 9, 2019
1 parent 9b25dbc commit 5c6065e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 68 deletions.
65 changes: 0 additions & 65 deletions pkg/cmd/login_test.go

This file was deleted.

8 changes: 5 additions & 3 deletions pkg/login/client_login.go
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/stripe/stripe-cli/pkg/validators"
)

var execCommand = exec.Command

const stripeCLIAuthURL = "https://dashboard.stripe.com/stripecli/auth"

// Links provides the URLs for the CLI to continue the login flow
Expand Down Expand Up @@ -70,11 +72,11 @@ func openBrowser(url string) error {

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
err = execCommand("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
err = execCommand("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
err = execCommand("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/login/client_login_test.go
Expand Up @@ -2,13 +2,80 @@ package login

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/stripe/stripe-cli/pkg/profile"
)

func TestLogin(t *testing.T) {
if os.Getenv("OPEN_URL") == "1" {
os.Exit(0)
return
}

execCommand = func(string, ...string) *exec.Cmd {
cmd := exec.Command(os.Args[0], "-test.run=TestLogin")
cmd.Env = []string{"OPEN_URL=1"}
return cmd
}
defer func() { execCommand = exec.Command }()

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 := 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(authURL, p)
assert.NoError(t, err)
}

func TestGetLinks(t *testing.T) {
expectedLinks := Links{
BrowserURL: "https://stripe.com/browser",
Expand Down

0 comments on commit 5c6065e

Please sign in to comment.