Let's move on to our last test.
We're going to make sure that we return it for a fourth.
If you're trying to purchase a canceled order now this test is going to be just a little bit tricky
because it implies that we're going to get past this check right here where we make sure that the person
making the request is the same person who originally created the order.
Now with this check right here effectively means for our test is that when we initially create the order
we have to stick in a user I.D. or signing user I.D. the order and then make a request with that same
user I.D. and it's actually kind of challenging right now with our current authentication setup member
to authenticate ourselves inside of our tests.
We call that global sign in function.
You might recall that that Google sign in function was defined inside of our test directory and a setup
dot t s file.
So about halfway down here is that global signing function and the first thing we do is create a user
or essentially a payload to stick into the Jason web token with a random I.D..
So we essentially need to extract that idea right there in a sign it to the user I.D. property of the
order that we are trying to purchase.
Now we could definitely try to figure out some way to maybe return or give us access to that idea right
there from the signing function but a way easier way to do this would be to just make an optional argument
to the sine function where we could provide an I.D. ahead of time if we wanted to.
And that's what we're going to do.
Way easier and more straightforward.
So I'm going to say that the sine function is going to receive an optional argument of I.D. wrong place
for the question mark.
So I'll say that I.D. is an optional argument.
That's what the question mark right there refers to.
And it will be of type String then for payload right here rather than always generating a random I.D.
we will say if we provided an I.D. then use the I.D..
Otherwise generate a new one.
And so that's how we would express that.
We can say I.D. or so if I.D. is not defined then we will execute the case right here.
And if he is prove what is provided we will not execute the or a case and we'll just assign the idea
that was provided to the I.D. property that's pretty much it.
Now back inside of our test file for our test right here I'm going to first create a new user I.D. So
once again there'll be a mongoose types object I.D. to hex string.
Well then use that to build up a new order and we will assign this as the user I.D. for that order itself
rather than building an entire order from scratch right here.
I'll just copy paste some code we had written on the previous test is all that stuff is on a percent
reusable copy it that entire block right there and then paste it in underneath.
And now rather than randomly generating the user I.D. All instead provide the user I.D. that we had
just generated a moment ago now we can make our actual request to purchase this thing when we make the
request.
We will make sure that we provide the user I.D. that we have just generated to that sign and function.
And so it will be a await request app.
We'll make a post request to API payments and we will set the cookie to global sign in and then very
critically.
Right here to sign and function we'll provide that user I.D. that we have previously generated the user
I.D. like so and you'll notice I'm getting an error here and it's probably because typescript is just
being a little bit angry.
I'm gonna save this and then reload.
I might have just not saved that setup test file.
Now I think that made it work.
No it didn't much like that set up test file again nuts.
It's there.
Oh you know what it is.
We need to update our interface as well.
My mistake.
Back up here here's our interface.
You have to say that this thing is going to take an optional I.D. as a string.
That's better
get much better.
Now everything's working as expected.
Well now of course before we actually make this request one other update we have to make on the status
property for the order when we build it.
We also have to update the order status to canceled because that's what we're really trying to test
out here.
We'll change order status to canceled like so now we can actually finish up the test itself.
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
to get back a status of 400
is let's say that and the other tests are doing back over here.
Looks like it's all working as expected as usual.
Let's make this thing fail about this time to make it fail.
I will just never save the order.
That should give us a forum for instead of the expected 400 and yet there we go again.
So that is it.
That is some initial testing.
So now that we've got this order kind of prepped and ready to be purchased we're now going to take a
look at how to use the stripe api library just a little bit more in-depth and then eventually integrate
it into our into our project.