1 00:00:01,330 --> 00:00:05,680 Let's put together a couple of quick tests to make sure that we handle these three different cases appropriately 2 00:00:06,370 --> 00:00:11,710 so inside of my roots directory I'm going to make a new folder of underscore underscore test underscore 3 00:00:11,740 --> 00:00:15,850 underscore and an inside that I'll make a new file of new tests. 4 00:00:15,920 --> 00:00:17,350 Yes. 5 00:00:17,420 --> 00:00:21,850 It has been a while since we tested out a root handler but remember it's a lot easier to test about 6 00:00:21,860 --> 00:00:24,500 handler than a listener or a publisher. 7 00:00:24,500 --> 00:00:29,000 All we really have to do is make use of the request or the Super Test library. 8 00:00:29,000 --> 00:00:31,460 We're also going to make sure that we get our Express app. 9 00:00:31,550 --> 00:00:35,280 We're going to make a request to express app and that's pretty much it. 10 00:00:35,310 --> 00:00:43,730 So at the very top we'll import request from super test and we'll also get our Express app from up to 11 00:00:43,730 --> 00:00:44,390 directories 12 00:00:48,590 --> 00:00:50,800 so we can start to write out a couple of different test cases. 13 00:00:52,060 --> 00:00:56,380 Maybe the first test case we should write out is make sure that we throw an error if we tried to purchase 14 00:00:56,380 --> 00:01:05,550 an order that does not exist so I'll put in a description here of it returns a for a for when purchasing 15 00:01:06,300 --> 00:01:08,100 an order that does not exist 16 00:01:11,580 --> 00:01:13,710 let's write out the other test cases as well. 17 00:01:13,710 --> 00:01:18,780 So we should also handle something where we are trying to pay for the order as a different user and 18 00:01:18,780 --> 00:01:25,320 something around making sure that we cannot order or paper in order after it's been canceled so it's 19 00:01:27,700 --> 00:01:34,340 returns a 4 or 1 when purchasing an order that doesn't belong to the user 20 00:01:37,580 --> 00:01:50,100 and it returns a 400 when purchasing a canceled order. 21 00:01:50,350 --> 00:01:54,780 So now we can write out some actual implementation for each of these naturally we will start off with 22 00:01:54,780 --> 00:01:57,620 the 4 0 4 first instead of here. 23 00:01:57,840 --> 00:02:08,500 I'll put in an a wait we'll make a request to app I want to make a post request to API flash payments 24 00:02:10,880 --> 00:02:17,000 I do need to set a cookie on this thing using that global sine function because we are requiring a user 25 00:02:17,000 --> 00:02:20,000 to be authenticated to make a request to this handler. 26 00:02:20,240 --> 00:02:30,700 So we do have to call the set cookie to global dot sign in and we're going to send along some imaginary 27 00:02:30,700 --> 00:02:39,660 token for right now and an order I.D. for well nothing and this order I.D. might end up throwing some 28 00:02:39,660 --> 00:02:44,100 kind of error when we make a query with it we might get some kind of air from mongo saying hey sorry 29 00:02:44,130 --> 00:02:49,440 but this thing is not an actual Mongoose I.D. or Mongo DB I.D. So rather than throwing in a gibberish 30 00:02:49,530 --> 00:02:56,880 maggot or idea right there order I.D. let's generate a real looking I.D. using Mongoose once again the 31 00:02:56,880 --> 00:03:04,370 very top I will import Mongoose from Mongoose and then for the order I.D. we'll throw in Mongoose dot 32 00:03:04,420 --> 00:03:08,060 types object I.D. to X string 33 00:03:11,030 --> 00:03:16,590 and then after we send that request off we will expect to get back a four for and that should be at 34 00:03:18,620 --> 00:03:26,450 let's say this right away and make sure that this test succeeds back inside my terminal I still have 35 00:03:26,480 --> 00:03:31,160 all my tests running it looks like that test just passed as usual let's try to make it actually fail 36 00:03:31,970 --> 00:03:38,010 I want to send along or I'll go back over to my root handler and I'm going to remove that check to make 37 00:03:38,010 --> 00:03:44,610 sure that we actually have an order you'll notice that the other checks right here start to fail as 38 00:03:44,610 --> 00:03:45,420 well. 39 00:03:46,700 --> 00:03:51,080 It's all to combat those out temporarily. 40 00:03:51,110 --> 00:03:56,270 So now our test should fail saying that we get back a status code of 200 instead of the expected for 41 00:03:56,270 --> 00:03:59,340 a four and that is exactly what we get. 42 00:03:59,390 --> 00:04:04,050 I feel like the tests we just wrote is probably working as expected. 43 00:04:04,050 --> 00:04:06,400 All right so let's take care of the other two cases. 44 00:04:06,400 --> 00:04:09,780 So next up is the for one when purchasing an order. 45 00:04:09,780 --> 00:04:15,050 When this thing does not belong to us you might think they were going to have to create two users to 46 00:04:15,050 --> 00:04:16,560 test this out or something like that. 47 00:04:16,580 --> 00:04:17,360 We really don't. 48 00:04:17,360 --> 00:04:22,460 We really just have to create one order save into the database with a realistic looking at user I.D. 49 00:04:22,820 --> 00:04:26,830 and then just make a request and try to pay for that order as any arbitrary user. 50 00:04:27,000 --> 00:04:29,930 And we'll we will make sure that we get back it for one. 51 00:04:30,110 --> 00:04:34,020 So first things first we do have to create a real order and save it to the database. 52 00:04:34,120 --> 00:04:36,330 For that we have to make use of our order model. 53 00:04:36,350 --> 00:04:46,510 Let's import it at the very top going to imports order from up to directories models order then downside 54 00:04:46,620 --> 00:04:47,220 of our test. 55 00:04:47,250 --> 00:04:53,860 We'll do a on order is a wait or see me order not Bill. 56 00:04:53,860 --> 00:04:58,480 There we go no await statement just yet and we do have to get this thing a couple of different properties 57 00:04:58,480 --> 00:04:59,570 unfortunately. 58 00:04:59,680 --> 00:05:05,410 So we have to give it the I.D. version user I.D. price and status with the I.D. We do want this thing 59 00:05:05,410 --> 00:05:06,210 to look realistic. 60 00:05:06,220 --> 00:05:11,050 So we'll throw in a mongoose types object I.D. to hex string 61 00:05:13,830 --> 00:05:15,050 for the user I.D.. 62 00:05:15,120 --> 00:05:17,520 Well this can be any idea whatsoever. 63 00:05:17,520 --> 00:05:19,400 But let's make it look realistic. 64 00:05:19,470 --> 00:05:21,040 Just for tests sake. 65 00:05:21,090 --> 00:05:22,380 Again not important. 66 00:05:22,410 --> 00:05:28,570 We can't have the thing be gibberish if we wanted to but I'll just make it look like a real I.D. I got 67 00:05:28,570 --> 00:05:36,060 a hard coded version to be zero I will hard to code the price to 20 and then finally our status to set 68 00:05:36,060 --> 00:05:38,830 this we do need to import our order status in them. 69 00:05:39,150 --> 00:05:40,350 We'll make sure I do that at the top. 70 00:05:40,350 --> 00:05:41,280 From our common module 71 00:05:49,130 --> 00:05:57,110 then we can set the status right here as order status dot created after we create the order will then 72 00:05:57,110 --> 00:05:59,890 save it within a waits order not save. 73 00:05:59,920 --> 00:06:04,750 And now we can actually make our request and try to pay for this thing rather than writing out the entire 74 00:06:04,750 --> 00:06:05,780 request chain once again. 75 00:06:05,820 --> 00:06:08,590 I'll just copy paste some code from the previous test. 76 00:06:08,640 --> 00:06:09,960 It's going to take all that stuff right there. 77 00:06:09,960 --> 00:06:15,850 Copy it and then paste it in. 78 00:06:15,860 --> 00:06:20,740 So now instead of giving a randomly generated order idea right here I'm going to instead use the idea 79 00:06:20,750 --> 00:06:23,040 of the order that we have just saved to the database. 80 00:06:23,160 --> 00:06:32,430 I'll specify order dot I.D. and I will expect this to give us back if we're a 1 would save that. 81 00:06:32,540 --> 00:06:37,930 Take a look at our test Yep what's good as usual. 82 00:06:37,950 --> 00:06:39,440 Let's just try to make this thing fail. 83 00:06:39,480 --> 00:06:43,560 So how about rather than going and commenting outside those checks we'll try to expect a different status 84 00:06:43,560 --> 00:06:44,170 code. 85 00:06:44,300 --> 00:06:50,310 We'll expect about a 200 and that's to just confirm that we do in fact get back a 4 0 1. 86 00:06:50,560 --> 00:06:54,440 Yep looks good. 87 00:06:54,720 --> 00:06:55,560 Let's take a pause right here. 88 00:06:55,590 --> 00:06:58,170 When we come back the next video we will take care of this last test.