1 00:00:01,280 --> 00:00:04,730 Let's take care of the validation test and the actual success case as well. 2 00:00:04,730 --> 00:00:09,260 In this video that we should build to get through it pretty quickly to first get started by writing 3 00:00:09,290 --> 00:00:12,800 in a test here or doing some validation. 4 00:00:12,800 --> 00:00:16,840 So as usual I do need to make sure that a ticket actually already exists. 5 00:00:16,850 --> 00:00:18,650 So go back up to the previous test. 6 00:00:18,870 --> 00:00:26,040 I'm going to copy that creation logic right there and I'll put it down inside of here like so now in 7 00:00:26,040 --> 00:00:31,660 this case we want to make a request ask the same user who we were when we created the ticket. 8 00:00:31,760 --> 00:00:35,520 So I going to save a reference to the cookie that we're generating right here so that I can pretend 9 00:00:35,520 --> 00:00:38,170 to be the same user over multiple requests. 10 00:00:38,170 --> 00:00:46,190 So I going to cut out that global dot sign in and right above I'll say cookie is global sign and and 11 00:00:46,270 --> 00:00:50,460 I'll stick the cookie in there OK. 12 00:00:50,490 --> 00:00:52,020 So we are creating that ticket. 13 00:00:52,050 --> 00:00:56,160 Now I want to try to make some requests to update the thing and as we try to update it we're going to 14 00:00:56,160 --> 00:00:58,330 put in some invalid values inside of here. 15 00:00:58,380 --> 00:01:03,510 We're just going to write out an assertion to say that we get back some kind of error saying hey sorry 16 00:01:03,510 --> 00:01:12,060 four hundred you provided us some bad inputs so we'll do an a wait request to app once again we want 17 00:01:12,060 --> 00:01:14,730 to do a put request to a very specific ticket. 18 00:01:14,740 --> 00:01:16,250 So I going to use those tactics. 19 00:01:16,290 --> 00:01:22,110 Put in API tickets and then it will stick in the I.D. from the ticket that was created and we get that 20 00:01:22,110 --> 00:01:30,400 out of the response the response body I.D. will then set the cookie so that I am the exact same user 21 00:01:32,420 --> 00:01:41,310 and then let's send along How about an invalid title and I'll stick with the same price of 20 so in 22 00:01:41,310 --> 00:01:41,940 this case. 23 00:01:41,940 --> 00:01:45,960 Well we want to make sure every ticket always has a title that some value inside of it. 24 00:01:46,750 --> 00:01:53,130 So I'm going to expect to get back a 400 we could definitely do another follow up one to make sure that 25 00:01:53,130 --> 00:01:55,570 we get a four hundred if the title does not exist. 26 00:01:55,620 --> 00:01:57,720 If you want to write out that test totally fine. 27 00:01:57,720 --> 00:02:02,520 Otherwise I'll just do another check to make sure that a price that is invalid results in at 400 as 28 00:02:02,520 --> 00:02:03,300 well. 29 00:02:03,300 --> 00:02:04,560 So I'm going to copy that request 30 00:02:07,750 --> 00:02:09,280 paste it down below. 31 00:02:09,310 --> 00:02:15,100 In this case I'll put in a valid title but an invalid price so price is going to invalid if it is in 32 00:02:15,100 --> 00:02:17,080 the negatives so negative 10. 33 00:02:17,130 --> 00:02:17,980 That's your work just fine. 34 00:02:19,800 --> 00:02:23,180 All right as usual I will say this in a flip back over 35 00:02:26,820 --> 00:02:31,620 and now it looks like we do have a failing test which is good in this case the reason it's failing is 36 00:02:31,620 --> 00:02:36,120 that you and I expected to see a 400 but instead we got a 200. 37 00:02:36,120 --> 00:02:41,670 So right now we're erroneously sending back a 200 even though we put in some very bad inputs. 38 00:02:41,670 --> 00:02:45,900 So let's now go add in some validation steps and we're going to make sure that that 200 that we get 39 00:02:46,110 --> 00:02:48,870 turns into a 400. 40 00:02:49,010 --> 00:02:49,260 All right. 41 00:02:49,260 --> 00:02:52,620 Back over inside of my update file. 42 00:02:52,840 --> 00:02:54,710 We definitely need to do some validation. 43 00:02:54,730 --> 00:03:01,350 Luckily we've already imported body and validate requests so right after our require off middleware 44 00:03:02,190 --> 00:03:06,720 let's put in an array we're gonna write out our different validators in here and then after that array 45 00:03:06,810 --> 00:03:12,990 I'll make sure right away that I add in the validate request middleware so validate requests and we'll 46 00:03:12,990 --> 00:03:16,450 do some checks on the incoming body. 47 00:03:16,650 --> 00:03:22,510 So first we will check our title and just like we had in our creation steps we'll pretty much just make 48 00:03:22,510 --> 00:03:24,040 sure that this thing exists. 49 00:03:24,130 --> 00:03:36,530 So not is empty has a message of title is required and then check out our price and for this let's make 50 00:03:36,530 --> 00:03:46,090 sure that we've got a float here and we want to make sure it's greater than 0 with a message of price 51 00:03:47,530 --> 00:03:51,470 must be provided and must be greater than zero. 52 00:03:51,490 --> 00:03:56,170 I think we use a better message back in our creation handler if we want to go look up that error message 53 00:03:56,170 --> 00:03:57,130 and use a consistent one. 54 00:03:57,130 --> 00:03:59,710 Feel free to do so okay. 55 00:03:59,740 --> 00:04:00,620 So that looks good. 56 00:04:00,820 --> 00:04:08,350 Let's say this rerun our tests back over here everything's passing. 57 00:04:08,350 --> 00:04:09,380 Very good. 58 00:04:09,400 --> 00:04:11,200 Now just one last thing to do. 59 00:04:11,200 --> 00:04:17,740 Let's make sure that we actually can update a ticket the very last test down here as usual. 60 00:04:17,750 --> 00:04:21,700 Once again we need to create a ticket ahead of time so we can actually edit it. 61 00:04:21,800 --> 00:04:24,930 I'm going to do a little bit of copy paste from the test right above. 62 00:04:25,230 --> 00:04:29,420 So from the one we were just working on I'm going to grab where recreate the cookie and then make the 63 00:04:29,420 --> 00:04:36,380 initial ticket copy all that go down to our happy path right here. 64 00:04:36,380 --> 00:04:40,640 Pace that in and let's try to edit the ticket. 65 00:04:42,140 --> 00:04:43,590 So we'll do and await. 66 00:04:43,740 --> 00:04:48,720 Request to app. 67 00:04:48,850 --> 00:04:55,090 I'm gonna do a not opposed but a put and once again we're gonna use some string interpolation to API 68 00:04:55,450 --> 00:05:07,140 tickets response body I.D. I'm gonna set the exact same cookie on their and I'll send along our actual 69 00:05:07,140 --> 00:05:15,490 update to make about this time we give it a legible title I'll say new title and a price of one hundred 70 00:05:16,830 --> 00:05:19,590 coming out of that I'm going to expect to get a two hundred 71 00:05:23,720 --> 00:05:26,270 and then finally we could be happy at the two hundred. 72 00:05:26,270 --> 00:05:31,280 Or alternatively we could do one more follow up request to try to now fetch this ticket and write out 73 00:05:31,280 --> 00:05:36,990 an assertion and say this thing was actually updated we did not do that on the earlier test. 74 00:05:37,000 --> 00:05:41,380 Up here we were making sure that update did not occur if the user was not logged in. 75 00:05:41,390 --> 00:05:45,500 So since we did not do it not there let's do it down here just we can see what that would really look 76 00:05:45,500 --> 00:05:46,280 like. 77 00:05:46,280 --> 00:05:50,900 So I want to make a follow up request and just say OK on fetching this ticket after it's been updated 78 00:05:51,140 --> 00:05:55,690 it actually has the new title and the new price to fetch that ticket. 79 00:05:55,690 --> 00:05:58,940 We will make another request here and going to save the response as a variable. 80 00:05:58,940 --> 00:06:03,910 I would call ticket response we'll do our request to app. 81 00:06:03,910 --> 00:06:08,440 I want to make a get request and I want to fetch a very particular ticket so I gonna make a get request 82 00:06:08,440 --> 00:06:12,650 to a very slow your El API tickets and then the idea that ticket. 83 00:06:12,850 --> 00:06:20,230 So API tickets response body I.D. I'll send that off. 84 00:06:20,230 --> 00:06:24,130 We do not actually have to set any cookies or anything like that because we do not require authentication 85 00:06:24,130 --> 00:06:30,330 to fetch details about a single ticket down inside of here let's write out our expectation. 86 00:06:30,410 --> 00:06:35,560 So we're going to expect the response body right here inside there we should see details about that 87 00:06:35,560 --> 00:06:40,660 ticket in particular we should have the title and a price the title should be equal to needed new title 88 00:06:40,810 --> 00:06:43,170 and the price should be equal to hundred. 89 00:06:43,330 --> 00:06:49,250 So we will expect ticket response body title to equal 90 00:06:52,240 --> 00:06:54,030 new title. 91 00:06:54,250 --> 00:06:56,670 I'm going to do a little copy paste. 92 00:06:56,670 --> 00:07:01,680 I'll check the price and make sure that is equal to 100 All right. 93 00:07:01,710 --> 00:07:04,970 Let's save this and just make sure that this test is failing. 94 00:07:05,160 --> 00:07:09,560 So I just wanna make sure that it's not erroneously passing for some reason it's back in my terminal. 95 00:07:09,720 --> 00:07:11,550 Yep looks like we're good to go. 96 00:07:11,580 --> 00:07:17,980 The update was received but never actually applied so let's go and now do a little bit of update to 97 00:07:17,980 --> 00:07:20,180 our root handler itself. 98 00:07:20,210 --> 00:07:29,080 I'm gonna go back over to the root handler handling updates right here then we're gonna go right after 99 00:07:29,170 --> 00:07:33,880 we do the check to see if this user actually owns the ticket and if they do own it we're going to apply 100 00:07:33,880 --> 00:07:39,430 the update to apply the update we can use the set commands on the ticket document that we already fetch 101 00:07:40,240 --> 00:07:44,710 whenever we call set we can pass in an object that contains the properties we want to update this thing 102 00:07:44,710 --> 00:07:55,310 to so we want to change the title to wreck dot body dot titled and price to wreck body writes 103 00:07:58,740 --> 00:08:03,570 After setting some new properties on the ticket that just makes updates to the document in memory it 104 00:08:03,570 --> 00:08:08,590 does actually persist those updates back to our Mongo DB database after calling set we then have to 105 00:08:08,590 --> 00:08:15,030 do and a weight ticket dot save and that's what's going to actually persist everything back it just 106 00:08:15,030 --> 00:08:20,610 you know a little bit of trivia after we call save those updates will be persisted to the database and 107 00:08:20,610 --> 00:08:26,550 mongoose is gonna make sure that any further updates either due to some post save hooks pre save hooks 108 00:08:26,790 --> 00:08:32,130 something done by Mongo DB itself or whatever else all other possible updates will be persisted back 109 00:08:32,310 --> 00:08:34,840 to this ticket document that we already have a reference to. 110 00:08:35,040 --> 00:08:39,420 So we do not have to say refresh this ticket to get the updated version that's been saved. 111 00:08:39,420 --> 00:08:45,810 We already have the updated version right here so we can continue to rely upon results sent to send 112 00:08:45,810 --> 00:08:48,670 back details about this updated ticket. 113 00:08:48,840 --> 00:08:56,820 Let's say this take a look at our test see how we're doing so looks like we're green across the board. 114 00:08:56,880 --> 00:08:57,940 Fantastic. 115 00:08:57,950 --> 00:08:59,150 This is a super long video. 116 00:08:59,190 --> 00:09:03,690 So let's take a pause right now and do a quick wrap up of all these different root handlers in just 117 00:09:03,690 --> 00:09:03,990 a moment.