Skip to content

Instantly share code, notes, and snippets.

@dougogodinho
Last active February 13, 2016 22:06
Show Gist options
  • Save dougogodinho/3b802d59f2566d645066 to your computer and use it in GitHub Desktop.
Save dougogodinho/3b802d59f2566d645066 to your computer and use it in GitHub Desktop.
<?php
use App\User;
class UserTest extends TestCase
{
/**
* @var \Faker\Generator
*/
private $faker;
/**
* UserPositiveTest constructor.
*/
public function __construct()
{
$this->faker = Faker\Factory::create('pt_BR');
}
/**
* Make a random user array.
*
* @param array $attributes
* @return array
*/
protected function factory($attributes = [])
{
return array_merge([
'name' => $this->faker->name,
'email' => $this->faker->email,
'password' => $pass = str_random(10),
'password_confirmation' => $pass,
], $attributes);
}
/**
* Test for Store, Login and Logout actions.
*/
public function testStoreLoginLogout()
{
$user = $this->factory();
$structure = ['id', 'name', 'email'];
// store
$this->json('post', '/user', $user)->seeJsonStructure($structure);
// login
$this->json('post', '/login', $user)->seeJsonStructure($structure);
// login check
$this->json('get', '/login')->seeJsonStructure($structure);
// logout
$this->json('post', '/logout')->seeJsonEquals([]);
// login check empty
$this->json('get', '/login')->seeJsonEquals([]);
}
/**
* Test for Show action.
*/
public function testShow()
{
$user = User::orderByRaw('RAND()')->first();
$this->json('get', '/user/' . $user->id)->seeJsonEquals($user->toArray());
}
/**
* Test for Update action.
*/
public function testUpdate1()
{
$user = User::orderByRaw('RAND()')->first();
// fast login
$this->be($user);
$this->json('put', '/user/' . $user['id'], $this->factory([
'password' => '',
'password_confirmation' => ''
]));
$check = User::find($user['id']);
$this->seeJsonEquals($check->toArray());
$this->assertEquals($user->password, $check->password);
}
/**
* Test for Update action.
*/
public function testUpdate2()
{
$user = User::orderByRaw('RAND()')->first();
$this->be($user);
// edit yourself (new password)
$data = $this->factory();
$this->json('put', '/user/' . $user['id'], $data)->seeJsonEquals(User::find($user['id'])->toArray());
$check = User::find($user['id']);
$this->assertNotEquals($user->password, $check->password);
// check pass encription
$this->assertTrue(Hash::check($data['password'], $check->password));
}
//////////////////////////////
//////////////////////////////
//// NEGATIVE TESTS BELOW ////
//////////////////////////////
//////////////////////////////
/**
* Negative Test for Store action.
* Invalid email, name and password confirmation
*/
public function testNegativeStore1()
{
$this->json('post', '/user', $this->factory([
'name' => 'Uub',
'email' => 'invalid@email',
'password_confirmation' => 'wrong'
]))->seeJsonStructure([
'errors' => [
'name',
'email',
'password',
'password_confirmation',
]
])->seeStatusCode(422);
}
/**
* Negative Test for Store action.
* Not unique email and invalid passwords
*/
public function testNegativeStore2()
{
$user = User::orderByRaw('RAND()')->first();
$this->json('post', '/user', $this->factory([
'email' => $user->email,
'password' => $pass = str_random(5),
'password_confirmation' => $pass,
]))->seeJsonStructure([
'errors' => [
'email',
'password',
'password_confirmation',
]
]);
}
/**
* Negative Test for Login action.
* With and without existent e-mail
*/
public function testNegativeLogin()
{
$errorStructure = ['errors' => ['email', 'password']];
$this->json('post', '/login', $this->factory())->seeJsonStructure($errorStructure)->seeStatusCode(401);
$data = $this->factory(['email' => User::orderByRaw('RAND()')->first()->email]);
$this->json('post', '/login', $data)->seeJsonStructure($errorStructure)->seeStatusCode(401);
}
/**
* Negative Test for Update action.
* Invalid email, name and password confirmation
*/
public function testNegativeUpdate1()
{
$user = User::orderByRaw('RAND()')->first();
$this->be($user);
$this->json('put', '/user/' . $user->id, $this->factory([
'name' => 'Uub',
'email' => 'invalid@email',
'password_confirmation' => 'wrong'
]))->seeJsonStructure([
'errors' => [
'name',
'email',
'password',
'password_confirmation',
]
])->seeStatusCode(422);
}
/**
* Negative Test for Update action.
* Not unique email and invalid passwords
*/
public function testNegativeUpdate2()
{
$user = User::orderByRaw('RAND()')->first();
$this->be($user);
$another_user = User::where('id', '!=', $user->id)->orderByRaw('RAND()')->first();
$this->json('put', '/user/' . $user->id, $this->factory([
'email' => $another_user->email,
'password' => $pass = str_random(5),
'password_confirmation' => $pass,
]))->seeJsonStructure([
'errors' => [
'email',
'password',
'password_confirmation',
]
]);
}
/**
* Negative Test for Update action.
* Not logged in error
*/
public function testNegativeUpdate3()
{
$user = User::orderByRaw('RAND()')->first();
$this->json('put', '/user/' . $user->id, $this->factory())->seeStatusCode(401);
}
/**
* Negative Test for Update action.
* Logged as another user
*/
public function testNegativeUpdate4()
{
$user = User::orderByRaw('RAND()')->first();
$this->be($user);
$another_user = User::where('id', '!=', $user->id)->orderByRaw('RAND()')->first();
$this->json('put', '/user/' . $another_user->id, $this->factory())->seeStatusCode(403);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment