1 00:00:00,860 --> 00:00:04,910 I would really like to write out one or more two quick tests around our sign up Route handler at this 2 00:00:04,910 --> 00:00:09,680 point we've really only been making sure that the responses that come back are of an appropriate status 3 00:00:09,680 --> 00:00:10,400 code. 4 00:00:10,400 --> 00:00:15,940 So in this case we're gonna take a look at a slightly more in depth test just slightly so in the side 5 00:00:15,940 --> 00:00:19,870 of our route handler as a quick reminder we take a look at the email we make sure the email is not in 6 00:00:19,870 --> 00:00:20,610 use. 7 00:00:20,650 --> 00:00:23,100 We then create and save a user. 8 00:00:23,230 --> 00:00:27,100 We then generate our Jason web token and then we store it on that session object. 9 00:00:27,100 --> 00:00:31,440 Remember that such an object is going to be turned into a string by Cookie session. 10 00:00:31,580 --> 00:00:37,210 A cookie session Middleware is then going to attempt to send this cookie back over to the user's browser 11 00:00:37,210 --> 00:00:43,210 inside the response to be really precise on what happens there in order to send that information back. 12 00:00:43,210 --> 00:00:46,900 It cookie session Middleware is going to set a header on the response. 13 00:00:46,900 --> 00:00:52,890 This header has a name of set dash cookie so to make sure that we actually generate a J somewhere token 14 00:00:52,920 --> 00:00:54,710 and send it back and said that response. 15 00:00:54,720 --> 00:01:02,070 Let's make sure that after signing up successfully a response has a header of set dash cookie we could 16 00:01:02,070 --> 00:01:05,350 always also try to inspect that cookie and make sure as adjacent a token. 17 00:01:05,370 --> 00:01:06,690 Well let's start small right now. 18 00:01:06,720 --> 00:01:09,700 Let's just make sure that the response has that header. 19 00:01:09,950 --> 00:01:10,220 OK. 20 00:01:10,250 --> 00:01:13,170 So to write out this test I'm gonna go back or add my test file. 21 00:01:13,310 --> 00:01:18,660 I'm gonna put together another block at the very bottom and I'll say something like it sets a cookie 22 00:01:19,470 --> 00:01:21,780 after successful sign up 23 00:01:26,090 --> 00:01:30,950 then inside of here we're going to make another successful sign up request rather than writing out from 24 00:01:30,950 --> 00:01:33,950 scratch I'll just copy paste the one we did in the last test. 25 00:01:34,010 --> 00:01:41,330 Make sure you copy paste the one that has a status code of 2 a 1 not the 400 I'll paste I didn't like 26 00:01:41,330 --> 00:01:47,950 some so now after making this request we need to somehow inspect the response that comes back from it. 27 00:01:48,190 --> 00:01:52,420 It turns out that when we make a request for a super test this entire recall right here returns the 28 00:01:52,420 --> 00:01:59,290 response so we can say response like so and that right there is our response object the same kind of 29 00:01:59,290 --> 00:02:04,480 response object that we get access to inside of our request handlers like that one right there so we 30 00:02:04,480 --> 00:02:08,980 can take a look at that response and try to inspect the data that we're trying to send back inside of 31 00:02:08,980 --> 00:02:11,170 it or also inspect the headers. 32 00:02:11,170 --> 00:02:21,090 Of course we care about the headers in this case so we're gonna write out expect response dot get so 33 00:02:21,100 --> 00:02:25,240 get right here is a method that is built into the response that allows us to look up any of the headers 34 00:02:25,240 --> 00:02:31,120 that have been set in the response the header that we were going to try to look up is set dash cookie 35 00:02:31,180 --> 00:02:37,690 like so make sure use the capital letters as I did so we're going to attempt to pull out that cookie 36 00:02:38,620 --> 00:02:48,390 and then we will expect that it is defined so we'll write out not to be defined like so OK so let's 37 00:02:48,390 --> 00:02:49,590 say this and run this test. 38 00:02:49,590 --> 00:02:51,060 Now I want you to know right away. 39 00:02:51,060 --> 00:02:52,430 This test is going to fail. 40 00:02:52,470 --> 00:02:57,310 But nonetheless let's run it anyways if I flip back over. 41 00:02:57,430 --> 00:03:03,190 Yep I see as matter of fact we got a value of undefined when we try to get that set cookie header which 42 00:03:03,190 --> 00:03:09,220 means that there is no set cookie header inside the response so it looks like our test is failing entirely 43 00:03:09,520 --> 00:03:13,990 even though we know for a fact that we've been able to sign up using this request handler. 44 00:03:14,440 --> 00:03:16,240 So what's going on right now. 45 00:03:16,240 --> 00:03:21,040 Well this is a very very small technical issue and this is why I really want to highlight it specifically 46 00:03:21,040 --> 00:03:26,580 in a test around checking to see if that cookie exists as a reminder back inside of our app dot TSA 47 00:03:26,660 --> 00:03:31,990 file which is where we create our Express app and wire up a bunch of middleware when we wired up that 48 00:03:31,990 --> 00:03:33,090 cookie session library. 49 00:03:33,100 --> 00:03:39,700 We said secure true right there secure true means that cookies are only going to be shared when someone 50 00:03:39,700 --> 00:03:45,510 is making a request to our server over an HTC CPS connection when we make use of Super Test. 51 00:03:45,520 --> 00:03:48,480 We are not making an H2 G.P.S. connection. 52 00:03:48,550 --> 00:03:51,700 We are making plain HDP requests. 53 00:03:51,760 --> 00:03:56,920 Cookie session sees that and says oh I don't care that I'm in a test environment I'm only sending cookies 54 00:03:57,010 --> 00:03:58,810 if this is a secure request. 55 00:03:58,840 --> 00:04:05,230 In this case it's not we're not making a secure request in the test environment so we could either figure 56 00:04:05,230 --> 00:04:11,410 out how to make a secure request or alternatively what would be way easier to do and way more time effective 57 00:04:12,010 --> 00:04:17,680 we can decide to change this secure property to False if we are in a test environment. 58 00:04:17,680 --> 00:04:22,030 So if we are running our test through just we're going to flip the value of this thing to false and 59 00:04:22,030 --> 00:04:29,060 that will allow cookie session to set that cookie even if the request is not secure to do so we'll take 60 00:04:29,060 --> 00:04:35,570 a look at another environment variable we're gonna put in here a process in be node underscore EMV not 61 00:04:35,570 --> 00:04:41,700 equal to test so whenever just runs our tests at the terminal that's going to set this node environment 62 00:04:41,700 --> 00:04:44,170 variable equal to the string test. 63 00:04:44,210 --> 00:04:49,140 So we're saying that if equals is equal to tests give us false otherwise if we're in any other kind 64 00:04:49,140 --> 00:04:56,460 of environment be at development or production then set secure to true let's say this now look back 65 00:04:56,460 --> 00:05:02,420 over our terminal and now we're gonna see that test pass awesome so again I really just want to highlight 66 00:05:02,420 --> 00:05:06,530 the fact that we needed to add in this little change right here to get all of our authentication stuff 67 00:05:06,530 --> 00:05:13,330 working in the test environment all right now as I mentioned there are a ton of different tests we could 68 00:05:13,350 --> 00:05:18,310 run right out around our sign up request handler but I think that what we've put together so far is 69 00:05:18,430 --> 00:05:19,480 pretty much good for right now. 70 00:05:19,480 --> 00:05:22,000 I don't want to totally overdo this testing stuff. 71 00:05:22,000 --> 00:05:26,720 In addition I would like to write out some tests for our other request handlers as well. 72 00:05:26,980 --> 00:05:28,700 So let's take a quick pause right here. 73 00:05:28,710 --> 00:05:32,650 We're gonna start to write out some request handlers for all these other seeing some test for these 74 00:05:32,650 --> 00:05:33,960 other request handlers. 75 00:05:34,000 --> 00:05:38,170 Muslims are going to be pretty similar in nature but there will be one or two locations where we run 76 00:05:38,170 --> 00:05:39,750 into some other interesting issues. 77 00:05:40,070 --> 00:05:42,300 So let's start to write out these tests in just a moment.