1 00:00:01,420 --> 00:00:02,660 Our test suite is up and running. 2 00:00:02,680 --> 00:00:07,600 So let's create a test file to make sure that the new root handler is working as expected inside of 3 00:00:07,600 --> 00:00:08,500 my roots directory. 4 00:00:08,500 --> 00:00:15,190 I'll make a new folder of underscore underscore past underscore underscore and then inside that directory 5 00:00:15,220 --> 00:00:17,850 I'll make a new file of new test. 6 00:00:17,880 --> 00:00:19,020 Yes. 7 00:00:19,190 --> 00:00:22,900 So we're gonna write out some number of tests inside this file to make sure that the new root handler 8 00:00:22,900 --> 00:00:24,880 is working as expected. 9 00:00:24,880 --> 00:00:29,750 I'm gonna go backwards to the new root handler really quickly you might recall from the ticket service 10 00:00:29,780 --> 00:00:35,310 we wrote out a decent number of tests to make sure that some different root handlers enforced authentication. 11 00:00:35,330 --> 00:00:40,850 We also wrote out some test to make sure that users were providing some valid body information or parameters 12 00:00:40,850 --> 00:00:42,940 with the request as well. 13 00:00:42,940 --> 00:00:46,730 Weirdly took a look at how to write those tests so we're not going to focus on writing those tests for 14 00:00:46,730 --> 00:00:47,930 the order service. 15 00:00:48,020 --> 00:00:51,420 If you want to add those tests in and it definitely is worth adding them in. 16 00:00:51,440 --> 00:00:53,320 Feel free to do so on your own. 17 00:00:53,390 --> 00:00:57,680 You can write out some test to make sure that a user is authenticated and if they're not we should respond 18 00:00:57,680 --> 00:01:02,450 with an error and we should make sure that we have a test that checks to make sure that the body the 19 00:01:02,450 --> 00:01:04,670 request has a valid ticket idea as well. 20 00:01:04,670 --> 00:01:06,320 So totally up to you if you want to write those. 21 00:01:06,380 --> 00:01:09,970 We're not gonna worry about them going forward because we really took a look at it in the past. 22 00:01:10,040 --> 00:01:13,800 Instead we're going to really focus on the business logic of our different handlers. 23 00:01:14,060 --> 00:01:19,850 So we're going to make sure that we handle the case in which a user tries to reserve a ticket that does 24 00:01:19,850 --> 00:01:20,470 not exist. 25 00:01:21,820 --> 00:01:26,110 Make sure we handle the case in which a user tries to reserve a ticket that has already been reserved 26 00:01:27,190 --> 00:01:29,530 and then finally handle the success case as well. 27 00:01:29,560 --> 00:01:32,410 When a user actually successfully reserves a ticket. 28 00:01:33,060 --> 00:01:36,790 So in total we're going to focus on writing just three tests around this handler. 29 00:01:36,800 --> 00:01:40,490 So let's get to it back inside of the new test file. 30 00:01:40,540 --> 00:01:47,930 We're gonna write out the three statements right away so we'll say how about it returns an error if 31 00:01:47,930 --> 00:01:50,570 the user tries to reserve. 32 00:01:50,630 --> 00:01:53,340 Actually Let's shorten this thing about returns an error. 33 00:01:53,360 --> 00:01:57,400 If the ticket does not exist let's be a little bit more direct. 34 00:01:57,530 --> 00:02:03,520 Rather than writing out a super long description then how about returns an error. 35 00:02:03,580 --> 00:02:06,830 If the ticket is already reserved 36 00:02:09,580 --> 00:02:13,300 and then finally it reserves a ticket. 37 00:02:13,300 --> 00:02:20,160 So this will be our success case in which everything goes as expected now in all the tests we had written 38 00:02:20,160 --> 00:02:20,760 previously. 39 00:02:20,790 --> 00:02:25,170 We essentially could just write out some implementation inside of each of these different get statements 40 00:02:25,230 --> 00:02:26,160 and that was it. 41 00:02:26,400 --> 00:02:30,420 But now that we're working on a slightly more complicated service where we're essentially operating 42 00:02:30,420 --> 00:02:35,100 over multiple different types of records we're going to see that in some cases we've got to do a decent 43 00:02:35,130 --> 00:02:37,890 amount of setup before any test runs. 44 00:02:37,890 --> 00:02:43,560 So for example with our second and third cases right here we're going to need to make sure that there 45 00:02:43,560 --> 00:02:47,430 is a valid ticket inside the database so we can actually find. 46 00:02:47,430 --> 00:02:52,410 So before we write out any test implementation or actually any logic to make the request itself we have 47 00:02:52,410 --> 00:02:55,290 to manually save a ticket into the database. 48 00:02:55,290 --> 00:02:56,250 So just keep that in mind. 49 00:02:57,690 --> 00:03:00,340 OK so let's take care of this first statement first. 50 00:03:00,650 --> 00:03:05,720 Let's add in a couple of import statements at the very top to make the request so we'll get to request 51 00:03:06,970 --> 00:03:14,010 from Super Test and app from up to directories at 52 00:03:18,290 --> 00:03:19,980 so then for the very first one right here. 53 00:03:20,060 --> 00:03:23,560 Pretty much just make the request make sure we get a forum for it back. 54 00:03:23,720 --> 00:03:29,150 Do keep in mind as well that even though we are not going to directly test and make sure that we've 55 00:03:29,150 --> 00:03:34,010 got this body validation going on this body validation is still going to be executed. 56 00:03:34,040 --> 00:03:39,170 So in other words we do have to make sure that we provide some kind of valid I.D. for the ticket I.D. 57 00:03:39,170 --> 00:03:46,490 in the body the request when I say valid I.D. I mean a valid Mongo D.B. object I.D. So we do have to 58 00:03:46,580 --> 00:03:53,470 import Mongoose at the top as well so we can generate randomly a valid object I.D. at the very top I 59 00:03:53,480 --> 00:03:59,140 will get Mongoose from fungus then inside this first test right here. 60 00:03:59,150 --> 00:04:04,370 Let's randomly generate an I.D. will then make a request attempting to create a new order and we're 61 00:04:04,370 --> 00:04:08,610 going to simply expected to return a forum for status code. 62 00:04:08,710 --> 00:04:15,310 Don't say ticket is mongers types object ideas. 63 00:04:15,310 --> 00:04:16,920 That's how we create a new random idea. 64 00:04:16,930 --> 00:04:21,660 We've seen that once before and we'll go ahead and make our request. 65 00:04:21,670 --> 00:04:25,310 We will await request to app. 66 00:04:25,420 --> 00:04:31,660 I'm going to do a post request to API orders and then remember we are requiring authentication. 67 00:04:31,680 --> 00:04:34,780 So we have to set our cookie in the same way we had previously. 68 00:04:34,970 --> 00:04:41,800 So to do so we had copied over that implementation from our test setup file so we want to call sign 69 00:04:41,830 --> 00:04:47,090 in off the global object and we're going to use that to set the cookie header on our request. 70 00:04:47,630 --> 00:04:52,830 So we'll say dot set cookie global dot sign it 71 00:04:56,500 --> 00:05:01,180 will then send along in the body the request the ticket idea that we just generated in the step previously. 72 00:05:01,260 --> 00:05:02,670 So I'll put in a ticket. 73 00:05:02,800 --> 00:05:05,310 Ticket idea. 74 00:05:05,350 --> 00:05:09,430 And naturally we can condense that down to just ticket ideas and probably condense it down to one line 75 00:05:09,460 --> 00:05:09,950 as well. 76 00:05:11,030 --> 00:05:17,070 And finally we will expect to get back up for a for. 77 00:05:17,100 --> 00:05:18,830 So that should be it for the very first test. 78 00:05:18,840 --> 00:05:23,280 Let's say this look back over your terminal and see how we're doing. 79 00:05:23,340 --> 00:05:28,600 It looks like that test is passing just to make sure that it actually is passing correctly. 80 00:05:28,620 --> 00:05:31,590 I'm going to go back over to the new test handler itself. 81 00:05:31,630 --> 00:05:35,230 I'm gonna find where we are throwing a not found error. 82 00:05:35,320 --> 00:05:39,970 I'm going to copy the bad requester and put it right above the not found error. 83 00:05:40,000 --> 00:05:45,840 So now we're going to get back a status code for a one as opposed to a four a four or submit a four 84 00:05:45,840 --> 00:05:46,270 hundred. 85 00:05:46,270 --> 00:05:48,580 That's what our battery cluster is going to throw. 86 00:05:48,580 --> 00:05:53,280 So this is going to of course our test to fail just to make sure that it's failing in the correct way. 87 00:05:53,460 --> 00:05:55,170 I'll save that put back over. 88 00:05:55,170 --> 00:05:55,490 All right. 89 00:05:55,600 --> 00:05:57,620 Definitely getting the correct thing here. 90 00:05:57,700 --> 00:06:00,700 So we got 400 instead of the four or four that was expected. 91 00:06:00,700 --> 00:06:03,370 Which means our test is probably working as expected. 92 00:06:04,250 --> 00:06:05,430 Well that's our first test. 93 00:06:05,450 --> 00:06:08,090 Quick pause right here and I'll take care of the next two in just a moment.