@ -17,15 +17,15 @@ describe("/rows", () => {
row = basicRow ( table . _ id )
} )
const loadRow = async id =>
const loadRow = async ( id , status = 200 ) =>
await request
. get ( ` /api/ ${ table . _ id } /rows/ ${ id } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
. expect ( status )
describe ( "save, load, update, delete " , ( ) => {
describe ( "save, load, update" , ( ) => {
it ( "returns a success message when the row is created" , async ( ) => {
const res = await request
. post ( ` /api/ ${ row . tableId } /rows ` )
@ -217,38 +217,152 @@ describe("/rows", () => {
expect ( savedRow . body . description ) . toEqual ( existing . description )
expect ( savedRow . body . name ) . toEqual ( "Updated Name" )
} )
it ( "should throw an error when given improper types" , async ( ) => {
const existing = await config . createRow ( )
await request
. patch ( ` /api/ ${ table . _ id } /rows/ ${ existing . _ id } ` )
. send ( {
_ id : existing . _ id ,
_ rev : existing . _ rev ,
tableId : table . _ id ,
name : 1 ,
} )
. set ( config . defaultHeaders ( ) )
. expect ( 400 )
} )
} )
describe ( "destroy" , ( ) => {
it ( "should be able to delete a row" , async ( ) => {
const createdRow = await config . createRow ( row )
const res = await request
. delete ( ` /api/ ${ table . _ id } /rows/ ${ createdRow . _ id } / ${ createdRow . _ rev } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( res . body . ok ) . toEqual ( true )
} )
it ( "shouldn't allow deleting a row in a table which is different to the one the row was created on" , async ( ) => {
const createdRow = await config . createRow ( row )
await request
. delete ( ` /api/wrong_table/rows/ ${ createdRow . _ id } / ${ createdRow . _ rev } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 400 )
} )
} )
describe ( "validate" , ( ) => {
it ( "should return no errors on valid row" , async ( ) => {
const result = await request
const res = await request
. post ( ` /api/ ${ table . _ id } /rows/validate ` )
. send ( { name : "ivan" } )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( result . body . valid ) . toBe ( true )
expect ( Object . keys ( result . body . errors ) ) . toEqual ( [ ] )
expect ( res . body . valid ) . toBe ( true )
expect ( Object . keys ( res . body . errors ) ) . toEqual ( [ ] )
} )
it ( "should errors on invalid row" , async ( ) => {
const result = await request
const res = await request
. post ( ` /api/ ${ table . _ id } /rows/validate ` )
. send ( { name : 1 } )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( result . body . valid ) . toBe ( false )
expect ( Object . keys ( result . body . errors ) ) . toEqual ( [ "name" ] )
expect ( res . body . valid ) . toBe ( false )
expect ( Object . keys ( res . body . errors ) ) . toEqual ( [ "name" ] )
} )
} )
describe ( "enrich row unit test" , ( ) => {
describe ( "bulkDelete" , ( ) => {
it ( "should be able to delete a bulk set of rows" , async ( ) => {
const row1 = await config . createRow ( )
const row2 = await config . createRow ( )
const res = await request
. post ( ` /api/ ${ table . _ id } /rows ` )
. send ( {
type : "delete" ,
rows : [
row1 ,
row2 ,
]
} )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( res . body . length ) . toEqual ( 2 )
await loadRow ( row1 . _ id , 404 )
} )
} )
describe ( "search" , ( ) => {
it ( "should run a search on the table" , async ( ) => {
const row = await config . createRow ( )
// add another row that shouldn't be found
await config . createRow ( {
... basicRow ( ) ,
name : "Other Contact" ,
} )
const res = await request
. post ( ` /api/ ${ table . _ id } /rows/search ` )
. send ( {
query : {
name : "Test" ,
} ,
pagination : { pageSize : 25 , page : 0 }
} )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( res . body . length ) . toEqual ( 1 )
expect ( res . body [ 0 ] . _ id ) . toEqual ( row . _ id )
} )
} )
describe ( "fetchView" , ( ) => {
it ( "should be able to fetch tables contents via 'view'" , async ( ) => {
const row = await config . createRow ( )
const res = await request
. get ( ` /api/views/all_ ${ table . _ id } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( res . body . length ) . toEqual ( 1 )
expect ( res . body [ 0 ] . _ id ) . toEqual ( row . _ id )
} )
it ( "should throw an error if view doesn't exist" , async ( ) => {
await request
. get ( ` /api/views/derp ` )
. set ( config . defaultHeaders ( ) )
. expect ( 400 )
} )
it ( "should be able to run on a view" , async ( ) => {
const view = await config . createView ( )
const row = await config . createRow ( )
const res = await request
. get ( ` /api/views/ ${ view . _ id } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( res . body . length ) . toEqual ( 1 )
expect ( res . body [ 0 ] . _ id ) . toEqual ( row . _ id )
} )
} )
describe ( "user testing" , ( ) => {
} )
describe ( "fetchEnrichedRows" , ( ) => {
it ( "should allow enriching some linked rows" , async ( ) => {
const table = await config . createLinkedTable ( )
const firstRow = await config . createRow ( {
@ -262,30 +376,45 @@ describe("/rows", () => {
link : [ { _ id : firstRow . _ id } ] ,
tableId : table . _ id ,
} )
const enriched = await outputProcessing ( config . getAppId ( ) , table , [ secondRow ] )
expect ( enriched [ 0 ] . link . length ) . toBe ( 1 )
expect ( enriched [ 0 ] . link [ 0 ] . _ id ) . toBe ( firstRow . _ id )
expect ( enriched [ 0 ] . link [ 0 ] . primaryDisplay ) . toBe ( "Test Contact" )
// test basic enrichment
const resBasic = await request
. get ( ` /api/ ${ table . _ id } /rows/ ${ secondRow . _ id } ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( resBasic . body . link [ 0 ] . _ id ) . toBe ( firstRow . _ id )
expect ( resBasic . body . link [ 0 ] . primaryDisplay ) . toBe ( "Test Contact" )
// test full enrichment
const resEnriched = await request
. get ( ` /api/ ${ table . _ id } / ${ secondRow . _ id } /enrich ` )
. set ( config . defaultHeaders ( ) )
. expect ( 'Content-Type' , /json/ )
. expect ( 200 )
expect ( resEnriched . body . link . length ) . toBe ( 1 )
expect ( resEnriched . body . link [ 0 ] . _ id ) . toBe ( firstRow . _ id )
expect ( resEnriched . body . link [ 0 ] . name ) . toBe ( "Test Contact" )
expect ( resEnriched . body . link [ 0 ] . description ) . toBe ( "original description" )
} )
} )
it ( "should allow enriching attachment rows" , async ( ) => {
const table = await config . createAttachmentTable ( )
const row = await config . createRow ( {
name : "test" ,
description : "test" ,
attachment : [ {
url : "/test/thing" ,
} ] ,
tableId : table . _ id ,
describe ( "attachments" , ( ) => {
it ( "should allow enriching attachment rows" , async ( ) => {
const table = await config . createAttachmentTable ( )
const row = await config . createRow ( {
name : "test" ,
description : "test" ,
attachment : [ {
url : "/test/thing" ,
} ] ,
tableId : table . _ id ,
} )
// the environment needs configured for this
await setup . switchToCloudForFunction ( async ( ) => {
const enriched = await outputProcessing ( config . getAppId ( ) , table , [ row ] )
expect ( enriched [ 0 ] . attachment [ 0 ] . url ) . toBe ( ` /app-assets/assets/ ${ config . getAppId ( ) } /test/thing ` )
} )
} )
// the environment needs configured for this
env . CLOUD = 1
env . SELF_HOSTED = 1
const enriched = await outputProcessing ( config . getAppId ( ) , table , [ row ] )
expect ( enriched [ 0 ] . attachment [ 0 ] . url ) . toBe ( ` /app-assets/assets/ ${ config . getAppId ( ) } /test/thing ` )
// remove env config
env . CLOUD = undefined
env . SELF_HOSTED = undefined
} )
} )