1 00:00:01,370 --> 00:00:03,050 I'm inside my Setup Dot T S file. 2 00:00:03,110 --> 00:00:06,000 I'm at the very bottom and I'm taking a look at that sign in function. 3 00:00:06,110 --> 00:00:09,980 We had originally created this function to be used back inside of our auto service. 4 00:00:10,010 --> 00:00:15,740 The goal of the function was to come up with some email and password then make a request to sign up 5 00:00:15,740 --> 00:00:17,280 with that email and password. 6 00:00:17,300 --> 00:00:21,790 We then took the cookie out of the response and returned the cookie from the function. 7 00:00:21,800 --> 00:00:26,360 Now this function right here were just fine in cyber auto service because the auto service had a root 8 00:00:26,360 --> 00:00:32,300 handler for signing up unfortunately the same thing is not going to work inside of our tickets service. 9 00:00:32,430 --> 00:00:36,900 The ticket service at no point time is ever going to have a route like this where we're going to try 10 00:00:36,900 --> 00:00:39,290 to create some kind of account for the user. 11 00:00:39,930 --> 00:00:43,710 So unfortunately we're not going to be able to use this kind of approach right here to somehow get a 12 00:00:43,710 --> 00:00:49,330 cookie you might be thinking Hey during our test suite or for the tickets test suite let's just make 13 00:00:49,330 --> 00:00:53,450 a request over to the off service to sign up for an account and we can get a cookie that way. 14 00:00:53,500 --> 00:00:58,420 Well we definitely do not want to set up any kind of inter service dependency while we are running tests 15 00:00:58,560 --> 00:01:02,770 in the words when we're running tests for the ticket service we should not try to reach out and make 16 00:01:02,770 --> 00:01:08,100 requests to anything else inside our application whatsoever under any circumstance. 17 00:01:08,200 --> 00:01:11,510 All tests we run side of year should be 100 percent self-contained. 18 00:01:11,800 --> 00:01:17,680 So in short the code we have right here is just not going to work to somehow get a cookie but we still 19 00:01:17,680 --> 00:01:22,120 need to have some ability to kind of fake being signed in inside of our test environment while we're 20 00:01:22,120 --> 00:01:23,410 running these tests. 21 00:01:23,410 --> 00:01:25,260 So how are we gonna do that. 22 00:01:25,270 --> 00:01:30,730 Well remember this overall function the whole goal of it is to really just return a cookie and inside 23 00:01:30,730 --> 00:01:38,100 that cookie is the Jason Webb token so then mind I won't go back over to the browser I want to try to 24 00:01:38,100 --> 00:01:40,180 sign up for our application once again. 25 00:01:40,310 --> 00:01:45,270 I want to take a look at that cookie that gets returned to us when we successfully sign up. 26 00:01:45,270 --> 00:01:50,940 So back inside my browser I'm going to navigate to our application at ticketing dot Dev. 27 00:01:50,940 --> 00:01:56,800 You'll notice I am not currently assigned in I'm going to go in a tent to sign up and before I sign 28 00:01:56,800 --> 00:02:03,690 up I'm gonna open up my network request tab and then go to enter in some random email address and a 29 00:02:03,690 --> 00:02:07,430 password after I sign up. 30 00:02:07,430 --> 00:02:11,660 I can see these sign up requests and I see two requests to current user. 31 00:02:11,780 --> 00:02:13,610 We do not really need to make two requests. 32 00:02:13,610 --> 00:02:18,640 That is a bug that we're gonna fix up all a bit later on when we go back to the react application again. 33 00:02:18,740 --> 00:02:23,450 But what's more relevant I want to take a look at those current user requests so I'll click on either 34 00:02:23,450 --> 00:02:30,260 one I'm gonna go to the headers tab and I'll take a look at the request headers under request headers 35 00:02:30,320 --> 00:02:35,830 is the cookie that is being sent from my browser up to the server to remember this thing right here. 36 00:02:35,840 --> 00:02:38,990 That is what actually identifies us to the server. 37 00:02:38,990 --> 00:02:43,820 If we could just take that information right there and send it to the server in any arbitrary request 38 00:02:44,090 --> 00:02:46,680 we will be considered to be authenticated. 39 00:02:46,760 --> 00:02:52,610 So ultimately that cookie that we just on the browser is what we want to return from this function. 40 00:02:52,610 --> 00:02:56,680 So rather than trying to obtain a real cookie by going through the sign up process. 41 00:02:56,840 --> 00:03:02,060 What if we could somehow fabricate a cookie or just make one on our own. 42 00:03:02,060 --> 00:03:04,500 In this function and then return it. 43 00:03:04,520 --> 00:03:06,320 Well that's exactly what we're gonna do. 44 00:03:06,410 --> 00:03:10,150 We're going to build our own cookie up from scratch inside of here. 45 00:03:10,190 --> 00:03:15,290 We're then going to return it from this function and then we can use that cookie to sign into our application 46 00:03:15,530 --> 00:03:22,230 while we are running our tests for tickets and this process is going to be just a little bit complicated. 47 00:03:22,330 --> 00:03:26,550 But to be honest to you it's going to build upon everything that we've already done around authentication. 48 00:03:26,580 --> 00:03:30,430 So this is really all the same stuff we're just doing some of these steps by hand. 49 00:03:30,430 --> 00:03:37,690 Instead the first thing I want to do is take the actual cookie data inside of here and just give you 50 00:03:37,690 --> 00:03:40,670 a quick reminder of what's actually inside of here. 51 00:03:40,780 --> 00:03:47,280 So I'm going to copy everything after express colon sesh equals so it will probably start with an E. 52 00:03:47,280 --> 00:03:54,180 why I can take everything to the end I'm going to copy that and then we're gonna go to a Web site that 53 00:03:54,180 --> 00:04:00,840 we took a look at a while ago Base64 decode dot org I'm going gonna paste that cookie information side 54 00:04:00,840 --> 00:04:09,240 of here and then decode it so the cookie data is encoded in base64 when we decode it and get back some 55 00:04:09,240 --> 00:04:13,020 actual plain text or we get back is a chase on object. 56 00:04:13,050 --> 00:04:15,000 So this is not a javascript object right here. 57 00:04:15,000 --> 00:04:16,930 This is Jason data. 58 00:04:17,070 --> 00:04:24,690 It has the key of JWT and then the value for that is our actual Jason Webb token so with all this in 59 00:04:24,690 --> 00:04:27,770 mind here's what we have to do back inside of signing. 60 00:04:27,810 --> 00:04:32,580 Let's write out some comments just to guide ourselves and delete everything inside there. 61 00:04:32,700 --> 00:04:36,060 I'll then replace it with a couple of comments. 62 00:04:36,080 --> 00:04:43,190 The first thing I'm going to do is build a data on a Web token payload so remember for us the payload 63 00:04:43,520 --> 00:04:49,310 is going to have or be an object that has an I.D. an email and was there anything else inside there. 64 00:04:49,310 --> 00:04:53,660 I think there's technically also the issue that time but really for you and I we really just care about 65 00:04:53,660 --> 00:05:00,590 the I.D. an email it's all we really have to do is build up that object that's pretty much it after 66 00:05:00,590 --> 00:05:08,190 that we're going to create the Jason Webb token we're gonna create the Jason Webb token by running that 67 00:05:08,190 --> 00:05:13,530 same kind of G2 T A SINE FUNCTION that we had put together back when we were doing our authentication 68 00:05:13,530 --> 00:05:15,630 stuff back in the off middleware. 69 00:05:15,810 --> 00:05:21,590 Remember when we created Jason Webb token we have to include or make use of a Jason Webb token key and 70 00:05:21,600 --> 00:05:24,780 we already defined that up here inside of our before all statements 71 00:05:28,490 --> 00:05:35,270 were then going to create or can build up this session objects that really just means take the Jason 72 00:05:35,270 --> 00:05:41,480 Webb token We just generated and stick it into an object where it has a key of J.W. T and then a value 73 00:05:41,480 --> 00:05:44,080 of the Jason one token we just built that's all that really means. 74 00:05:44,600 --> 00:05:54,210 So pretty much just build an object looks like JT t my JWT we're then going to turn that session into 75 00:05:54,300 --> 00:06:04,750 Jason and then after that we're going to take Jason and encode it as base 64 as we just saw because 76 00:06:04,750 --> 00:06:09,110 we had to run that thing through the basics of our decode tool when our data is actually set inside 77 00:06:09,110 --> 00:06:09,790 the cookie. 78 00:06:09,790 --> 00:06:14,290 The cookie session Middleware is going to encode it as base64 and that's why it turns up as that kind 79 00:06:14,290 --> 00:06:16,840 of long string as characters. 80 00:06:16,930 --> 00:06:21,970 And then finally we're going to return a String. 81 00:06:22,190 --> 00:06:26,140 That's the cookie with the encoded data. 82 00:06:26,190 --> 00:06:28,010 But what I mean by this right here. 83 00:06:28,040 --> 00:06:32,170 Remember in order to actually send this cookie along here's the cookie right here. 84 00:06:32,270 --> 00:06:37,520 We have to take the cookie data or the real session data itself and we're gonna set it on this key right 85 00:06:37,520 --> 00:06:39,910 here of express coal and sash equals. 86 00:06:40,190 --> 00:06:44,810 And then that's going to be encoded cookie data so basically just build up a string that looks like 87 00:06:44,810 --> 00:06:46,690 that right there OK. 88 00:06:46,720 --> 00:06:49,010 So we definitely have our steps laid out for us here. 89 00:06:49,030 --> 00:06:53,140 Now I know a lot of stuff might seem a little bit daunting but to be honest with you the code for this 90 00:06:53,170 --> 00:06:56,630 is gonna be super straightforward really super straightforward. 91 00:06:56,650 --> 00:06:59,740 It's me basically exactly each step you see right here. 92 00:06:59,890 --> 00:07:00,990 Each in turn. 93 00:07:01,240 --> 00:07:05,290 And as soon as we put this thing together we're gonna have a foolproof way of simulating ourselves being 94 00:07:05,290 --> 00:07:07,540 authenticated in the test environment. 95 00:07:07,600 --> 00:07:08,370 Quick pause here. 96 00:07:08,410 --> 00:07:09,600 Build this up in just a moment.