2FAuth/tests/Unit/UserTest.php

201 lines
4.2 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
/**
2020-01-14 11:50:20 +01:00
* test creation of another user via API
2019-05-25 23:51:20 +02:00
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-25 23:51:20 +02:00
*/
2020-01-14 11:50:20 +01:00
public function testMoreThanOneUserCreation()
2019-05-25 23:51:20 +02:00
{
$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
]);
2020-01-14 11:50:20 +01:00
$response->assertStatus(400);
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()
{
2020-01-14 11:50:20 +01:00
2019-05-25 23:51:20 +02:00
$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([
2020-01-14 11:50:20 +01:00
'message' => ['token']
2019-05-29 11:04:12 +02:00
]);
}
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' => ''
]);
2020-01-20 14:23:31 +01:00
$response->assertStatus(422);
2020-01-10 13:43:36 +01:00
}
/**
* 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([
2020-01-14 11:50:20 +01:00
'message' => 'unauthorised'
2020-01-10 13:43:36 +01:00
]);
}
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([
2020-01-14 11:50:20 +01:00
'message' => 'signed out',
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 testGetUserDetails()
{
2020-01-14 11:50:20 +01:00
$user = User::find(1);
$response = $this->actingAs($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
}
2020-01-14 11:50:20 +01:00
/**
* test User creation via API
*
* @test
*/
public function testUserCreation()
{
// we delete the existing user
User::destroy(1);
$response = $this->json('POST', '/api/register', [
'name' => 'newUser',
'email' => 'newUser@example.org',
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertStatus(200)
->assertJsonStructure([
'message' => ['token', 'name']
]);
}
2019-05-25 23:51:20 +02:00
}