2FAuth/tests/Unit/UserTest.php

176 lines
3.6 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',
2020-01-10 13:43:36 +01:00
'password_confirmation' => 'test',
2019-05-25 23:51:20 +02:00
]);
2019-05-29 11:04:12 +02:00
$response->assertStatus(200)
->assertJsonStructure([
'success' => ['token', 'name']
]);
2019-05-25 23:51:20 +02:00
}
2020-01-10 13:43:36 +01:00
/**
* test User creation with missing values via API
*
* @test
*/
public function testUserCreationWithMissingValues()
{
$response = $this->json('POST', '/api/register', [
'name' => '',
'email' => '',
'password' => '',
'password_confirmation' => '',
]);
$response->assertStatus(400);
}
/**
* test User creation with invalid values via API
*
* @test
*/
public function testUserCreationWithInvalidData()
{
$response = $this->json('POST', '/api/register', [
'name' => 'testInvalid',
'email' => 'email',
'password' => 'test',
'password_confirmation' => 'tset',
]);
$response->assertStatus(400);
}
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']
]);
}
2020-01-10 13:43:36 +01:00
/**
* test User login with missing values via API
*
* @test
*/
public function testUserLoginWithMissingValues()
{
$response = $this->json('POST', '/api/login', [
'email' => '',
'password' => ''
]);
$response->assertStatus(400);
}
/**
* test User login with invalid credentials via API
*
* @test
*/
public function testUserLoginWithInvalidCredential()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response->assertStatus(401)
->assertJson([
'error' => 'Unauthorised'
]);
}
2019-05-29 11:04:12 +02:00
/**
* 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
}
}