@@ -186,7 +186,7 @@ describe('DefinedSchemas', () => {
186186 let schema = await new Parse . Schema ( 'Test' ) . get ( ) ;
187187 expect ( schema . fields ) . toEqual ( fields ) ;
188188
189- await server . config . databaseController . schemaCache . clear ( ) ;
189+ fields . anotherObject = { type : 'Object' } ;
190190 // Update
191191 await new DefinedSchemas ( schemas , server . config ) . execute ( ) ;
192192 schema = await new Parse . Schema ( 'Test' ) . get ( ) ;
@@ -278,6 +278,7 @@ describe('DefinedSchemas', () => {
278278 cleanUpIndexes ( schema ) ;
279279 expect ( schema . indexes ) . toEqual ( indexes ) ;
280280
281+ indexes . complex2 = { createdAt : 1 , ACL : 1 } ;
281282 await new DefinedSchemas ( schemas , server . config ) . execute ( ) ;
282283 schema = await new Parse . Schema ( 'Test' ) . get ( ) ;
283284 cleanUpIndexes ( schema ) ;
@@ -388,6 +389,38 @@ describe('DefinedSchemas', () => {
388389 testSchema = await new Parse . Schema ( 'Test' ) . get ( ) ;
389390 expect ( testSchema . classLevelPermissions ) . toEqual ( expectedTestCLP ) ;
390391 } ) ;
392+ it ( 'should save CLP' , async ( ) => {
393+ const server = await reconfigureServer ( ) ;
394+
395+ const expectedTestCLP = {
396+ find : { } ,
397+ count : { requiresAuthentication : true } ,
398+ get : { 'role:Admin' : true } ,
399+ create : { 'role:ARole' : true , requiresAuthentication : true } ,
400+ update : { requiresAuthentication : true } ,
401+ delete : { requiresAuthentication : true } ,
402+ addField : { } ,
403+ protectedFields : { '*' : [ 'aField' ] , 'role:Admin' : [ 'anotherField' ] } ,
404+ } ;
405+ const schemas = [
406+ {
407+ className : 'Test' ,
408+ fields : { aField : { type : 'String' } , anotherField : { type : 'Object' } } ,
409+ classLevelPermissions : expectedTestCLP ,
410+ } ,
411+ ] ;
412+ await new DefinedSchemas ( schemas , server . config ) . execute ( ) ;
413+
414+ let testSchema = await new Parse . Schema ( 'Test' ) . get ( ) ;
415+ expect ( testSchema . classLevelPermissions ) . toEqual ( expectedTestCLP ) ;
416+
417+ expectedTestCLP . update = { } ;
418+ expectedTestCLP . create = { requiresAuthentication : true } ;
419+
420+ await new DefinedSchemas ( schemas , server . config ) . execute ( ) ;
421+ testSchema = await new Parse . Schema ( 'Test' ) . get ( ) ;
422+ expect ( testSchema . classLevelPermissions ) . toEqual ( expectedTestCLP ) ;
423+ } ) ;
391424 it ( 'should force addField to empty' , async ( ) => {
392425 const server = await reconfigureServer ( ) ;
393426 const schemas = [ { className : 'Test' , classLevelPermissions : { addField : { '*' : true } } } ] ;
@@ -413,7 +446,76 @@ describe('DefinedSchemas', () => {
413446 } ) ;
414447 } ) ;
415448
416- xit ( 'should disable class endpoint when schemas provided to avoid dual source of truth' ) ;
417- xit ( 'should only enable delete class endpoint since' ) ;
418- xit ( 'should run beforeSchemasMigration before execution of DefinedSchemas' ) ;
449+ it ( 'should not delete automatically classes' , async ( ) => {
450+ await reconfigureServer ( { schemas : [ { className : '_User' } , { className : 'Test' } ] } ) ;
451+
452+ await reconfigureServer ( { schemas : [ { className : '_User' } ] } ) ;
453+
454+ const schema = await new Parse . Schema ( 'Test' ) . get ( ) ;
455+ expect ( schema . className ) . toEqual ( 'Test' ) ;
456+ } ) ;
457+
458+ it ( 'should disable class PUT/POST endpoint when schemas provided to avoid dual source of truth' , async ( ) => {
459+ await reconfigureServer ( { schemas : [ { className : '_User' } , { className : 'Test' } ] } ) ;
460+ await reconfigureServer ( { schemas : [ { className : '_User' } ] } ) ;
461+
462+ const schema = await new Parse . Schema ( 'Test' ) . get ( ) ;
463+ expect ( schema . className ) . toEqual ( 'Test' ) ;
464+
465+ const schemas = await Parse . Schema . all ( ) ;
466+ expect ( schemas . length ) . toEqual ( 4 ) ;
467+
468+ try {
469+ await new Parse . Schema ( 'Test' ) . save ( ) ;
470+ } catch ( e ) {
471+ expect ( e . message ) . toContain ( 'cannot perform this operation when schemas options is used.' ) ;
472+ }
473+
474+ try {
475+ await new Parse . Schema ( '_User' ) . update ( ) ;
476+ } catch ( e ) {
477+ expect ( e . message ) . toContain ( 'cannot perform this operation when schemas options is used.' ) ;
478+ }
479+ } ) ;
480+ it ( 'should only enable delete class endpoint since' , async ( ) => {
481+ await reconfigureServer ( { schemas : [ { className : '_User' } , { className : 'Test' } ] } ) ;
482+ await reconfigureServer ( { schemas : [ { className : '_User' } ] } ) ;
483+
484+ let schemas = await Parse . Schema . all ( ) ;
485+ expect ( schemas . length ) . toEqual ( 4 ) ;
486+
487+ await new Parse . Schema ( '_User' ) . delete ( ) ;
488+ schemas = await Parse . Schema . all ( ) ;
489+ expect ( schemas . length ) . toEqual ( 3 ) ;
490+ } ) ;
491+ it ( 'should run beforeSchemasMigration before execution of DefinedSchemas' , async ( ) => {
492+ let before = false ;
493+ const server = await reconfigureServer ( {
494+ schemas : [ { className : '_User' } , { className : 'Test' } ] ,
495+ beforeSchemasMigration : async ( ) => {
496+ expect ( before ) . toEqual ( false ) ;
497+ before = true ;
498+ } ,
499+ } ) ;
500+ before = true ;
501+ expect ( before ) . toEqual ( true ) ;
502+ expect ( server ) . toBeDefined ( ) ;
503+ } ) ;
504+ it ( 'should use logger in case of error' , async ( ) => {
505+ const server = await reconfigureServer ( { schemas : [ { className : '_User' } ] } ) ;
506+
507+ const error = new Error ( 'A test error' ) ;
508+ const logger = require ( '../lib/logger' ) . logger ;
509+ spyOn ( logger , 'error' ) . and . callThrough ( ) ;
510+ spyOn ( Parse . Schema , 'all' ) . and . callFake ( async ( ) => {
511+ throw error ;
512+ } ) ;
513+
514+ await new DefinedSchemas (
515+ [ { className : 'Test' , fields : { aField : { type : 'String' } } } ] ,
516+ server . config
517+ ) . execute ( ) ;
518+
519+ expect ( logger . error ) . toHaveBeenCalledWith ( error ) ;
520+ } ) ;
419521} ) ;
0 commit comments