2FAuth/tests/Feature/Http/Auth/UserControllerTest.php

147 lines
3.7 KiB
PHP
Raw Normal View History

2022-03-31 08:38:35 +02:00
<?php
2022-03-31 12:09:25 +02:00
namespace Tests\Feature\Http\Auth;
2022-03-31 08:38:35 +02:00
2022-07-30 17:51:02 +02:00
use App\Facades\Settings;
2022-11-22 15:15:52 +01:00
use App\Models\User;
2022-03-31 08:38:35 +02:00
use Illuminate\Support\Facades\Config;
2022-11-22 15:15:52 +01:00
use Tests\FeatureTestCase;
2022-03-31 08:38:35 +02:00
2022-12-09 10:52:17 +01:00
/**
* @covers \App\Http\Controllers\Auth\UserController
* @covers \App\Http\Middleware\RejectIfDemoMode
*/
2022-03-31 08:38:35 +02:00
class UserControllerTest extends FeatureTestCase
{
/**
* @var \App\Models\User
2022-11-22 15:15:52 +01:00
*/
2022-03-31 08:38:35 +02:00
protected $user;
private const NEW_USERNAME = 'Jane DOE';
2022-11-22 15:15:52 +01:00
2022-03-31 08:38:35 +02:00
private const NEW_EMAIL = 'janedoe@example.org';
2022-11-22 15:15:52 +01:00
private const PASSWORD = 'password';
2022-03-31 08:38:35 +02:00
/**
* @test
*/
2022-12-13 12:07:29 +01:00
public function setUp() : void
2022-03-31 08:38:35 +02:00
{
parent::setUp();
$this->user = User::factory()->create();
}
/**
* @test
*/
public function test_update_user_returns_success()
{
$response = $this->actingAs($this->user, 'web-guard')
->json('PUT', '/user', [
2022-11-22 15:15:52 +01:00
'name' => self::NEW_USERNAME,
'email' => self::NEW_EMAIL,
2022-03-31 08:38:35 +02:00
'password' => self::PASSWORD,
])
->assertOk()
->assertExactJson([
2022-11-22 15:15:52 +01:00
'name' => self::NEW_USERNAME,
2022-03-31 08:38:35 +02:00
'id' => $this->user->id,
'email' => self::NEW_EMAIL,
]);
}
/**
* @test
*/
public function test_update_user_in_demo_mode_returns_unchanged_user()
{
2022-07-30 17:51:02 +02:00
Settings::set('isDemoApp', true);
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'web-guard')
->json('PUT', '/user', [
2022-11-22 15:15:52 +01:00
'name' => self::NEW_USERNAME,
'email' => self::NEW_EMAIL,
2022-03-31 08:38:35 +02:00
'password' => self::PASSWORD,
])
->assertOk()
->assertExactJson([
2022-11-22 15:15:52 +01:00
'name' => $this->user->name,
2022-03-31 08:38:35 +02:00
'id' => $this->user->id,
'email' => $this->user->email,
]);
}
/**
* @test
*/
public function test_update_user_passing_wrong_password_returns_bad_request()
{
$response = $this->actingAs($this->user, 'web-guard')
->json('PUT', '/user', [
2022-11-22 15:15:52 +01:00
'name' => self::NEW_USERNAME,
'email' => self::NEW_EMAIL,
2022-03-31 08:38:35 +02:00
'password' => 'wrongPassword',
])
->assertStatus(400);
}
/**
* @test
*/
public function test_update_user_with_invalid_data_returns_validation_error()
{
$response = $this->actingAs($this->user, 'web-guard')
->json('PUT', '/user', [
2022-11-22 15:15:52 +01:00
'name' => '',
'email' => '',
2022-03-31 08:38:35 +02:00
'password' => self::PASSWORD,
])
->assertStatus(422);
}
/**
* @test
*/
public function test_delete_user_returns_success()
{
$response = $this->actingAs($this->user, 'web-guard')
->json('DELETE', '/user', [
'password' => self::PASSWORD,
])
->assertNoContent();
}
/**
* @test
*/
public function test_delete_user_in_demo_mode_returns_unauthorized()
{
Config::set('2fauth.config.isDemoApp', true);
2022-07-30 17:51:02 +02:00
Settings::set('isDemoApp', true);
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'web-guard')
->json('DELETE', '/user', [
'password' => self::PASSWORD,
])
->assertUnauthorized()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2022-03-31 08:38:35 +02:00
]);
}
/**
* @test
*/
public function test_delete_user_passing_wrong_password_returns_bad_request()
{
$response = $this->actingAs($this->user, 'web-guard')
->json('DELETE', '/user', [
'password' => 'wrongPassword',
])
->assertStatus(400);
}
2022-11-22 15:15:52 +01:00
}