-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSendGridTestsKit.swift
More file actions
102 lines (88 loc) · 3.64 KB
/
SendGridTestsKit.swift
File metadata and controls
102 lines (88 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import AsyncHTTPClient
import SendGridKit
import Testing
struct SendGridKitTests {
var client: SendGridClient
init() {
// TODO: Replace with a valid API key to test
client = SendGridClient(httpClient: HTTPClient.shared, apiKey: "YOUR-API-KEY")
}
@Test func sendEmail() async throws {
// TODO: Replace to address with the email address you'd like to recieve your test email
let emailAddress = EmailAddress("TO-ADDRESS")
// TODO: Replace from address with the email address associated with your verified Sender Identity
let fromEmailAddress = EmailAddress(email: "FROM-ADDRESS", name: "Test")
let personalization = Personalization(to: [emailAddress], subject: "Test Email")
let attachment = EmailAttachment(
content: "Hello, World!".data(using: .utf8)!.base64EncodedString(),
type: "text/plain",
filename: "hello.txt",
disposition: .attachment
)
let emailContent = EmailContent("This email was sent using SendGridKit!")
let setting = Setting(enable: true)
let mailSettings = MailSettings(
bypassListManagement: setting,
bypassSpamManagement: setting,
bypassBounceManagement: setting,
footer: Footer(enable: true, text: "footer", html: "<strong>footer</strong>"),
sandboxMode: setting
)
let trackingSettings = TrackingSettings(
clickTracking: ClickTracking(enable: true, enableText: true),
openTracking: OpenTracking(enable: true, substitutionTag: "open_tracking"),
subscriptionTracking: SubscriptionTracking(
enable: true,
text: "sub_text",
html: "<strong>sub_html</strong>",
substitutionTag: "sub_tag"
),
ganalytics: GoogleAnalytics(
enable: true,
utmSource: "utm_source",
utmMedium: "utm_medium",
utmTerm: "utm_term",
utmContent: "utm_content",
utmCampaign: "utm_campaign"
)
)
let asm = AdvancedSuppressionManager(groupID: 21, groupsToDisplay: ["group1", "group2"])
let email = SendGridEmail(
personalizations: [personalization],
from: fromEmailAddress,
content: [emailContent],
attachments: [attachment],
asm: asm,
mailSettings: mailSettings,
trackingSettings: trackingSettings
)
try await withKnownIssue {
try await client.send(email: email)
} when: {
// TODO: Replace with `false` when you have a valid API key
true
}
}
@Test func dynamicTemplateData() async throws {
struct DynamicTemplateData: Codable, Sendable {
let text: String
let integer: Int
let double: Double
}
let dynamicTemplateData = DynamicTemplateData(
text: "Hello, World!", integer: 42, double: 3.14)
// TODO: Replace the addresses with real email addresses
let personalization = Personalization(
to: [EmailAddress("TO-ADDRESS")], subject: "Test Email",
dynamicTemplateData: dynamicTemplateData)
let email = SendGridEmail(
personalizations: [personalization], from: EmailAddress("FROM-ADDRESS"),
content: [EmailContent("Hello, World!")])
try await withKnownIssue {
try await client.send(email: email)
} when: {
// TODO: Replace with `false` when you have a valid API key
true
}
}
}