Lesson Description

The "Auth Login Test" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through how to test login endpoints, emphasizing checks for successful authentication and proper error handling.

Preview
Close

Transcript from the "Auth Login Test" Lesson

[00:00:00]
>> Scott Moss: Let's see, how about we test for, oh yeah, let's just do a login one, that'll make sense. So let's go back to our auth test. We'll make a new Describe here. And we'll say let's do a post to.

[00:00:14]
And we'll say let's do a post to. Slash API slash auth slash login. And. Let's test, it should log in with valid credentials.

[00:00:38]
Let's test, it should log in with valid credentials. And you'll want to get rid of that it on line 34. Like 34. Oh, I didn't even see that.

[00:00:54]
Oh, I didn't even see that. Nice, good catch. Cool, so It should log on to valid credentials, so now we do want to create that test user. So we'll say test user equals await create that test user.

[00:01:15]
So we'll say test user equals await create that test user. And I'm not gonna overwrite anything. I'll just use. Whatever it gets back here and then.

[00:01:38]
Whatever it gets back here and then. Credentials is it's just gonna be. Email test. User email.

[00:01:59]
User email. What's wrong with you? Oh, Test user user.email. And then password.

[00:02:15]
And then password. Test user. Raw password. Yup.

[00:02:37]
Yup. And let's make the request. So response equals await. Request app dot post.

[00:03:02]
Request app dot post. API slash auth slash login. And then we want to send up these credentials. And I expect a 200.

[00:03:29]
And I expect a 200. I think I put a 200, it could be 200, 201, whatever we put here. Technically it should be a 201, so I'll put a 201 and if it's broken, we'll Go change it. So 201.

[00:03:47]
So 201. And then some more assertions we can expect, the response dot body. To have. Property, message and you could put the value here too, but I'm not gonna.

[00:04:05]
Property, message and you could put the value here too, but I'm not gonna. I'm not gonna check against strings. That message could be anything unless I have like an enum for that message or a const for that message. I don't wanna, that's just gonna be so pain to like change all the time.

[00:04:29]
I don't wanna, that's just gonna be so pain to like change all the time. Response dot body, to have property. User, for sure, and expect. The body to have.

[00:04:44]
The body to have. Token. For sure, otherwise, I'm not really signed in, am I? And then not to have password which I can just copy it.

[00:04:57]
And then not to have password which I can just copy it. OK, so I think this might fail like a 200, but we'll change it if we need to Let's see what happens. Cool, that one passed the first one, and then the second one, doesn't like me. Oh no, it did, it failed exactly where I thought I was gonna fail actually, so.

[00:05:13]
Oh no, it did, it failed exactly where I thought I was gonna fail actually, so. Let's just change that one to a. To, what did I put? 201 here, it's 201 here.

[00:05:31]
201 here, it's 201 here. Where is it getting a 200 then? 200. Expected 201 got 200.

[00:05:45]
Expected 201 got 200. From login? that's 200, 201, no? Hm.

[00:05:57]
Hm. Where are you getting that from? 200. I don't even see the 200 anywhere.

[00:06:14]
I don't even see the 200 anywhere. 401. 201. I don't know if I move it up.

[00:06:34]
I don't know if I move it up. Try that. Is the default 200? Yeah, if, yeah, by default, if you, yeah, I guess you can't put it after you put like the JSON method, yeah, the default, it, yeah, if you don't, if you send anything back, it's always a by default, so because I did a JSON it was like 200, so cool and this is why you test things, Great, any questions on these tests?

[00:06:34]
Yeah, if, yeah, by default, if you, yeah, I guess you can't put it after you put like the JSON method, yeah, the default, it, yeah, if you don't, if you send anything back, it's always a by default, so because I did a JSON it was like 200, so cool and this is why you test things, Great, any questions on these tests? It's pretty straightforward. I mean for the most part, every single route that you test will be some version of this is like cool what route am I Testing? What do I need to send up if anything and what do I expect to get back from the response object?

[00:06:34]
What do I need to send up if anything and what do I expect to get back from the response object? That's really it. It's not much else. This would not cover things like.

[00:06:34]
This would not cover things like. Side effects, but for side effects because they are side effects and you don't really care you're not waiting for them you're not waiting for a result or you don't really care the result of them you'd only test that side effect got called anyway you're not really expecting a result from a side effect, otherwise it wouldn't be a side effect, so. Outside of that, it pretty much tests the whole flow of, you know, what you wanna do, so. I think that's I think it's enough Testing.

[00:06:34]
I think that's I think it's enough Testing. We got more, but even the habit itself is mostly the same. It's just, as you can see, it's pretty much the same thing. I guess the only thing that's different that I do wanna go over.

[00:06:34]
I guess the only thing that's different that I do wanna go over. Would be me. Go down here. And so far these routes were just for.

[00:06:34]
And so far these routes were just for. Authorization, but what about a route that needs authorization, so what would that look like? That's, it's mostly the same. You gotta just pass a header and now that's it.

[00:06:34]
You gotta just pass a header and now that's it. You just that token that you get, you just pass it the same way we do in Postman. That's it. Everything else is the same, it's not, no different.

[00:06:34]
Everything else is the same, it's not, no different. That's all we do. So, Yeah, I would say I'm satisfied with showing you this amount of Testing, everything else is mostly just the same and repetitive so let's get on to deployment

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now