Skip to content

Commit

Permalink
fix up CreateReport test
Browse files Browse the repository at this point in the history
  • Loading branch information
CampGareth committed Nov 12, 2018
1 parent bb819de commit 8f851db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions handlers/api/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func (s Simulation) CreateReport(c *gin.Context) {
err = s.Repo.StoreReport(sim.ID, report)
default:
err = errors.New("Not a valid report version")
fmt.Println("This isn't a valid report according to the header")
}

if err != nil {
Expand Down
29 changes: 25 additions & 4 deletions handlers/api/simulation_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package api

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -48,13 +53,29 @@ func TestSimulationCreateReport(t *testing.T) {
simRepo := models.NewMockSimulationRepo(mockCtrl)
simRepo.EXPECT().ByID("foosim").Return(models.Simulation{ID: "foosim", Token: "footoken"}, nil)
simRepo.EXPECT().StoreReport("foosim", models.Report{}).Return(nil)
s := Simulation{
Repo: simRepo,
}

c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Set("token", "footoken")
// Required to pass token authentication.
reqURL, err := (&url.URL{}).Parse("/?token=footoken")
if err != nil {
t.Fatalf("Failed to parse hardcoded test URL: %v", err)
}
c.Request = &http.Request{URL: reqURL}
// Required to reach a point where json binding could occur.
c.Request.Header = http.Header{}
c.Request.Header.Add("Content-Type", "application/vnd.reconfigure.io/reports-v1+json")

emptyReport, err := json.Marshal(&models.Report{})
c.Request.Body = ioutil.NopCloser(bytes.NewReader(emptyReport))
if err != nil {
t.Fatalf("Failed to json.Marshal an empty report: %v", err)
}

c.Params = append(c.Params, gin.Param{Key: "id", Value: "foosim"})

s := Simulation{
Repo: simRepo,
}
s.CreateReport(c)
if c.Writer.Status() != 200 {
t.Error("Expected 200 status, got: ", c.Writer.Status())
Expand Down

0 comments on commit 8f851db

Please sign in to comment.