|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Users\Api; |
| 4 | + |
| 5 | +use App\Models\Company; |
| 6 | +use App\Models\Department; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Testing\Fluent\AssertableJson; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +class StoreUsersTest extends TestCase |
| 12 | +{ |
| 13 | + public function testRequiresPermission() |
| 14 | + { |
| 15 | + $this->actingAsForApi(User::factory()->create()) |
| 16 | + ->postJson(route('api.users.store'), [ |
| 17 | + 'first_name' => 'Joe', |
| 18 | + 'username' => 'joe', |
| 19 | + 'password' => 'joe_password', |
| 20 | + 'password_confirmation' => 'joe_password', |
| 21 | + ]) |
| 22 | + ->assertForbidden(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testCompanyIdNeedsToBeInteger() |
| 26 | + { |
| 27 | + $company = Company::factory()->create(); |
| 28 | + |
| 29 | + $this->actingAsForApi(User::factory()->createUsers()->create()) |
| 30 | + ->postJson(route('api.users.store'), [ |
| 31 | + 'company_id' => [$company->id], |
| 32 | + 'first_name' => 'Joe', |
| 33 | + 'username' => 'joe', |
| 34 | + 'password' => 'joe_password', |
| 35 | + 'password_confirmation' => 'joe_password', |
| 36 | + ]) |
| 37 | + ->assertStatusMessageIs('error') |
| 38 | + ->assertJson(function (AssertableJson $json) { |
| 39 | + $json->has('messages.company_id')->etc(); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + public function testDepartmentIdNeedsToBeInteger() |
| 44 | + { |
| 45 | + $department = Department::factory()->create(); |
| 46 | + |
| 47 | + $this->actingAsForApi(User::factory()->createUsers()->create()) |
| 48 | + ->postJson(route('api.users.store'), [ |
| 49 | + 'department_id' => [$department->id], |
| 50 | + 'first_name' => 'Joe', |
| 51 | + 'username' => 'joe', |
| 52 | + 'password' => 'joe_password', |
| 53 | + 'password_confirmation' => 'joe_password', |
| 54 | + ]) |
| 55 | + ->assertStatusMessageIs('error') |
| 56 | + ->assertJson(function (AssertableJson $json) { |
| 57 | + $json->has('messages.department_id')->etc(); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + public function testCanStoreUser() |
| 62 | + { |
| 63 | + $this->actingAsForApi(User::factory()->createUsers()->create()) |
| 64 | + ->postJson(route('api.users.store'), [ |
| 65 | + 'first_name' => 'Darth', |
| 66 | + 'username' => 'darthvader', |
| 67 | + 'password' => 'darth_password', |
| 68 | + 'password_confirmation' => 'darth_password', |
| 69 | + ]) |
| 70 | + ->assertStatusMessageIs('success') |
| 71 | + ->assertOk(); |
| 72 | + |
| 73 | + $this->assertDatabaseHas('users', [ |
| 74 | + 'first_name' => 'Darth', |
| 75 | + 'username' => 'darthvader', |
| 76 | + ]); |
| 77 | + } |
| 78 | +} |
0 commit comments