Skip to content

Commit

Permalink
Merge pull request #871 from Jrank2013/locked_exercise_reporting
Browse files Browse the repository at this point in the history
Error message returned if the track is locked
  • Loading branch information
Katrina Owen authored Aug 21, 2019
2 parents a51b6ec + 31c4c10 commit d95de6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/download.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
netURL "net/url"
"os"
Expand Down Expand Up @@ -187,7 +189,14 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
}
defer res.Body.Close()

if err := json.NewDecoder(res.Body).Decode(&d.payload); err != nil {
if res.StatusCode < 200 || res.StatusCode > 299 {
return nil, decodedAPIError(res)
}

body, _ := ioutil.ReadAll(res.Body)
res.Body = ioutil.NopCloser(bytes.NewReader(body))

if err := json.Unmarshal(body, &d.payload); err != nil {
return nil, decodedAPIError(res)
}

Expand Down
34 changes: 34 additions & 0 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,40 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
assert.True(t, os.IsNotExist(err), "It should not write the file if empty.")
}

func TestDownloadError(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"error": {"type": "error", "message": "test error"}}`)
})

ts := httptest.NewServer(handler)
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "submit-err-tmp-dir")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)

cfg := config.Config{
Persister: config.InMemoryPersister{},
UserViperConfig: v,
DefaultBaseURL: "http://example.com",
}

flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
setupDownloadFlags(flags)
flags.Set("uuid", "value")

err = runDownload(cfg, flags, []string{})

assert.Equal(t, "test error", err.Error())

}

const payloadTemplate = `
{
"solution": {
Expand Down

0 comments on commit d95de6b

Please sign in to comment.