1 00:00:01,060 --> 00:00:02,320 Let's move on to our last test. 2 00:00:02,320 --> 00:00:04,540 We're going to make sure that we return it for a fourth. 3 00:00:04,540 --> 00:00:08,800 If you're trying to purchase a canceled order now this test is going to be just a little bit tricky 4 00:00:08,830 --> 00:00:12,850 because it implies that we're going to get past this check right here where we make sure that the person 5 00:00:12,850 --> 00:00:16,570 making the request is the same person who originally created the order. 6 00:00:16,570 --> 00:00:21,550 Now with this check right here effectively means for our test is that when we initially create the order 7 00:00:21,760 --> 00:00:27,190 we have to stick in a user I.D. or signing user I.D. the order and then make a request with that same 8 00:00:27,280 --> 00:00:32,860 user I.D. and it's actually kind of challenging right now with our current authentication setup member 9 00:00:32,890 --> 00:00:34,770 to authenticate ourselves inside of our tests. 10 00:00:34,780 --> 00:00:37,050 We call that global sign in function. 11 00:00:37,090 --> 00:00:41,320 You might recall that that Google sign in function was defined inside of our test directory and a setup 12 00:00:41,320 --> 00:00:43,060 dot t s file. 13 00:00:43,060 --> 00:00:50,100 So about halfway down here is that global signing function and the first thing we do is create a user 14 00:00:50,130 --> 00:00:54,530 or essentially a payload to stick into the Jason web token with a random I.D.. 15 00:00:54,530 --> 00:00:59,670 So we essentially need to extract that idea right there in a sign it to the user I.D. property of the 16 00:00:59,670 --> 00:01:03,030 order that we are trying to purchase. 17 00:01:03,070 --> 00:01:07,870 Now we could definitely try to figure out some way to maybe return or give us access to that idea right 18 00:01:07,870 --> 00:01:13,420 there from the signing function but a way easier way to do this would be to just make an optional argument 19 00:01:13,420 --> 00:01:17,890 to the sine function where we could provide an I.D. ahead of time if we wanted to. 20 00:01:17,890 --> 00:01:18,850 And that's what we're going to do. 21 00:01:18,850 --> 00:01:21,000 Way easier and more straightforward. 22 00:01:21,100 --> 00:01:28,420 So I'm going to say that the sine function is going to receive an optional argument of I.D. wrong place 23 00:01:28,420 --> 00:01:30,140 for the question mark. 24 00:01:30,220 --> 00:01:32,190 So I'll say that I.D. is an optional argument. 25 00:01:32,200 --> 00:01:34,710 That's what the question mark right there refers to. 26 00:01:34,960 --> 00:01:41,090 And it will be of type String then for payload right here rather than always generating a random I.D. 27 00:01:42,130 --> 00:01:46,930 we will say if we provided an I.D. then use the I.D.. 28 00:01:47,240 --> 00:01:51,050 Otherwise generate a new one. 29 00:01:51,080 --> 00:01:52,190 And so that's how we would express that. 30 00:01:52,190 --> 00:01:57,950 We can say I.D. or so if I.D. is not defined then we will execute the case right here. 31 00:01:57,950 --> 00:02:03,410 And if he is prove what is provided we will not execute the or a case and we'll just assign the idea 32 00:02:03,440 --> 00:02:08,620 that was provided to the I.D. property that's pretty much it. 33 00:02:08,640 --> 00:02:19,430 Now back inside of our test file for our test right here I'm going to first create a new user I.D. So 34 00:02:19,450 --> 00:02:26,180 once again there'll be a mongoose types object I.D. to hex string. 35 00:02:26,300 --> 00:02:30,950 Well then use that to build up a new order and we will assign this as the user I.D. for that order itself 36 00:02:31,670 --> 00:02:34,100 rather than building an entire order from scratch right here. 37 00:02:34,210 --> 00:02:38,570 I'll just copy paste some code we had written on the previous test is all that stuff is on a percent 38 00:02:38,570 --> 00:02:44,380 reusable copy it that entire block right there and then paste it in underneath. 39 00:02:44,390 --> 00:02:50,300 And now rather than randomly generating the user I.D. All instead provide the user I.D. that we had 40 00:02:50,300 --> 00:02:58,430 just generated a moment ago now we can make our actual request to purchase this thing when we make the 41 00:02:58,430 --> 00:02:58,880 request. 42 00:02:58,880 --> 00:03:04,190 We will make sure that we provide the user I.D. that we have just generated to that sign and function. 43 00:03:04,480 --> 00:03:09,170 And so it will be a await request app. 44 00:03:09,170 --> 00:03:17,250 We'll make a post request to API payments and we will set the cookie to global sign in and then very 45 00:03:17,250 --> 00:03:17,730 critically. 46 00:03:17,760 --> 00:03:22,740 Right here to sign and function we'll provide that user I.D. that we have previously generated the user 47 00:03:22,740 --> 00:03:29,770 I.D. like so and you'll notice I'm getting an error here and it's probably because typescript is just 48 00:03:29,770 --> 00:03:31,010 being a little bit angry. 49 00:03:31,000 --> 00:03:32,790 I'm gonna save this and then reload. 50 00:03:32,890 --> 00:03:36,010 I might have just not saved that setup test file. 51 00:03:36,170 --> 00:03:37,530 Now I think that made it work. 52 00:03:37,530 --> 00:03:42,160 No it didn't much like that set up test file again nuts. 53 00:03:42,200 --> 00:03:42,640 It's there. 54 00:03:42,670 --> 00:03:43,850 Oh you know what it is. 55 00:03:43,850 --> 00:03:46,000 We need to update our interface as well. 56 00:03:46,010 --> 00:03:46,700 My mistake. 57 00:03:46,760 --> 00:03:48,710 Back up here here's our interface. 58 00:03:48,710 --> 00:03:52,880 You have to say that this thing is going to take an optional I.D. as a string. 59 00:03:52,880 --> 00:03:53,360 That's better 60 00:03:56,990 --> 00:03:58,020 get much better. 61 00:03:58,020 --> 00:04:02,310 Now everything's working as expected. 62 00:04:02,540 --> 00:04:08,090 Well now of course before we actually make this request one other update we have to make on the status 63 00:04:08,090 --> 00:04:09,680 property for the order when we build it. 64 00:04:09,680 --> 00:04:13,790 We also have to update the order status to canceled because that's what we're really trying to test 65 00:04:13,790 --> 00:04:14,620 out here. 66 00:04:14,840 --> 00:04:21,690 We'll change order status to canceled like so now we can actually finish up the test itself. 67 00:04:21,770 --> 00:04:31,450 So I'm going to send along an order I.D. of order dot I.D. and a token of whatever and then I will expect 68 00:04:31,450 --> 00:04:34,690 to get back a status of 400 69 00:04:37,590 --> 00:04:40,960 is let's say that and the other tests are doing back over here. 70 00:04:40,960 --> 00:04:43,580 Looks like it's all working as expected as usual. 71 00:04:43,600 --> 00:04:46,360 Let's make this thing fail about this time to make it fail. 72 00:04:46,360 --> 00:04:48,160 I will just never save the order. 73 00:04:48,400 --> 00:04:54,990 That should give us a forum for instead of the expected 400 and yet there we go again. 74 00:04:54,990 --> 00:04:55,950 So that is it. 75 00:04:55,950 --> 00:04:57,620 That is some initial testing. 76 00:04:57,840 --> 00:05:02,340 So now that we've got this order kind of prepped and ready to be purchased we're now going to take a 77 00:05:02,340 --> 00:05:07,970 look at how to use the stripe api library just a little bit more in-depth and then eventually integrate 78 00:05:07,970 --> 00:05:09,630 it into our into our project.