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

feat: Add create issue credential #6

Merged
merged 1 commit into from
Jan 15, 2020
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
16 changes: 16 additions & 0 deletions cmd/vc-rest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright SecureKey Technologies Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0

module github.com/trustbloc/edge-service/cmd/vc-rest

replace github.com/trustbloc/edge-service => ../..

require (
github.com/gorilla/mux v1.7.3
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
github.com/trustbloc/edge-service v0.0.0
)

go 1.13
200 changes: 200 additions & 0 deletions cmd/vc-rest/go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions cmd/vc-rest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"log"

"github.com/spf13/cobra"

"github.com/trustbloc/edge-service/cmd/vc-rest/startcmd"
)

func main() {
rootCmd := &cobra.Command{
Use: "vc-rest",
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}

rootCmd.AddCommand(startcmd.GetStartCmd(&startcmd.HTTPServer{}))

if err := rootCmd.Execute(); err != nil {
log.Fatalf("Failed to run vc-rest: %s", err.Error())
}
}
25 changes: 25 additions & 0 deletions cmd/vc-rest/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"os"
"testing"
)

// Correct behaviour is for main to finish with exit code 0.
// This test fails otherwise. However, this can't be checked by the unit test framework. The *testing.T argument is
// only there so that this test gets picked up by the framework but otherwise we don't need it.
func TestWithoutUserAgs(t *testing.T) { //nolint - see above
setUpArgs()
main()
}

// Strips out the extra args that the unit test framework adds
// This allows main() to execute as if it was called directly from the command line
func setUpArgs() {
os.Args = os.Args[:1]
}
95 changes: 95 additions & 0 deletions cmd/vc-rest/startcmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package startcmd

import (
"errors"
"net/http"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

"github.com/trustbloc/edge-service/pkg/restapi/vc/operation"
cmdutils "github.com/trustbloc/edge-service/pkg/utils/cmd"
)

const (
hostURLFlagName = "host-url"
hostURLFlagShorthand = "u"
hostURLFlagUsage = "URL to run the vc-rest instance on. Format: HostName:Port."
hostURLEnvKey = "VC_REST_HOST_URL"
)

var errMissingHostURL = errors.New("host URL not provided")

type vcRestParameters struct {
srv server
hostURL string
}

type server interface {
ListenAndServe(host string, router http.Handler) error
}

// HTTPServer represents an actual HTTP server implementation.
type HTTPServer struct{}

// ListenAndServe starts the server using the standard Go HTTP server implementation.
func (s *HTTPServer) ListenAndServe(host string, router http.Handler) error {
return http.ListenAndServe(host, router)
}

// GetStartCmd returns the Cobra start command.
func GetStartCmd(srv server) *cobra.Command {
startCmd := createStartCmd(srv)

createFlags(startCmd)

return startCmd
}

func createStartCmd(srv server) *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start vc-rest",
Long: "Start vc-rest inside the edge-service",
RunE: func(cmd *cobra.Command, args []string) error {
hostURL, err := cmdutils.GetUserSetVar(cmd, hostURLFlagName, hostURLEnvKey)
if err != nil {
return err
}
parameters := &vcRestParameters{
srv: srv,
hostURL: hostURL,
}
return startEdgeService(parameters)
},
}
}

func createFlags(startCmd *cobra.Command) {
startCmd.Flags().StringP(hostURLFlagName, hostURLFlagShorthand, "", hostURLFlagUsage)
}

func startEdgeService(parameters *vcRestParameters) error {
if parameters.hostURL == "" {
return errMissingHostURL
}

vcService := operation.New()

handlers := vcService.GetRESTHandlers()
router := mux.NewRouter()

for _, handler := range handlers {
router.HandleFunc(handler.Path(), handler.Handle()).Methods(handler.Method())
}

err := parameters.srv.ListenAndServe(parameters.hostURL, router)

return err
}
95 changes: 95 additions & 0 deletions cmd/vc-rest/startcmd/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package startcmd

import (
"net/http"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

type mockServer struct{}

func (s *mockServer) ListenAndServe(host string, handler http.Handler) error {
return nil
}

func TestStartCmdContents(t *testing.T) {
startCmd := GetStartCmd(&mockServer{})

require.Equal(t, "start", startCmd.Use)
require.Equal(t, "Start vc-rest", startCmd.Short)
require.Equal(t, "Start vc-rest inside the edge-service", startCmd.Long)

checkFlagPropertiesCorrect(t, startCmd, hostURLFlagName, hostURLFlagShorthand, hostURLFlagUsage)
}

func TestStartCmdWithBlankHostArg(t *testing.T) {
startCmd := GetStartCmd(&mockServer{})

args := []string{"--" + hostURLFlagName, ""}
startCmd.SetArgs(args)

err := startCmd.Execute()

require.Equal(t, errMissingHostURL.Error(), err.Error())
}

func TestStartCmdWithMissingHostArg(t *testing.T) {
startCmd := GetStartCmd(&mockServer{})

err := startCmd.Execute()

require.Equal(t,
"Neither host-url (command line flag) nor VC_REST_HOST_URL (environment variable) have been set.",
err.Error())
}

func TestStartEdgeStoreWithBlankHost(t *testing.T) {
parameters := &vcRestParameters{hostURL: ""}

err := startEdgeService(parameters)
require.NotNil(t, err)
require.Equal(t, errMissingHostURL, err)
}

func TestStartCmdValidArgs(t *testing.T) {
startCmd := GetStartCmd(&mockServer{})

args := []string{"--" + hostURLFlagName, "localhost:8080"}
startCmd.SetArgs(args)

err := startCmd.Execute()

require.Nil(t, err)
}

func TestStartCmdValidArgsEnvVar(t *testing.T) {
startCmd := GetStartCmd(&mockServer{})

err := os.Setenv(hostURLEnvKey, "localhost:8080")
require.Nil(t, err)

err = startCmd.Execute()

require.Nil(t, err)
}

func checkFlagPropertiesCorrect(t *testing.T, cmd *cobra.Command, flagName, flagShorthand, flagUsage string) {
flag := cmd.Flag(flagName)

require.NotNil(t, flag)
require.Equal(t, flagName, flag.Name)
require.Equal(t, flagShorthand, flag.Shorthand)
require.Equal(t, flagUsage, flag.Usage)
require.Equal(t, "", flag.Value.String())

flagAnnotations := flag.Annotations
require.Nil(t, flagAnnotations)
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ module github.com/trustbloc/edge-service
go 1.13

require (
github.com/go-language-server/uri v0.2.0 // indirect
github.com/gorilla/mux v1.7.3
github.com/hyperledger/aries-framework-go v0.1.0
github.com/hyperledger/aries-framework-go v0.1.1-0.20200113160002-41ea60bef355
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
golang.org/x/tools v0.0.0-20191223235410-3721262b3e7c // indirect
)
Loading