Skip to content

Commit

Permalink
Merge pull request #305 from LerianStudio/test/MIDAZ-304
Browse files Browse the repository at this point in the history
Test/MIDAZ-304
  • Loading branch information
maxwelbm authored Nov 26, 2024
2 parents 6f68872 + 88149c3 commit a0bd256
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 14 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test_integration_cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
on:
push:
branches:
- develop
pull_request:
branches:
- develop

jobs:
integration-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Setup .env files
run: make set-env

- name: Start Docker Compose Services
run: make up

- name: Install CLI
run: cd components/mdz && make install-local && cd -

- name: Wait for services to be ready
run: sleep 10
timeout-minutes: 2

- name: Run Integration Tests
run: make test_integration_cli
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ all-services:
$(MAKE) -C $(LEDGER_DIR) $(COMMAND) && \
$(MAKE) -C $(TRANSACTION_DIR) $(COMMAND)

test_integration_cli:
go test -v -tags=integration ./components/mdz/test/integration/...

goreleaser:
@echo "$(BLUE)Creating release snapshot...$(NC)"
goreleaser release --snapshot --skip-publish --rm-dist
Expand All @@ -162,4 +165,4 @@ tidy:
generate-docs-all:
@echo "$(BLUE)Executing command to generate swagger...$(NC)"
$(MAKE) -C $(LEDGER_DIR) generate-docs && \
$(MAKE) -C $(TRANSACTION_DIR) generate-docs
$(MAKE) -C $(TRANSACTION_DIR) generate-docs
10 changes: 9 additions & 1 deletion components/mdz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ perfsprint: get-perfsprint-deps
test:
go test ./...

.PHONY : build
.PHONY: test_integration
test_integration:
go test -v -tags=integration ./test/integration/...

.PHONY: build
build:
go version
go build -ldflags "$(LDFLAGS)" -o ./bin/$(NAME) ./main.go

.PHONY: install-local
install-local: build
sudo cp -r bin/mdz /usr/local/bin
4 changes: 3 additions & 1 deletion components/mdz/internal/rest/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func (r *account) Create(
return nil, fmt.Errorf("marshalling JSON: %v", err)
}

body := bytes.NewReader(jsonData)

uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/portfolios/%s/accounts",
r.Factory.Env.URLAPILedger, organizationID, ledgerID, portfolioID)

req, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(jsonData))
req, err := http.NewRequest(http.MethodPost, uri, body)
if err != nil {
return nil, errors.New("creating request: " + err.Error())
}
Expand Down
14 changes: 5 additions & 9 deletions components/mdz/pkg/cmd/account/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@ func (f *factoryAccountCreate) createRequestFromFlags(account *mmodel.CreateAcco
return err
}

if account.Alias != nil {
if len(f.Alias) > 0 {
account.Alias = &f.Alias
}

account.Type = f.Type

if account.ParentAccountID != nil {
if len(f.ParentAccountID) > 0 {
account.ParentAccountID = &f.ParentAccountID
}

if account.ProductID != nil {
if len(f.ProductID) > 0 {
account.ProductID = &f.ProductID
}

if account.PortfolioID != nil {
if len(f.PortfolioID) > 0 {
account.PortfolioID = &f.PortfolioID
}

if account.EntityID != nil {
if len(f.EntityID) > 0 {
account.EntityID = &f.EntityID
}

Expand Down Expand Up @@ -153,10 +153,6 @@ func (f *factoryAccountCreate) createRequestFromFlags(account *mmodel.CreateAcco
account.AllowReceiving = &allowReceive
}

if account.EntityID != nil {
account.EntityID = &f.EntityID
}

var metadata map[string]any
if err := json.Unmarshal([]byte(f.Metadata), &metadata); err != nil {
return errors.New("Error parsing metadata: " + err.Error())
Expand Down
13 changes: 12 additions & 1 deletion components/mdz/pkg/cmd/account/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,23 @@ func (f *factoryAccountDescribe) describePrint(account *mmodel.Account) {
tbl.AddRow("ID:", account.ID)
tbl.AddRow("Asset Code:", account.AssetCode)
tbl.AddRow("Name:", account.Name)
tbl.AddRow("Entity ID:", account.EntityID)

if account.EntityID != nil {
tbl.AddRow("Entity ID:", *account.EntityID)
}

if account.ProductID != nil {
tbl.AddRow("Product ID:", *account.ProductID)
}

if account.ParentAccountID != nil {
tbl.AddRow("Parent Account ID:", *account.ParentAccountID)
}

if account.Alias != nil {
tbl.AddRow("Alias:", *account.Alias)
}

tbl.AddRow("Type:", account.Type)
tbl.AddRow("Status Code:", account.Status.Code)

Expand Down
3 changes: 2 additions & 1 deletion components/mdz/pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root
import (
"errors"

"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/account"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/asset"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/ledger"
"github.com/LerianStudio/midaz/components/mdz/pkg/cmd/login"
Expand All @@ -29,7 +30,7 @@ func (f *factoryRoot) setCmds(cmd *cobra.Command) {
cmd.AddCommand(asset.NewCmdAsset(f.factory))
cmd.AddCommand(portfolio.NewCmdPortfolio(f.factory))
cmd.AddCommand(product.NewCmdProduct(f.factory))
cmd.AddCommand(asset.NewCmdAsset(f.factory))
cmd.AddCommand(account.NewCmdAccount(f.factory))
}

func (f *factoryRoot) setFlags(cmd *cobra.Command) {
Expand Down
73 changes: 73 additions & 0 deletions components/mdz/test/integration/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//go:build integration
// +build integration

package integration

import (
"bytes"
"os"
"os/exec"
"testing"
"time"

"github.com/Netflix/go-expect"
"gotest.tools/golden"
)

func TestMDZLogin(t *testing.T) {
cmd := exec.Command("mdz", "login", "--username", "user_john", "--password", "Lerian@123")

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
t.Fatalf("Error executing command: %v\nStderr: %s", err, stderr.String())
}

golden.AssertBytes(t, []byte(stdout.String()), "out_login_flags.golden")
}

func TestMDZLoginIt(t *testing.T) {
console, err := expect.NewConsole(expect.WithStdout(os.Stdout))
if err != nil {
t.Fatalf("Error creating console: %v", err)
}
defer console.Close()

cmd := exec.Command("mdz", "login")
cmd.Stdin = console.Tty()
cmd.Stdout = console.Tty()
cmd.Stderr = console.Tty()

errChan := make(chan error, 1)

go func() {
if err := cmd.Start(); err != nil {
errChan <- err
return
}

if err := cmd.Wait(); err != nil {
errChan <- err
return
}

errChan <- nil
}()

time.Sleep(1 * time.Second)
down(t, console)
enter(t, console)
sendInput(t, console, "user_john")
sendInput(t, console, "Lerian@123")
time.Sleep(1 * time.Second)

if _, err := console.ExpectString("successfully logged in"); err != nil {
t.Fatalf("Failed test: %v", err)
}

if err := <-errChan; err != nil {
t.Fatalf("Command execution error: %v", err)
}
}
49 changes: 49 additions & 0 deletions components/mdz/test/integration/mdz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# Setting environment variables (if necessary)
MDZ_CMD="mdz"
USERNAME="user_john"
PASSWORD="Lerian@123"
LEGAL_NAME="Soul LLCT"
DOING_BUSINESS_AS="The ledger.io"
LEGAL_DOCUMENT="48784548000104"
STATUS_CODE="ACTIVE"
DESCRIPTION="Test Ledger"
LINE1="Av Santso"
LINE2="VJ 222"
ZIP_CODE="04696040"
CITY="West"
STATE="VJ"
COUNTRY="MG"
METADATA='{"chave1": "valor1", "chave2": 2, "chave3": true}'

# Function to execute commands and capture the output
run_command() {
echo "Executando: $1"
OUTPUT=$($1)
echo "$OUTPUT"
}

login_output=$(run_command "$MDZ_CMD login --username $USERNAME --password $PASSWORD")
echo "$login_output"

create_output=$(run_command "$MDZ_CMD organization create --legal-name $LEGAL_NAME --doing-business-as $DOING_BUSINESS_AS --legal-document $LEGAL_DOCUMENT --code $STATUS_CODE --description $DESCRIPTION --line1 $LINE1 --line2 $LINE2 --zip-code $ZIP_CODE --city $CITY --state $STATE --country $COUNTRY --metadata $METADATA")
echo "$create_output"

list_output=$(run_command "$MDZ_CMD organization list")
echo "$list_output"

ORG_ID=$(echo "$list_output" | grep -oP '[0-9a-fA-F-]{36}' | head -n 1)
if [ -z "$ORG_ID" ]; then
echo "Erro: No ID found!"
exit 1
fi
echo "organization id: $ORG_ID"

describe_output=$(run_command "$MDZ_CMD organization describe --organization-id $ORG_ID")
echo "$describe_output"

update_output=$(run_command "$MDZ_CMD organization update --organization-id $ORG_ID --legal-name 'Updated Name' --doing-business-as 'Updated Business' --country 'BR'")
echo "$update_output"

echo "Test completed!"
Loading

0 comments on commit a0bd256

Please sign in to comment.