2FAuth/tests/Unit/UserTest.php

104 lines
2.1 KiB
PHP
Raw Normal View History

2019-05-25 23:51:20 +02:00
<?php
namespace Tests\Unit;
2019-05-29 11:04:12 +02:00
use App\User;
2019-06-06 13:40:06 +02:00
use Tests\TestCase;
2019-05-25 23:51:20 +02:00
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
{
2019-06-06 13:40:06 +02:00
/** @var \App\User */
protected $user;
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create();
}
2019-05-25 23:51:20 +02:00
/**
* test User creation via API
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-25 23:51:20 +02:00
*/
public function testUserCreation()
{
$response = $this->json('POST', '/api/register', [
'name' => 'testCreate',
2019-06-06 13:40:06 +02:00
'email' => 'testCreate@example.org',
2019-05-25 23:51:20 +02:00
'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
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-25 23:51:20 +02:00
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
2019-06-06 13:40:06 +02:00
'email' => $this->user->email,
'password' => 'password'
2019-05-25 23:51:20 +02:00
]);
2019-05-29 11:04:12 +02:00
$response->assertStatus(200)
->assertJsonStructure([
'success' => ['token']
]);
}
/**
* test User logout via API
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-29 11:04:12 +02:00
*/
public function testUserLogout()
{
2019-06-06 13:40:06 +02:00
$credentials = [
'email' => $this->user->email,
'password' => 'password'
2019-05-29 11:04:12 +02:00
];
2019-06-06 13:40:06 +02:00
Auth::attempt($credentials);
2019-05-29 11:04:12 +02:00
$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
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-29 11:04:12 +02:00
*/
public function testGetUserDetails()
{
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
2019-05-29 11:04:12 +02:00
->json('GET', '/api/user')
->assertStatus(200)
2019-06-06 13:40:06 +02:00
->assertJsonStructure(['id', 'name', 'email']);
2019-05-25 23:51:20 +02:00
}
}