Skip to content

Commit

Permalink
Merge branch 'main' into feat/SPV-1337
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain authored Jan 3, 2025
2 parents 7ac376b + fa3b616 commit b007593
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
run: make test-all-db-ci

- name: Update code coverage
uses: codecov/codecov-action@v5.1.1
uses: codecov/codecov-action@v5.1.2
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
Expand Down
3 changes: 2 additions & 1 deletion actions/admin/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func RegisterRoutes(handlersManager *handlers.Manager) {
adminGroupOld.POST("/xpubs/count", handlers.AsAdmin(xpubsCount))
adminGroupOld.POST("/webhooks/subscriptions", handlers.AsAdmin(subscribeWebhookOld))
adminGroupOld.DELETE("/webhooks/subscriptions", handlers.AsAdmin(unsubscribeWebhookOld))
adminGroupOld.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooks))
adminGroupOld.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooksOld))

adminGroupOld.GET("/transactions/:id", handlers.AsAdmin(getTxAdminByIDOld))
adminGroupOld.GET("/transactions", handlers.AsAdmin(getTransactionsOld))
Expand Down Expand Up @@ -71,6 +71,7 @@ func RegisterRoutes(handlersManager *handlers.Manager) {
adminGroup.GET("/utxos", handlers.AsAdmin(utxosSearch))

// webhooks
adminGroup.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooks))
adminGroup.POST("/webhooks/subscriptions", handlers.AsAdmin(subscribeWebhook))
adminGroup.DELETE("/webhooks/subscriptions", handlers.AsAdmin(unsubscribeWebhook))

Expand Down
25 changes: 25 additions & 0 deletions actions/admin/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -61,3 +62,27 @@ func unsubscribeWebhook(c *gin.Context, _ *reqctx.AdminContext) {

c.Status(http.StatusOK)
}

// getAllWebhooks will return all the stored webhooks
// @Summary Get All Webhooks
// @Description Get All Webhooks currently subscribed to
// @Tags Admin
// @Produce json
// @Success 200 {object} []models.Webhook "List of webhooks"
// @Failure 500 "Internal server error - Error while getting all webhooks"
// @Router /api/v1/admin/webhooks/subscriptions [get]
// @Security x-auth-xpub
func getAllWebhooks(c *gin.Context, _ *reqctx.AdminContext) {
wh, err := reqctx.Engine(c).GetWebhooks(c.Request.Context())
if err != nil {
spverrors.ErrorResponse(c, err, reqctx.Logger(c))
return
}

webhookDTOs := make([]*models.Webhook, len(wh))
for i, w := range wh {
webhookDTOs[i] = mappings.MapToWebhookContract(w)
}

c.JSON(http.StatusOK, webhookDTOs)
}
4 changes: 2 additions & 2 deletions actions/admin/webhooks_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func unsubscribeWebhookOld(c *gin.Context, _ *reqctx.AdminContext) {
c.JSON(http.StatusOK, true)
}

// getAllWebhooks will return all the stored webhooks
// getAllWebhooksOld will return all the stored webhooks
// @DeprecatedRouter /v1/admin/webhooks/subscriptions [get]
// @Summary Get All Webhooks
// @Description Get All Webhooks currently subscribed to
Expand All @@ -74,7 +74,7 @@ func unsubscribeWebhookOld(c *gin.Context, _ *reqctx.AdminContext) {
// @Failure 500 "Internal server error - Error while getting all webhooks"
// @Router /v1/admin/webhooks/subscriptions [get]
// @Security x-auth-xpub
func getAllWebhooks(c *gin.Context, _ *reqctx.AdminContext) {
func getAllWebhooksOld(c *gin.Context, _ *reqctx.AdminContext) {
wh, err := reqctx.Engine(c).GetWebhooks(c.Request.Context())
if err != nil {
spverrors.ErrorResponse(c, err, reqctx.Logger(c))
Expand Down
72 changes: 72 additions & 0 deletions actions/admin/webhooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package admin_test

import (
"testing"

"github.com/bitcoin-sv/spv-wallet/actions/testabilities"
testengine "github.com/bitcoin-sv/spv-wallet/engine/testabilities"
)

func TestAdminWebhooks(t *testing.T) {
t.Run("subscribe, get and unsubscribe webhook", func(t *testing.T) {
// given:
given, then := testabilities.New(t)
cleanup := given.StartedSPVWalletWithConfiguration(testengine.WithNotificationsEnabled())
defer cleanup()

// and:
client := given.HttpClient().ForAdmin()

// and:
webhook := map[string]string{
"url": "http://localhost:8080",
"tokenHeader": "Authorization",
"tokenValue": "123",
}

// when:
res, _ := client.R().Get("/api/v1/admin/webhooks/subscriptions")

// then:
then.Response(res).
IsOK().
WithJSONf(`[]`)

// when:
res, _ = client.
R().
SetBody(webhook).
Post("/api/v1/admin/webhooks/subscriptions")

// then:
then.Response(res).IsOK()

// when:
res, _ = client.R().Get("/api/v1/admin/webhooks/subscriptions")

// then:
then.Response(res).
IsOK().
WithJSONf(`[{
"url": "http://localhost:8080",
"banned": false
}]`)

// when:
res, _ = client.
R().
SetBody(map[string]string{"url": webhook["url"]}).
Delete("/api/v1/admin/webhooks/subscriptions")

// then:
then.Response(res).IsOK()

// when:
res, _ = client.R().Get("/api/v1/admin/webhooks/subscriptions")

// then:
then.Response(res).
IsOK().
WithJSONf(`[]`)
})
}
38 changes: 0 additions & 38 deletions actions/utxos/routes_test.go

This file was deleted.

65 changes: 65 additions & 0 deletions actions/utxos/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package utxos_test

import (
"testing"

"github.com/bitcoin-sv/spv-wallet/actions/testabilities"
"github.com/bitcoin-sv/spv-wallet/engine/tester/fixtures"
)

func TestUserUTXOs(t *testing.T) {
t.Run("return UTXOs for user", func(t *testing.T) {
// given:
given, then := testabilities.New(t)
cleanup := given.StartedSPVWallet()
defer cleanup()

// and:
client := given.HttpClient().ForGivenUser(fixtures.Sender)

// when:
res, _ := client.R().Get("/api/v1/utxos")

// then:
then.Response(res).
IsOK().
WithJSONf(`{
"content": [],
"page": {
"number": 1,
"size": 50,
"totalElements": 0,
"totalPages": 0
}
}`)

})

t.Run("try to return UTXOs for admin", func(t *testing.T) {
// given:
given, then := testabilities.New(t)
cleanup := given.StartedSPVWallet()
defer cleanup()
client := given.HttpClient().ForAdmin()

// when:
res, _ := client.R().Get("/api/v1/utxos")

// then:
then.Response(res).IsUnauthorizedForAdmin()
})

t.Run("return UTXOs for anonymous", func(t *testing.T) {
// given:
given, then := testabilities.New(t)
cleanup := given.StartedSPVWallet()
defer cleanup()
client := given.HttpClient().ForAnonymous()

// when:
res, _ := client.R().Get("/api/v1/utxos")

// then:
then.Response(res).IsUnauthorized()
})
}
42 changes: 0 additions & 42 deletions actions/utxos/utxo_test.go

This file was deleted.

29 changes: 29 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,35 @@ const docTemplate = `{
}
},
"/api/v1/admin/webhooks/subscriptions": {
"get": {
"security": [
{
"x-auth-xpub": []
}
],
"description": "Get All Webhooks currently subscribed to",
"produces": [
"application/json"
],
"tags": [
"Admin"
],
"summary": "Get All Webhooks",
"responses": {
"200": {
"description": "List of webhooks",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Webhook"
}
}
},
"500": {
"description": "Internal server error - Error while getting all webhooks"
}
}
},
"post": {
"security": [
{
Expand Down
29 changes: 29 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,35 @@
}
},
"/api/v1/admin/webhooks/subscriptions": {
"get": {
"security": [
{
"x-auth-xpub": []
}
],
"description": "Get All Webhooks currently subscribed to",
"produces": [
"application/json"
],
"tags": [
"Admin"
],
"summary": "Get All Webhooks",
"responses": {
"200": {
"description": "List of webhooks",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Webhook"
}
}
},
"500": {
"description": "Internal server error - Error while getting all webhooks"
}
}
},
"post": {
"security": [
{
Expand Down
18 changes: 18 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3277,6 +3277,24 @@ paths:
summary: Unsubscribe to a webhook
tags:
- Admin
get:
description: Get All Webhooks currently subscribed to
produces:
- application/json
responses:
"200":
description: List of webhooks
schema:
items:
$ref: '#/definitions/models.Webhook'
type: array
"500":
description: Internal server error - Error while getting all webhooks
security:
- x-auth-xpub: []
summary: Get All Webhooks
tags:
- Admin
post:
description: Subscribe to a webhook to receive notifications
parameters:
Expand Down
Loading

0 comments on commit b007593

Please sign in to comment.