JayM
5 months ago
685
4
Testing

User not logged in can create changes.

Posted 5 months ago by JayM

I'm new to Laravel so I'm not sure what I'm going wrong and would appreciate some clarification if someone has time. In my routes file, I have a middleware group set up so that I only want a person that is logged in to access. There is no user role at this point in my program.

Route::group(['middleware' => 'auth'], function() {
    #More Routes Here
    Route::post('/school/{school}/building', '[email protected]');
});

So I did a simple test and it's failing. I'm getting a change in my database, and a 302 response when I hit the above path.

public function unauthorized_users_cannot_add_a_building()
    {
        $school = factory(School::class)->create();

        $attributes = factory(Building::class)->raw(['school_id' => $school->id]);

        $this->post($school->path() . '/building', $attributes)
            ->assertStatus(403); #returns a 302

        $this->assertDatabaseMissing('buildings', $attributes); #returns false
    }

I don't have anything special in my BuildingController file so why is my test flawed or do I have something not configured correctly?

When I use withoutExeptionHandling I will receive from the line that says $this->post(...);

1) Tests\Feature\SchoolBuildingTest::unauthorized_users_cannot_add_a_building
Illuminate\Auth\AuthenticationException: Unauthenticated.

Please sign in or create an account to participate in this conversation.