Testing Login/Logout Links0:00
Another task in our to-do list is to make sure that a member can log in. And since we're using ChatStream, which is already fully tested, I don't want to test ChatStream again, because I just trust that ChatStream is already working and built very well by the Laravel team. But something else I want to make sure is that we see specific links on our home page to log in if we're not logged in, and to log out if we are logged in. So this is something that I see in testing quite a lot.
and to log out if we are logged in. So this is something that I see in testing quite a lot. There are things that we don't need to test, but sometimes we just want to make sure that they are used or that we can use them. The same goes with Livewire later when we build test components. Okay, let's start adding two more tests to our PageHomeTest. We want to make sure that it includes log in if not logged in. And the second one, the opposite now, it includes log out if logged in.
And the second one, the opposite now, it includes log out if logged in. Again, we're going to act on the search at the same time because we will do a GET request to our home page, pages/home. As always, we want to make sure that we get a positive response back. And yeah, what do we want to test? First, I want to make sure that I see some text. So for the log in button, for example, let's say it's called log in. And then we also want to make sure that there is a route to log in. And the route log in is already given through chat stream.
Adding Login Link1:30
And then we also want to make sure that there is a route to log in. And the route log in is already given through chat stream. And this currently is failing because there is nothing on our home page. So let's fix this. So what I'm going to do here is I'm going to add here a new link to our log in page and the text is logging. And as always, I try to write only the code that makes my test pass. And it's working now. You can see this. Of course, I already know that if we are logged in, we have to do something differently.
Of course, I already know that if we are logged in, we have to do something differently. But for now, I'm fine with just making my test pass. Now we want something similar for our second test here. We want to make sure that we see some text like log out. And the route is also called log out. Okay, this should not be failing. Yes, it does. Okay, so now we have to adapt our code. And we can use the guest directive here. So if the User is a guest, we want to show our log in link.
Implementing Logout Form2:32
And we can use the guest directive here. So if the User is a guest, we want to show our log in link. But when the User is already logged in, we want to show a log out button. We can't just do this with a link because this needs to be submitted with a POST request. So this means we need a form here with an action to the route logout, which is given through chat stream. And the method should be POST. So you want to send a POST request. And then in order to prevent cross-site request forgery,
So you want to send a POST request. And then in order to prevent cross-site request forgery, we need to add this token here from csrf. And then we can make use of the button. Type is submit and the text should be log out. Let's run this test and it's failing because we don't see log out. So let's take another look again. Yeah, so here we are currently not logged in. So let's add our new logIn as User function. And this is now passing and let's run all of them.
Disabling User Registration3:27
So let's add our new login as User function. And this is now passing and let's run all of them. Yeah, so they are all now passing. All right, so far so good. We are not logging in and logging out from the homepage. But one thing that we probably have overseen when installing Chatstream is that it also comes with registration. And if we check out our Fortify settings, which is what Chatstream builds on a level, we can see that here still feature registration is enabled.
which is what chatstream builds on a level, we can see that here still feature registration is enabled. And that's definitely something that we don't want to do in our application because we want to create the accounts for our users ourselves when they have purchased something. Okay, so this means we can just comment this out. But yes, always we want to write a test first to make sure that it is working as expected. So let's add another test to our pagesResponseTest. And we want to make sure that it does not find chatstream registration page.
So let's add another test to our pages response test. And we want to make sure that it does not find chat stream registration page. All right, we are acting and inserting again at the same time by making a get request to the register page. So we see the route is already given through chat stream. And we want to make sure that we get a 404 response back, which means assertNotFound. Let's run this and this is failing. Okay, now that we have a failing test, it's a good time to uncomment here this registration feature.
Okay, now that we have a failing test, it's a good time to uncomment here this registration feature. Let's run the test again. And it's still not working because RouteNotFoundException. Because now this route provided by chat stream is not given anymore. But I still know that it had this URL. So that's why I'm just switching out not using the route anymore, but using the register string. Okay, nice. All of the tests are working. So this means we didn't break anything, which is a good thing.
Testing Dashboard Logout5:16
Okay, nice. All of the tests are working. So this means we didn't break anything, which is a good thing. And we've now made sure that registration is not possible inside our application. But we not only have a homepage, we also have a dashboard. And it's important that we can log out from the dashboard as well. It includes log out, that's the name. Then we're going to act and assert. And for this test, we need to be logged in. Then we're making a request to our dashboard. We want to make sure everything is okay.
Then we're making a request to our dashboard. We want to make sure everything is okay. And then again, I want to make sure that we see some text. Log out again. And we want to see the routes or assert routes and then the route log out. Let's run this test and it's not working. Let's see why. Route dashboard not defined. All right, we have changed this. Yep, like this.
All right, we have changed this. Yep, like this. Let's run this again and it's still not working. It's not finding the text. Let me check here. Oh yeah, it's just because it has a capital O. Let's change this as well. Log out like this. And now this test is also working. As always, let's run all of our tests.
And now this test is also working. As always, let's run all of our tests. They all look good. We already have 40 tests now, which is already pretty good. And they are working, which is even better. Okay, we've now made sure that there are ways for the User to log in and log out from the homepage and from the members page. This means we can check this to do off and we are finished for this video. Thank you.
Thank you.
