diff --git a/.github/workflows/integration_tests.yaml b/.github/workflows/integration_tests.yaml new file mode 100644 index 0000000..db0fc81 --- /dev/null +++ b/.github/workflows/integration_tests.yaml @@ -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 \ No newline at end of file diff --git a/controllers/auth.ctrl.go b/controllers/auth.ctrl.go index 50e5ef8..c888caf 100644 --- a/controllers/auth.ctrl.go +++ b/controllers/auth.ctrl.go @@ -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 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 { return err } - if err := c.Validate(&body); err != nil { return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) } @@ -42,8 +46,8 @@ func (controller *AuthController) Auth(c echo.Context) error { return err } - return c.JSON(http.StatusOK, echo.Map{ - "refresh_token": refreshToken, - "access_token": accessToken, + return c.JSON(http.StatusOK, &AuthResponseBody{ + RefreshToken: refreshToken, + AccessToken: accessToken, }) } diff --git a/controllers/create.ctrl.go b/controllers/create.ctrl.go index 7e25f40..618665a 100644 --- a/controllers/create.ctrl.go +++ b/controllers/create.ctrl.go @@ -16,6 +16,11 @@ func NewCreateUserController(svc *service.LndhubService) *CreateUserController { return &CreateUserController{svc: svc} } +type CreateUserResponseBody struct { + Login string `json:"login"` + Password string `json:"password"` +} + // CreateUser : Create user Controller func (controller *CreateUserController) CreateUser(c echo.Context) error { // optional parameters that we currently do not use @@ -34,10 +39,7 @@ func (controller *CreateUserController) CreateUser(c echo.Context) error { return err } - var ResponseBody struct { - Login string `json:"login"` - Password string `json:"password"` - } + var ResponseBody CreateUserResponseBody ResponseBody.Login = user.Login ResponseBody.Password = user.Password diff --git a/integration_tests/auth_test.go b/integration_tests/auth_test.go index 0277bf5..d00a0f0 100644 --- a/integration_tests/auth_test.go +++ b/integration_tests/auth_test.go @@ -1,25 +1,76 @@ package integration_tests import ( + "bytes" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" "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" ) type UserAuthTestSuite struct { 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 (UserAuthTestSuite) TestAuth() { - +func (suite *UserAuthTestSuite) TestAuth() { + var buf bytes.Buffer + assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.AuthRequestBody{ + Login: suite.userLogin.Login, + 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) { diff --git a/integration_tests/create_test.go b/integration_tests/create_test.go index bdfb14d..8f67b0d 100644 --- a/integration_tests/create_test.go +++ b/integration_tests/create_test.go @@ -1,27 +1,101 @@ package integration_tests import ( + "bytes" + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" "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/uptrace/bun/migrate" ) type CreateUserTestSuite struct { 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 (CreateUserTestSuite) TestCreate() { - +func (suite *CreateUserTestSuite) TestCreate() { + e := echo.New() + e.HTTPErrorHandler = responses.HTTPErrorHandler + e.Validator = &lib.CustomValidator{Validator: validator.New()} + 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) { 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 +} diff --git a/lib/service/invoicesubscription.go b/lib/service/invoicesubscription.go index ec26c4b..2cc06f1 100644 --- a/lib/service/invoicesubscription.go +++ b/lib/service/invoicesubscription.go @@ -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 !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()) } else {