improve 4 test

This commit is contained in:
2025-05-11 11:10:40 +03:30
parent 0a2b324041
commit f2fda29041
4 changed files with 196 additions and 197 deletions

View File

@@ -12,33 +12,6 @@ class LoginTest extends TestCase
{ {
use RefreshDatabase, WithFaker; use RefreshDatabase, WithFaker;
// public function test_user_can_login()
// {
// $this->withoutExceptionHandling();
//
// $this->artisan('passport:install');
//
// $user = User::factory()->create();
//
// $response = $this->postJson('/api/auth/login', [
// 'username' => $user->username,
// 'password' => 'password',
// ]);
//
// $token = Token::where('user_id', $user->id)->first();
//
// $response->assertStatus(200)
// ->assertJson([
// 'message' => __('messages.successful')
// ]);
//
// $this->assertDatabaseHas('oauth_access_tokens' , [
// 'user_id' => $user->id,
// ]);
//
// $this->assertInstanceOf(Token::class, $token);
// }
public function test_user_can_login() public function test_user_can_login()
{ {
$user = User::factory() $user = User::factory()
@@ -85,6 +58,7 @@ class LoginTest extends TestCase
'password' => __('validation.required', ['attribute' => 'رمز عبور']) 'password' => __('validation.required', ['attribute' => 'رمز عبور'])
]); ]);
} }
public function test_telephone_id_is_required() public function test_telephone_id_is_required()
{ {
$response = $this->postJson('/server/auth/login', [ $response = $this->postJson('/server/auth/login', [

View File

@@ -1,129 +1,114 @@
<?php <?php
//
//namespace Tests\Feature\Call; namespace Tests\Feature\Call;
//
//use App\Models\Call; use App\Models\Call;
//use App\Models\Permission; use App\Models\Permission;
//use App\Models\User; use App\Models\User;
//use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
//use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
//use Illuminate\Support\Facades\Config; use Illuminate\Testing\Fluent\AssertableJson;
//use Illuminate\Testing\Fluent\AssertableJson; use Tests\TestCase;
//use Tests\TestCase;
// class ListTest extends TestCase
//class ListTest extends TestCase {
//{ use RefreshDatabase, WithFaker;
// use RefreshDatabase, WithFaker;
// public function test_unauthenticated_user_cannot_access(): void
// public function test_unauthenticated_user_cannot_access(): void {
// { $response = $this->getJson(route('calls.list'));
// $response = $this->getJson(route('calls.list')); $response->assertStatus(401);
// $response->assertStatus(401); }
// }
// public function test_user_without_permission_cannot_access(): void
// public function test_user_without_permission_cannot_access(): void {
// { $user = User::factory()->create();
// $user = User::factory()->create(); $this->actingAs($user);
// $this->actingAs($user);
// $response = $this->getJson(route('calls.list'));
// $response = $this->getJson(route('calls.list')); $response->assertForbidden();
// $response->assertForbidden(); }
// }
// public function test_size_must_be_integer(): void
// public function test_phone_number_must_be_string(): void {
// { $user = User::factory()
// $user = User::factory() ->has(Permission::factory([
// ->has(Permission::factory([ 'name' => 'manage_calls',
// 'name' => 'manage_calls', 'name_fa' => 'مدیریت تماس‌ها',
// 'name_fa' => 'مدیریت تماس‌ها', ]))->create();
// ])) $this->actingAs($user);
// ->create();
// $response = $this->getJson(route('calls.list', [
// // 🟢 احراز هویت کاربر قبل از ارسال درخواست 'size' => $this->faker->word,
// $this->actingAs($user); ]));
//
// $response = $this->getJson(route('calls.list', [ $response->assertUnprocessable()
// 'phone_number' => $this->faker->in, ->assertInvalid([
// ])); 'size' => __('validation.integer', ['attribute' => 'حداکثر تعداد']),
// ]);
// $response->assertUnprocessable() }
// ->assertInvalid([
// 'phone_number' => __('validation.string', ['attribute' => 'شماره تلفن']), public function test_calls_are_limited_by_size_parameter(): void
// ]); {
// }
// $user = User::factory()
// ->has(Permission::factory()->state([
// public function test_size_must_be_integer(): void 'id' => 3,
// { 'name' => 'manage_calls',
// $user = $this->createUserWithPermission(); 'name_fa' => 'مدیریت تماس‌ها',
// $this->actingAs($user); ]))
// ->create();
// $response = $this->getJson(route('calls.list', [ Call::factory()->count(5)->create();
// 'size' => 'not-an-integer',
// ]));
// $response = $this->actingAs($user)->getJson(route('calls.list', [
// $response->assertUnprocessable() 'size' => $this->faker->numberBetween(1, 4),
// ->assertInvalid([ ]));
// 'size' => __('validation.integer', ['attribute' => 'حداکثر تعداد']),
// ]); $response->assertOk()
// } ->assertJson(fn (AssertableJson $json) =>
// $json->has('data')
// public function test_calls_are_returned_with_limit(): void ->has('data.0', fn (AssertableJson $json) => $json->hasAll([
// { 'id',
// $user = $this->createUserWithPermission(); 'operator_id',
// $this->actingAs($user); 'operator_full_name',
// 'operator_username',
// Call::factory()->count(8)->create(); 'telephone_id',
// 'caller_phone_number',
// $response = $this->getJson(route('calls.list', ['size' => 5])); 'description',
// 'category_id',
// $response->assertOk() 'category_name',
// ->assertJsonCount(5, 'data') 'subcategory_id',
// ->assertJson(fn (AssertableJson $json) => 'subcategory_name',
// $json->has('message') 'created_at',
// ->has('data') ]))
// ->has('data.0', fn (AssertableJson $json) => );
// $json->hasAll([
// 'id', }
// 'operator_id',
// 'operator_full_name',
// 'operator_username', public function test_calls_are_filtered_by_phone_number(): void
// 'telephone_id', {
// 'caller_phone_number', $user = User::factory()
// 'description', ->has(Permission::factory([
// 'category_id', 'name' => 'manage_calls',
// 'category_name', 'name_fa' => 'مدیریت تماس‌ها',
// 'subcategory_id', ]))->create();
// 'subcategory_name',
// 'created_at', $targetPhone = '09' . $this->faker->numberBetween(100000000, 999999999);
// ])
// ) Call::factory()->count(4)->create([
// ); 'caller_phone_number' => $targetPhone,
// } ]);
//
// public function test_calls_are_filtered_by_phone_number(): void Call::factory()->count(2)->create();
// {
// $user = $this->createUserWithPermission();
// $this->actingAs($user); $response = $this->actingAs($user)->getJson(route('calls.list', ['phone_number' => $targetPhone]));
//
// $targetPhone = '09' . $this->faker->numberBetween(100000000, 999999999); $response->assertOk()
// Call::factory()->count(4)->create(['caller_phone_number' => $targetPhone]); ->assertJsonCount(4, 'data')
// Call::factory()->count(2)->create(); // other numbers ->assertJsonFragment(['caller_phone_number' => $targetPhone]);
// }
// $response = $this->getJson(route('calls.list', ['phone_number' => $targetPhone]));
// }
// $response->assertOk()
// ->assertJsonCount(4, 'data')
// ->assertJsonFragment(['caller_phone_number' => $targetPhone]);
// }
//
// private function createUserWithPermission(): User
// {
// return User::factory()
// ->has(Permission::factory([
// 'name' => 'manage_calls',
// 'name_fa' => 'مدیریت تماس‌ها',
// ]))
// ->create();
// }
//}

View File

@@ -2,14 +2,13 @@
namespace Tests\Feature\Call; namespace Tests\Feature\Call;
use App\Models\Call;
use App\Models\CallHistory;
use App\Models\Event; use App\Models\Event;
use App\Models\User; use App\Models\User;
use App\Enums\CallEvents; use App\Enums\CallEvents;
use App\Services\Notification\NotificationService; use App\Services\Notification\NotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Mockery\MockInterface; use Mockery\MockInterface;
use Tests\TestCase; use Tests\TestCase;
@@ -36,7 +35,7 @@ class StoreTest extends TestCase
$response = $this->actingAs($user)->postJson(route('calls.store'), [ $response = $this->actingAs($user)->postJson(route('calls.store'), [
'extension' => $this->faker->numberBetween(1000, 9999), 'extension' => $this->faker->numberBetween(1000, 9999),
'caller_id' => $this->faker->numerify('09#########'), 'caller_id' => $this->faker->randomNumber(),
]); ]);
$response->assertUnprocessable() $response->assertUnprocessable()
@@ -50,8 +49,8 @@ class StoreTest extends TestCase
$user = User::factory()->create(); $user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('calls.store'), [ $response = $this->actingAs($user)->postJson(route('calls.store'), [
'extension' => 'invalid-ext-' . $this->faker->uuid, 'extension' => $this->faker->uuid,
'caller_id' => $this->faker->numerify('09#########'), 'caller_id' => $this->faker->randomNumber(),
]); ]);
$response->assertUnprocessable() $response->assertUnprocessable()
@@ -77,10 +76,11 @@ class StoreTest extends TestCase
public function test_can_create_a_call(): void public function test_can_create_a_call(): void
{ {
$operator = User::factory()->make(); // bypass fillable $operator = User::factory()->create();
$operator->telephone_id = 'tel-' . $this->faker->unique()->numerify('###'); $operator->telephone_id = 'tel-' . $this->faker->unique()->numerify('###');
$operator->save(); $operator->save();
DB::table('events')->truncate();
$this->artisan('db:seed --class=EventSeeder'); $this->artisan('db:seed --class=EventSeeder');
$this->mock(NotificationService::class, function (MockInterface $mock) { $this->mock(NotificationService::class, function (MockInterface $mock) {

View File

@@ -5,7 +5,6 @@ namespace Tests\Feature\Call;
use App\Enums\CallEvents; use App\Enums\CallEvents;
use App\Models\Call; use App\Models\Call;
use App\Models\Category; use App\Models\Category;
use App\Models\Event;
use App\Models\Permission; use App\Models\Permission;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -16,6 +15,55 @@ class UpdateTest extends TestCase
{ {
use RefreshDatabase, WithFaker; use RefreshDatabase, WithFaker;
public function test_operator_is_not_authenticated(): void
{
$response = $this->postJson(route('calls.update', ['call' => 1]));
$response->assertUnauthorized();
}
public function test_operator_without_unappropriated_permission_is_forbidden_to_access_calls_data(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$response = $this->actingAs($user)->postJson(route('calls.update', ['call' => $call->id]));
$response->assertForbidden();
}
public function test_operator_can_not_access_to_other_operators_calls(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->create([
'telephone_id' => $telephoneId,
]);
$another_operator = User::factory([
'id' => $this->faker->randomNumber(2, true)
])->create();
$call = Call::factory([
'telephone_id' => $another_operator->telephone_id
])->create();
$response = $this->actingAs($user)->postJson(route('calls.update', ['call' => $call->id]));
$response->assertForbidden();
}
public function test_non_owner_user_cannot_update_call(): void public function test_non_owner_user_cannot_update_call(): void
{ {
$callOwner = User::factory()->create([ $callOwner = User::factory()->create([
@@ -26,14 +74,12 @@ class UpdateTest extends TestCase
'telephone_id' => $callOwner->telephone_id, 'telephone_id' => $callOwner->telephone_id,
]); ]);
$unauthorizedUser = User::factory()->create(); // no permission $user = User::factory()->create();
$this->actingAs($unauthorizedUser); $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
'category_id' => $this->faker->randomNumber(),
$response = $this->postJson(route('calls.update', $call->id), [ 'subcategory_id' => $this->faker->randomNumber(),
'category_id' => 1, 'description' => $this->faker->sentence
'subcategory_id' => 2,
'description' => '...',
]); ]);
$response->assertForbidden(); $response->assertForbidden();
@@ -56,9 +102,7 @@ class UpdateTest extends TestCase
'telephone_id' => $telephoneId, 'telephone_id' => $telephoneId,
]); ]);
$this->actingAs($user); $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), []);
$response = $this->postJson(route('calls.update', $call->id), []);
$response->assertUnprocessable() $response->assertUnprocessable()
->assertInvalid([ ->assertInvalid([
@@ -81,15 +125,13 @@ class UpdateTest extends TestCase
]); ]);
$call = Call::factory()->create([ $call = Call::factory()->create([
'telephone_id' => $telephoneId, 'telephone_id' =>$user->telephone_id,
]); ]);
$this->actingAs($user); $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
'category_id' =>$this->faker->sentence(3),
$response = $this->postJson(route('calls.update', $call->id), [ 'subcategory_id' => $this->faker->sentence(3),
'category_id' => 'not-an-int', 'description' => $this->faker->sentence,
'subcategory_id' => 'also-string',
'description' => 'توضیح',
]); ]);
$response->assertUnprocessable() $response->assertUnprocessable()
@@ -116,12 +158,12 @@ class UpdateTest extends TestCase
'telephone_id' => $telephoneId, 'telephone_id' => $telephoneId,
]); ]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 1, $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
'subcategory_id' => 2, 'category_id' => $this->faker->randomNumber(),
'description' => ['not', 'a', 'string'], 'subcategory_id' => $this->faker->randomNumber(),
'description' => $this->faker->randomNumber(),
]); ]);
$response->assertUnprocessable() $response->assertUnprocessable()
@@ -147,12 +189,12 @@ class UpdateTest extends TestCase
'telephone_id' => $telephoneId, 'telephone_id' => $telephoneId,
]); ]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 12345, $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
'subcategory_id' => 67890, 'category_id' => $this->faker->randomNumber(),
'description' => '...', 'subcategory_id' => $this->faker->randomNumber(),
'description' => $this->faker->sentence,
]); ]);
$response->assertUnprocessable() $response->assertUnprocessable()
@@ -179,14 +221,12 @@ class UpdateTest extends TestCase
]); ]);
$category = Category::factory()->create(); $category = Category::factory()->create();
$description = $this->faker->sentence;
$this->actingAs($user); $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => $category->category_id, 'category_id' => $category->category_id,
'subcategory_id' => $category->subcategory_id, 'subcategory_id' => $category->subcategory_id,
'description' => 'توضیحات تستی', 'description' => $description,
]); ]);
$response->assertOk() $response->assertOk()
->assertJson([ ->assertJson([
@@ -197,8 +237,8 @@ class UpdateTest extends TestCase
'id' => $call->id, 'id' => $call->id,
'category_id' => $category->category_id, 'category_id' => $category->category_id,
'subcategory_id' => $category->subcategory_id, 'subcategory_id' => $category->subcategory_id,
'description' => 'توضیحات تستی', 'description' => $description,
]); ]);
$this->assertDatabaseHas('call_histories', [ $this->assertDatabaseHas('call_histories', [
'call_id' => $call->id, 'call_id' => $call->id,