Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDP-1156] Remove email_sender_type and sms_sender_type from tenant config #262

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +migrate Up
ALTER TABLE public.tenants
DROP COLUMN email_sender_type,
DROP COLUMN sms_sender_type;

DROP TYPE public.email_sender_type;
DROP TYPE public.sms_sender_type;

-- +migrate Down

CREATE TYPE public.email_sender_type AS ENUM ('AWS_EMAIL', 'DRY_RUN');
CREATE TYPE public.sms_sender_type AS ENUM ('TWILIO_SMS', 'AWS_SMS', 'DRY_RUN');

ALTER TABLE public.tenants
ADD COLUMN email_sender_type email_sender_type DEFAULT 'DRY_RUN'::email_sender_type,
ADD COLUMN sms_sender_type sms_sender_type DEFAULT 'DRY_RUN'::sms_sender_type;
2 changes: 0 additions & 2 deletions dev/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ for tenant in "${tenants[@]}"; do
-d '{
"name": "'"$tenant"'",
"organization_name": "'"$tenant"'",
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": "'"$baseURL"'",
"sdp_ui_base_url": "'"$sdpUIBaseURL"'",
"owner_email": "'"$ownerEmail"'",
Expand Down
2 changes: 0 additions & 2 deletions internal/integrationtests/admin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type CreateTenantRequest struct {
OwnerFirstName string `json:"owner_first_name"`
OwnerLastName string `json:"owner_last_name"`
OrganizationName string `json:"organization_name"`
EmailSenderType string `json:"email_sender_type"`
SMSSenderType string `json:"sms_sender_type"`
BaseURL string `json:"base_url"`
SDPUIBaseURL string `json:"sdp_ui_base_url"`
DistributionAccount string `json:"distribution_account"`
Expand Down
2 changes: 0 additions & 2 deletions internal/integrationtests/integration_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,6 @@ func (it *IntegrationTestsService) CreateTestData(ctx context.Context, opts Inte
OwnerFirstName: "John",
OwnerLastName: "Doe",
OrganizationName: "Integration Tests Organization",
EmailSenderType: "DRY_RUN",
SMSSenderType: "DRY_RUN",
BaseURL: "http://localhost:8000",
SDPUIBaseURL: "http://localhost:3000",
})
Expand Down
16 changes: 6 additions & 10 deletions stellar-multitenant/internal/httphandler/tenants_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ func (h TenantsHandler) Post(rw http.ResponseWriter, req *http.Request) {
}

tnt, err = h.Manager.UpdateTenantConfig(ctx, &tenant.TenantUpdate{
ID: tnt.ID,
EmailSenderType: &reqBody.EmailSenderType,
SMSSenderType: &reqBody.SMSSenderType,
BaseURL: &reqBody.BaseURL,
ID: tnt.ID,
BaseURL: &reqBody.BaseURL,
})
if err != nil {
httperror.InternalError(ctx, "Could not update tenant config", err, nil).Render(rw)
Expand Down Expand Up @@ -122,12 +120,10 @@ func (t TenantsHandler) Patch(w http.ResponseWriter, r *http.Request) {
tenantID := chi.URLParam(r, "id")

tnt, err := t.Manager.UpdateTenantConfig(ctx, &tenant.TenantUpdate{
ID: tenantID,
EmailSenderType: reqBody.EmailSenderType,
SMSSenderType: reqBody.SMSSenderType,
BaseURL: reqBody.BaseURL,
SDPUIBaseURL: reqBody.SDPUIBaseURL,
Status: reqBody.Status,
ID: tenantID,
BaseURL: reqBody.BaseURL,
SDPUIBaseURL: reqBody.SDPUIBaseURL,
Status: reqBody.Status,
})
if err != nil {
if errors.Is(tenant.ErrEmptyUpdateTenant, err) {
Expand Down
66 changes: 1 addition & 65 deletions stellar-multitenant/internal/httphandler/tenants_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ func Test_TenantHandler_Get(t *testing.T) {
{
"id": %q,
"name": %q,
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
Expand All @@ -151,8 +149,6 @@ func Test_TenantHandler_Get(t *testing.T) {
{
"id": %q,
"name": %q,
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
Expand Down Expand Up @@ -187,8 +183,6 @@ func Test_TenantHandler_Get(t *testing.T) {
{
"id": %q,
"name": %q,
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
Expand Down Expand Up @@ -220,8 +214,6 @@ func Test_TenantHandler_Get(t *testing.T) {
{
"id": %q,
"name": %q,
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
Expand Down Expand Up @@ -317,8 +309,6 @@ func Test_TenantHandler_Post(t *testing.T) {
"owner_last_name": "owner_last_name is required",
"organization_name": "organization_name is required",
"base_url": "invalid base URL value",
"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]",
"sdp_ui_base_url": "invalid SDP UI base URL value"
}
}
Expand Down Expand Up @@ -357,8 +347,6 @@ func Test_TenantHandler_Post(t *testing.T) {
"owner_first_name": "Owner",
"owner_last_name": "Owner",
"organization_name": "My Aid Org",
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": "https://backend.sdp.org",
"sdp_ui_base_url": "https://aid-org.sdp.org",
"is_default": false
Expand All @@ -384,8 +372,6 @@ func Test_TenantHandler_Post(t *testing.T) {
{
"id": %q,
"name": "aid-org",
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": "https://backend.sdp.org",
"sdp_ui_base_url": "https://aid-org.sdp.org",
"status": "TENANT_PROVISIONED",
Expand Down Expand Up @@ -463,8 +449,6 @@ func Test_TenantHandler_Post(t *testing.T) {
"owner_first_name": "Owner",
"owner_last_name": "Owner",
"organization_name": "My Aid Org",
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": "https://backend.sdp.org",
"sdp_ui_base_url": "https://aid-org.sdp.org"
}
Expand Down Expand Up @@ -543,7 +527,7 @@ func Test_TenantHandler_Patch(t *testing.T) {

t.Run("returns NotFound when tenant does not exist", func(t *testing.T) {
rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodPatch, "/tenants/unknown", strings.NewReader(`{"email_sender_type": "AWS_EMAIL"}`))
req, err := http.NewRequest(http.MethodPatch, "/tenants/unknown", strings.NewReader(`{"base_url": "http://localhost"}`))
require.NoError(t, err)
r.ServeHTTP(rr, req)

Expand All @@ -558,14 +542,6 @@ func Test_TenantHandler_Patch(t *testing.T) {
assert.JSONEq(t, expectedRespBody, string(respBody))
})

t.Run("returns BadRequest when EmailSenderType is not valid", func(t *testing.T) {
runBadRequestPatchTest(t, r, url, "email_sender_type", "invalid email sender type. Expected one of these values: [AWS_EMAIL DRY_RUN]")
})

t.Run("returns BadRequest when SMSSenderType is not valid", func(t *testing.T) {
runBadRequestPatchTest(t, r, url, "sms_sender_type", "invalid sms sender type. Expected one of these values: [TWILIO_SMS AWS_SMS DRY_RUN]")
})

t.Run("returns BadRequest when BaseURL is not valid", func(t *testing.T) {
runBadRequestPatchTest(t, r, url, "base_url", "invalid base URL value")
})
Expand All @@ -578,41 +554,9 @@ func Test_TenantHandler_Patch(t *testing.T) {
runBadRequestPatchTest(t, r, url, "status", "invalid status value")
})

t.Run("successfully updates EmailSenderType of a tenant", func(t *testing.T) {
reqBody := `{"email_sender_type": "AWS_EMAIL"}`
expectedRespBody := `
"email_sender_type": "AWS_EMAIL",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
"distribution_account": "GCTNUNQVX7BNIP5AUWW2R4YC7G6R3JGUDNMGT7H62BGBUY4A4V6ROAAH",
"is_default": false,
`

runSuccessfulRequestPatchTest(t, r, ctx, dbConnectionPool, handler, reqBody, expectedRespBody)
})

t.Run("successfully updates SMSSenderType of a tenant", func(t *testing.T) {
reqBody := `{"SMS_sender_type": "TWILIO_SMS"}`
expectedRespBody := `
"email_sender_type": "DRY_RUN",
"sms_sender_type": "TWILIO_SMS",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
"distribution_account": "GCTNUNQVX7BNIP5AUWW2R4YC7G6R3JGUDNMGT7H62BGBUY4A4V6ROAAH",
"is_default": false,
`

runSuccessfulRequestPatchTest(t, r, ctx, dbConnectionPool, handler, reqBody, expectedRespBody)
})

t.Run("successfully updates BaseURL of a tenant", func(t *testing.T) {
reqBody := `{"base_url": "http://valid.com"}`
expectedRespBody := `
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": "http://valid.com",
"sdp_ui_base_url": null,
"status": "TENANT_CREATED",
Expand All @@ -626,8 +570,6 @@ func Test_TenantHandler_Patch(t *testing.T) {
t.Run("successfully updates SDPUIBaseURL of a tenant", func(t *testing.T) {
reqBody := `{"sdp_ui_base_url": "http://valid.com"}`
expectedRespBody := `
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": "http://valid.com",
"status": "TENANT_CREATED",
Expand All @@ -641,8 +583,6 @@ func Test_TenantHandler_Patch(t *testing.T) {
t.Run("successfully updates Status of a tenant", func(t *testing.T) {
reqBody := `{"status": "TENANT_ACTIVATED"}`
expectedRespBody := `
"email_sender_type": "DRY_RUN",
"sms_sender_type": "DRY_RUN",
"base_url": null,
"sdp_ui_base_url": null,
"status": "TENANT_ACTIVATED",
Expand All @@ -655,16 +595,12 @@ func Test_TenantHandler_Patch(t *testing.T) {

t.Run("successfully updates all fields of a tenant", func(t *testing.T) {
reqBody := `{
"email_sender_type": "AWS_EMAIL",
"sms_sender_type": "AWS_SMS",
"base_url": "http://valid.com",
"sdp_ui_base_url": "http://valid.com",
"status": "TENANT_ACTIVATED"
}`

expectedRespBody := `
"email_sender_type": "AWS_EMAIL",
"sms_sender_type": "AWS_SMS",
"base_url": "http://valid.com",
"sdp_ui_base_url": "http://valid.com",
"status": "TENANT_ACTIVATED",
Expand Down
42 changes: 10 additions & 32 deletions stellar-multitenant/internal/validators/tenant_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ import (
var validTenantName *regexp.Regexp = regexp.MustCompile(`^[a-z-]+$`)

type TenantRequest struct {
Name string `json:"name"`
OwnerEmail string `json:"owner_email"`
OwnerFirstName string `json:"owner_first_name"`
OwnerLastName string `json:"owner_last_name"`
OrganizationName string `json:"organization_name"`
EmailSenderType tenant.EmailSenderType `json:"email_sender_type"`
SMSSenderType tenant.SMSSenderType `json:"sms_sender_type"`
BaseURL string `json:"base_url"`
SDPUIBaseURL string `json:"sdp_ui_base_url"`
Name string `json:"name"`
OwnerEmail string `json:"owner_email"`
OwnerFirstName string `json:"owner_first_name"`
OwnerLastName string `json:"owner_last_name"`
OrganizationName string `json:"organization_name"`
BaseURL string `json:"base_url"`
SDPUIBaseURL string `json:"sdp_ui_base_url"`
}

type UpdateTenantRequest struct {
EmailSenderType *tenant.EmailSenderType `json:"email_sender_type"`
SMSSenderType *tenant.SMSSenderType `json:"sms_sender_type"`
BaseURL *string `json:"base_url"`
SDPUIBaseURL *string `json:"sdp_ui_base_url"`
Status *tenant.TenantStatus `json:"status"`
BaseURL *string `json:"base_url"`
SDPUIBaseURL *string `json:"sdp_ui_base_url"`
Status *tenant.TenantStatus `json:"status"`
}

type DefaultTenantRequest struct {
Expand Down Expand Up @@ -65,12 +61,6 @@ func (tv *TenantValidator) ValidateCreateTenantRequest(reqBody *TenantRequest) *
tv.Check(reqBody.OrganizationName != "", "organization_name", "organization_name is required")

var err error
reqBody.EmailSenderType, err = tenant.ParseEmailSenderType(string(reqBody.EmailSenderType))
tv.CheckError(err, "email_sender_type", fmt.Sprintf("invalid email sender type. Expected one of these values: %s", []tenant.EmailSenderType{tenant.AWSEmailSenderType, tenant.DryRunEmailSenderType}))

reqBody.SMSSenderType, err = tenant.ParseSMSSenderType(string(reqBody.SMSSenderType))
tv.CheckError(err, "sms_sender_type", fmt.Sprintf("invalid sms sender type. Expected one of these values: %s", []tenant.SMSSenderType{tenant.TwilioSMSSenderType, tenant.AWSSMSSenderType, tenant.DryRunSMSSenderType}))

if _, err = url.ParseRequestURI(reqBody.BaseURL); err != nil {
tv.Check(false, "base_url", "invalid base URL value")
}
Expand All @@ -93,18 +83,6 @@ func (tv *TenantValidator) ValidateUpdateTenantRequest(reqBody *UpdateTenantRequ
}

var err error
if reqBody.EmailSenderType != nil {
emailSenderType, emailErr := tenant.ParseEmailSenderType(string(*reqBody.EmailSenderType))
tv.CheckError(emailErr, "email_sender_type", fmt.Sprintf("invalid email sender type. Expected one of these values: %s", []tenant.EmailSenderType{tenant.AWSEmailSenderType, tenant.DryRunEmailSenderType}))
reqBody.EmailSenderType = &emailSenderType
}

if reqBody.SMSSenderType != nil {
SMSSenderType, smsErr := tenant.ParseSMSSenderType(string(*reqBody.SMSSenderType))
tv.CheckError(smsErr, "sms_sender_type", fmt.Sprintf("invalid sms sender type. Expected one of these values: %s", []tenant.SMSSenderType{tenant.TwilioSMSSenderType, tenant.AWSSMSSenderType, tenant.DryRunSMSSenderType}))
reqBody.SMSSenderType = &SMSSenderType
}

if reqBody.BaseURL != nil {
if _, err = url.ParseRequestURI(*reqBody.BaseURL); err != nil {
tv.Check(false, "base_url", "invalid base URL value")
Expand Down
Loading
Loading