mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-23 23:55:02 +01:00
Feature: integration testing
Add integration tests with testify/suite. Moved some structs outside of controller funcs so we can re-use them in the testing package. Add CI workflow for running tests on every push.
This commit is contained in:
18
.github/workflows/integration_tests.yaml
vendored
Normal file
18
.github/workflows/integration_tests.yaml
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
name: Integration tests
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [ 1.16.x ]
|
||||||
|
platform: [ ubuntu-latest ]
|
||||||
|
runs-on: ${{ matrix.platform }}
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Run tests
|
||||||
|
run: go test -v -cover -coverpkg=./... ./integration_tests
|
||||||
@@ -19,20 +19,24 @@ func NewAuthController(svc *service.LndhubService) *AuthController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AuthRequestBody struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
}
|
||||||
|
type AuthResponseBody struct {
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
}
|
||||||
|
|
||||||
// Auth : Auth Controller
|
// Auth : Auth Controller
|
||||||
func (controller *AuthController) Auth(c echo.Context) error {
|
func (controller *AuthController) Auth(c echo.Context) error {
|
||||||
type RequestBody struct {
|
|
||||||
Login string `json:"login"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
RefreshToken string `json:"refresh_token"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var body RequestBody
|
var body AuthRequestBody
|
||||||
|
|
||||||
if err := c.Bind(&body); err != nil {
|
if err := c.Bind(&body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Validate(&body); err != nil {
|
if err := c.Validate(&body); err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
||||||
}
|
}
|
||||||
@@ -42,8 +46,8 @@ func (controller *AuthController) Auth(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, echo.Map{
|
return c.JSON(http.StatusOK, &AuthResponseBody{
|
||||||
"refresh_token": refreshToken,
|
RefreshToken: refreshToken,
|
||||||
"access_token": accessToken,
|
AccessToken: accessToken,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ func NewCreateUserController(svc *service.LndhubService) *CreateUserController {
|
|||||||
return &CreateUserController{svc: svc}
|
return &CreateUserController{svc: svc}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateUserResponseBody struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
// CreateUser : Create user Controller
|
// CreateUser : Create user Controller
|
||||||
func (controller *CreateUserController) CreateUser(c echo.Context) error {
|
func (controller *CreateUserController) CreateUser(c echo.Context) error {
|
||||||
// optional parameters that we currently do not use
|
// optional parameters that we currently do not use
|
||||||
@@ -34,10 +39,7 @@ func (controller *CreateUserController) CreateUser(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var ResponseBody struct {
|
var ResponseBody CreateUserResponseBody
|
||||||
Login string `json:"login"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
ResponseBody.Login = user.Login
|
ResponseBody.Login = user.Login
|
||||||
ResponseBody.Password = user.Password
|
ResponseBody.Password = user.Password
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,76 @@
|
|||||||
package integration_tests
|
package integration_tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/getAlby/lndhub.go/controllers"
|
||||||
|
"github.com/getAlby/lndhub.go/lib"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/responses"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/service"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserAuthTestSuite struct {
|
type UserAuthTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
|
Service *service.LndhubService
|
||||||
|
echo *echo.Echo
|
||||||
|
userLogin *controllers.CreateUserResponseBody
|
||||||
}
|
}
|
||||||
|
|
||||||
func (UserAuthTestSuite) SetupSuite() {
|
func (suite *UserAuthTestSuite) SetupSuite() {
|
||||||
|
svc, err := LndHubTestServiceInit()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error initializing test service: %v", err)
|
||||||
|
}
|
||||||
|
suite.Service = svc
|
||||||
|
e := echo.New()
|
||||||
|
|
||||||
|
e.HTTPErrorHandler = responses.HTTPErrorHandler
|
||||||
|
e.Validator = &lib.CustomValidator{Validator: validator.New()}
|
||||||
|
suite.echo = e
|
||||||
|
//create user
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/create", bytes.NewReader([]byte{}))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
controller := controllers.NewCreateUserController(suite.Service)
|
||||||
|
responseBody := controllers.CreateUserResponseBody{}
|
||||||
|
assert.NoError(suite.T(), controller.CreateUser(c))
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&responseBody))
|
||||||
|
suite.userLogin = &responseBody
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *UserAuthTestSuite) TearDownSuite() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (UserAuthTestSuite) TearDownSuite() {
|
func (suite *UserAuthTestSuite) TestAuth() {
|
||||||
|
var buf bytes.Buffer
|
||||||
}
|
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.AuthRequestBody{
|
||||||
|
Login: suite.userLogin.Login,
|
||||||
func (UserAuthTestSuite) TestAuth() {
|
Password: suite.userLogin.Password,
|
||||||
|
}))
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/auth", &buf)
|
||||||
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := suite.echo.NewContext(req, rec)
|
||||||
|
controller := controllers.NewAuthController(suite.Service)
|
||||||
|
responseBody := &controllers.AuthResponseBody{}
|
||||||
|
assert.NoError(suite.T(), controller.Auth(c))
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&responseBody))
|
||||||
|
assert.NotEmpty(suite.T(), responseBody.AccessToken)
|
||||||
|
assert.NotEmpty(suite.T(), responseBody.RefreshToken)
|
||||||
|
fmt.Printf("Succesfully got a token: %s\n", responseBody.AccessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserAuthTestSuite(t *testing.T) {
|
func TestUserAuthTestSuite(t *testing.T) {
|
||||||
|
|||||||
@@ -1,27 +1,101 @@
|
|||||||
package integration_tests
|
package integration_tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/getAlby/lndhub.go/controllers"
|
||||||
|
"github.com/getAlby/lndhub.go/db"
|
||||||
|
"github.com/getAlby/lndhub.go/db/migrations"
|
||||||
|
"github.com/getAlby/lndhub.go/lib"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/responses"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/service"
|
||||||
|
"github.com/getAlby/lndhub.go/lnd"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
"github.com/uptrace/bun/migrate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateUserTestSuite struct {
|
type CreateUserTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
|
Service *service.LndhubService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (CreateUserTestSuite) SetupSuite() {
|
func (suite *CreateUserTestSuite) SetupSuite() {
|
||||||
|
svc, err := LndHubTestServiceInit()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error initializing test service: %v", err)
|
||||||
|
}
|
||||||
|
suite.Service = svc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *CreateUserTestSuite) TearDownSuite() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (CreateUserTestSuite) TearDownSuite() {
|
func (suite *CreateUserTestSuite) TestCreate() {
|
||||||
|
e := echo.New()
|
||||||
}
|
e.HTTPErrorHandler = responses.HTTPErrorHandler
|
||||||
|
e.Validator = &lib.CustomValidator{Validator: validator.New()}
|
||||||
func (CreateUserTestSuite) TestCreate() {
|
req := httptest.NewRequest(http.MethodPost, "/create", bytes.NewReader([]byte{}))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
controller := controllers.NewCreateUserController(suite.Service)
|
||||||
|
responseBody := controllers.CreateUserResponseBody{}
|
||||||
|
if assert.NoError(suite.T(), controller.CreateUser(c)) {
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&responseBody))
|
||||||
|
assert.NotEmpty(suite.T(), responseBody.Login)
|
||||||
|
assert.NotEmpty(suite.T(), responseBody.Password)
|
||||||
|
fmt.Printf("Sucessfully created user with login %ss\n", responseBody.Login)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateUserTestSuite(t *testing.T) {
|
func TestCreateUserTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(CreateUserTestSuite))
|
suite.Run(t, new(CreateUserTestSuite))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LndHubTestServiceInit() (*service.LndhubService, error) {
|
||||||
|
c := &service.Config{
|
||||||
|
DatabaseUri: "file:data_test.db",
|
||||||
|
JWTSecret: []byte("SECRET"),
|
||||||
|
LNDAddress: "rpc.lnd1.regtest.getalby.com:443",
|
||||||
|
LNDMacaroonHex: "0201036c6e6402f801030a10e2133a1cac2c5b4d56e44e32dc64c8551201301a160a0761646472657373120472656164120577726974651a130a04696e666f120472656164120577726974651a170a08696e766f69636573120472656164120577726974651a210a086d616361726f6f6e120867656e6572617465120472656164120577726974651a160a076d657373616765120472656164120577726974651a170a086f6666636861696e120472656164120577726974651a160a076f6e636861696e120472656164120577726974651a140a057065657273120472656164120577726974651a180a067369676e6572120867656e657261746512047265616400000620c4f9783e0873fa50a2091806f5ebb919c5dc432e33800b401463ada6485df0ed",
|
||||||
|
}
|
||||||
|
dbConn, err := db.Open(c.DatabaseUri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
migrator := migrate.NewMigrator(dbConn, migrations.Migrations)
|
||||||
|
err = migrator.Init(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to init migrations: %w", err)
|
||||||
|
}
|
||||||
|
_, err = migrator.Migrate(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to migrate: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{
|
||||||
|
Address: c.LNDAddress,
|
||||||
|
MacaroonHex: c.LNDMacaroonHex,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to initialize lnd service client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &service.LndhubService{
|
||||||
|
Config: c,
|
||||||
|
DB: dbConn,
|
||||||
|
LndClient: lndClient,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func (svc *LndhubService) ProcessInvoiceUpdate(ctx context.Context, rawInvoice *
|
|||||||
|
|
||||||
// if the invoice is NOT settled we just update the invoice state
|
// if the invoice is NOT settled we just update the invoice state
|
||||||
if !rawInvoice.Settled {
|
if !rawInvoice.Settled {
|
||||||
svc.Logger.Infof("Invoice not settled invoice_id:%v state:", invoice.ID, rawInvoice.State.String())
|
svc.Logger.Infof("Invoice not settled invoice_id:%v state: %s", invoice.ID, rawInvoice.State.String())
|
||||||
invoice.State = strings.ToLower(rawInvoice.State.String())
|
invoice.State = strings.ToLower(rawInvoice.State.String())
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user