Skip to content

Commit 918df77

Browse files
authored
CBG-4945: Fix default sync function dry run (#7937)
1 parent 90198ba commit 918df77

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

db/crud.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,12 +1711,16 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, newDoc,
17111711
}
17121712
var output *channels.ChannelMapperOutput
17131713
var syncErr error
1714-
if syncFn == "" {
1714+
if syncFn == "" && db.ChannelMapper != nil {
17151715
output, err = db.ChannelMapper.MapToChannelsAndAccess(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
17161716
if err != nil {
17171717
return nil, &base.SyncFnDryRunError{Err: err}
17181718
}
17191719
} else {
1720+
if syncFn == "" {
1721+
scopeAndCollectionName := db.ScopeAndCollectionName()
1722+
syncFn = channels.GetDefaultSyncFunction(scopeAndCollectionName.Scope, scopeAndCollectionName.Collection)
1723+
}
17201724
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
17211725
syncRunner, err := channels.NewSyncRunner(ctx, syncFn, jsTimeout)
17221726
if err != nil {

rest/diagnostic_doc_api_test.go

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,28 @@ func TestSyncFuncDryRun(t *testing.T) {
10041004
base.SkipImportTestsIfNotEnabled(t)
10051005
base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
10061006

1007+
rt := NewRestTester(t, &RestTesterConfig{
1008+
PersistentConfig: true,
1009+
})
1010+
defer rt.Close()
1011+
1012+
dbConfig := rt.NewDbConfig()
1013+
rt.CreateDatabase("db", dbConfig)
1014+
1015+
// When the tests run with named scopes and collections, then the default
1016+
// sync function is:
1017+
// function(doc){channel("<collection_name>");}
1018+
// when the tests run in default scope and collection then the default
1019+
// sync function is:
1020+
// function(doc){channel(doc.channels);}
1021+
var defaultChannelName string
1022+
dbc, _ := rt.GetSingleTestDatabaseCollection()
1023+
if dbc.IsDefaultCollection() {
1024+
defaultChannelName = "chanNew"
1025+
} else {
1026+
defaultChannelName = dbc.Name
1027+
}
1028+
10071029
tests := []struct {
10081030
name string
10091031
dbSyncFunction string
@@ -1077,7 +1099,7 @@ func TestSyncFuncDryRun(t *testing.T) {
10771099
syncFunction: "function(doc) {channel(doc.channel); access(doc.accessUser, doc.accessChannel); role(doc.accessUser, doc.role); expiry(doc.expiry);}",
10781100
document: map[string]any{"accessChannel": []string{"dynamicChan5412"}, "accessUser": "user", "channel": []string{"dynamicChan222"}, "expiry": 10},
10791101
existingDoc: true,
1080-
existingDocID: "doc1",
1102+
existingDocID: "custom_sync_func-db_sync_func-doc_body-existing_doc-no_doc_id",
10811103
existingDocBody: `{"accessChannel": ["dynamicChan5412"],"accessUser": "user","channel": ["dynamicChan222"],"expiry":10}`,
10821104
expectedOutput: SyncFnDryRun{
10831105
Channels: base.SetFromArray([]string{"dynamicChan222"}),
@@ -1106,9 +1128,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11061128
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11071129
syncFunction: "",
11081130
document: map[string]any{"user": map[string]any{"num": 150}, "channel": "abc"},
1109-
docID: "doc",
1131+
docID: "no_custom_sync_func-db_sync_func-doc_body-no_existing_doc-doc_id-sync_func_exception_typeError",
11101132
existingDoc: true,
1111-
existingDocID: "doc",
1133+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-no_existing_doc-doc_id-sync_func_exception_typeError",
11121134
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11131135
expectedOutput: SyncFnDryRun{
11141136
Exception: "Error returned from Sync Function: TypeError: Cannot access member '0' of undefined",
@@ -1120,9 +1142,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11201142
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11211143
syncFunction: "",
11221144
document: map[string]any{"user": map[string]any{"num": 120, "name": []string{"user2"}}, "channel": "channel2"},
1123-
docID: "doc",
1145+
docID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id",
11241146
existingDoc: true,
1125-
existingDocID: "doc",
1147+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id",
11261148
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11271149
expectedOutput: SyncFnDryRun{
11281150
Channels: base.SetFromArray([]string{"channel2"}),
@@ -1136,9 +1158,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11361158
name: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11371159
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11381160
syncFunction: "",
1139-
docID: "doc",
1161+
docID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11401162
existingDoc: true,
1141-
existingDocID: "doc",
1163+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11421164
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11431165
expectedOutput: SyncFnDryRun{
11441166
Channels: base.SetFromArray([]string{"channel1"}),
@@ -1154,7 +1176,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11541176
syncFunction: "",
11551177
docID: "doc404",
11561178
existingDoc: true,
1157-
existingDocID: "doc",
1179+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-invalid_doc_id",
11581180
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11591181
expectedOutput: SyncFnDryRun{},
11601182
expectedStatus: http.StatusNotFound,
@@ -1165,7 +1187,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11651187
syncFunction: "",
11661188
docID: "",
11671189
existingDoc: true,
1168-
existingDocID: "doc",
1190+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-no_doc_id",
11691191
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11701192
expectedOutput: SyncFnDryRun{},
11711193
expectedStatus: http.StatusBadRequest,
@@ -1177,7 +1199,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11771199
document: map[string]any{"channel": "channel2"},
11781200
docID: "",
11791201
existingDoc: true,
1180-
existingDocID: "doc22",
1202+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-no_doc_id-new_doc_channel",
11811203
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
11821204
expectedOutput: SyncFnDryRun{
11831205
Channels: base.SetFromArray([]string{"channel2"}),
@@ -1191,9 +1213,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11911213
dbSyncFunction: "function(doc,oldDoc){if(oldDoc){ channel(oldDoc.channel)} else {channel(doc.channel)} }",
11921214
syncFunction: "",
11931215
document: map[string]any{"channel": "chanNew"},
1194-
docID: "doc22",
1216+
docID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id-old_doc_channel",
11951217
existingDoc: true,
1196-
existingDocID: "doc22",
1218+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id-old_doc_channel",
11971219
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
11981220
expectedOutput: SyncFnDryRun{
11991221
Channels: base.SetFromArray([]string{"chanOld"}),
@@ -1207,24 +1229,44 @@ func TestSyncFuncDryRun(t *testing.T) {
12071229
dbSyncFunction: "function(doc,oldDoc){if(oldDoc){ channel(oldDoc.channel)} else {channel(doc.channel)} }",
12081230
syncFunction: "",
12091231
document: `{"channel": "chanNew", "oldchannel":}`,
1210-
docID: "doc22",
1232+
docID: "no_custom_sync_func-db_sync_func-invalid_doc_body-existing_doc-doc_id",
12111233
existingDoc: true,
1212-
existingDocID: "doc22",
1234+
existingDocID: "no_custom_sync_func-db_sync_func-invalid_doc_body-existing_doc-doc_id",
12131235
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
12141236
expectedOutput: SyncFnDryRun{},
12151237
expectedStatus: http.StatusBadRequest,
12161238
},
1239+
{
1240+
name: "no_custom_sync_func-default_db_sync_func-doc_body-no_existing_doc-no_doc_id",
1241+
document: map[string]any{"channels": "chanNew"},
1242+
existingDoc: false,
1243+
expectedOutput: SyncFnDryRun{
1244+
Channels: base.SetFromArray([]string{defaultChannelName}),
1245+
Access: channels.AccessMap{},
1246+
Roles: channels.AccessMap{},
1247+
},
1248+
expectedStatus: http.StatusOK,
1249+
},
1250+
{
1251+
name: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
1252+
docID: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
1253+
existingDoc: true,
1254+
existingDocID: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
1255+
existingDocBody: `{"channels": "chanNew"}`,
1256+
expectedOutput: SyncFnDryRun{
1257+
Channels: base.SetFromArray([]string{defaultChannelName}),
1258+
Access: channels.AccessMap{},
1259+
Roles: channels.AccessMap{},
1260+
},
1261+
expectedStatus: http.StatusOK,
1262+
},
12171263
}
12181264

12191265
for _, test := range tests {
12201266
t.Run(test.name, func(t *testing.T) {
1221-
rt := NewRestTester(t, &RestTesterConfig{
1222-
PersistentConfig: true,
1223-
SyncFn: test.dbSyncFunction,
1224-
})
1225-
defer rt.Close()
1226-
1227-
RequireStatus(t, rt.CreateDatabase("db", rt.NewDbConfig()), http.StatusCreated)
1267+
dbConfig := rt.NewDbConfig()
1268+
dbConfig.Sync = &test.dbSyncFunction
1269+
RequireStatus(t, rt.SendAdminRequest("PUT", "/{{.keyspace}}/_config/sync", test.dbSyncFunction), http.StatusOK)
12281270

12291271
url := "/{{.keyspace}}/_sync"
12301272
if test.existingDoc {

0 commit comments

Comments
 (0)