@@ -33,6 +33,7 @@ import (
33
33
34
34
"github.com/coder/quartz"
35
35
36
+ "github.com/google/go-cmp/cmp"
36
37
"github.com/google/uuid"
37
38
smtpmock "github.com/mocktools/go-smtp-mock/v2"
38
39
"github.com/stretchr/testify/assert"
@@ -985,27 +986,29 @@ func TestNotificationTemplates_Golden(t *testing.T) {
985
986
t .Run (tc .name , func (t * testing.T ) {
986
987
t .Parallel ()
987
988
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
- }()
1007
989
t .Run ("smtp" , func (t * testing.T ) {
1008
990
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
+
1009
1012
// nolint:gocritic // Unit test.
1010
1013
ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitSuperLong ))
1011
1014
@@ -1133,39 +1136,48 @@ func TestNotificationTemplates_Golden(t *testing.T) {
1133
1136
1134
1137
wantBody , err := os .ReadFile (goldenFile )
1135
1138
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
+ )
1137
1144
})
1138
1145
1139
1146
t .Run ("webhook" , func (t * testing.T ) {
1140
1147
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
+
1141
1169
// nolint:gocritic // Unit test.
1142
1170
ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitSuperLong ))
1143
1171
1144
1172
// Spin up the mock webhook server
1173
+ var body []byte
1174
+ var readErr error
1175
+ var webhookReceived bool
1145
1176
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1146
1177
w .WriteHeader (http .StatusOK )
1147
1178
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
1169
1181
}))
1170
1182
t .Cleanup (server .Close )
1171
1183
@@ -1209,8 +1221,34 @@ func TestNotificationTemplates_Golden(t *testing.T) {
1209
1221
)
1210
1222
require .NoError (t , err )
1211
1223
1212
- err = webhookManager .Stop (ctx )
1224
+ require .Eventually (t , func () bool {
1225
+ return webhookReceived
1226
+ }, testutil .WaitShort , testutil .IntervalFast )
1227
+
1213
1228
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 ))
1214
1252
})
1215
1253
})
1216
1254
}
0 commit comments