Skip to content
Permalink
Browse files

Episode 33

  • Loading branch information...
JeffreyWay committed Mar 14, 2019
1 parent d735571 commit 96ddf7cd477d31733dd72dd67f9472cda5a4ccbd
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProjectInvitationRequest;
use App\Project;
use App\User;
class ProjectInvitationsController extends Controller
{
/**
* Invite a new user to the project.
*
* @param Project $project
* @param ProjectInvitationRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Project $project, ProjectInvitationRequest $request)
{
$user = User::whereEmail(request('email'))->first();
$project->invite($user);
return redirect($project->path());
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
class ProjectInvitationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('update', $this->route('project'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => ['required', Rule::exists('users', 'email')]
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [
'email.exists' => 'The user you are inviting must have a Birdboard account.'
];
}
}
@@ -21,6 +21,8 @@
Route::post('/projects/{project}/tasks', 'ProjectTasksController@store');
Route::patch('/projects/{project}/tasks/{task}', 'ProjectTasksController@update');
Route::post('/projects/{project}/invitations', 'ProjectInvitationsController@store');
Route::get('/home', 'HomeController@index')->name('home');
});
@@ -12,7 +12,45 @@ class InvitationsTest extends TestCase
use RefreshDatabase;
/** @test */
function a_project_can_invite_a_user()
function non_owners_may_not_invite_users()
{
$this->actingAs(factory(User::class)->create())
->post(ProjectFactory::create()->path() . '/invitations')
->assertStatus(403);
}
/** @test */
function a_project_owner_can_invite_a_user()
{
$project = ProjectFactory::create();
$userToInvite = factory(User::class)->create();
$this->actingAs($project->owner)
->post($project->path() . '/invitations', [
'email' => $userToInvite->email
])
->assertRedirect($project->path());
$this->assertTrue($project->members->contains($userToInvite));
}
/** @test */
function the_email_address_must_be_associated_with_a_valid_birdboard_account()
{
$project = ProjectFactory::create();
$this->actingAs($project->owner)
->post($project->path() . '/invitations', [
'email' => 'notauser@example.com'
])
->assertSessionHasErrors([
'email' => 'The user you are inviting must have a Birdboard account.'
]);
}
/** @test */
function invited_users_may_update_project_details()
{
$project = ProjectFactory::create();

0 comments on commit 96ddf7c

Please sign in to comment.
You can’t perform that action at this time.