github.com/clerkinc/clerk-sdk-go@v1.49.1/tests/integration/templates_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/clerkinc/clerk-sdk-go/clerk"
    11  )
    12  
    13  func TestTemplates(t *testing.T) {
    14  	client := createClient()
    15  	templateType := "email"
    16  
    17  	// Get all email templates
    18  	templates, err := client.Templates().ListAll(templateType)
    19  	if err != nil {
    20  		t.Fatalf("Templates.ListAll returned error: %v", err)
    21  	}
    22  	if templates == nil {
    23  		t.Fatalf("Templates.ListAll returned nil")
    24  	}
    25  
    26  	for _, template := range templates {
    27  		slug := template.Slug
    28  
    29  		// Make sure we can read each template
    30  		tmpl, err := client.Templates().Read(templateType, slug)
    31  		if err != nil {
    32  			t.Fatalf("Templates.Read returned error: %v", err)
    33  		}
    34  		if tmpl == nil {
    35  			t.Fatalf("Templates.Read returned nil")
    36  		}
    37  
    38  		// Preview each template with sample data
    39  		fromEmailName := "marketing"
    40  		replyToEmailName := "support"
    41  		templatePreview, err := client.Templates().Preview(templateType, slug, &clerk.PreviewTemplateRequest{
    42  			Subject:          "{{AppName}} is da bomb",
    43  			Body:             "<p><a href=\"{{AppURL}}\">{{AppName}}</a> is the greatest app of all time!</p>",
    44  			FromEmailName:    &fromEmailName,
    45  			ReplyToEmailName: &replyToEmailName,
    46  		})
    47  		if err != nil {
    48  			t.Fatalf("Templates.Preview returned error: %v", err)
    49  		}
    50  		if templatePreview == nil {
    51  			t.Errorf("Templates.Preview returned nil")
    52  		}
    53  	}
    54  }
    55  
    56  func TestTemplates_Upsert(t *testing.T) {
    57  	client := createClient()
    58  
    59  	// Update one of the templates, just to make sure that the Upsert method works.
    60  	templateType := "email"
    61  	slug := "organization_invitation"
    62  	requiredVariable := "{{action_url}}"
    63  	deliveredByClerk := false
    64  	fromEmailName := "marketing"
    65  	replyToEmailName := "support"
    66  	upsertedTemplate, err := client.Templates().Upsert(templateType, slug, &clerk.UpsertTemplateRequest{
    67  		Name:             "Remarketing email",
    68  		Subject:          "Unmissable opportunity",
    69  		Markup:           "",
    70  		Body:             fmt.Sprintf("Click %s for free unicorns", requiredVariable),
    71  		FromEmailName:    &fromEmailName,
    72  		ReplyToEmailName: &replyToEmailName,
    73  		DeliveredByClerk: &deliveredByClerk,
    74  	})
    75  	if err != nil {
    76  		t.Fatalf("Templates.Update returned error: %v", err)
    77  	}
    78  	if upsertedTemplate == nil {
    79  		t.Errorf("Templates.Upsert returned nil")
    80  	}
    81  }