Skip to content
Merged
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
16 changes: 16 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

const msgWelcomePleaseConfigure = `

Welcome to Exercism!

To get started, you need to configure the the tool with your API token.
Find your token at

%s

Then run the configure command:

%s configure --token=YOUR_TOKEN

`
16 changes: 5 additions & 11 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ latest solution.
Download other people's solutions by providing the UUID.
`,
RunE: func(cmd *cobra.Command, args []string) error {
token, err := cmd.Flags().GetString("token")
usrCfg, err := config.NewUserConfig()
if err != nil {
return err
}
if token != "" {
RootCmd.SetArgs([]string{"configure", "--token", token})
if err := RootCmd.Execute(); err != nil {
return err
}
if usrCfg.Token == "" {
tokenURL := config.InferSiteURL(usrCfg.APIBaseURL) + "/my/settings"
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If a user calls this for the first time without having run configure ever tokenURL is empty. Now that we have made the migration should it just be https://exercism.io/my/settings? Or do we still want to try an infer the Token URL?

}

uuid, err := cmd.Flags().GetString("uuid")
if err != nil {
return err
Expand All @@ -51,10 +50,6 @@ Download other people's solutions by providing the UUID.
if uuid == "" && exercise == "" {
return errors.New("need an --exercise name or a solution --uuid")
}
usrCfg, err := config.NewUserConfig()
if err != nil {
return err
}

var slug string
if uuid == "" {
Expand Down Expand Up @@ -229,7 +224,6 @@ func initDownloadCmd() {
downloadCmd.Flags().StringP("uuid", "u", "", "the solution UUID")
downloadCmd.Flags().StringP("track", "t", "", "the track ID")
downloadCmd.Flags().StringP("exercise", "e", "", "the exercise slug")
downloadCmd.Flags().StringP("token", "k", "", "authentication token used to connect to the site")
}

func init() {
Expand Down
152 changes: 96 additions & 56 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,38 @@ import (
"github.com/stretchr/testify/assert"
)

const payloadTemplate = `
{
"solution": {
"id": "bogus-id",
"user": {
"handle": "alice",
"is_requester": true
},
"exercise": {
"id": "bogus-exercise",
"instructions_url": "http://example.com/bogus-exercise",
"auto_approve": false,
"track": {
"id": "bogus-track",
"language": "Bogus Language"
}
},
"file_download_base_url": "%s",
"files": [
"%s",
"%s",
"%s"
],
"iteration": {
"submitted_at": "2017-08-21t10:11:12.130z"
}
func TestDownloadWithoutToken(t *testing.T) {
oldOut := Out
oldErr := Err
Out = ioutil.Discard
Err = ioutil.Discard
defer func() {
Out = oldOut
Err = oldErr
}()

cmdTest := &CommandTest{
Cmd: downloadCmd,
InitFn: initDownloadCmd,
Args: []string{"fakeapp", "download", "--exercise=bogus-exercise"},
}
cmdTest.Setup(t)
defer cmdTest.Teardown(t)

ts := fakeDownloadServer()
defer ts.Close()

userCfg := config.NewEmptyUserConfig()
userCfg.Workspace = cmdTest.TmpDir
userCfg.APIBaseURL = ts.URL
err := userCfg.Write()
assert.NoError(t, err)

err = cmdTest.App.Execute()
if assert.Error(t, err) {
assert.Regexp(t, "Welcome to Exercism", err.Error())
}
}
`

func TestDownload(t *testing.T) {
oldOut := Out
Expand All @@ -61,10 +64,10 @@ func TestDownload(t *testing.T) {
cmdTest.Setup(t)
defer cmdTest.Teardown(t)

mockServer := makeMockServer()
defer mockServer.Close()
ts := fakeDownloadServer()
defer ts.Close()

err := writeFakeUserConfigSettings(cmdTest.TmpDir, mockServer.URL)
err := writeFakeUserConfigSettings(cmdTest.TmpDir, ts.URL)
assert.NoError(t, err)

testCases := []struct {
Expand Down Expand Up @@ -104,14 +107,52 @@ func TestDownload(t *testing.T) {
assert.True(t, os.IsNotExist(err), "It should not write the file if empty.")
}

func TestDownloadArgs(t *testing.T) {
tests := []struct {
args []string
expectedError string
}{
{
args: []string{"bogus"}, // providing just an exercise slug without the flag
expectedError: "need an --exercise name or a solution --uuid",
},
{
args: []string{""}, // providing no args
expectedError: "need an --exercise name or a solution --uuid",
},
}

for _, test := range tests {
cmdTest := &CommandTest{
Cmd: downloadCmd,
InitFn: initDownloadCmd,
Args: append([]string{"fakeapp", "download"}, test.args...),
}
cmdTest.Setup(t)
userCfg := config.NewEmptyUserConfig()
userCfg.Workspace = cmdTest.TmpDir
userCfg.APIBaseURL = "http://example.com"
userCfg.Token = "abc123"
err := userCfg.Write()
assert.NoError(t, err)

cmdTest.App.SetOutput(ioutil.Discard)
defer cmdTest.Teardown(t)
err = cmdTest.App.Execute()

assert.EqualError(t, err, test.expectedError)
}
}

func writeFakeUserConfigSettings(tmpDirPath, serverURL string) error {
userCfg := config.NewEmptyUserConfig()
userCfg.Workspace = tmpDirPath
userCfg.APIBaseURL = serverURL
userCfg.Token = "abc123"
return userCfg.Write()
}

func makeMockServer() *httptest.Server {
func fakeDownloadServer() *httptest.Server {
mux := http.NewServeMux()
server := httptest.NewServer(mux)

Expand All @@ -136,35 +177,34 @@ func makeMockServer() *httptest.Server {
})

return server

}

func TestDownloadArgs(t *testing.T) {
tests := []struct {
args []string
expectedError string
}{
{
args: []string{"bogus"}, // providing just an exercise slug without the flag
expectedError: "need an --exercise name or a solution --uuid",
const payloadTemplate = `
{
"solution": {
"id": "bogus-id",
"user": {
"handle": "alice",
"is_requester": true
},
{
args: []string{""}, // providing no args
expectedError: "need an --exercise name or a solution --uuid",
"exercise": {
"id": "bogus-exercise",
"instructions_url": "http://example.com/bogus-exercise",
"auto_approve": false,
"track": {
"id": "bogus-track",
"language": "Bogus Language"
}
},
}

for _, test := range tests {
cmdTest := &CommandTest{
Cmd: downloadCmd,
InitFn: initDownloadCmd,
Args: append([]string{"fakeapp", "download"}, test.args...),
"file_download_base_url": "%s",
"files": [
"%s",
"%s",
"%s"
],
"iteration": {
"submitted_at": "2017-08-21t10:11:12.130z"
}
cmdTest.Setup(t)
cmdTest.App.SetOutput(ioutil.Discard)
defer cmdTest.Teardown(t)
err := cmdTest.App.Execute()

assert.EqualError(t, err, test.expectedError)
}
}
`
17 changes: 1 addition & 16 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,7 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er

if usrCfg.GetString("token") == "" {
tokenURL := config.InferSiteURL(usrCfg.GetString("apibaseurl")) + "/my/settings"
msg := `

Welcome to Exercism!

To get started, you need to configure the the tool with your API token.
Find your token at

%s

Then run the configure command:


%s configure --token=YOUR_TOKEN

`
return fmt.Errorf(msg, tokenURL, BinaryName)
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
}

if usrCfg.GetString("workspace") == "" {
Expand Down