@@ -173,28 +173,71 @@ describe('Courses', () => {
173173 } ) ;
174174
175175 describe ( '/POST course/:courseId/reviews' , ( ) => {
176+ let review = {
177+ user : '57029ed4795118be119cc438' ,
178+ postedOn : "2016-02-04T21:22:00.000Z" ,
179+ rating : 4 ,
180+ review : "Lorem ipsum This is a review that I am writing, blah blah blah de blah"
181+ } ;
182+ let courseId = "57029ed4795118be119cc43d" ;
183+
176184 it ( 'should POST when a review is supplied with proper information and has an authorized user' , ( done ) => {
177- let review = {
178- user : '57029ed4795118be119cc438' ,
179- postedOn : "2016-02-04T21:22:00.000Z" ,
180- rating : 4 ,
181- review : "Lorem ipsum This is a review that I am writing, blah blah blah de blah"
185+ successPostReview ( courseId , validAuth , review , done ) ;
186+ } ) ;
187+
188+ it ( 'should POST only required attribute for review when supplied with garbage parameters' , ( done ) => {
189+ let excessReview = review ;
190+ excessReview . extra = 'this is extra param' ;
191+ successPostReview ( courseId , validAuth , excessReview , done ) ;
192+ } ) ;
193+
194+ it ( 'should not POST when user is not authorized' , ( done ) => {
195+ let invalidAuth = {
196+ user : 'invalid@notvalid.com' ,
197+ pass : 'notValidPass'
182198 } ;
183- let courseId = "57029ed4795118be119cc43d" ;
184- postReview ( courseId , validAuth , review , done ) ;
199+ failPostReview ( 401 , "Access Denied: Wrong email or password" , courseId , invalidAuth , review , done ) ;
185200 } ) ;
186201
187- function postReview ( courseId , validAuth , review , done ) {
202+ it ( 'should not POST when review has out of bounds rating' , ( done ) => {
203+ let badReview = review ;
204+ badReview . rating = 20 ;
205+ failPostReview ( 500 , "Review validation failed: rating: Path `rating` (20) is more than maximum allowed value (5)." , courseId , validAuth , badReview , done ) ;
206+ } ) ;
207+
208+ it ( 'should not POST when rating is missing' , ( done ) => {
209+ let badReview = review ;
210+ badReview . rating = null ;
211+ failPostReview ( 500 , "Review validation failed: rating: Path `rating` is required." , courseId , validAuth , badReview , done ) ;
212+ } ) ;
213+
214+ it ( 'should not POST when not supplied with a review' , ( done ) => {
215+ let badReview = { } ;
216+ failPostReview ( 500 , "Review validation failed: rating: Path `rating` is required." , courseId , validAuth , badReview , done ) ;
217+ } ) ;
218+
219+ function successPostReview ( courseId , auth , review , done ) {
188220 chai . request ( server )
189221 . post ( courseIndexLink + courseId + '/reviews' )
190- . auth ( validAuth . user , validAuth . pass )
222+ . auth ( auth . user , auth . pass )
191223 . send ( review )
192224 . end ( ( err , res ) => {
193- res . body . should . have . property ( 'message' ) . equal ( "Review Successfully added to Course!" ) ;
194225 res . should . have . status ( 201 ) ;
195226 done ( ) ;
196227 } ) ;
197228 } ;
229+
230+ function failPostReview ( status , msg , courseId , auth , review , done ) {
231+ chai . request ( server )
232+ . post ( courseIndexLink + courseId + '/reviews' )
233+ . auth ( auth . user , auth . pass )
234+ . send ( review )
235+ . end ( ( err , res ) => {
236+ res . body . should . have . property ( 'message' ) . equal ( msg ) ;
237+ res . should . have . status ( status ) ;
238+ done ( ) ;
239+ } ) ;
240+ } ;
198241 } ) ;
199242
200243} ) ;
0 commit comments