Skip to content

Commit

Permalink
Merge 6eedd3d into e804f5a
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Dec 17, 2015
2 parents e804f5a + 6eedd3d commit 2e70cf2
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 14 deletions.
89 changes: 89 additions & 0 deletions api/app/Console/Commands/CreateUserCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Console\Commands;

use App\Models\Role;
use App\Models\User;
use App\Models\UserCredential;
use Illuminate\Console\Command;
use Illuminate\Support\Debug\Dumper;
use Illuminate\Support\Facades\Validator;
use Rhumsaa\Uuid\Uuid;

class CreateUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create user with credentials.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$email = $this->ask('Enter email');
$name = $this->ask('Enter first name');
$username = $this->ask('Enter username', strtolower($name));
$password = $this->secret('Enter password');

$roles = $this->choice('What roles should be applied? (comma separate options)', Role::$roles, null, null, true);

$userData = [
'user_id' => Uuid::uuid4(),
'email' => $email,
'first_name' => $name,
'username' => $username,
];

$validationRules = User::getValidationRules($userData['user_id']);
unset($validationRules[User::getPrimaryKey()]);
$validator = Validator::make($userData, $validationRules);

if ($validator->fails()) {
$this->error('Validation failed:');
// @codeCoverageIgnoreStart
if (env('APP_ENV') != 'testing') {
(new Dumper)->dump($validator->errors()->toArray());
}
// @codeCoverageIgnoreEnd
return 1;
}

$user = new User($userData);
$user->save();

$user->roles()->sync($roles);

$user->setCredential(new UserCredential(['password' => $password]));

$this->info('Successfully created user:');

// @codeCoverageIgnoreStart
if (env('APP_ENV') != 'testing') {
(new Dumper)->dump($user->fresh()->toArray());
}
// @codeCoverageIgnoreEnd

return 0;
}
}
1 change: 1 addition & 0 deletions api/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
'App\Console\Commands\ApiaryValidateCommand',
'App\Console\Commands\GenerateKeysCommand',
'App\Console\Commands\CreateUserCommand',
];

/**
Expand Down
31 changes: 17 additions & 14 deletions api/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,24 @@ class User extends IndexedModel implements AuthenticatableContract, SocialiteAut

/**
* Model validation.
*
* @var array
* @param null $entityId
* @return array
*/
protected static $validationRules = [
'user_id' => 'required|uuid',
'username' => 'required|between:3,50|alpha_dash_space',
'email' => 'required|email',
'email_confirmed' => 'date',
'first_name' => 'string',
'last_name' => 'string',
'country' => 'country',
'region_code' => 'string|supported_region',
'timezone_identifier' => 'timezone',
'avatar_img_id' => 'uuid',
];
public static function getValidationRules($entityId = null)
{
return [
'user_id' => 'required|uuid',
'username' => 'required|between:3,50|alpha_dash_space|unique:users,username,'.$entityId.',user_id',
'email' => 'required|email|unique:users,email,'.$entityId.',user_id',
'email_confirmed' => 'date',
'first_name' => 'string',
'last_name' => 'string',
'country' => 'country',
'region_code' => 'string|supported_region',
'timezone_identifier' => 'timezone',
'avatar_img_id' => 'uuid',
];
}

/**
* The attributes that should be mutated to datetimes.
Expand Down
91 changes: 91 additions & 0 deletions api/tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

use App\Console\Commands\GenerateKeysCommand;
use App\Models\Role;
use App\Models\User;
use Mockery as m;
use App\Console\Commands\CreateUserCommand;

class CommandTest extends TestCase
{
Expand All @@ -19,6 +23,7 @@ public function testGenerateKeysCommand()
->shouldReceive('put')->andReturn(true);

$this->app->instance('Illuminate\Filesystem\Filesystem', $file);
/** @var GenerateKeysCommand $cmd */
$cmd = $this->app->make('App\Console\Commands\GenerateKeysCommand');

$this->assertEquals(0, $cmd->handle());
Expand All @@ -32,8 +37,94 @@ public function testGenerateKeysCommandMakeDirectory()
->shouldReceive('put')->andReturn(true);

$this->app->instance('Illuminate\Filesystem\Filesystem', $file);
/** @var GenerateKeysCommand $cmd */
$cmd = $this->app->make('App\Console\Commands\GenerateKeysCommand');

$this->assertEquals(0, $cmd->handle());
}

public function testCreateUserCommand()
{
$email = 'john.command.example@example.com';
$name = 'John';
$username = 'john';
$password = 'hunter2';
$role = Role::ADMIN_ROLE;

$cmd = $this->mockUserCreateCommand($email, $name, $username, $password, $role);

$cmd->shouldReceive('info')->once()->andReturn(null);

$this->assertEquals(0, $cmd->handle());

/** @var User $userCreated */
$userCreated = User::where('email', '=', $email)->first();

$this->assertNotNull($userCreated, 'User was not created');

$this->assertEquals($email, $userCreated->email);
$this->assertEquals($name, $userCreated->first_name);
$this->assertEquals($username, $userCreated->username);

$this->assertTrue($userCreated->roles->contains(function ($key, Role $roleModel) use ($role) {
return $roleModel->key == $role;
}), 'Role is applied');

$this->assertTrue(Auth::attempt(['email' => $email, 'password' => $password]), 'Credentials work.');
}

public function testCreateInvalidUserCommand()
{
$email = 'invalid-email';
$name = 'John';
$username = 'john';
$password = 'hunter2';
$role = Role::ADMIN_ROLE;

$cmd = $this->mockUserCreateCommand($email, $name, $username, $password, $role);

$cmd->shouldReceive('error')->once()->andReturn(null);
$this->assertEquals(1, $cmd->handle());
}

/**
* @param $email
* @param $name
* @param $username
* @param $password
* @param $role
* @return CreateUserCommand
*/
private function mockUserCreateCommand($email, $name, $username, $password, $role)
{
/** @var CreateUserCommand $cmd */
$cmd = Mockery::mock(CreateUserCommand::class.'[ask, secret, choice, info, error]');

$cmd->shouldReceive('ask')
->with('Enter email')
->once()
->andReturn($email);

$cmd->shouldReceive('ask')
->with('Enter first name')
->once()
->andReturn($name);

$cmd->shouldReceive('ask')
->with('Enter username', $username)
->once()
->andReturn($username);

$cmd->shouldReceive('secret')
->with('Enter password')
->once()
->andReturn($password);

$cmd->shouldReceive('choice')
->with('What roles should be applied? (comma separate options)', ROLE::$roles, null, null, true)
->once()
->andReturn([$role]);

return $cmd;
}
}

0 comments on commit 2e70cf2

Please sign in to comment.