1 00:00:01,020 --> 00:00:06,090 As I mentioned three or four times now in the last two videos right here the code we have to sign up 2 00:00:06,300 --> 00:00:09,090 and get the cookie and make a follow request definitely works. 3 00:00:09,090 --> 00:00:14,490 But remember on future services we're going to have to be making authenticated requests a ton in all 4 00:00:14,490 --> 00:00:18,590 of our different test files and if we have to go through the same setup every single time that's going 5 00:00:18,590 --> 00:00:20,310 to get really old really quickly. 6 00:00:20,310 --> 00:00:25,080 So in this video we're going to build a little helper function that's going to allow us to very easily 7 00:00:25,140 --> 00:00:31,840 sign up for some account and then make a follow up request using that accounts cookie so to put this 8 00:00:31,840 --> 00:00:33,170 little helper function together. 9 00:00:33,210 --> 00:00:37,690 I'm going to find the test directory I'll find you Setup Dot T S file inside there. 10 00:00:37,930 --> 00:00:41,260 We're going to write out a global function inside this file. 11 00:00:41,260 --> 00:00:45,640 So is going to be a function assigned to the global scope so we can very easily use it from all of our 12 00:00:45,640 --> 00:00:47,230 different test files. 13 00:00:47,260 --> 00:00:49,990 You do not have to use a global function for this. 14 00:00:50,050 --> 00:00:55,000 I am just doing it for convenience so I do not have to add in it very repetitive import statements into 15 00:00:55,000 --> 00:00:57,520 all of my different test files at the top. 16 00:00:57,520 --> 00:01:02,090 If you do not want to use a global function you could always create a separate file inside this text 17 00:01:02,110 --> 00:01:06,670 directory and call it something like auth helper or something like that and put the function or about 18 00:01:06,670 --> 00:01:10,960 to write inside there and export it and then imported into any relevant test file. 19 00:01:10,960 --> 00:01:16,290 So again I am only making this into a global function or globally scope function just for ease of use. 20 00:01:16,300 --> 00:01:17,410 That is it. 21 00:01:17,410 --> 00:01:21,940 The other thing I want to mention is that this global sine in function or assignment function is not 22 00:01:21,940 --> 00:01:27,310 going to be available inside of our normal application code because we're going to define it inside 23 00:01:27,310 --> 00:01:28,870 the Setup Dot T S file. 24 00:01:28,960 --> 00:01:34,100 It will only be available inside of our app in the test environment all right. 25 00:01:34,110 --> 00:01:41,750 So I'm gonna go down to the bottom and I want to create a globally scope function called sine in going 26 00:01:41,790 --> 00:01:48,820 to mark this thing as being async you'll notice I get an error right away from sine and right here. 27 00:01:48,820 --> 00:01:53,830 That's because typescript is saying hey on the global variable or the global scope and yes there is 28 00:01:53,830 --> 00:01:56,990 no function called sign it or no property called sign in. 29 00:01:57,220 --> 00:01:58,520 We're going to fix that up in just a month. 30 00:01:58,630 --> 00:02:03,700 But right now let's put together some implementation for this thing so inside of here at the very top 31 00:02:04,120 --> 00:02:05,920 I'm going to define some email 32 00:02:08,820 --> 00:02:16,970 and some password that we're going to try to authenticate with so after getting the e-mail and right 33 00:02:16,970 --> 00:02:17,650 there. 34 00:02:17,690 --> 00:02:24,560 Well then I tend to make a request off and sign up as this user with that email and that password so 35 00:02:24,740 --> 00:02:32,560 inside of here I'm going to say constant response is a wait we'll make a request to our app I'm gonna 36 00:02:32,560 --> 00:02:42,850 make a post request to API users sign up I'm going to send along the email and a password and then again 37 00:02:42,850 --> 00:02:47,180 it is not required for us to make an expectation about the response or the status quo that comes out 38 00:02:47,180 --> 00:02:53,530 of this but I'll do it anyways just make sure that these sign up went as expected so I can expect to 39 00:02:53,530 --> 00:02:58,770 get back a two a one right away we are getting an error out of request right here because we have not 40 00:02:58,770 --> 00:03:05,030 imported it into this file let's go to the top and import request very quickly so at the top I will 41 00:03:05,030 --> 00:03:12,290 import requests from to protest you'll notice now that we are also finally making use of the app import 42 00:03:12,290 --> 00:03:18,680 that we had added in previously Okay so now back down here we're going to at once again take a look 43 00:03:18,680 --> 00:03:24,920 at that response and pull our authentication cookie out of it we'll say down here counts cookie is response 44 00:03:25,100 --> 00:03:33,750 not get set Cookie now we want to take that cookie right there and somehow make it really easy to use 45 00:03:33,750 --> 00:03:39,000 inside of some follow up request there's definitely two ways we could do this we could either simply 46 00:03:39,060 --> 00:03:45,050 return the cookie and if we did that then back inside of any given test file or we'd really have to 47 00:03:45,050 --> 00:03:53,870 do is replace all that stuff right there and that's it's something like this 48 00:03:57,430 --> 00:04:01,480 now of course we are getting an air around sign up we'll fix that in just a moment but this is definitely 49 00:04:01,540 --> 00:04:03,100 one way to handle this. 50 00:04:03,220 --> 00:04:08,120 So anytime we need to make an authenticator request we'll get some cookie and then we can make our follow 51 00:04:08,120 --> 00:04:13,480 up request just making sure that we set that cookie property on the request itself but think that this 52 00:04:13,480 --> 00:04:19,730 is totally appropriate right here so I can go back over to the setup dot t s file I'm going to save 53 00:04:19,730 --> 00:04:21,260 this. 54 00:04:21,370 --> 00:04:26,890 We're going to make sure that we tell typescript that there is a global sign in property so to do so 55 00:04:26,980 --> 00:04:30,830 I'm gonna go to the top of the file and up here. 56 00:04:30,990 --> 00:04:35,720 I'm going to write out a little bit of typescript code just to tell typescript that there is some global 57 00:04:35,720 --> 00:04:37,860 property called sign up. 58 00:04:37,990 --> 00:04:43,340 So we're gonna write out declare global namespace node j us. 59 00:04:43,400 --> 00:04:50,710 Make sure you've got capital J us on their interface global to notice that this looks very similar to 60 00:04:50,710 --> 00:04:55,580 that little interface augmentation that we did previously when we're working with Express. 61 00:04:55,810 --> 00:05:00,190 So inside of this global interface which has already been defined we are just adding some additional 62 00:05:00,190 --> 00:05:01,720 properties to it. 63 00:05:01,840 --> 00:05:10,480 We're gonna say that there is an additional property that is called sign in don't put sign in. 64 00:05:10,650 --> 00:05:17,340 Well then say that this is going to be a function that is going to return a promise and that promise 65 00:05:17,460 --> 00:05:22,920 is going to resolve itself with a cookie and to be precise on what a cookie is. 66 00:05:22,920 --> 00:05:28,560 It's an array of strings so we're going to say that this promise is going to eventually resolve itself 67 00:05:28,590 --> 00:05:31,020 which we are resolved with a value of 68 00:05:34,810 --> 00:05:36,400 array of string. 69 00:05:36,400 --> 00:05:41,530 So that's how we indicate a promise that's going to resolve with some kind of value okay. 70 00:05:41,570 --> 00:05:42,990 So we can now save this. 71 00:05:43,020 --> 00:05:48,690 I'm gonna go back down to the bottom and you'll notice that era has now gone away completely. 72 00:05:48,770 --> 00:05:53,540 I'll go back over to current user you'll notice that I had made a little mistake in the name of the 73 00:05:53,540 --> 00:05:54,200 function right here. 74 00:05:54,200 --> 00:05:55,700 Over here I called it sign up. 75 00:05:55,910 --> 00:06:01,010 But over inside the tests are the set of file a cuddle called sign in either one is totally fine. 76 00:06:01,070 --> 00:06:06,050 Technically we are doing a sign up operation as opposed to sign in maybe even a better name for this 77 00:06:06,050 --> 00:06:08,760 would be something like get cookie or get off the cookie. 78 00:06:08,870 --> 00:06:10,030 We'll just stick with sign. 79 00:06:10,070 --> 00:06:13,010 That's good enough for now. 80 00:06:13,040 --> 00:06:15,960 Now it's still getting an error out of that. 81 00:06:16,010 --> 00:06:16,750 Why is that. 82 00:06:18,030 --> 00:06:20,150 So I'm pretty sure that this is an issue. 83 00:06:20,150 --> 00:06:24,360 Oh scuse me Lobel that's ion my mistake. 84 00:06:25,400 --> 00:06:25,970 All right. 85 00:06:26,180 --> 00:06:27,440 There we go. 86 00:06:27,440 --> 00:06:31,700 So again we are using this global thing right here just to avoid an additional import seemed at the 87 00:06:31,700 --> 00:06:32,270 top. 88 00:06:32,270 --> 00:06:37,040 Again if you don't like this just go ahead and move that sign in function to a separate file and then 89 00:06:37,040 --> 00:06:39,610 import it into the Test File. 90 00:06:39,610 --> 00:06:41,100 All right so let's say this. 91 00:06:41,110 --> 00:06:47,440 I mean I've looked back over my terminal checked my tests and we are good to go. 92 00:06:47,500 --> 00:06:47,740 All right. 93 00:06:47,780 --> 00:06:49,110 That's pretty much it. 94 00:06:49,110 --> 00:06:51,820 So now we can start to take that sign in function. 95 00:06:51,820 --> 00:06:56,080 Are we're going to eventually move it around to a couple of other services or you eventually put together 96 00:06:56,080 --> 00:06:57,310 and start to test as well. 97 00:06:58,410 --> 00:06:59,560 Kansas looks pretty good. 98 00:06:59,560 --> 00:07:03,510 I would like to put together one last test and current user and make sure that we make requests while 99 00:07:03,520 --> 00:07:04,640 not authenticated. 100 00:07:04,720 --> 00:07:08,180 We get back undefined or no is what it should be. 101 00:07:08,260 --> 00:07:12,160 As our current user in response let's secure that last test in just a moment.