Skip to content

Commit

Permalink
Added tests, converted to gin
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Feb 26, 2024
1 parent b2c0671 commit b423c3f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 26 deletions.
47 changes: 47 additions & 0 deletions actions/contacts/contacts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package contacts

import (
"testing"

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/tests"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

// TestSuite is for testing the entire package using real/mocked services
type TestSuite struct {
tests.TestSuite
}

// SetupSuite runs at the start of the suite
func (ts *TestSuite) SetupSuite() {
ts.BaseSetupSuite()
}

// TearDownSuite runs after the suite finishes
func (ts *TestSuite) TearDownSuite() {
ts.BaseTearDownSuite()
}

// SetupTest runs before each test
func (ts *TestSuite) SetupTest() {
ts.BaseSetupTest()

// Load the router & register routes
ts.Router = gin.Default()
require.NotNil(ts.T(), ts.Router)
routes := NewHandler(ts.AppConfig, ts.Services)
routes.RegisterAPIEndpoints(ts.Router.Group("/" + config.APIVersion))
}

// TearDownTest runs after each test
func (ts *TestSuite) TearDownTest() {
ts.BaseTearDownTest()
}

// TestTestSuite kick-starts all suite tests
func TestTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
27 changes: 13 additions & 14 deletions actions/contacts/create.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package contacts

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// create will make a new model using the services defined in the action object
Expand All @@ -22,26 +21,26 @@ import (
// @Success 201
// @Router /v1/contact [post]
// @Security bux-auth-xpub
func (c *Action) create(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
params := apirouter.GetParams(req)
func (a *Action) create(c *gin.Context) {

fullName := params.GetString("full_name")
paymail := params.GetString("paymail")
pubKey := params.GetString("pubKey")
metadata := params.GetJSON("metadata")
fullName := c.GetString("full_name")
paymail := c.GetString("paymail")
pubKey := c.GetString("pubKey")

opts := c.Services.SpvWalletEngine.DefaultModelOptions()
if metadata != nil {
opts = append(opts, engine.WithMetadatas(metadata))
var requestBody CreateContact

opts := a.Services.SpvWalletEngine.DefaultModelOptions()
if requestBody.Metadata != nil {
opts = append(opts, engine.WithMetadatas(requestBody.Metadata))
}

contact, err := c.Services.SpvWalletEngine.NewContact(req.Context(), fullName, paymail, pubKey, opts...)
contact, err := a.Services.SpvWalletEngine.NewContact(c.Request.Context(), fullName, paymail, pubKey, opts...)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusUnprocessableEntity, err.Error())
c.JSON(http.StatusUnprocessableEntity, err.Error())
return
}

contract := mappings.MapToContactContract(contact)

apirouter.ReturnResponse(w, req, http.StatusCreated, contract)
c.JSON(http.StatusCreated, contract)
}
8 changes: 8 additions & 0 deletions actions/contacts/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package contacts

import "github.com/bitcoin-sv/spv-wallet/engine"

// CreateContact is the model for creating a contact
type CreateContact struct {
Metadata engine.Metadata `json:"metadata"`
}
22 changes: 10 additions & 12 deletions actions/contacts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ package contacts
import (
"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/config"
apirouter "github.com/mrz1836/go-api-router"
"github.com/bitcoin-sv/spv-wallet/server/routes"
"github.com/gin-gonic/gin"
)

// Action is an extension of actions.Action for this package
type Action struct {
actions.Action
}

// RegisterRoutes register all the package specific routes
func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, services *config.AppServices) {
a, require := actions.NewStack(appConfig, services)
require.Use(a.RequireAuthentication)
// NewHandler creates the specific package routes
func NewHandler(appConfig *config.AppConfig, services *config.AppServices) routes.APIEndpointsFunc {
action := &Action{actions.Action{AppConfig: appConfig, Services: services}}

// Use the authentication middleware wrapper - this will only check for a valid xPub
aBasic, requireBasic := actions.NewStack(appConfig, services)
requireBasic.Use(aBasic.RequireBasicAuthentication)
apiEndpoints := routes.APIEndpointsFunc(func(router *gin.RouterGroup) {
contactGroup := router.Group("/contact")
contactGroup.POST("", action.create)
})

// Load the actions and set the services
action := &Action{actions.Action{AppConfig: a.AppConfig, Services: a.Services}}

router.HTTPRouter.POST("/"+config.APIVersion+"/contact", action.Request(router, requireBasic.Wrap(action.create)))
return apiEndpoints
}
34 changes: 34 additions & 0 deletions actions/contacts/routes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package contacts

import (
"testing"

"github.com/bitcoin-sv/spv-wallet/config"
"github.com/stretchr/testify/assert"
)

// TestBaseRegisterRoutes will test routes
func (ts *TestSuite) TestRegisterRoutes() {
ts.T().Run("test routes", func(t *testing.T) {
testCases := []struct {
method string
url string
}{
{"POST", "/" + config.APIVersion + "/contact"},
}

ts.Router.Routes()

for _, testCase := range testCases {
found := false
for _, routeInfo := range ts.Router.Routes() {
if testCase.url == routeInfo.Path && testCase.method == routeInfo.Method {
assert.NotNil(t, routeInfo.HandlerFunc)
found = true
break
}
}
assert.True(t, found)
}
})
}
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func SetupServerRoutes(appConfig *config.AppConfig, services *config.AppServices
transactionBasicRoutes, transactionAPIRoutes, transactionCallbackRoutes := transactions.NewHandler(appConfig, services)
utxoAPIRoutes := utxos.NewHandler(appConfig, services)
xPubAPIRoutes := xpubs.NewHandler(appConfig, services)
contactAPIRoutes := contacts.NewHandler(appConfig, services)

routes := []interface{}{
// Admin routes
Expand All @@ -141,6 +142,8 @@ func SetupServerRoutes(appConfig *config.AppConfig, services *config.AppServices
utxoAPIRoutes,
// xPub routes
xPubAPIRoutes,
// Contact routes
contactAPIRoutes,
}

prefix := "/" + config.APIVersion
Expand Down

0 comments on commit b423c3f

Please sign in to comment.