Skip to content

Commit d492d09

Browse files
committed
feat(notifications): add golden file for custom appearance
1 parent 51d8d33 commit d492d09

File tree

1 file changed

+0
-180
lines changed

1 file changed

+0
-180
lines changed

coderd/notifications/notifications_test.go

-180
Original file line numberDiff line numberDiff line change
@@ -1640,183 +1640,3 @@ func (n *acquireSignalingInterceptor) AcquireNotificationMessages(ctx context.Co
16401640
n.acquiredChan <- struct{}{}
16411641
return messages, err
16421642
}
1643-
1644-
func TestNotificationTemplates_GoldenWithCustomAppearance(t *testing.T) {
1645-
t.Parallel()
1646-
1647-
if !dbtestutil.WillUsePostgres() {
1648-
t.Skip("This test requires postgres; it relies on the notification templates added by migrations in the database")
1649-
}
1650-
1651-
const (
1652-
username = "bob"
1653-
password = "🤫"
1654-
1655-
hello = "localhost"
1656-
1657-
from = "system@coder.com"
1658-
hint = "run \"DB=ci make update-golden-files\" and commit the changes"
1659-
)
1660-
1661-
payload := types.MessagePayload{
1662-
Labels: map[string]string{
1663-
"name": "bobby-workspace",
1664-
"reason": "autodeleted due to dormancy",
1665-
"initiator": "autobuild",
1666-
},
1667-
}
1668-
1669-
// Spin up the DB
1670-
db, logger, user := func() (database.Store, *slog.Logger, *codersdk.User) {
1671-
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
1672-
firstUser := coderdtest.CreateFirstUser(t, adminClient)
1673-
1674-
_, user := coderdtest.CreateAnotherUserMutators(
1675-
t,
1676-
adminClient,
1677-
firstUser.OrganizationID,
1678-
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
1679-
func(r *codersdk.CreateUserRequestWithOrgs) {
1680-
r.Username = "bobby"
1681-
r.Email = "bobby@coder.com"
1682-
r.Name = "Bobby"
1683-
},
1684-
)
1685-
return api.Database, &api.Logger, &user
1686-
}()
1687-
1688-
// nolint:gocritic // Unit test.
1689-
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
1690-
1691-
err := db.UpsertApplicationName(ctx, "CustomApplication")
1692-
require.NoError(t, err)
1693-
1694-
err = db.UpsertLogoURL(ctx, "https://custom.application")
1695-
require.NoError(t, err)
1696-
1697-
// smtp config shared between client and server
1698-
smtpConfig := codersdk.NotificationsEmailConfig{
1699-
Hello: hello,
1700-
From: from,
1701-
1702-
Auth: codersdk.NotificationsEmailAuthConfig{
1703-
Username: username,
1704-
Password: password,
1705-
},
1706-
}
1707-
1708-
// Spin up the mock SMTP server
1709-
backend := smtptest.NewBackend(smtptest.Config{
1710-
AuthMechanisms: []string{sasl.Login},
1711-
1712-
AcceptedIdentity: smtpConfig.Auth.Identity.String(),
1713-
AcceptedUsername: username,
1714-
AcceptedPassword: password,
1715-
})
1716-
1717-
// Create a mock SMTP server which conditionally listens for plain or TLS connections.
1718-
srv, listen, err := smtptest.CreateMockSMTPServer(backend, false)
1719-
require.NoError(t, err)
1720-
t.Cleanup(func() {
1721-
err := srv.Shutdown(ctx)
1722-
require.NoError(t, err)
1723-
})
1724-
1725-
var hp serpent.HostPort
1726-
require.NoError(t, hp.Set(listen.Addr().String()))
1727-
smtpConfig.Smarthost = hp
1728-
1729-
// Start mock SMTP server in the background.
1730-
var wg sync.WaitGroup
1731-
wg.Add(1)
1732-
go func() {
1733-
defer wg.Done()
1734-
assert.NoError(t, srv.Serve(listen))
1735-
}()
1736-
1737-
// Wait for the server to become pingable.
1738-
require.Eventually(t, func() bool {
1739-
cl, err := smtptest.PingClient(listen, false, smtpConfig.TLS.StartTLS.Value())
1740-
if err != nil {
1741-
t.Logf("smtp not yet dialable: %s", err)
1742-
return false
1743-
}
1744-
1745-
if err = cl.Noop(); err != nil {
1746-
t.Logf("smtp not yet noopable: %s", err)
1747-
return false
1748-
}
1749-
1750-
if err = cl.Close(); err != nil {
1751-
t.Logf("smtp didn't close properly: %s", err)
1752-
return false
1753-
}
1754-
1755-
return true
1756-
}, testutil.WaitShort, testutil.IntervalFast)
1757-
1758-
smtpCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1759-
smtpCfg.SMTP = smtpConfig
1760-
1761-
smtpManager, err := notifications.NewManager(
1762-
smtpCfg,
1763-
db,
1764-
defaultHelpers(),
1765-
createMetrics(),
1766-
logger.Named("manager"),
1767-
)
1768-
require.NoError(t, err)
1769-
1770-
smtpManager.Run(ctx)
1771-
1772-
notificationCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1773-
1774-
smtpEnqueuer, err := notifications.NewStoreEnqueuer(
1775-
notificationCfg,
1776-
db,
1777-
defaultHelpers(),
1778-
logger.Named("enqueuer"),
1779-
quartz.NewReal(),
1780-
)
1781-
require.NoError(t, err)
1782-
1783-
_, err = smtpEnqueuer.EnqueueWithData(
1784-
ctx,
1785-
user.ID,
1786-
notifications.TemplateWorkspaceDeleted,
1787-
payload.Labels,
1788-
payload.Data,
1789-
user.Username,
1790-
user.ID,
1791-
)
1792-
require.NoError(t, err)
1793-
1794-
// Wait for the message to be fetched
1795-
var msg *smtptest.Message
1796-
require.Eventually(t, func() bool {
1797-
msg = backend.LastMessage()
1798-
return msg != nil && len(msg.Contents) > 0
1799-
}, testutil.WaitShort, testutil.IntervalFast)
1800-
1801-
body := normalizeGoldenEmail([]byte(msg.Contents))
1802-
1803-
err = smtpManager.Stop(ctx)
1804-
require.NoError(t, err)
1805-
1806-
goldenFile := filepath.Join("testdata", "rendered-templates", "smtp", "TemplateWorkspaceDeleted_WithCustomAppearance.html.golden")
1807-
1808-
if *updateGoldenFiles {
1809-
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
1810-
require.NoError(t, err, "want no error creating golden file directory")
1811-
err = os.WriteFile(goldenFile, body, 0o600)
1812-
require.NoError(t, err, "want no error writing body golden file")
1813-
}
1814-
1815-
wantBody, err := os.ReadFile(goldenFile)
1816-
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1817-
require.Empty(
1818-
t,
1819-
cmp.Diff(wantBody, body),
1820-
fmt.Sprintf("golden file mismatch: %s. If this is expected, %s. (-want +got). ", goldenFile, hint),
1821-
)
1822-
}

0 commit comments

Comments
 (0)