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

Skip to content

Commit 3eb9c1e

Browse files
committed
chore(coderd/notifications): handle test assertions in the correct goroutine
1 parent 464b244 commit 3eb9c1e

File tree

1 file changed

+80
-42
lines changed

1 file changed

+80
-42
lines changed

coderd/notifications/notifications_test.go

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/coder/quartz"
3535

36+
"github.com/google/go-cmp/cmp"
3637
"github.com/google/uuid"
3738
smtpmock "github.com/mocktools/go-smtp-mock/v2"
3839
"github.com/stretchr/testify/assert"
@@ -985,27 +986,29 @@ func TestNotificationTemplates_Golden(t *testing.T) {
985986
t.Run(tc.name, func(t *testing.T) {
986987
t.Parallel()
987988

988-
// Spin up the DB
989-
db, logger, user := func() (*database.Store, *slog.Logger, *codersdk.User) {
990-
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
991-
db := api.Database
992-
firstUser := coderdtest.CreateFirstUser(t, adminClient)
993-
994-
_, user := coderdtest.CreateAnotherUserMutators(
995-
t,
996-
adminClient,
997-
firstUser.OrganizationID,
998-
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
999-
func(r *codersdk.CreateUserRequestWithOrgs) {
1000-
r.Username = tc.payload.UserUsername
1001-
r.Email = tc.payload.UserEmail
1002-
r.Name = tc.payload.UserName
1003-
},
1004-
)
1005-
return &db, &api.Logger, &user
1006-
}()
1007989
t.Run("smtp", func(t *testing.T) {
1008990
t.Parallel()
991+
992+
// Spin up the DB
993+
db, logger, user := func() (*database.Store, *slog.Logger, *codersdk.User) {
994+
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
995+
db := api.Database
996+
firstUser := coderdtest.CreateFirstUser(t, adminClient)
997+
998+
_, user := coderdtest.CreateAnotherUserMutators(
999+
t,
1000+
adminClient,
1001+
firstUser.OrganizationID,
1002+
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
1003+
func(r *codersdk.CreateUserRequestWithOrgs) {
1004+
r.Username = tc.payload.UserUsername
1005+
r.Email = tc.payload.UserEmail
1006+
r.Name = tc.payload.UserName
1007+
},
1008+
)
1009+
return &db, &api.Logger, &user
1010+
}()
1011+
10091012
// nolint:gocritic // Unit test.
10101013
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
10111014

@@ -1133,39 +1136,48 @@ func TestNotificationTemplates_Golden(t *testing.T) {
11331136

11341137
wantBody, err := os.ReadFile(goldenFile)
11351138
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1136-
require.Equal(t, string(wantBody), string(body), fmt.Sprintf("smtp notification does not match golden file. If this is expected, %s", hint))
1139+
require.Empty(
1140+
t,
1141+
cmp.Diff(wantBody, body),
1142+
fmt.Sprintf("golden file mismatch: %s. If this is expected, %s. (-want +got). ", goldenFile, hint),
1143+
)
11371144
})
11381145

11391146
t.Run("webhook", func(t *testing.T) {
11401147
t.Parallel()
1148+
1149+
// Spin up the DB
1150+
db, logger, user := func() (*database.Store, *slog.Logger, *codersdk.User) {
1151+
adminClient, _, api := coderdtest.NewWithAPI(t, nil)
1152+
db := api.Database
1153+
firstUser := coderdtest.CreateFirstUser(t, adminClient)
1154+
1155+
_, user := coderdtest.CreateAnotherUserMutators(
1156+
t,
1157+
adminClient,
1158+
firstUser.OrganizationID,
1159+
[]rbac.RoleIdentifier{rbac.RoleUserAdmin()},
1160+
func(r *codersdk.CreateUserRequestWithOrgs) {
1161+
r.Username = tc.payload.UserUsername
1162+
r.Email = tc.payload.UserEmail
1163+
r.Name = tc.payload.UserName
1164+
},
1165+
)
1166+
return &db, &api.Logger, &user
1167+
}()
1168+
11411169
// nolint:gocritic // Unit test.
11421170
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
11431171

11441172
// Spin up the mock webhook server
1173+
var body []byte
1174+
var readErr error
1175+
var webhookReceived bool
11451176
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11461177
w.WriteHeader(http.StatusOK)
11471178

1148-
body, err := io.ReadAll(r.Body)
1149-
require.NoError(t, err)
1150-
var prettyJSON bytes.Buffer
1151-
err = json.Indent(&prettyJSON, body, "", " ")
1152-
require.NoError(t, err)
1153-
1154-
content := normalizeGoldenWebhook(prettyJSON.Bytes())
1155-
1156-
partialName := strings.Split(t.Name(), "/")[1]
1157-
goldenFile := filepath.Join("testdata", "rendered-templates", "webhook", partialName+".json.golden")
1158-
if *updateGoldenFiles {
1159-
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
1160-
require.NoError(t, err, "want no error creating golden file directory")
1161-
err = os.WriteFile(goldenFile, content, 0o600)
1162-
require.NoError(t, err, "want no error writing body golden file")
1163-
return
1164-
}
1165-
1166-
wantBody, err := os.ReadFile(goldenFile)
1167-
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1168-
require.Equal(t, wantBody, content, fmt.Sprintf("smtp notification does not match golden file. If this is expected, %s", hint))
1179+
body, readErr = io.ReadAll(r.Body)
1180+
webhookReceived = true
11691181
}))
11701182
t.Cleanup(server.Close)
11711183

@@ -1209,8 +1221,34 @@ func TestNotificationTemplates_Golden(t *testing.T) {
12091221
)
12101222
require.NoError(t, err)
12111223

1212-
err = webhookManager.Stop(ctx)
1224+
require.Eventually(t, func() bool {
1225+
return webhookReceived
1226+
}, testutil.WaitShort, testutil.IntervalFast)
1227+
12131228
require.NoError(t, err)
1229+
1230+
// Handle the body that was read in the http server here.
1231+
// We need to do it here because we can't call require.* in a separate goroutine, such as the http server handler
1232+
require.NoError(t, readErr)
1233+
var prettyJSON bytes.Buffer
1234+
err = json.Indent(&prettyJSON, body, "", " ")
1235+
require.NoError(t, err)
1236+
1237+
content := normalizeGoldenWebhook(prettyJSON.Bytes())
1238+
1239+
partialName := strings.Split(t.Name(), "/")[1]
1240+
goldenFile := filepath.Join("testdata", "rendered-templates", "webhook", partialName+".json.golden")
1241+
if *updateGoldenFiles {
1242+
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
1243+
require.NoError(t, err, "want no error creating golden file directory")
1244+
err = os.WriteFile(goldenFile, content, 0o600)
1245+
require.NoError(t, err, "want no error writing body golden file")
1246+
return
1247+
}
1248+
1249+
wantBody, err := os.ReadFile(goldenFile)
1250+
require.NoError(t, err, fmt.Sprintf("missing golden notification body file. %s", hint))
1251+
require.Equal(t, wantBody, content, fmt.Sprintf("smtp notification does not match golden file. If this is expected, %s", hint))
12141252
})
12151253
})
12161254
}

0 commit comments

Comments
 (0)