In the last video we finished working on our mailer.
And I really hope that's the last of it because the censored stuff as we've spoken about is pretty nasty.
So we're now going to test out the mailer and make sure that it works the way we expect to make sure
that the mailer actually communicates itself over to send grid.
We have to remember to call the send function on the mailer that we create.
So let's flip back over to our survey route's file where we've already put together just a tiny bit
of test code to create a new mailer.
So your survey routes and we'll find where we created the mailer at the bottom and we're going to make
sure that we call send on this thing to make sure that the mailer actually attempts to send itself.
So we'll add in mailer got sent.
OK.
So it's now time to test out this route.
I want to somehow test this route.
I want to make a post request to it.
I want to verify that it requires us to be logged in.
That requires some number of credits and all these other conditions in here as well.
And eventually I want to make sure that we somehow send out an e-mail.
So at this point we have written a tremendous amount of code without really testing it.
So this is really the point of discovery where we decide whether or not everything worked out really
well or whether it didn't work out at all and we need to go back and kind of go back to the drawing
board so to speak.
The only question is how do we actually test our code.
How do we actually make a post request to API slash service.
We don't have anything inside of a re-act application right now we don't have any type of form that
we can use to send a post request to this thing but obviously I don't want to do all the Riak stuff
unless we are at least somewhat sure that the back end is going to work out as we expect.
Now you might be sitting there thinking Hey Stephen that's no problem.
Let's use a rest client.
So you might have heard about rest clients previously maybe one of my other courses or maybe working
on your own.
There are these little applications that can run on your local laptop or your local desktop called Rest
clients and they allow you to make API requests to any arbitrary endpoint on any given server.
So for example here's my resk client called postman.
I can make a get request to local host 5000 slashy API slash user to figure out whether or not I'm signed
in.
And so I make the request and it returns back nothing at all which means okay I'm probably not signed
in.
So we could use postman here but I want to point out one little issue that's really going to throw a
real wrench into doing any testing with post man.
And here's what it is.
Remember that our surveys route right here requires our user to be logged in and to have some number
of credits doing a off flow through any type of rest client is a huge pain in the rear.
So the only way for a user to log into our application is to go through that waterflow.
But if we want to do that inside of postbag to make sure that we are able to make requests logged in
as a current user it is a real pain in the neck to use post and for this that kind of thing.
So if you're sitting there thinking Hey let's use postman to test out this new route.
Handler Well we're not going to quite do that.
We're going to do things a little bit differently and a much easier and much more straightforward fashion
than using that postman client.
OK.
So let's figure out what that is.
Let's get down to it.
All right.
So what we're going to do is we're going to make sure that we have direct access to the X-Post module
inside of our browser from the re-act library.
We will then use the axis module to make a post request to our back and server from our re-act up manually.
Remember that any time we make a request from the re-act side of our application over to our back and
server cookies are included.
And so we will pass all the required logging checks.
So essentially we can use postman or we can use kind of like this fake rest client on the client side
of our application from inside the browser and we'll pass all the authentication checks without any
issue whatsoever.
So let's figure out exactly how this is going to work.
First off I'm going to change over to my client folder and inside if you're going to find my index G-S
file inside of the in that sarcy directory inside if you're going to import X-posts from X-posts and
I'm going to assign it to window dot ASIO's like so.
So this is going to be some very temporary test code that we have in here just for testing out this
back end drought that we are attempting to figure out whether or not it works OK.
So just very temporary right here.
So now if I make sure that my server is running and it looks like it is.
So here's my server right here.
I'm now going to flip over to the browser.
I'm going to change over to the re-act application.
I'm going to refresh the page just to make sure I'm current and if I open up my terminal and in the
javascript console I should be able to write X-ers and then see the actual library pop out.
And that's what you see right here.
This is the actual library so I can now access functions like Actio top post like so and make a post
request using all the cookies that are required that are provided at local hoze 3000.
So if we manually write in requests to our API endpoint right here to create a new survey we should
be able to test out all the emails and logic.
So with this we can at least get somewhere.
So we're now going to create a request object manually and then send it to our backend API.
So the first thing we need to do is to create a fake survey remember that we've been saying many times
that a survey will have properties like the subject the body all that kind good stuff.
So let's make a survey right now.
It's going to say Konst survey equals.
This is going to have a couple different properties.
Let's make sure our font's is nice and large so you can read it.
So we don't have a title of my title a subject of my subject will have a recipient's list and remembers
recipients right here.
And it's a string that is comma separated e-mails.
Now I've already taken the liberty of creating a brand new gmail account that we can easily test out
all this stuff with.
So I'm going to use the e-mail address for this account right here but for you of course I recommend
you use your own personal e-mail so you can verify that your server is working correctly.
So the e-mail address for this account is dev.
E-mail me at gmail dot com.
So that's what I'm going to use for my recipients right here.
So send.
E-mail the dotcom groups have e-mailed me at gmail dot com.
There we go.
And then do we require any other properties.
Let's go double check really quick.
We'll go back to our survey route's file.
So we've got titles subject.
I think we missed body.
We missed one of these other subject or body.
So we got subject to the last woman's body say a body of here's the body of the email.
OK.
So here's our object that represents the survey.
We're now going to make use of Axel's to send this to our back and server.
So we'll see X-post post slash CPI slash surveys and we'll pass in the survey as the second argument.
All right.
So now if we check out our request log right here you are going to see that the request is pending because
we are not properly responding to the request from our server right now.
Remember inside of our route handler we are sending that e-mail.
But we actually never terminate the request and send back a response.
However we should be able at least be able to see inside of our e-mail if we have something.
So if I flip over to gmail and look at my promotions tab you'll see.
OK here's our e-mail as the subject of my subject.
It says here is the body of the e-mail.
And it definitely appeared at the e-mail address.
Hooray.
So it looks like everything worked out pretty well here.
We definitely got the e-mails sent from our application over to this e-mail address.
Now at this point it is likely even probable that you might have not gotten this e-mail.
So if you did not get an e-mail what I'm going to recommend you do is immediately flip over to your
terminal and see if you have any big glaring error messages over here.
Anything that says hey something went really wrong.
If you do I recommend you do a little bit of trouble shooting to figure out exactly what happened after
you do a little bit of troubleshooting of course.
Feel free to hop over to the discussion linked to your code and let me know how I can help you figure
out what's going on.
Just remember always try to post a screenshot of the air.
So I've got a good idea of exactly what's happening.
All right.
So at this point hopefully e-mails are being sent out correctly.
So at this point we're going to do just a little bit of quick clean up on that debug code that we just
added.
So back inside the index stock J.S. file.
I'm going to clean up that X-ers import statement because we don't really want to have access on our
windows scope at all.
OK.
So that looks about.
So we now have the ability to send e-mails from our application but we're not quite done with our API
endpoint for creating a survey just yet.
So let's take a quick break and then continue in the next section.