Skip to content

Commit

Permalink
Merge pull request #762 from ipfs/allocations_filter
Browse files Browse the repository at this point in the history
Default allocations filter should be all
  • Loading branch information
hsanjuan committed Apr 24, 2019
2 parents 5a1dfc2 + fa8d5c9 commit 543656a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/rest/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,12 @@ func (api *API) allocationsHandler(w http.ResponseWriter, r *http.Request) {
for _, f := range strings.Split(filterStr, ",") {
filter |= types.PinTypeFromString(f)
}

if filter == types.BadType {
api.sendResponse(w, http.StatusBadRequest, errors.New("invalid filter value"), nil)
return
}

var pins []*types.Pin
err := api.rpcClient.CallContext(
r.Context(),
Expand Down Expand Up @@ -814,7 +820,7 @@ func (api *API) statusAllHandler(w http.ResponseWriter, r *http.Request) {
filterStr := queryValues.Get("filter")
filter := types.TrackerStatusFromString(filterStr)
if filter == types.TrackerStatusUndefined && filterStr != "" {
api.sendResponse(w, autoStatus, errors.New("invalid filter value"), nil)
api.sendResponse(w, http.StatusBadRequest, errors.New("invalid filter value"), nil)
return
}

Expand Down
19 changes: 19 additions & 0 deletions api/rest/restapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,19 @@ func TestAPIAllocationsEndpoint(t *testing.T) {
!resp[2].Cid.Equals(test.Cid3) {
t.Error("unexpected pin list: ", resp)
}

makeGet(t, rest, url(rest)+"/allocations", &resp)
if len(resp) != 3 ||
!resp[0].Cid.Equals(test.Cid1) || !resp[1].Cid.Equals(test.Cid2) ||
!resp[2].Cid.Equals(test.Cid3) {
t.Error("unexpected pin list: ", resp)
}

errResp := api.Error{}
makeGet(t, rest, url(rest)+"/allocations?filter=invalid", &errResp)
if errResp.Code != http.StatusBadRequest {
t.Error("an invalid filter value should 400")
}
}

testBothEndpoints(t, tf)
Expand Down Expand Up @@ -851,6 +864,12 @@ func TestAPIStatusAllEndpoint(t *testing.T) {
if len(resp7) != 2 {
t.Errorf("unexpected statusAll+filter=error,pinned resp:\n %+v", resp7)
}

var errorResp api.Error
makeGet(t, rest, url(rest)+"/pins?filter=invalid", &errorResp)
if errorResp.Code != http.StatusBadRequest {
t.Error("an invalid filter value should 400")
}
}

testBothEndpoints(t, tf)
Expand Down
2 changes: 2 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ func PinTypeFromString(str string) PinType {
return ShardType
case "all":
return AllType
case "":
return AllType
default:
return BadType
}
Expand Down

0 comments on commit 543656a

Please sign in to comment.