Skip to content

Commit

Permalink
updated each handler to check for correct version
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypadmanabhan committed May 26, 2021
1 parent ffcbade commit 88a9013
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 377 deletions.
27 changes: 25 additions & 2 deletions api/dimensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"sort"
"strconv"

"github.com/ONSdigital/dp-dataset-api/apierrors"
errs "github.com/ONSdigital/dp-dataset-api/apierrors"
Expand Down Expand Up @@ -32,6 +33,16 @@ func (api *DatasetAPI) getDimensions(w http.ResponseWriter, r *http.Request, lim
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": version, "func": "getDimensions"}
var err error

versionId, err := strconv.Atoi(version)
if err != nil {
log.Event(ctx, "invalid version request", log.ERROR, log.Error(err), logData)
return nil, 0, errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
return nil, 0, errs.ErrInvalidVersion
}

list, totalCount, err := func() ([]models.Dimension, int, error) {
authorised := api.authenticate(r, logData)

Expand All @@ -40,7 +51,7 @@ func (api *DatasetAPI) getDimensions(w http.ResponseWriter, r *http.Request, lim
state = models.PublishedState
}

versionDoc, err := api.dataStore.Backend.GetVersion(datasetID, edition, version, state)
versionDoc, err := api.dataStore.Backend.GetVersion(datasetID, edition, versionId, state)
if err != nil {
log.Event(ctx, "datastore.getversion returned an error", log.ERROR, log.Error(err), logData)
return nil, 0, err
Expand Down Expand Up @@ -141,6 +152,18 @@ func (api *DatasetAPI) getDimensionOptions(w http.ResponseWriter, r *http.Reques
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": versionID, "dimension": dimension, "func": "getDimensionOptions"}
authorised := api.authenticate(r, logData)

versionId, err := strconv.Atoi(versionID)
if err != nil {
log.Event(ctx, "invalid version requested", log.ERROR, log.Error(err), logData)
handleDimensionsErr(ctx, w, "invalid version", errs.ErrInvalidVersion, logData)
return nil, 0, err
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
handleDimensionsErr(ctx, w, "failed due to version not a positive integer", errs.ErrInvalidVersion, logData)
return nil, 0, err
}

var state string
if !authorised {
state = models.PublishedState
Expand All @@ -156,7 +179,7 @@ func (api *DatasetAPI) getDimensionOptions(w http.ResponseWriter, r *http.Reques
}

// ger version for provided dataset, edition and versionID
version, err := api.dataStore.Backend.GetVersion(datasetID, edition, versionID, state)
version, err := api.dataStore.Backend.GetVersion(datasetID, edition, versionId, state)
if err != nil {
handleDimensionsErr(ctx, w, "failed to get version", err, logData)
return nil, 0, err
Expand Down
39 changes: 19 additions & 20 deletions api/dimensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestGetDimensionsReturnsOk(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState}, nil
},
GetDimensionsFunc: func(datasetID, versionID string) ([]bson.M, error) {
Expand Down Expand Up @@ -56,13 +56,13 @@ func TestGetDimensionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, errs.ErrInternalServer
},
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldContainSubstring, errs.ErrInternalServer.Error())
Expand All @@ -74,13 +74,13 @@ func TestGetDimensionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, errs.ErrVersionNotFound
},
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusNotFound)
So(w.Body.String(), ShouldContainSubstring, errs.ErrVersionNotFound.Error())
Expand All @@ -92,7 +92,7 @@ func TestGetDimensionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState}, nil
},
GetDimensionsFunc: func(datasetID, versionID string) ([]bson.M, error) {
Expand All @@ -101,7 +101,7 @@ func TestGetDimensionsReturnsErrors(t *testing.T) {
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusNotFound)
So(w.Body.String(), ShouldContainSubstring, errs.ErrDimensionsNotFound.Error())
Expand All @@ -113,13 +113,13 @@ func TestGetDimensionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: "gobbly-gook"}, nil
},
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldContainSubstring, errs.ErrInternalServer.Error())
Expand All @@ -135,7 +135,7 @@ func TestGetDimensionOptionsReturnsOk(t *testing.T) {

// testing DataStore with 5 dimension options
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState, ID: "v1"}, nil
},
GetDimensionOptionsFunc: func(version *models.Version, dimensions string, offset, limit int) ([]*models.PublicDimensionOption, int, error) {
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
w := httptest.NewRecorder()

api := initAPIWithMockedStore(&storetest.StorerMock{})
api.getDimensionOptions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusBadRequest)
So(w.Body.String(), ShouldContainSubstring, errs.ErrTooManyQueryParameters.Error())
Expand All @@ -294,13 +294,13 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions/age/options", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, errs.ErrVersionNotFound
},
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensionOptions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusNotFound)
So(w.Body.String(), ShouldContainSubstring, errs.ErrVersionNotFound.Error())
Expand All @@ -311,7 +311,7 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions/age/options", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState}, nil
},
GetDimensionOptionsFunc: func(version *models.Version, dimensions string, offset, limit int) ([]*models.PublicDimensionOption, int, error) {
Expand All @@ -320,7 +320,7 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensionOptions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldContainSubstring, errs.ErrInternalServer.Error())
Expand All @@ -332,7 +332,7 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions/age/options?id=id1", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState}, nil
},
GetDimensionOptionsFromIDsFunc: func(version *models.Version, dimension string, ids []string) ([]*models.PublicDimensionOption, int, error) {
Expand All @@ -341,7 +341,7 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensionOptions(w, r, 20, 0)
api.Router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldContainSubstring, errs.ErrInternalServer.Error())
Expand All @@ -353,14 +353,13 @@ func TestGetDimensionOptionsReturnsErrors(t *testing.T) {
r := httptest.NewRequest("GET", "http://localhost:22000/datasets/123/editions/2017/versions/1/dimensions/age/options", nil)
w := httptest.NewRecorder()
mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: "gobbly-gook"}, nil
},
}

api := initAPIWithMockedStore(mockedDataStore)
api.getDimensionOptions(w, r, 20, 0)

api.Router.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldContainSubstring, errs.ErrInternalServer.Error())
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
Expand Down
13 changes: 12 additions & 1 deletion api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"net/http"
"strconv"

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
Expand All @@ -20,8 +21,18 @@ func (api *DatasetAPI) getMetadata(w http.ResponseWriter, r *http.Request) {
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": version}

b, err := func() ([]byte, error) {
versionId, err := strconv.Atoi(version)
if err != nil {
log.Event(ctx, "failed due to invalid version request", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion

}

versionDoc, err := api.dataStore.Backend.GetVersion(datasetID, edition, version, "")
versionDoc, err := api.dataStore.Backend.GetVersion(datasetID, edition, versionId, "")
if err != nil {
if err == errs.ErrVersionNotFound {
log.Event(ctx, "getMetadata endpoint: failed to find version for dataset edition", log.ERROR, log.Error(err), logData)
Expand Down
16 changes: 8 additions & 8 deletions api/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetMetadataReturnsOk(t *testing.T) {
CheckEditionExistsFunc: func(datasetID, edition, state string) error {
return nil
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return versionDoc, nil
},
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestGetMetadataReturnsOk(t *testing.T) {
CheckEditionExistsFunc: func(datasetID, edition, state string) error {
return nil
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return versionDoc, nil
},
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
w := httptest.NewRecorder()

mockedDataStore := &storetest.StorerMock{
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, errs.ErrInternalServer
},
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return nil, errs.ErrDatasetNotFound
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, nil
},
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
CheckEditionExistsFunc: func(datasetId, edition, state string) error {
return nil
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return versionDoc, nil
},
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
CheckEditionExistsFunc: func(datasetId, edition, state string) error {
return errs.ErrEditionNotFound
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return versionDoc, nil
},
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
CheckEditionExistsFunc: func(datasetId, edition, state string) error {
return nil
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return nil, errs.ErrVersionNotFound
},
}
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestGetMetadataReturnsError(t *testing.T) {
CheckEditionExistsFunc: func(datasetId, edition, state string) error {
return nil
},
GetVersionFunc: func(datasetID, edition, version, state string) (*models.Version, error) {
GetVersionFunc: func(datasetID, edition string, version int, state string) (*models.Version, error) {
return &models.Version{State: "gobbly-gook"}, nil
},
}
Expand Down
15 changes: 14 additions & 1 deletion api/publish_state_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
Expand All @@ -31,7 +32,19 @@ func (d *PublishCheck) Check(handle func(http.ResponseWriter, *http.Request), ac
version := vars["version"]
data := log.Data{"dataset_id": datasetID, "edition": edition, "version": version}

currentVersion, err := d.Datastore.GetVersion(datasetID, edition, version, "")
versionId, err := strconv.Atoi(version)
if err != nil {
log.Event(ctx, "failed due to invalid version request", log.ERROR, log.Error(err), data)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), data)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

currentVersion, err := d.Datastore.GetVersion(datasetID, edition, versionId, "")
if err != nil {
if err != errs.ErrVersionNotFound {
log.Event(ctx, "errored whilst retrieving version resource", log.ERROR, log.Error(err), data)
Expand Down
Loading

0 comments on commit 88a9013

Please sign in to comment.