WEBVTT

00:00.170 --> 00:02.350
Around creating a reservation.

00:02.360 --> 00:09.620
I'll create a new spec called Reservations dot end to end dot spec dot ts.

00:09.620 --> 00:13.730
And let's go ahead and actually rename our health check file.

00:13.730 --> 00:17.420
We can remove the dot app here.

00:17.420 --> 00:24.200
So we'll open up a new describe block and describe the reservations and then open up a function where

00:24.200 --> 00:25.610
we'll test reservations.

00:25.610 --> 00:29.390
Our first test will be to create.

00:29.390 --> 00:34.850
So we'll call this create and open up a function where we'll now we'll create a new reservation.

00:34.850 --> 00:40.460
So of course, before we actually create the reservation, we will need to authenticate with our authentication

00:40.460 --> 00:41.240
service.

00:41.240 --> 00:48.560
So let's set up a new before all block, which will contain an async function.

00:48.560 --> 00:54.200
And inside of here we're going to do the setup work for our test where we'll create a new user and get

00:54.200 --> 00:56.630
an authorization header for that user.

00:56.630 --> 00:58.880
After some refactoring to our auth service.

00:58.880 --> 01:05.430
First, we will call, await, Fetch and reach out to our auth service.

01:05.430 --> 01:12.210
Listening on Port 3001, we will provide an options object to fetch where we'll specify the method and

01:12.210 --> 01:18.960
make sure this is a post request and we'll specify a body where we'll call json.stringify, provide

01:18.960 --> 01:25.500
a new object and here we can provide our email, call it sleep or Nest app at gmail.com.

01:25.500 --> 01:27.480
And the password?

01:27.510 --> 01:30.870
I will give it a strong password as usual.

01:30.870 --> 01:37.530
Let's go ahead and actually take this object out and declare it in a new variable that we'll call user

01:37.740 --> 01:39.840
and paste it here.

01:40.380 --> 01:42.720
So now let's json.stringify the user.

01:42.720 --> 01:46.350
And now I want to call our auth service with our credentials to log in.

01:46.350 --> 01:53.940
And instead of using a set cookie to set a JWT cookie, I want to get the JWT sent back directly from

01:53.940 --> 01:58.800
the login call and then we can provide it as a header to our authentication service.

01:58.800 --> 02:04.980
So to accomplish this, let's firstly go to our auth service where we'll firstly go into our auth controller

02:04.980 --> 02:13.530
and instead of sending the user back in our login response, we'll actually get the JWT back directly

02:13.530 --> 02:18.120
from this login call and then we will respond with that JWT.

02:18.810 --> 02:21.420
So let's go to the auth service dot login.

02:21.420 --> 02:28.080
And in addition to setting the response cookie, we will now return the token back to the controller.

02:28.080 --> 02:32.610
So now that we returned the token back to the controller, we need to make sure that we actually end

02:32.610 --> 02:35.790
up using it in our JWT strategy.

02:35.790 --> 02:47.160
So here we'll also expand our JWT from request to accept a request dot headers dot authentication header

02:47.160 --> 02:50.460
so that we can pass a request header.

02:50.670 --> 02:56.460
But this will be useful if we operate our microservices on different domains because the cookies will

02:56.460 --> 02:58.110
not carry over in that case.

02:58.110 --> 03:04.710
So one more change we're going to have to make is to our JWT auth guard in our common folder where we

03:04.710 --> 03:09.180
actually send the request to the auth service to authenticate the JWT.

03:09.510 --> 03:14.940
You can see right now we're pulling the JWT directly off of the cookies only.

03:14.940 --> 03:21.810
Let's go ahead and add a new section to this variable that will be if we don't find the beat on the

03:21.840 --> 03:22.530
cookies.

03:22.560 --> 03:30.270
We'll also switch to HTTP, get the request, and this time we'll look for the dot headers dot authentication

03:30.270 --> 03:35.360
so that we pass the cookies or the headers here.

03:35.460 --> 03:41.340
Only make sure we change the headers authentication to a lowercase a here as all of our headers will

03:41.340 --> 03:43.020
start with a lowercase letter.

03:43.020 --> 03:50.370
Now we want our changes to be pushed up and built by Codebuild so that we can correctly test out our

03:50.370 --> 03:51.600
new end to end tests.

03:51.600 --> 04:00.600
So I'll go ahead and make sure that we go ahead and commit all this code and then push them up so that

04:00.600 --> 04:03.630
the code build will start a new build.

04:03.630 --> 04:05.070
So go ahead and push them up.

04:05.070 --> 04:10.590
So now back in our test, let's declare a new const here called Response and we'll call it Await Fetch.

04:10.620 --> 04:17.790
We'll call it HTTP auth service again and we will pass in the same method of post.

04:17.820 --> 04:24.660
Now make sure in our first fetch we're calling slash users root to actually create the user.

04:24.660 --> 04:29.070
And then in the second call we will call slash auth slash login.

04:29.070 --> 04:35.520
Now we'll provide the same body, which is our user and our credentials that we know exists at this

04:35.520 --> 04:37.830
point and we get the JWT back.

04:37.830 --> 04:46.170
So to actually parse the JWT, we'll call await response dot text because we returned JWT as plain text

04:46.170 --> 04:52.500
so we can simply console dot log the JWT out for now and in our first test block.

04:52.500 --> 04:57.900
Let's just expect true to be truthy so we can test this out.

04:57.900 --> 04:59.580
Let's proceed back in the terminal.

04:59.600 --> 05:03.520
By rerunning our test suite by running Docker compose up and end.

05:03.560 --> 05:09.800
So lastly, in order for this request to be sent properly, we also need to pass a header section where

05:09.800 --> 05:15.460
we're going to supply a content type of application slash JSON.

05:15.470 --> 05:21.130
So we'll copy this headers and also apply it to our request to log in to the system.

05:21.140 --> 05:23.930
Now we'll go ahead and rerun our tests by running Docker.

05:23.930 --> 05:25.460
Compose up end to end.

05:25.490 --> 05:32.390
Now we can see our test has passed and our console log statement, we can see the JWT access token supplied

05:32.390 --> 05:33.530
here in our test.

05:33.530 --> 05:42.950
So let's set a new variable in the top section of our test called JWT, which will be a string.

05:42.950 --> 05:48.230
And then we will set JWT down here so that the JWT is defined.

05:48.230 --> 05:53.120
So our setup work will be a test in of itself to show that the auth service is working properly.

05:53.120 --> 05:56.540
But now we'll actually test the creation of a new reservation.

05:56.540 --> 06:01.900
And to do this we'll create a new variable called response and set it equal to await Fetch.

06:01.900 --> 06:10.600
And now we'll call it HTTP reservations at Port 3000 and make sure this is an async function and I will

06:10.600 --> 06:17.500
supply our options object where we have the method of post, we will have a headers section that we

06:17.500 --> 06:23.230
can copy from a previous request and then the body which will be json.stringify.

06:23.230 --> 06:27.010
And now we need to provide the body of the reservations request.

06:27.040 --> 06:33.490
Now to do this we can actually just copy one of the requests we already have inside of Postman and paste

06:33.490 --> 06:34.120
this in.

06:38.220 --> 06:41.340
Just make sure to remove the outer object.

06:41.370 --> 06:41.730
Of course.

06:41.730 --> 06:46.860
Make sure we provide the authentication header which we will set equal to the JWT.

06:47.190 --> 06:57.060
And now at this point we can make a just assertion that we expect the response dot okay to be truthy

06:57.060 --> 07:05.250
and then we can parse the reservation response by calling await response dot JSON and let's log out

07:05.250 --> 07:08.520
the reservation that we get back from this request.

07:08.550 --> 07:15.240
Now of course, make sure we add a slash reservations in our URL here so that we actually post to the

07:15.240 --> 07:16.320
right spot.

07:16.320 --> 07:21.330
So after our changes to our app have been completed and cloud build, we'll go ahead and tell Docker

07:21.330 --> 07:25.010
compose to pull our images by running Docker, compose pull.

07:25.020 --> 07:29.130
Now we can rerun our tests by running Docker, compose up end to end.

07:29.130 --> 07:34.360
So you can see this reservation test has passed and we are getting this reservation back.

07:34.380 --> 07:39.040
Okay, so I'll get rid of our console log and now we can actually test out our get functionality as

07:39.040 --> 07:45.700
well to get the newly created reservation to make sure that the get functionality is working and that

07:45.700 --> 07:48.220
the reservation was actually persisted.

07:48.220 --> 07:54.790
So firstly, I'll rename our first response to response, create and update all of our variables to

07:54.790 --> 08:02.380
use, response, create, and then we'll create a new variable called response Get and call await Fetch

08:02.410 --> 08:11.500
HTTP reservations at Port 3000, slash reservations and change this to a template literal because if

08:11.500 --> 08:18.460
we recall in our reservations controller we can get a reservation by its underscore ID, which is exactly

08:18.460 --> 08:19.780
what we'll do here.

08:19.780 --> 08:27.490
We will actually use the newly created reservation response dot underscore ID to get the reservation

08:27.490 --> 08:34.060
by ID and then we'll rename reservation created reservation because now I want to create a new const

08:34.060 --> 08:42.280
called Reservation and set it equal to zero eight response, get dot JSON and now we can actually make

08:42.280 --> 08:49.570
an assertion that the created reservation from our CREATE request actually will to equal our reservation

08:49.570 --> 08:50.830
from our get request.

08:50.860 --> 08:58.000
Make sure we change the reservation variable here to created reservation dot underscore ID and then

08:58.030 --> 09:04.840
don't forget we need to provide an options object so that we can supply headers and set authentication

09:05.890 --> 09:07.210
to JWT.

09:07.870 --> 09:13.840
So after rerunning our test, you can see that our reservation test passed and our assertion is exactly

09:13.840 --> 09:19.090
correct in this test because the created reservation now equals the reservation from get.

09:19.090 --> 09:20.410
So this is great.

09:20.440 --> 09:25.150
Let's update the name of the test here to create and get because that's what we're testing.

09:25.150 --> 09:31.060
And I'll pull this functionality to create the reservation out into a.

09:31.780 --> 09:34.840
New function out here just to make it a bit cleaner.

09:34.840 --> 09:44.260
So I'll call this crate reservation, which will be an async function and it'll go ahead and just return

09:44.260 --> 09:46.930
the created reservation.

09:46.930 --> 09:55.120
And now inside of here we can call const created reservation is equal to await create reservation.

09:55.120 --> 09:58.290
And now this is just a bit cleaner and easier to read.

09:58.300 --> 10:03.340
Okay, so now we have a basic suite of end to end tests that will give us confidence that our production

10:03.340 --> 10:09.610
images one all start up because of our health check and also that our basic reservation functionality

10:09.610 --> 10:11.320
is functioning end to end.

10:11.350 --> 10:17.200
We're able to create a reservation and get that reservation and both of the responses match.

10:17.230 --> 10:18.040
Exactly.

10:18.040 --> 10:18.400
Okay.

10:18.400 --> 10:24.430
So back in our root Package.json, I want to update our test end to end command here to reflect our

10:24.430 --> 10:25.900
new end to end tests.

10:25.900 --> 10:32.060
So we'll change this command to Docker compose and then we'll specify a new argument called project

10:32.060 --> 10:37.460
directory, which essentially just allows us to choose where to run these commands from.

10:37.460 --> 10:42.860
In our case, we know that's the end to end folder and then we just call up end to end.

10:42.890 --> 10:49.820
So now all we have to do to execute our tests is go to the root of our project and run yarn test end

10:49.820 --> 10:52.460
to end and it'll kick off our end to end test.
