-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtesting.go
More file actions
222 lines (200 loc) · 6.87 KB
/
Copy pathtesting.go
File metadata and controls
222 lines (200 loc) · 6.87 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package email
import (
"bytes"
"encoding/json"
"io"
"time"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/theopenlane/newman/providers/mock"
)
// MockRuntimeConfig returns a RuntimeEmailConfig backed by the mock provider
// for use in integration test suites
func MockRuntimeConfig() *RuntimeEmailConfig {
return &RuntimeEmailConfig{ //nolint:gosec // test-only mock credentials
APIKey: "mock-api-key",
Provider: ProviderMock,
FromEmail: "test@example.com",
CompanyName: "MITB Company",
CompanyAddress: "123 Paper street",
Corporation: "MITB Corp",
SupportEmail: "support@example.com",
LogoURL: "https://example.com/logo.png",
HeaderLogoURL: "https://example.com/icon.png",
HeaderText: "MITB Portal",
RootURL: "https://example.com",
ProductURL: "https://app.example.com",
DocsURL: "https://docs.example.com",
TermsURL: "https://example.com/terms",
PrivacyURL: "https://example.com/privacy",
Tagline: "Compliance without the busywork",
Social: []SocialLink{
{Platform: "GitHub", IconURL: "https://example.com/github.png", URL: "https://github.com/example"},
{Platform: "LinkedIn", IconURL: "https://example.com/linkedin.png", URL: "https://linkedin.com/company/example"},
},
}
}
// MockSenderFromClient extracts the *mock.EmailSender from an Client.
// Returns nil if the sender is not a mock
func MockSenderFromClient(client any) *mock.EmailSender {
ec, ok := client.(*Client)
if !ok {
return nil
}
ms, ok := ec.Sender.(*mock.EmailSender)
if !ok {
return nil
}
return ms
}
// TestRecipient returns a RecipientInfo with the given email and placeholder name fields
func TestRecipient(toEmail string) RecipientInfo {
return RecipientInfo{
Email: toEmail,
FirstName: "Wilfred",
LastName: "Netherton",
}
}
// TestFixture returns a scaffolded JSON payload for the given dispatcher name.
// All required fields are populated with realistic test values; URL-resolved fields
// (NDAURL, AuthURL) are set directly so PreHooks that require a database are bypassed.
// Returns nil when no fixture is defined for the dispatcher
func TestFixture(name, toEmail string) json.RawMessage {
r := TestRecipient(toEmail)
fixtures := map[string]any{
"VerifyEmailRequest": VerifyEmailRequest{
RecipientInfo: r,
Token: "test-verify-token-12345",
},
"WelcomeRequest": WelcomeRequest{
RecipientInfo: r,
},
"InviteRequest": InviteRequest{
RecipientInfo: r,
InviterName: "Jane Smith",
OrgName: "Acme Corp",
Role: "admin",
Token: "test-invite-token-12345",
},
"InviteJoinedRequest": InviteJoinedRequest{
RecipientInfo: r,
OrgName: "Acme Corp",
},
"PasswordResetEmailRequest": PasswordResetEmailRequest{
RecipientInfo: r,
Token: "test-reset-token-12345",
},
"PasswordResetSuccessRequest": PasswordResetSuccessRequest{
RecipientInfo: r,
},
"SubscribeRequest": SubscribeRequest{
RecipientInfo: r,
OrgName: "Acme Corp",
Token: "test-subscribe-token-12345",
},
"VerifyBillingRequest": VerifyBillingRequest{
RecipientInfo: r,
Token: "test-billing-token-12345",
},
"TrustCenterNDARequestEmail": TrustCenterNDARequestEmail{
RecipientInfo: r,
OrgName: "SecureCorp",
NDAURL: "https://trustcenter.example.com/securecorp/access/sign-nda?token=test",
},
"TrustCenterNDASignedEmail": TrustCenterNDASignedEmail{
RecipientInfo: r,
OrgName: "SecureCorp",
TrustCenterURL: "https://trustcenter.example.com/securecorp?token=test",
AttachmentFilename: "SecureCorp-NDA-Signed.pdf",
AttachmentData: testAttestedNDAPDF(),
},
"TrustCenterAuthEmail": TrustCenterAuthEmail{
RecipientInfo: r,
OrgName: "SecureCorp",
AuthURL: "https://trustcenter.example.com/securecorp/auth?token=test",
},
"TrustCenterNDAApprovalRequestEmail": TrustCenterNDAApprovalRequestEmail{
RecipientInfo: RecipientInfo{Email: toEmail, Recipients: []string{toEmail}, FirstName: r.FirstName, LastName: r.LastName},
OrgName: "SecureCorp",
RequesterName: "Dolores Abernathy",
RequesterEmail: "dolores.abernathy@example.com",
},
"QuestionnaireAuthEmail": QuestionnaireAuthEmail{
RecipientInfo: r,
OrgName: "Acme Corp",
AssessmentName: "SOC 2 Type II Review",
AuthURL: "https://questionnaire.example.com/auth?token=test",
},
"BillingEmailChangedEmail": BillingEmailChangedEmail{
RecipientInfo: r,
OrgName: "Acme Corp",
OldBillingEmail: "old-billing@acme.com",
NewBillingEmail: "new-billing@acme.com",
ChangedAt: time.Now().UTC(),
},
"OrgDeletionNoticeEmail": OrgDeletionNoticeEmail{
RecipientInfo: r,
OrgName: "Acme Corp",
DeletionDate: time.Now().UTC().AddDate(0, 0, 7), //nolint:mnd
},
"BrandedMessageRequest": BrandedMessageRequest{
RecipientInfo: r,
CampaignContext: CampaignContext{
CampaignName: "Test Campaign",
CampaignDescription: "A test campaign to verify branded message rendering",
},
Subject: "{{ .companyName }} - Test Branded Message",
Preheader: "Hi {{ .firstName }}, this is a test branded message",
Title: "Welcome to {{ .companyName }}",
Intros: []string{"Hi {{ .firstName }}, this branded message was sent via the email-test CLI.", "All templates are rendering correctly for {{ .companyName }}."},
ButtonText: "Visit Dashboard",
ButtonLink: "{{ .rootURL }}/campaigns",
Outros: []string{"If you received this, the branded message template is working as expected.", "Questions? Reach us at {{ .supportEmail }}."},
},
}
fixture, ok := fixtures[name]
if !ok {
return nil
}
data, err := json.Marshal(fixture)
if err != nil {
return nil
}
return data
}
const testPDFFontSize = 14
// testAttestedNDAPDF generates a valid two-page PDF simulating an NDA with attestation page
func testAttestedNDAPDF() []byte {
page1 := testMinimalPDF("Non-Disclosure Agreement — SecureCorp")
page2 := testMinimalPDF("Signature Certification — Test Fixture")
var buf bytes.Buffer
if err := api.MergeRaw([]io.ReadSeeker{
bytes.NewReader(page1),
bytes.NewReader(page2),
}, &buf, false, nil); err != nil {
return page1
}
return buf.Bytes()
}
// testMinimalPDF creates a valid single-page PDF with the given title text
func testMinimalPDF(title string) []byte {
page := map[string]any{
"paper": "A4P",
"origin": "UpperLeft",
"fonts": map[string]any{
"f": map[string]any{"name": "Helvetica-Bold", "size": testPDFFontSize},
},
"pages": map[string]any{
"1": map[string]any{
"content": map[string]any{
"text": []map[string]any{
{"value": title, "pos": [2]float64{20, 20}, "font": map[string]any{"name": "$f"}},
},
},
},
},
}
jsonData, _ := json.Marshal(page)
var buf bytes.Buffer
_ = api.Create(nil, bytes.NewReader(jsonData), &buf, nil)
return buf.Bytes()
}