As I mentioned three or four times now in the last two videos right here the code we have to sign up
and get the cookie and make a follow request definitely works.
But remember on future services we're going to have to be making authenticated requests a ton in all
of our different test files and if we have to go through the same setup every single time that's going
to get really old really quickly.
So in this video we're going to build a little helper function that's going to allow us to very easily
sign up for some account and then make a follow up request using that accounts cookie so to put this
little helper function together.
I'm going to find the test directory I'll find you Setup Dot T S file inside there.
We're going to write out a global function inside this file.
So is going to be a function assigned to the global scope so we can very easily use it from all of our
different test files.
You do not have to use a global function for this.
I am just doing it for convenience so I do not have to add in it very repetitive import statements into
all of my different test files at the top.
If you do not want to use a global function you could always create a separate file inside this text
directory and call it something like auth helper or something like that and put the function or about
to write inside there and export it and then imported into any relevant test file.
So again I am only making this into a global function or globally scope function just for ease of use.
That is it.
The other thing I want to mention is that this global sine in function or assignment function is not
going to be available inside of our normal application code because we're going to define it inside
the Setup Dot T S file.
It will only be available inside of our app in the test environment all right.
So I'm gonna go down to the bottom and I want to create a globally scope function called sine in going
to mark this thing as being async you'll notice I get an error right away from sine and right here.
That's because typescript is saying hey on the global variable or the global scope and yes there is
no function called sign it or no property called sign in.
We're going to fix that up in just a month.
But right now let's put together some implementation for this thing so inside of here at the very top
I'm going to define some email
and some password that we're going to try to authenticate with so after getting the e-mail and right
there.
Well then I tend to make a request off and sign up as this user with that email and that password so
inside of here I'm going to say constant response is a wait we'll make a request to our app I'm gonna
make a post request to API users sign up I'm going to send along the email and a password and then again
it is not required for us to make an expectation about the response or the status quo that comes out
of this but I'll do it anyways just make sure that these sign up went as expected so I can expect to
get back a two a one right away we are getting an error out of request right here because we have not
imported it into this file let's go to the top and import request very quickly so at the top I will
import requests from to protest you'll notice now that we are also finally making use of the app import
that we had added in previously Okay so now back down here we're going to at once again take a look
at that response and pull our authentication cookie out of it we'll say down here counts cookie is response
not get set Cookie now we want to take that cookie right there and somehow make it really easy to use
inside of some follow up request there's definitely two ways we could do this we could either simply
return the cookie and if we did that then back inside of any given test file or we'd really have to
do is replace all that stuff right there and that's it's something like this
now of course we are getting an air around sign up we'll fix that in just a moment but this is definitely
one way to handle this.
So anytime we need to make an authenticator request we'll get some cookie and then we can make our follow
up request just making sure that we set that cookie property on the request itself but think that this
is totally appropriate right here so I can go back over to the setup dot t s file I'm going to save
this.
We're going to make sure that we tell typescript that there is a global sign in property so to do so
I'm gonna go to the top of the file and up here.
I'm going to write out a little bit of typescript code just to tell typescript that there is some global
property called sign up.
So we're gonna write out declare global namespace node j us.
Make sure you've got capital J us on their interface global to notice that this looks very similar to
that little interface augmentation that we did previously when we're working with Express.
So inside of this global interface which has already been defined we are just adding some additional
properties to it.
We're gonna say that there is an additional property that is called sign in don't put sign in.
Well then say that this is going to be a function that is going to return a promise and that promise
is going to resolve itself with a cookie and to be precise on what a cookie is.
It's an array of strings so we're going to say that this promise is going to eventually resolve itself
which we are resolved with a value of
array of string.
So that's how we indicate a promise that's going to resolve with some kind of value okay.
So we can now save this.
I'm gonna go back down to the bottom and you'll notice that era has now gone away completely.
I'll go back over to current user you'll notice that I had made a little mistake in the name of the
function right here.
Over here I called it sign up.
But over inside the tests are the set of file a cuddle called sign in either one is totally fine.
Technically we are doing a sign up operation as opposed to sign in maybe even a better name for this
would be something like get cookie or get off the cookie.
We'll just stick with sign.
That's good enough for now.
Now it's still getting an error out of that.
Why is that.
So I'm pretty sure that this is an issue.
Oh scuse me Lobel that's ion my mistake.
All right.
There we go.
So again we are using this global thing right here just to avoid an additional import seemed at the
top.
Again if you don't like this just go ahead and move that sign in function to a separate file and then
import it into the Test File.
All right so let's say this.
I mean I've looked back over my terminal checked my tests and we are good to go.
All right.
That's pretty much it.
So now we can start to take that sign in function.
Are we're going to eventually move it around to a couple of other services or you eventually put together
and start to test as well.
Kansas looks pretty good.
I would like to put together one last test and current user and make sure that we make requests while
not authenticated.
We get back undefined or no is what it should be.
As our current user in response let's secure that last test in just a moment.