Our Black Friday sale is now live! All individual subscriptions are 50% OFF. This week only!

konarktriv7

konarktriv7

Member Since 6 Months Ago

Experience Points
150
Total
Experience

4,850 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
150 XP
May
23
6 months ago
Activity icon

Started a new Conversation How To Get Middleware To Fire?

Morning / Afternoon / Evening,

I'm wondering if someone else has encountered a way to ensure that Middleware is running on your tests.

We're currently doing a lot of Auth::check() & Auth::user()-> checks in our methods, but we're moving to using Middleware on our constructors like so;

public function __construct() { parent::__construct();

  $this->middleware(['auth']);

} This is working just fine in real use cases, but inside the tests it's not working as intended.

I have the following method;

public function method() { if(Auth::user()->can('doSomething')) { } }

When inside my test I do the following;

    $request = Request::create('/route', 'GET', []);

    $controller = new MyController();
    $response = $controller->method($request);

But the test is complaining that the Auth::user() is null. Now, this is actually my intention. I'm wanting to write a test to ensure that my method is not accessible by those not authenticated in my application.

I was under the impression that even with calling $controller->method(); the __construct() should still be called, and so the Middleware should then handle the situation, and return the correct response.

Am I missing something obvious? I feel like I'm going around in circles going crazy.

Any help would be appreciated. I have had a search online, and even here on Laracasts, but I am unable to find something related to wanting to explicitly make sure that middleware is running on my tests.

Update: My issue has been solved, thank you. https://discord.software/ https://omegle.onl/

Activity icon

Started a new Conversation Sanctum: Issue With User Logout Case + TDD!!

I am writing a use case to logout a user, so on Logout request, I delete all the user tokens so that every token issued previously becomes invalid for further requests.

Following is the feature test case:

/** @test */
public function a_user_can_logout()
{

    // $this->withoutExceptionHandling();
    $this->jsonPost(route('api.user.logout'))->assertStatus(401);

    $user = factory('App\User')->create();

    $response = $this->json('POST', route('api.user.login'), [
        'email'=> $user->email,
        'password' => 'password'
    ])->assertStatus(200);
    
    $this->jsonPost(route('api.user.logout'), [], $response->json()['access_token'])->assertStatus(200);
            
    $this->jsonPost(route('api.user.profile'), [], $response->json()['access_token'])->assertStatus(401);
}

Now on logout request following method runs:

public function logout()
{
    auth()->user()->tokens()->delete();
    return response()->json(['message' => 'Successfully logged out']);
}

Now comes the bug part, in the last assertion i.e. $this->jsonPost(route('api.user.profile'), [], $response->json()['access_token'])->assertStatus(401); I am calling user profile API which should return the status as 401 but it always returns 200.

Screenshot 2020-04-14 at 8 12 47 PM

My profit method:

public function profile() { return response()->json(auth()->user()); } I tried to debug the issue and the following are my observations:

I am facing this issue when I run this feature test through the command line. If I test the same thing on POSTMAN Client the functionality is just working fine. So this issue is related to TDD.

There is a method in the class Illuminate/Auth/RequestGuard.php

public function user() { // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; }

   return $this->user = call_user_func(
       $this->callback, $this->request, $this->getProvider()
   );

} In this method Laravel is caching user object for performance optimization. And If we comment out the caching code i.e.

if (! is_null($this->user)) { return $this->user; } my feature test case runs perfectly.

Screenshot 2020-04-14 at 8 10 56 PM

Following is my route list:

Screenshot 2020-04-14 at 7 45 27 PM

Steps To Reproduce: Setup Laravel 7 and Sanctum Setup API routes for login, log out, and user profile or any sanctum token protected route. Create a feature test or use the above feature test code. Run the tests.

Update: My issue has been solved, thank you. https://nox.tips/ https://xender.vip/ https://testmyspeed.onl/