Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a9a66a

Browse files
author
defelmnq
committed
feat(notifications): add golden file for custom appearance
1 parent 34d6611 commit 0a9a66a

File tree

2 files changed

+225
-173
lines changed

2 files changed

+225
-173
lines changed

coderd/notifications/notifications_test.go

Lines changed: 135 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ func (n *acquireSignalingInterceptor) AcquireNotificationMessages(ctx context.Co
16121612
return messages, err
16131613
}
16141614

1615-
func TestNotificationTemplates_GoldenWithCustomLogoURL(t *testing.T) {
1615+
func TestNotificationTemplates_GoldenWithCustomAppearance(t *testing.T) {
16161616
t.Parallel()
16171617

16181618
if !dbtestutil.WillUsePostgres() {
@@ -1629,205 +1629,167 @@ func TestNotificationTemplates_GoldenWithCustomLogoURL(t *testing.T) {
16291629
hint = "run \"DB=ci make update-golden-files\" and commit the changes"
16301630
)
16311631

1632-
tests := []struct {
1633-
name string
1634-
id uuid.UUID
1635-
payload types.MessagePayload
1636-
}{
1637-
{
1638-
name: "TemplateWorkspaceDeleted",
1639-
id: notifications.TemplateWorkspaceDeleted,
1640-
payload: types.MessagePayload{
1641-
UserName: "Bobby",
1642-
UserEmail: "[email protected]",
1643-
UserUsername: "bobby",
1644-
Labels: map[string]string{
1645-
"name": "bobby-workspace",
1646-
"reason": "autodeleted due to dormancy",
1647-
"initiator": "autobuild",
1648-
},
1632+
var (
1633+
payload = types.MessagePayload{
1634+
Labels: map[string]string{
1635+
"name": "bobby-workspace",
1636+
"reason": "autodeleted due to dormancy",
1637+
"initiator": "autobuild",
16491638
},
1650-
},
1651-
}
1652-
1653-
// We must have a test case for every notification_template. This is enforced below:
1654-
allTemplates, err := enumerateAllTemplates(t)
1655-
require.NoError(t, err)
1656-
for _, name := range allTemplates {
1657-
var found bool
1658-
for _, tc := range tests {
1659-
if tc.name == name {
1660-
found = true
1661-
}
16621639
}
1640+
)
16631641

1664-
require.Truef(t, found, "could not find test case for %q", name)
1665-
}
1666-
1667-
for _, tc := range tests {
1668-
tc := tc
1669-
1670-
t.Run(tc.name, func(t *testing.T) {
1671-
t.Parallel()
1672-
1673-
t.Run("smtp", func(t *testing.T) {
1674-
t.Parallel()
1675-
1676-
// Spin up the DB
1677-
db, logger, user := func() (*database.Store, *slog.Logger, *codersdk.User) {
1678-
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
1679-
db := api.Database
1680-
db.UpsertApplicationName(context.Background(), "myNewValue")
1681-
firstUser := coderdtest.CreateFirstUser(t, adminClient)
1642+
// Spin up the DB
1643+
db, logger, user := func() (database.Store, *slog.Logger, *codersdk.User) {
1644+
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
1645+
firstUser := coderdtest.CreateFirstUser(t, adminClient)
1646+
1647+
_, user := coderdtest.CreateAnotherUserMutators(
1648+
t,
1649+
adminClient,
1650+
firstUser.OrganizationID,
1651+
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
1652+
func(r *codersdk.CreateUserRequestWithOrgs) {
1653+
r.Username = "bobby"
1654+
r.Email = "[email protected]"
1655+
r.Name = "Bobby"
1656+
},
1657+
)
1658+
return api.Database, &api.Logger, &user
1659+
}()
16821660

1683-
_, user := coderdtest.CreateAnotherUserMutators(
1684-
t,
1685-
adminClient,
1686-
firstUser.OrganizationID,
1687-
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
1688-
func(r *codersdk.CreateUserRequestWithOrgs) {
1689-
r.Username = tc.payload.UserUsername
1690-
r.Email = tc.payload.UserEmail
1691-
r.Name = tc.payload.UserName
1692-
},
1693-
)
1694-
return &db, &api.Logger, &user
1695-
}()
1661+
// nolint:gocritic // Unit test.
1662+
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
16961663

1697-
// nolint:gocritic // Unit test.
1698-
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
1664+
err := db.UpsertApplicationName(ctx, "CustomApplication")
1665+
require.NoError(t, err)
16991666

1700-
ddb := *db
1701-
err := ddb.UpsertLogoURL(ctx, "newURL")
1702-
require.NoError(t, err)
1667+
err = db.UpsertLogoURL(ctx, "https://custom.application")
1668+
require.NoError(t, err)
17031669

1704-
// smtp config shared between client and server
1705-
smtpConfig := codersdk.NotificationsEmailConfig{
1706-
Hello: hello,
1707-
From: from,
1670+
// smtp config shared between client and server
1671+
smtpConfig := codersdk.NotificationsEmailConfig{
1672+
Hello: hello,
1673+
From: from,
17081674

1709-
Auth: codersdk.NotificationsEmailAuthConfig{
1710-
Username: username,
1711-
Password: password,
1712-
},
1713-
}
1675+
Auth: codersdk.NotificationsEmailAuthConfig{
1676+
Username: username,
1677+
Password: password,
1678+
},
1679+
}
17141680

1715-
// Spin up the mock SMTP server
1716-
backend := smtptest.NewBackend(smtptest.Config{
1717-
AuthMechanisms: []string{sasl.Login},
1681+
// Spin up the mock SMTP server
1682+
backend := smtptest.NewBackend(smtptest.Config{
1683+
AuthMechanisms: []string{sasl.Login},
17181684

1719-
AcceptedIdentity: smtpConfig.Auth.Identity.String(),
1720-
AcceptedUsername: username,
1721-
AcceptedPassword: password,
1722-
})
1685+
AcceptedIdentity: smtpConfig.Auth.Identity.String(),
1686+
AcceptedUsername: username,
1687+
AcceptedPassword: password,
1688+
})
17231689

1724-
// Create a mock SMTP server which conditionally listens for plain or TLS connections.
1725-
srv, listen, err := smtptest.CreateMockSMTPServer(backend, false)
1726-
require.NoError(t, err)
1727-
t.Cleanup(func() {
1728-
err := srv.Shutdown(ctx)
1729-
require.NoError(t, err)
1730-
})
1690+
// Create a mock SMTP server which conditionally listens for plain or TLS connections.
1691+
srv, listen, err := smtptest.CreateMockSMTPServer(backend, false)
1692+
require.NoError(t, err)
1693+
t.Cleanup(func() {
1694+
err := srv.Shutdown(ctx)
1695+
require.NoError(t, err)
1696+
})
17311697

1732-
var hp serpent.HostPort
1733-
require.NoError(t, hp.Set(listen.Addr().String()))
1734-
smtpConfig.Smarthost = hp
1698+
var hp serpent.HostPort
1699+
require.NoError(t, hp.Set(listen.Addr().String()))
1700+
smtpConfig.Smarthost = hp
17351701

1736-
// Start mock SMTP server in the background.
1737-
var wg sync.WaitGroup
1738-
wg.Add(1)
1739-
go func() {
1740-
defer wg.Done()
1741-
assert.NoError(t, srv.Serve(listen))
1742-
}()
1702+
// Start mock SMTP server in the background.
1703+
var wg sync.WaitGroup
1704+
wg.Add(1)
1705+
go func() {
1706+
defer wg.Done()
1707+
assert.NoError(t, srv.Serve(listen))
1708+
}()
17431709

1744-
// Wait for the server to become pingable.
1745-
require.Eventually(t, func() bool {
1746-
cl, err := smtptest.PingClient(listen, false, smtpConfig.TLS.StartTLS.Value())
1747-
if err != nil {
1748-
t.Logf("smtp not yet dialable: %s", err)
1749-
return false
1750-
}
1710+
// Wait for the server to become pingable.
1711+
require.Eventually(t, func() bool {
1712+
cl, err := smtptest.PingClient(listen, false, smtpConfig.TLS.StartTLS.Value())
1713+
if err != nil {
1714+
t.Logf("smtp not yet dialable: %s", err)
1715+
return false
1716+
}
17511717

1752-
if err = cl.Noop(); err != nil {
1753-
t.Logf("smtp not yet noopable: %s", err)
1754-
return false
1755-
}
1718+
if err = cl.Noop(); err != nil {
1719+
t.Logf("smtp not yet noopable: %s", err)
1720+
return false
1721+
}
17561722

1757-
if err = cl.Close(); err != nil {
1758-
t.Logf("smtp didn't close properly: %s", err)
1759-
return false
1760-
}
1723+
if err = cl.Close(); err != nil {
1724+
t.Logf("smtp didn't close properly: %s", err)
1725+
return false
1726+
}
17611727

1762-
return true
1763-
}, testutil.WaitShort, testutil.IntervalFast)
1728+
return true
1729+
}, testutil.WaitShort, testutil.IntervalFast)
17641730

1765-
smtpCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1766-
smtpCfg.SMTP = smtpConfig
1731+
smtpCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1732+
smtpCfg.SMTP = smtpConfig
17671733

1768-
smtpManager, err := notifications.NewManager(
1769-
smtpCfg,
1770-
*db,
1771-
defaultHelpers(),
1772-
createMetrics(),
1773-
logger.Named("manager"),
1774-
)
1775-
require.NoError(t, err)
1734+
smtpManager, err := notifications.NewManager(
1735+
smtpCfg,
1736+
db,
1737+
defaultHelpers(),
1738+
createMetrics(),
1739+
logger.Named("manager"),
1740+
)
1741+
require.NoError(t, err)
17761742

1777-
smtpManager.Run(ctx)
1743+
smtpManager.Run(ctx)
17781744

1779-
notificationCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1745+
notificationCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
17801746

1781-
smtpEnqueuer, err := notifications.NewStoreEnqueuer(
1782-
notificationCfg,
1783-
*db,
1784-
defaultHelpers(),
1785-
logger.Named("enqueuer"),
1786-
quartz.NewReal(),
1787-
)
1788-
require.NoError(t, err)
1747+
smtpEnqueuer, err := notifications.NewStoreEnqueuer(
1748+
notificationCfg,
1749+
db,
1750+
defaultHelpers(),
1751+
logger.Named("enqueuer"),
1752+
quartz.NewReal(),
1753+
)
1754+
require.NoError(t, err)
17891755

1790-
_, err = smtpEnqueuer.EnqueueWithData(
1791-
ctx,
1792-
user.ID,
1793-
tc.id,
1794-
tc.payload.Labels,
1795-
tc.payload.Data,
1796-
user.Username,
1797-
user.ID,
1798-
)
1799-
require.NoError(t, err)
1756+
_, err = smtpEnqueuer.EnqueueWithData(
1757+
ctx,
1758+
user.ID,
1759+
notifications.TemplateWorkspaceDeleted,
1760+
payload.Labels,
1761+
payload.Data,
1762+
user.Username,
1763+
user.ID,
1764+
)
1765+
require.NoError(t, err)
18001766

1801-
// Wait for the message to be fetched
1802-
var msg *smtptest.Message
1803-
require.Eventually(t, func() bool {
1804-
msg = backend.LastMessage()
1805-
return msg != nil && len(msg.Contents) > 0
1806-
}, testutil.WaitShort, testutil.IntervalFast)
1767+
// Wait for the message to be fetched
1768+
var msg *smtptest.Message
1769+
require.Eventually(t, func() bool {
1770+
msg = backend.LastMessage()
1771+
return msg != nil && len(msg.Contents) > 0
1772+
}, testutil.WaitShort, testutil.IntervalFast)
18071773

1808-
body := normalizeGoldenEmail([]byte(msg.Contents))
1774+
body := normalizeGoldenEmail([]byte(msg.Contents))
18091775

1810-
err = smtpManager.Stop(ctx)
1811-
require.NoError(t, err)
1776+
err = smtpManager.Stop(ctx)
1777+
require.NoError(t, err)
18121778

1813-
partialName := strings.Split(t.Name(), "/")[1]
1814-
goldenFile := filepath.Join("testdata", "rendered-templates", "smtp", partialName+".html.golden")
1815-
if *updateGoldenFiles {
1816-
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
1817-
require.NoError(t, err, "want no error creating golden file directory")
1818-
err = os.WriteFile(goldenFile, body, 0o600)
1819-
require.NoError(t, err, "want no error writing body golden file")
1820-
return
1821-
}
1779+
goldenFile := filepath.Join("testdata", "rendered-templates", "smtp", "TemplateWorkspaceDeleted_WithCustomAppearance.html.golden")
18221780

1823-
wantBody, err := os.ReadFile(goldenFile)
1824-
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1825-
require.Empty(
1826-
t,
1827-
cmp.Diff(wantBody, body),
1828-
fmt.Sprintf("golden file mismatch: %s. If this is expected, %s. (-want +got). ", goldenFile, hint),
1829-
)
1830-
})
1831-
})
1781+
if *updateGoldenFiles {
1782+
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
1783+
require.NoError(t, err, "want no error creating golden file directory")
1784+
err = os.WriteFile(goldenFile, body, 0o600)
1785+
require.NoError(t, err, "want no error writing body golden file")
18321786
}
1787+
1788+
wantBody, err := os.ReadFile(goldenFile)
1789+
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1790+
require.Empty(
1791+
t,
1792+
cmp.Diff(wantBody, body),
1793+
fmt.Sprintf("golden file mismatch: %s. If this is expected, %s. (-want +got). ", goldenFile, hint),
1794+
)
18331795
}

0 commit comments

Comments
 (0)