Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve pagination parameters from the correct location #627

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/server/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const (
PageTokenParam = "pageToken"
)

// ParsePaginationParams reads the PageSizeParam and PageTokenParam from the URL parameters and populates the passed in
// ParsePaginationQueryValues reads the PageSizeParam and PageTokenParam from the URL parameters and populates the passed in
// pageRequest. The value encoded in PageTokenParam is assumed to be the base64url encoding of a PageToken. It is an
// error for the query params to be different from the query params encoded in the PageToken. Any error during the
// execution is responded to using the passed in gin.Context. The return value corresponds to whether there was an
// error within the function.
func ParsePaginationParams(c *gin.Context, pageRequest *PageRequest) bool {
pageSizeStr := framework.GetParam(c, PageSizeParam)
func ParsePaginationQueryValues(c *gin.Context, pageRequest *PageRequest) bool {
pageSizeStr := framework.GetQueryValue(c, PageSizeParam)

if pageSizeStr != nil {
pageSize, err := strconv.Atoi(*pageSizeStr)
Expand All @@ -48,7 +48,7 @@ func ParsePaginationParams(c *gin.Context, pageRequest *PageRequest) bool {
pageRequest.PageSize = &pageSize
}

queryPageToken := framework.GetParam(c, PageTokenParam)
queryPageToken := framework.GetQueryValue(c, PageTokenParam)
if queryPageToken != nil {
errMsg := "token value cannot be decoded"
tokenData, err := base64.RawURLEncoding.DecodeString(*queryPageToken)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/router/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (dr DIDRouter) ListDIDsByMethod(c *gin.Context) {
Deleted: getIsDeleted,
}
var pageRequest pagination.PageRequest
if pagination.ParsePaginationParams(c, &pageRequest) {
if pagination.ParsePaginationQueryValues(c, &pageRequest) {
return
}
getDIDsRequest.PageRequest = pageRequest.ToServicePage()
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/router/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (pr PresentationRouter) ListSubmissions(c *gin.Context) {
}

var pageRequest pagination.PageRequest
if pagination.ParsePaginationParams(c, &pageRequest) {
if pagination.ParsePaginationQueryValues(c, &pageRequest) {
return
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/server/server_did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,11 @@ func TestDIDAPI(t *testing.T) {

w := httptest.NewRecorder()
badParams := url.Values{
"method": []string{"key"},
"pageSize": []string{"1"},
"pageToken": []string{"made up token"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/dids/key?"+badParams.Encode(), nil)
c := newRequestContextWithURLValues(w, req, badParams)
c := newRequestContextWithParams(w, req, map[string]string{"method": "key"})
didService.ListDIDsByMethod(c)
assert.Contains(tt, w.Body.String(), "token value cannot be decoded")
})
Expand All @@ -536,11 +535,10 @@ func TestDIDAPI(t *testing.T) {

w := httptest.NewRecorder()
params := url.Values{
"method": []string{"key"},
"pageSize": []string{"1"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/dids/key?"+params.Encode(), nil)
c := newRequestContextWithURLValues(w, req, params)
c := newRequestContextWithParams(w, req, map[string]string{"method": "key"})

didRouter.ListDIDsByMethod(c)

Expand All @@ -553,7 +551,7 @@ func TestDIDAPI(t *testing.T) {
w = httptest.NewRecorder()
params["pageToken"] = []string{listDIDsByMethodResponse.NextPageToken}
req = httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/dids/key?"+params.Encode(), nil)
c = newRequestContextWithURLValues(w, req, params)
c = newRequestContextWithParams(w, req, map[string]string{"method": "key"})

didRouter.ListDIDsByMethod(c)

Expand All @@ -576,12 +574,11 @@ func TestDIDAPI(t *testing.T) {

w := httptest.NewRecorder()
params := url.Values{
"method": []string{"key"},
"pageSize": []string{"1"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/dids/key?"+params.Encode(), nil)

c := newRequestContextWithURLValues(w, req, params)
c := newRequestContextWithParams(w, req, map[string]string{"method": "key"})
didRouter.ListDIDsByMethod(c)
assert.True(tt, util.Is2xxResponse(w.Result().StatusCode))

Expand All @@ -595,7 +592,7 @@ func TestDIDAPI(t *testing.T) {
params["pageToken"] = []string{listDIDsByMethodResponse.NextPageToken}
params["deleted"] = []string{"true"}
req = httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/dids/key?"+params.Encode(), nil)
c = newRequestContextWithURLValues(w, req, params)
c = newRequestContextWithParams(w, req, map[string]string{"method": "key"})
didRouter.ListDIDsByMethod(c)
assert.Equal(tt, http.StatusBadRequest, w.Result().StatusCode)
assert.Contains(tt, w.Body.String(), "page token must be for the same query")
Expand Down
12 changes: 6 additions & 6 deletions pkg/server/server_presentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func TestPresentationAPI(t *testing.T) {
"pageSize": []string{"-1"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+badParams.Encode(), nil)
c := newRequestContextWithURLValues(w, req, badParams)
c := newRequestContext(w, req)
pRouter.ListSubmissions(c)

assert.Contains(tt, w.Body.String(), "'pageSize' must be greater than 0")
Expand All @@ -426,7 +426,7 @@ func TestPresentationAPI(t *testing.T) {
"pageToken": []string{"made up token"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+badParams.Encode(), nil)
c := newRequestContextWithURLValues(w, req, badParams)
c := newRequestContext(w, req)
pRouter.ListSubmissions(c)

assert.Contains(tt, w.Body.String(), "token value cannot be decoded")
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestPresentationAPI(t *testing.T) {
"pageSize": []string{"1"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+params.Encode(), nil)
c := newRequestContextWithURLValues(w, req, params)
c := newRequestContext(w, req)

pRouter.ListSubmissions(c)

Expand All @@ -480,7 +480,7 @@ func TestPresentationAPI(t *testing.T) {
w = httptest.NewRecorder()
params["pageToken"] = []string{listSubmissionResponse.NextPageToken}
req = httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+params.Encode(), nil)
c = newRequestContextWithURLValues(w, req, params)
c = newRequestContext(w, req)

pRouter.ListSubmissions(c)

Expand Down Expand Up @@ -526,7 +526,7 @@ func TestPresentationAPI(t *testing.T) {
"pageSize": []string{"1"},
}
req := httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+params.Encode(), nil)
c := newRequestContextWithURLValues(w, req, params)
c := newRequestContext(w, req)

pRouter.ListSubmissions(c)

Expand All @@ -540,7 +540,7 @@ func TestPresentationAPI(t *testing.T) {
params["pageToken"] = []string{listSubmissionResponse.NextPageToken}
params["filter"] = []string{"status=\"pending\""}
req = httptest.NewRequest(http.MethodGet, "https://ssi-service.com/v1/presentations/submissions?"+params.Encode(), nil)
c = newRequestContextWithURLValues(w, req, params)
c = newRequestContext(w, req)

pRouter.ListSubmissions(c)
assert.Equal(tt, http.StatusBadRequest, w.Result().StatusCode)
Expand Down
29 changes: 7 additions & 22 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,32 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/TBD54566975/ssi-sdk/credential/exchange"
"github.com/gin-gonic/gin"

"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/service/issuance"
"github.com/tbd54566975/ssi-service/pkg/service/manifest/model"
"github.com/tbd54566975/ssi-service/pkg/service/webhook"
"github.com/tbd54566975/ssi-service/pkg/testutil"

manifestsdk "github.com/TBD54566975/ssi-sdk/credential/manifest"
"github.com/TBD54566975/ssi-sdk/crypto"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

credmodel "github.com/tbd54566975/ssi-service/internal/credential"

"github.com/tbd54566975/ssi-service/config"
credmodel "github.com/tbd54566975/ssi-service/internal/credential"
"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/server/router"
"github.com/tbd54566975/ssi-service/pkg/service/credential"
"github.com/tbd54566975/ssi-service/pkg/service/did"
svcframework "github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/issuance"
"github.com/tbd54566975/ssi-service/pkg/service/keystore"
"github.com/tbd54566975/ssi-service/pkg/service/manifest"
"github.com/tbd54566975/ssi-service/pkg/service/manifest/model"
"github.com/tbd54566975/ssi-service/pkg/service/schema"
"github.com/tbd54566975/ssi-service/pkg/service/webhook"
"github.com/tbd54566975/ssi-service/pkg/storage"
"github.com/tbd54566975/ssi-service/pkg/testutil"
)

const (
Expand Down Expand Up @@ -131,16 +126,6 @@ func newRequestContextWithParams(w http.ResponseWriter, req *http.Request, param
return c
}

func newRequestContextWithURLValues(w http.ResponseWriter, req *http.Request, params url.Values) *gin.Context {
c := newRequestContext(w, req)
for k, vs := range params {
for _, v := range vs {
c.AddParam(k, v)
}
}
return c
}

func getValidCreateManifestRequest(issuerDID, verificationMethodID, schemaID string) router.CreateManifestRequest {
return router.CreateManifestRequest{
IssuerDID: issuerDID,
Expand Down
Loading