Commit 0e975777 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

create users invalid input tests

parent 85a3a7c7
......@@ -75,7 +75,7 @@ class UsersController extends ApiController {
'display_name' => 'required|string|max:255',
'emplid' => 'nullable|string|max:7|min:6',
'email' => 'required|email|max:255',
'password' => 'required|string|max:255',
'password' => 'nullable|string|min:15|max:255',
'doorcode' => 'required|string|numeric|digits_between:4,255',
'expires_at' => 'nullable|string|date|max:255',
]
......
<?php
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
class CreateUserApiTest extends TestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
/**
* @throws BindingResolutionException
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
}
/**
* @param array $data
*/
protected function handleTest(array $data): void {
$this->response = $this->postJson('/users', $data);
}
public function invalidUserProvider(): array {
$user = new User(0, 'first', 'last', 'display', null, 't@t.com', '', '43252643', null, null, null);
return [
// Missing one of the required
[
[
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['first_name' => ['The first name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['last_name' => ['The last name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['display_name' => ['The display name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'doorcode' => $user->getDoorcode(),
],
['email' => ['The email field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
],
['doorcode' => ['The doorcode field is required.']],
],
// emplid is too small
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'emplid' => 'small',
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['emplid' => ['The emplid must be at least 6 characters.']],
],
// emplid is too large
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'emplid' => 'waaaaaaaaaaay to large',
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['emplid' => ['The emplid may not be greater than 7 characters.']],
],
// expires at isnt a date
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
'expires_at' => 'bad date',
],
['expires_at' => ['The expires at is not a valid date.']],
],
// doorcode isnt a number
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => 'not a number',
],
['doorcode' => ['The doorcode must be a number.', 'The doorcode must be between 4 and 255 digits.']],
],
// doorcode is too small
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => '000',
],
['doorcode' => ['The doorcode must be between 4 and 255 digits.']],
],
// email invalid
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => 'not an email',
'doorcode' => $user->getDoorcode(),
],
['email' => ['The email must be a valid email address.']],
],
];
}
/**
* @test
* @param array $data
* @param array $message
* @dataProvider invalidUserProvider
*/
public function it_tests_the_validation_rules(array $data, array $message): void {
$this->handleTest($data);
$this->response->assertJsonFragment($message);
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment