Skip to content

Commit b612de8

Browse files
Add support for importing email templates (#988)
* Add support for importing email templates * added mock and testcases for email template * updated the docs * updated email_template.go && email_provider.go to automatic generate mocks --------- Co-authored-by: Kunal Dawar <[email protected]> Co-authored-by: KunalOfficial <[email protected]>
1 parent 0181f8d commit b612de8

8 files changed

+249
-4
lines changed

docs/auth0_terraform_generate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ auth0 terraform generate [flags]
3333
```
3434
--force Skip confirmation.
3535
-o, --output-dir string Output directory for the generated Terraform config files. If not provided, the files will be saved in the current working directory. (default "./")
36-
-r, --resources strings Resource types to generate Terraform config for. If not provided, config files for all available resources will be generated. (default [auth0_action,auth0_attack_protection,auth0_branding,auth0_client,auth0_client_grant,auth0_connection,auth0_custom_domain,auth0_email_provider,auth0_guardian,auth0_organization,auth0_pages,auth0_prompt,auth0_prompt_custom_text,auth0_resource_server,auth0_role,auth0_tenant,auth0_trigger_actions])
36+
-r, --resources strings Resource types to generate Terraform config for. If not provided, config files for all available resources will be generated. (default [auth0_action,auth0_attack_protection,auth0_branding,auth0_client,auth0_client_grant,auth0_connection,auth0_custom_domain,auth0_email_provider,auth0_email_template,auth0_guardian,auth0_organization,auth0_pages,auth0_prompt,auth0_prompt_custom_text,auth0_resource_server,auth0_role,auth0_tenant,auth0_trigger_actions])
3737
```
3838

3939

internal/auth0/email_provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:generate mockgen -source=email_provider.go -destination=mock/email_provider_mock.go -package=mock
2+
13
package auth0
24

35
import (

internal/auth0/email_template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:generate mockgen -source=email_template.go -destination=mock/email_template_mock.go -package=mock
2+
13
package auth0
24

35
import (

internal/auth0/mock/email_provider_mock.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/auth0/mock/email_template_mock.go

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cli/terraform.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func (i *terraformInputs) parseResourceFetchers(api *auth0.API) ([]resourceDataF
7474
fetchers = append(fetchers, &customDomainResourceFetcher{api})
7575
case "auth0_email_provider":
7676
fetchers = append(fetchers, &emailProviderResourceFetcher{api})
77+
case "auth0_email_template":
78+
fetchers = append(fetchers, &emailTemplateResourceFetcher{api})
7779
case "auth0_guardian":
7880
fetchers = append(fetchers, &guardianResourceFetcher{})
7981
case "auth0_log_stream":

internal/cli/terraform_fetcher.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/auth0/auth0-cli/internal/auth0"
1212
)
1313

14-
var defaultResources = []string{"auth0_action", "auth0_attack_protection", "auth0_branding", "auth0_client", "auth0_client_grant", "auth0_connection", "auth0_custom_domain", "auth0_email_provider", "auth0_guardian", "auth0_organization", "auth0_pages", "auth0_prompt", "auth0_prompt_custom_text", "auth0_resource_server", "auth0_role", "auth0_tenant", "auth0_trigger_actions"}
14+
var defaultResources = []string{"auth0_action", "auth0_attack_protection", "auth0_branding", "auth0_client", "auth0_client_grant", "auth0_connection", "auth0_custom_domain", "auth0_email_provider", "auth0_email_template", "auth0_guardian", "auth0_organization", "auth0_pages", "auth0_prompt", "auth0_prompt_custom_text", "auth0_resource_server", "auth0_role", "auth0_tenant", "auth0_trigger_actions"}
1515

1616
type (
1717
importDataList []importDataItem
@@ -54,6 +54,10 @@ type (
5454
api *auth0.API
5555
}
5656

57+
emailTemplateResourceFetcher struct {
58+
api *auth0.API
59+
}
60+
5761
guardianResourceFetcher struct{}
5862
logStreamResourceFetcher struct {
5963
api *auth0.API
@@ -245,6 +249,29 @@ func (f *emailProviderResourceFetcher) FetchData(ctx context.Context) (importDat
245249
}, nil
246250
}
247251

252+
func (f *emailTemplateResourceFetcher) FetchData(ctx context.Context) (importDataList, error) {
253+
var data importDataList
254+
255+
templates := []string{`verify_email`, `reset_email`, `welcome_email`, `blocked_account`, `stolen_credentials`, `enrollment_email`, `mfa_oob_code`, `change_password`, `password_reset`}
256+
257+
for _, template := range templates {
258+
emailTemplate, err := f.api.EmailTemplate.Read(ctx, template)
259+
if err != nil {
260+
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
261+
continue
262+
}
263+
return nil, err
264+
}
265+
266+
data = append(data, importDataItem{
267+
ResourceName: "auth0_email_template." + sanitizeResourceName(emailTemplate.GetTemplate()),
268+
ImportID: sanitizeResourceName(emailTemplate.GetTemplate()),
269+
})
270+
}
271+
272+
return data, nil
273+
}
274+
248275
func (f *guardianResourceFetcher) FetchData(_ context.Context) (importDataList, error) {
249276
return []importDataItem{
250277
{

internal/cli/terraform_fetcher_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,124 @@ func TestEmailProviderResourceFetcher_FetchData(t *testing.T) {
639639
assert.EqualError(t, err, "failed to read email provider")
640640
})
641641
}
642+
func TestEmailTemplateResourceFetcher_FetchData(t *testing.T) {
643+
t.Run("it successfully retrieves email templates data", func(t *testing.T) {
644+
ctrl := gomock.NewController(t)
645+
defer ctrl.Finish()
646+
647+
emailTemplateAPI := mock.NewMockEmailTemplateAPI(ctrl)
648+
templates := []string{
649+
"verify_email", "reset_email", "welcome_email",
650+
"blocked_account", "stolen_credentials",
651+
"enrollment_email", "mfa_oob_code",
652+
"change_password", "password_reset",
653+
}
654+
655+
for _, tmpl := range templates {
656+
emailTemplateAPI.EXPECT().
657+
Read(gomock.Any(), tmpl).
658+
Return(&management.EmailTemplate{Template: auth0.String(tmpl)}, nil)
659+
}
660+
661+
fetcher := emailTemplateResourceFetcher{
662+
api: &auth0.API{
663+
EmailTemplate: emailTemplateAPI,
664+
},
665+
}
666+
667+
expectedData := importDataList{
668+
{
669+
ResourceName: "auth0_email_template.verify_email",
670+
ImportID: "verify_email",
671+
},
672+
{
673+
ResourceName: "auth0_email_template.reset_email",
674+
ImportID: "reset_email",
675+
},
676+
{
677+
ResourceName: "auth0_email_template.welcome_email",
678+
ImportID: "welcome_email",
679+
},
680+
{
681+
ResourceName: "auth0_email_template.blocked_account",
682+
ImportID: "blocked_account",
683+
},
684+
{
685+
ResourceName: "auth0_email_template.stolen_credentials",
686+
ImportID: "stolen_credentials",
687+
},
688+
{
689+
ResourceName: "auth0_email_template.enrollment_email",
690+
ImportID: "enrollment_email",
691+
},
692+
{
693+
ResourceName: "auth0_email_template.mfa_oob_code",
694+
ImportID: "mfa_oob_code",
695+
},
696+
{
697+
ResourceName: "auth0_email_template.change_password",
698+
ImportID: "change_password",
699+
},
700+
{
701+
ResourceName: "auth0_email_template.password_reset",
702+
ImportID: "password_reset",
703+
},
704+
}
705+
706+
data, err := fetcher.FetchData(context.Background())
707+
assert.NoError(t, err)
708+
assert.Equal(t, expectedData, data)
709+
})
710+
711+
t.Run("it does not generate email template import data if email template does not exist", func(t *testing.T) {
712+
ctrl := gomock.NewController(t)
713+
defer ctrl.Finish()
714+
715+
mErr := mockManagamentError{status: http.StatusNotFound}
716+
emailTemplateAPI := mock.NewMockEmailTemplateAPI(ctrl)
717+
templates := []string{
718+
"verify_email", "reset_email", "welcome_email",
719+
"blocked_account", "stolen_credentials",
720+
"enrollment_email", "mfa_oob_code",
721+
"change_password", "password_reset",
722+
}
723+
724+
for _, tmpl := range templates {
725+
emailTemplateAPI.EXPECT().
726+
Read(gomock.Any(), tmpl).
727+
Return(nil, mErr)
728+
}
729+
730+
fetcher := emailTemplateResourceFetcher{
731+
api: &auth0.API{
732+
EmailTemplate: emailTemplateAPI,
733+
},
734+
}
735+
736+
data, err := fetcher.FetchData(context.Background())
737+
assert.NoError(t, err)
738+
assert.Len(t, data, 0)
739+
})
740+
741+
t.Run("it returns an error if api call fails", func(t *testing.T) {
742+
ctrl := gomock.NewController(t)
743+
defer ctrl.Finish()
744+
745+
emailTemplateAPI := mock.NewMockEmailTemplateAPI(ctrl)
746+
emailTemplateAPI.EXPECT().
747+
Read(gomock.Any(), gomock.Any()).
748+
Return(nil, fmt.Errorf("failed to read email template"))
749+
750+
fetcher := emailTemplateResourceFetcher{
751+
api: &auth0.API{
752+
EmailTemplate: emailTemplateAPI,
753+
},
754+
}
755+
756+
_, err := fetcher.FetchData(context.Background())
757+
assert.EqualError(t, err, "failed to read email template")
758+
})
759+
}
642760

643761
func TestLogStreamResourceFetcher_FetchData(t *testing.T) {
644762
t.Run("it successfully retrieves log streams data", func(t *testing.T) {

0 commit comments

Comments
 (0)