Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #295 from bstasyszyn/292
Browse files Browse the repository at this point in the history
feat: Add REST endpoint for Sidetree version
  • Loading branch information
fqutishat authored May 4, 2020
2 parents 2348ae1 + d51f86a commit 7f6fa70
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/peer/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (c *sidetreeService) LoadSidetreeHandlers(mspID, peerID string) ([]sidetree
return nil, err
}

cfg.Version = kv.ComponentVersion

handlers = append(handlers, cfg)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/peer/sidetreesvc/channelctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestChannelController_Update(t *testing.T) {

time.Sleep(20 * time.Millisecond)
require.Len(t, ctrl.Invocations()[eventMethod], count+1)
require.Len(t, m.RESTHandlers(), 9)
require.Len(t, m.RESTHandlers(), 11)

t.Run("Update peer config -> success", func(t *testing.T) {
count := len(ctrl.Invocations()[eventMethod])
Expand Down
2 changes: 2 additions & 0 deletions pkg/peer/sidetreesvc/restsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func newRESTHandlers(
)
}

handlers = append(handlers, sidetreehandler.NewVersionHandler(channelID, cfg))

return &restHandlers{
channelID: channelID,
namespace: cfg.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion pkg/peer/sidetreesvc/restsvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestRESTHandlers(t *testing.T) {
rh, err := newRESTHandlers(channel1, nsCfg, bw, pp, os, restCfg)
require.NoError(t, err)
require.NotNil(t, rh)
require.Len(t, rh.HTTPHandlers(), 2)
require.Len(t, rh.HTTPHandlers(), 3)
})

t.Run("No resolver or batch-writer role -> no handlers", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/rest/sidetreehandler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
type Config struct {
Authorization authhandler.Config

// Version contains the version of the Sidetree endpoint
Version string
// DocType specifies the document type (DID or File index)
DocType DocumentType
// Namespace is the namespace prefix used in the ID of the document
Expand Down
27 changes: 27 additions & 0 deletions pkg/rest/sidetreehandler/versionhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package sidetreehandler

import (
"github.com/trustbloc/sidetree-fabric/pkg/rest/versionhandler"
)

const (
moduleName = "Sidetree"
)

// Version handles version requests
type Version struct {
*versionhandler.Version
}

// NewVersionHandler returns a new Version handler
func NewVersionHandler(channelID string, cfg Config) *Version {
return &Version{
Version: versionhandler.New(channelID, cfg.BasePath, moduleName, cfg.Version),
}
}
59 changes: 59 additions & 0 deletions pkg/rest/sidetreehandler/versionhandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package sidetreehandler

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"

"github.com/trustbloc/sidetree-fabric/pkg/httpserver"
"github.com/trustbloc/sidetree-fabric/pkg/rest/versionhandler"
)

const (
channel1 = "channel1"
version = "0.1.3"
)

func TestNewVersionHandler(t *testing.T) {
handlerCfg := Config{
BasePath: "/sidetree",
}

h := NewVersionHandler(channel1, handlerCfg)
require.NotNil(t, h)

require.Equal(t, "/sidetree/version", h.Path())
require.Equal(t, http.MethodGet, h.Method())
}

func TestVersion_Handler(t *testing.T) {
handlerCfg := Config{
BasePath: "/sidetree",
Version: version,
}

h := NewVersionHandler(channel1, handlerCfg)
require.NotNil(t, h)

rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/sidetree/version", nil)

h.Handler()(rw, req)

require.Equal(t, http.StatusOK, rw.Result().StatusCode)
require.Equal(t, httpserver.ContentTypeJSON, rw.Header().Get(httpserver.ContentTypeHeader))

resp := &versionhandler.Response{}
require.NoError(t, json.Unmarshal(rw.Body.Bytes(), resp))
require.Equal(t, moduleName, resp.Name)
require.Equal(t, version, resp.Version)
}
4 changes: 4 additions & 0 deletions test/bddtests/features/did-sidetree.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Feature:
Given the authorization bearer token for "GET" requests to path "/sidetree/0.0.1/identifiers" is set to "${did_r}"
And the authorization bearer token for "POST" requests to path "/sidetree/0.0.1/operations" is set to "${did_w}"

When an HTTP GET is sent to "https://localhost:48326/sidetree/0.0.1/version"
Then the JSON path "name" of the response equals "Sidetree"
And the JSON path "version" of the response equals "0.1.3"

When client sends request to "https://localhost:48426/sidetree/0.0.1/operations" to create DID document in namespace "did:sidetree"
Then check success response contains "#didDocumentHash"

Expand Down

0 comments on commit 7f6fa70

Please sign in to comment.