Skip to content

Commit

Permalink
fix: get network type from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioTeixeira95 committed Nov 17, 2023
1 parent 026a7a3 commit 82adf47
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 55 deletions.
1 change: 1 addition & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func (c *ServeCommand) Command(serverService ServerServiceInterface, monitorServ
adminServeOpts.Environment = globalOptions.environment
adminServeOpts.GitCommit = globalOptions.gitCommit
adminServeOpts.Version = globalOptions.version
adminServeOpts.NetworkPassphrase = globalOptions.networkPassphrase
},
Run: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
Expand Down
1 change: 1 addition & 0 deletions cmd/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func Test_serve(t *testing.T) {
EmailMessengerClient: messengerClient,
DatabaseDSN: randomDatabaseDSN,
GitCommit: "1234567890abcdef",
NetworkPassphrase: network.TestNetworkPassphrase,
Port: 8003,
Version: "x.y.z",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/render/httpjson"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/internal/provisioning"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/internal/validators"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/tenant"
Expand All @@ -18,6 +19,7 @@ import (
type TenantsHandler struct {
Manager *tenant.Manager
ProvisioningManager *provisioning.Manager
NetworkType utils.NetworkType
}

func (t TenantsHandler) GetAll(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -70,7 +72,7 @@ func (h TenantsHandler) PostTenants(rw http.ResponseWriter, req *http.Request) {
tnt, err := h.ProvisioningManager.ProvisionNewTenant(
ctx, reqBody.Name, reqBody.OwnerFirstName,
reqBody.OwnerLastName, reqBody.OwnerEmail, reqBody.OrganizationName,
reqBody.SDPUIBaseURL, reqBody.NetworkType,
reqBody.SDPUIBaseURL, string(h.NetworkType),
)
if err != nil {
httperror.InternalError(ctx, "Could not provision a new tenant", err, nil).Render(rw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/internal/provisioning"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/tenant"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -224,6 +225,7 @@ func Test_TenantHandler_Post(t *testing.T) {
handler := TenantsHandler{
Manager: m,
ProvisioningManager: p,
NetworkType: utils.TestnetNetworkType,
}

t.Run("returns BadRequest with invalid request body", func(t *testing.T) {
Expand Down Expand Up @@ -252,7 +254,6 @@ func Test_TenantHandler_Post(t *testing.T) {
"email_sender_type": "invalid email sender type. Expected one of these values: [AWS_EMAIL DRY_RUN]",
"sms_sender_type": "invalid sms sender type. Expected one of these values: [TWILIO_SMS AWS_SMS DRY_RUN]",
"cors_allowed_origins": "provide at least one CORS allowed origins",
"network_type": "invalid network type provided. Expected one of these values: pubnet or testnet",
"sdp_ui_base_url": "invalid SDP UI base URL value",
"sep10_signing_public_key": "invalid public key",
"distribution_public_key": "invalid public key"
Expand Down Expand Up @@ -289,7 +290,6 @@ func Test_TenantHandler_Post(t *testing.T) {
"enable_recaptcha": true,
"enable_mfa": false,
"cors_allowed_origins": ["*"],
"network_type": "testnet",
"base_url": "https://backend.sdp.org",
"sdp_ui_base_url": "https://aid-org.sdp.org",
"sep10_signing_public_key": %q,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type TenantRequest struct {
OwnerFirstName string `json:"owner_first_name"`
OwnerLastName string `json:"owner_last_name"`
OrganizationName string `json:"organization_name"`
NetworkType string `json:"network_type"`
EmailSenderType tenant.EmailSenderType `json:"email_sender_type"`
SMSSenderType tenant.SMSSenderType `json:"sms_sender_type"`
SEP10SigningPublicKey string `json:"sep10_signing_public_key"`
Expand Down Expand Up @@ -49,7 +48,6 @@ func (tv *TenantValidator) ValidateCreateTenantRequest(reqBody *TenantRequest) *
tv.Check(reqBody.OwnerFirstName != "", "owner_first_name", "owner_first_name is required")
tv.Check(reqBody.OwnerLastName != "", "owner_last_name", "owner_last_name is required")
tv.Check(reqBody.OrganizationName != "", "organization_name", "organization_name is required")
tv.Check(reqBody.NetworkType == "pubnet" || reqBody.NetworkType == "testnet", "network_type", "invalid network type provided. Expected one of these values: pubnet or testnet")

var err error
reqBody.EmailSenderType, err = tenant.ParseEmailSenderType(string(reqBody.EmailSenderType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
"email_sender_type": "invalid email sender type. Expected one of these values: [AWS_EMAIL DRY_RUN]",
"sms_sender_type": "invalid sms sender type. Expected one of these values: [TWILIO_SMS AWS_SMS DRY_RUN]",
"cors_allowed_origins": "provide at least one CORS allowed origins",
"network_type": "invalid network type provided. Expected one of these values: pubnet or testnet",
"sdp_ui_base_url": "invalid SDP UI base URL value",
"sep10_signing_public_key": "invalid public key",
"distribution_public_key": "invalid public key",
Expand All @@ -51,7 +50,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
"email_sender_type": "invalid email sender type. Expected one of these values: [AWS_EMAIL DRY_RUN]",
"sms_sender_type": "invalid sms sender type. Expected one of these values: [TWILIO_SMS AWS_SMS DRY_RUN]",
"cors_allowed_origins": "provide at least one CORS allowed origins",
"network_type": "invalid network type provided. Expected one of these values: pubnet or testnet",
"sdp_ui_base_url": "invalid SDP UI base URL value",
"sep10_signing_public_key": "invalid public key",
"distribution_public_key": "invalid public key",
Expand All @@ -66,7 +64,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "pubnet",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: keypair.MustRandom().Address(),
Expand All @@ -93,7 +90,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "",
OwnerLastName: "",
OrganizationName: "",
NetworkType: "pubnet",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: keypair.MustRandom().Address(),
Expand Down Expand Up @@ -124,41 +120,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
assert.Equal(t, map[string]interface{}{}, tv.Errors)
})

t.Run("validates the network type successfully", func(t *testing.T) {
tv := NewTenantValidator()
reqBody := &TenantRequest{
Name: "aid-org",
OwnerEmail: "[email protected]",
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "invalid",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: keypair.MustRandom().Address(),
DistributionPublicKey: keypair.MustRandom().Address(),
EnableMFA: true,
EnableReCAPTCHA: true,
CORSAllowedOrigins: []string{"*"},
SDPUIBaseURL: "http://localhost:3000",
BaseURL: "http://localhost:8000",
}

tv.ValidateCreateTenantRequest(reqBody)
assert.True(t, tv.HasErrors())
assert.Equal(t, map[string]interface{}{"network_type": "invalid network type provided. Expected one of these values: pubnet or testnet"}, tv.Errors)

reqBody.NetworkType = "pubnet"
tv.Errors = map[string]interface{}{}
tv.ValidateCreateTenantRequest(reqBody)
assert.False(t, tv.HasErrors())

reqBody.NetworkType = "testnet"
tv.Errors = map[string]interface{}{}
tv.ValidateCreateTenantRequest(reqBody)
assert.False(t, tv.HasErrors())
})

t.Run("validates the email sender type successfully", func(t *testing.T) {
tv := NewTenantValidator()
reqBody := &TenantRequest{
Expand All @@ -167,7 +128,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "pubnet",
EmailSenderType: "invalid",
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: keypair.MustRandom().Address(),
Expand Down Expand Up @@ -197,7 +157,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "pubnet",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: "invalid",
SEP10SigningPublicKey: keypair.MustRandom().Address(),
Expand Down Expand Up @@ -227,7 +186,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "pubnet",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: "invalid",
Expand Down Expand Up @@ -255,7 +213,6 @@ func TestTenantValidator_ValidateCreateTenantRequest(t *testing.T) {
OwnerFirstName: "Owner",
OwnerLastName: "Owner",
OrganizationName: "Aid Org",
NetworkType: "pubnet",
EmailSenderType: tenant.AWSEmailSenderType,
SMSSenderType: tenant.TwilioSMSSenderType,
SEP10SigningPublicKey: keypair.MustRandom().Address(),
Expand Down
13 changes: 11 additions & 2 deletions stellar-multitenant/pkg/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/middleware"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/internal/httphandler"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/internal/provisioning"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-multitenant/pkg/tenant"
Expand All @@ -32,6 +33,8 @@ type ServeOptions struct {
EmailMessengerClient message.MessengerClient
Environment string
GitCommit string
NetworkPassphrase string
networkType utils.NetworkType
Port int
tenantManager *tenant.Manager
tenantProvisioningManager *provisioning.Manager
Expand All @@ -43,7 +46,7 @@ func (opts *ServeOptions) SetupDependencies() error {
// Setup Database:
dbConnectionPool, err := db.OpenDBConnectionPool(opts.DatabaseDSN)
if err != nil {
return fmt.Errorf("error connecting to the database: %w", err)
return fmt.Errorf("connecting to the database: %w", err)
}

opts.dbConnectionPool = dbConnectionPool
Expand All @@ -55,12 +58,17 @@ func (opts *ServeOptions) SetupDependencies() error {
provisioning.WithMessengerClient(opts.EmailMessengerClient),
)

opts.networkType, err = utils.GetNetworkTypeFromNetworkPassphrase(opts.NetworkPassphrase)
if err != nil {
return fmt.Errorf("parsing network type: %w", err)
}

return nil
}

func StartServe(opts ServeOptions, httpServer HTTPServerInterface) error {
if err := opts.SetupDependencies(); err != nil {
return fmt.Errorf("error starting dependencies: %w", err)
return fmt.Errorf("starting dependencies: %w", err)
}

// Start the server
Expand Down Expand Up @@ -108,6 +116,7 @@ func handleHTTP(opts *ServeOptions) *chi.Mux {
tenantsHandler := httphandler.TenantsHandler{
Manager: opts.tenantManager,
ProvisioningManager: opts.tenantProvisioningManager,
NetworkType: opts.networkType,
}
r.Get("/", tenantsHandler.GetAll)
r.Post("/", tenantsHandler.PostTenants)
Expand Down
12 changes: 7 additions & 5 deletions stellar-multitenant/pkg/serve/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/stellar/go/network"
supporthttp "github.com/stellar/go/support/http"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
Expand Down Expand Up @@ -31,11 +32,12 @@ func Test_Serve(t *testing.T) {
defer dbConnectionPool.Close()

opts := ServeOptions{
DatabaseDSN: dbt.DSN,
Environment: "test",
GitCommit: "1234567890abcdef",
Port: 8003,
Version: "x.y.z",
DatabaseDSN: dbt.DSN,
Environment: "test",
GitCommit: "1234567890abcdef",
NetworkPassphrase: network.TestNetworkPassphrase,
Port: 8003,
Version: "x.y.z",
}

// Mock supportHTTPRun
Expand Down

0 comments on commit 82adf47

Please sign in to comment.