Hi All!
I have a unique scenario that allows me to not test on of my Routes. I have a Post with the column author_id which I am trying to include after the form request so that it forces the post author_id to be the Authenticated user. This prevents a malicious User from changing a User ID in the form fields and adding a Post as a different User.
However... I've found that I cannot test this code and I receive 403 errors on trying to pass a Post with author_id, as author_id is not included in the SavePost validation rules. I also cannot remove author_id and post because Auth::user()->id is not recognised in the controller method when running as a test.
My Test
$post = factory(Post::class)->create(
[
'author_id' => $this->user->id
]
);
$this->post('/posts', $post->toArray())
->assertStatus(200);
My POST method
public function store(SavePost $request)
{
$request->merge(['author_id' => Auth::user()->id]);
$post = Post::create($request->all());
}
I could just include author_id in the validation rules as it's not currently in the Post create form so what are the odds of someone guessing the field and adding?
Anyone ever had similar encounters when looking after the security of basic POST/UPDATE methods?