2019-05-25 23:51:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use Tests\TestCase;
|
2019-05-29 11:04:12 +02:00
|
|
|
use App\User;
|
2019-05-25 23:51:20 +02:00
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Illuminate\Auth\Authenticatable;
|
2019-05-29 11:04:12 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-05-25 23:51:20 +02:00
|
|
|
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* test User creation via API
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUserCreation()
|
|
|
|
{
|
|
|
|
$response = $this->json('POST', '/api/register', [
|
|
|
|
'name' => 'testCreate',
|
|
|
|
'email' => str_random(10) . '@test.com',
|
|
|
|
'password' => 'test',
|
|
|
|
]);
|
|
|
|
|
2019-05-29 11:04:12 +02:00
|
|
|
$response->assertStatus(200)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'success' => ['token', 'name']
|
|
|
|
]);
|
2019-05-25 23:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test User login via API
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUserLogin()
|
|
|
|
{
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
|
|
'email' => 'test@test.com',
|
|
|
|
'password' => 'test'
|
|
|
|
]);
|
|
|
|
|
2019-05-29 11:04:12 +02:00
|
|
|
$response->assertStatus(200)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'success' => ['token']
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test User logout via API
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUserLogout()
|
|
|
|
{
|
|
|
|
$user = ['email' => 'test@test.com',
|
|
|
|
'password' => 'test'
|
|
|
|
];
|
|
|
|
|
|
|
|
Auth::attempt($user);
|
|
|
|
$token = Auth::user()->createToken('testToken')->accessToken;
|
|
|
|
$headers = ['Authorization' => "Bearer $token"];
|
|
|
|
|
|
|
|
$response = $this->json('POST', '/api/logout', [], $headers)
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertJson([
|
|
|
|
'success' => 'signed out',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test User logout via API
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetUserDetails()
|
|
|
|
{
|
|
|
|
$user = \App\User::find(1);
|
|
|
|
|
|
|
|
$response = $this->actingAs($user, 'api')
|
|
|
|
->json('GET', '/api/user')
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertJsonFragment([
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'testLogin',
|
|
|
|
'email' => 'test@test.com',
|
|
|
|
]);
|
2019-05-25 23:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|