@@ -12,6 +12,7 @@ const BASE_URL = process.env.OSEM_TEST_BASE_URL,
1212 senseBoxSchema = require ( '../data/senseBoxSchema' ) ,
1313 getUserSchema = require ( '../data/getUserSchema' ) ,
1414 getUserBoxesSchema = require ( '../data/getUserBoxesSchema' ) ,
15+ getUserBoxSchema = require ( '../data/getUserBoxSchema' ) ,
1516 custom_valid_sensebox = require ( '../data/custom_valid_sensebox' ) ;
1617
1718describe ( 'openSenseMap API Routes: /boxes' , function ( ) {
@@ -211,6 +212,32 @@ describe('openSenseMap API Routes: /boxes', function () {
211212 } ) ;
212213 } ) ;
213214
215+ it ( 'should let users retrieve one of their boxes with all fields' , function ( ) {
216+ let boxId ;
217+
218+ return chakram
219+ . get ( `${ BASE_URL } /users/me/boxes` , {
220+ headers : { Authorization : `Bearer ${ jwt } ` } ,
221+ } )
222+ . then ( function ( response ) {
223+ expect ( response ) . to . have . status ( 200 ) ;
224+ expect ( response ) . to . have . schema ( getUserBoxesSchema ) ;
225+
226+ return response ;
227+ } )
228+ . then ( function ( response ) {
229+ boxId = response . body . data . boxes [ 0 ] . _id ;
230+
231+ return chakram . get ( `${ BASE_URL } /users/me/boxes/${ boxId } ` , {
232+ headers : { Authorization : `Bearer ${ jwt } ` } ,
233+ } ) ;
234+ } )
235+ . then ( function ( response ) {
236+ expect ( response ) . to . have . status ( 200 ) ;
237+ expect ( response ) . to . have . schema ( getUserBoxSchema ) ;
238+ } ) ;
239+ } ) ;
240+
214241 it ( 'should return a box as geojson' , function ( ) {
215242 return chakram . get ( `${ BASE_URL } /boxes/${ boxId } ?format=geojson` )
216243 . then ( function ( response ) {
@@ -707,6 +734,46 @@ describe('openSenseMap API Routes: /boxes', function () {
707734 } ) ;
708735 } ) ;
709736
737+ it ( 'should deny to retrieve a box of other user' , function ( ) {
738+ let otherJwt , otherBoxId ;
739+
740+ return chakram
741+ . post ( `${ BASE_URL } /users/sign-in` , {
742+ name : 'mrtest2' ,
743+ 744+ password : '12345678' ,
745+ } )
746+ . then ( function ( response ) {
747+ expect ( response ) . to . have . status ( 200 ) ;
748+ expect ( response ) . to . have . header (
749+ 'content-type' ,
750+ 'application/json; charset=utf-8'
751+ ) ;
752+
753+ expect ( response . body . token ) . to . exist ;
754+
755+ otherJwt = response . body . token ;
756+
757+ return chakram . get ( `${ BASE_URL } /users/me/boxes` , {
758+ headers : { Authorization : `Bearer ${ otherJwt } ` } ,
759+ } ) ;
760+ } )
761+ . then ( function ( response ) {
762+ otherBoxId = response . body . data . boxes [ 0 ] . _id ;
763+
764+ return chakram . get ( `${ BASE_URL } /users/me/boxes/${ otherBoxId } ` , {
765+ headers : { Authorization : `Bearer ${ jwt } ` } ,
766+ } ) ;
767+ } )
768+ . then ( function ( response ) {
769+ expect ( response ) . to . have . status ( 403 ) ;
770+ expect ( response ) . to . have . json ( {
771+ code : 'Forbidden' ,
772+ message : 'User does not own this senseBox' ,
773+ } ) ;
774+ } ) ;
775+ } ) ;
776+
710777 it ( 'should allow to filter boxes by grouptag' , function ( ) {
711778 return chakram . get ( `${ BASE_URL } /boxes?grouptag=newgroup` )
712779 . then ( function ( response ) {
0 commit comments