Skip to content

Commit 64f49af

Browse files
authored
Merge pull request #16432 from marcusmoore/bug/sc-24475
Added validation around user store endpoint
2 parents c9f55bf + 25395e9 commit 64f49af

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

app/Http/Requests/SaveUserRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function response(array $errors)
3333
public function rules()
3434
{
3535
$rules = [
36-
'department_id' => 'nullable|exists:departments,id',
36+
'department_id' => 'nullable|integer|exists:departments,id',
3737
'manager_id' => 'nullable|exists:users,id',
38-
'company_id' => ['nullable','exists:companies,id']
38+
'company_id' => ['nullable', 'integer', 'exists:companies,id']
3939
];
4040

4141
switch ($this->method()) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)