diff --git a/handlers/api/simulation.go b/handlers/api/simulation.go index 8eebb284..02210a6a 100644 --- a/handlers/api/simulation.go +++ b/handlers/api/simulation.go @@ -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 { diff --git a/handlers/api/simulation_test.go b/handlers/api/simulation_test.go index 07ebb50c..833d8491 100644 --- a/handlers/api/simulation_test.go +++ b/handlers/api/simulation_test.go @@ -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" @@ -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())