@ -1907,4 +1907,146 @@ describe('[Rooms]', function () {
} ) ;
} ) ;
} ) ;
describe ( 'rooms.images' , ( ) => {
let testUserCreds = null ;
before ( async ( ) => {
const user = await createUser ( ) ;
testUserCreds = await login ( user . username , password ) ;
} ) ;
const uploadFile = async ( { roomId , file } ) => {
const { body } = await request
. post ( api ( ` rooms.upload/ ${ roomId } ` ) )
. set ( credentials )
. attach ( 'file' , file )
. expect ( 'Content-Type' , 'application/json' )
. expect ( 200 ) ;
return body . message . attachments [ 0 ] ;
} ;
const getIdFromImgPath = ( link ) => {
return link . split ( '/' ) [ 2 ] ;
} ;
it ( 'should return an error when user is not logged in' , async ( ) => {
await request . get ( api ( 'rooms.images' ) ) . expect ( 401 ) ;
} ) ;
it ( 'should return an error when the required parameter "roomId" is not provided' , async ( ) => {
await request . get ( api ( 'rooms.images' ) ) . set ( credentials ) . expect ( 400 ) ;
} ) ;
it ( 'should return an error when the required parameter "roomId" is not a valid room' , async ( ) => {
await request . get ( api ( 'rooms.images' ) ) . set ( credentials ) . query ( { roomId : 'invalid' } ) . expect ( 403 ) ;
} ) ;
it ( 'should return an error when room is valid but user is not part of it' , async ( ) => {
const { body } = await createRoom ( { type : 'p' , name : ` test- ${ Date . now ( ) } ` } ) ;
const {
group : { _id : roomId } ,
} = body ;
await request . get ( api ( 'rooms.images' ) ) . set ( testUserCreds ) . query ( { roomId } ) . expect ( 403 ) ;
await deleteRoom ( { type : 'p' , roomId } ) ;
} ) ;
it ( 'should return an empty array when room is valid and user is part of it but there are no images' , async ( ) => {
const { body } = await createRoom ( { type : 'p' , usernames : [ credentials . username ] , name : ` test- ${ Date . now ( ) } ` } ) ;
const {
group : { _id : roomId } ,
} = body ;
await request
. get ( api ( 'rooms.images' ) )
. set ( credentials )
. query ( { roomId } )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'files' ) . and . to . be . an ( 'array' ) . and . to . have . lengthOf ( 0 ) ;
} ) ;
await deleteRoom ( { type : 'p' , roomId } ) ;
} ) ;
it ( 'should return an array of images when room is valid and user is part of it and there are images' , async ( ) => {
const { body } = await createRoom ( { type : 'p' , usernames : [ credentials . username ] , name : ` test- ${ Date . now ( ) } ` } ) ;
const {
group : { _id : roomId } ,
} = body ;
const { title _link } = await uploadFile ( {
roomId ,
file : fs . createReadStream ( path . join ( process . cwd ( ) , imgURL ) ) ,
} ) ;
const fileId = getIdFromImgPath ( title _link ) ;
await request
. get ( api ( 'rooms.images' ) )
. set ( credentials )
. query ( { roomId } )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'files' ) . and . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 ) ;
expect ( res . body . files [ 0 ] ) . to . have . property ( '_id' , fileId ) ;
} ) ;
await deleteRoom ( { type : 'p' , roomId } ) ;
} ) ;
it ( 'should return multiple images when room is valid and user is part of it and there are multiple images' , async ( ) => {
const { body } = await createRoom ( { type : 'p' , usernames : [ credentials . username ] , name : ` test- ${ Date . now ( ) } ` } ) ;
const {
group : { _id : roomId } ,
} = body ;
const { title _link : link1 } = await uploadFile ( {
roomId ,
file : fs . createReadStream ( path . join ( process . cwd ( ) , imgURL ) ) ,
} ) ;
const { title _link : link2 } = await uploadFile ( {
roomId ,
file : fs . createReadStream ( path . join ( process . cwd ( ) , imgURL ) ) ,
} ) ;
const fileId1 = getIdFromImgPath ( link1 ) ;
const fileId2 = getIdFromImgPath ( link2 ) ;
await request
. get ( api ( 'rooms.images' ) )
. set ( credentials )
. query ( { roomId } )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'files' ) . and . to . be . an ( 'array' ) . and . to . have . lengthOf ( 2 ) ;
expect ( res . body . files . find ( ( file ) => file . _id === fileId1 ) ) . to . exist ;
expect ( res . body . files . find ( ( file ) => file . _id === fileId2 ) ) . to . exist ;
} ) ;
await deleteRoom ( { type : 'p' , roomId } ) ;
} ) ;
it ( 'should allow to filter images passing the startingFromId parameter' , async ( ) => {
const { body } = await createRoom ( { type : 'p' , usernames : [ credentials . username ] , name : ` test- ${ Date . now ( ) } ` } ) ;
const {
group : { _id : roomId } ,
} = body ;
const { title _link } = await uploadFile ( {
roomId ,
file : fs . createReadStream ( path . join ( process . cwd ( ) , imgURL ) ) ,
} ) ;
await uploadFile ( {
roomId ,
file : fs . createReadStream ( path . join ( process . cwd ( ) , imgURL ) ) ,
} ) ;
const fileId2 = getIdFromImgPath ( title _link ) ;
await request
. get ( api ( 'rooms.images' ) )
. set ( credentials )
. query ( { roomId , startingFromId : fileId2 } )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'files' ) . and . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 ) ;
expect ( res . body . files [ 0 ] ) . to . have . property ( '_id' , fileId2 ) ;
} ) ;
await deleteRoom ( { type : 'p' , roomId } ) ;
} ) ;
} ) ;
} ) ;