Kész tesztelés

This commit is contained in:
Sándor Máté Magony
2025-05-02 16:57:39 +02:00
parent aac71b9c74
commit 7b3467ee70
8 changed files with 215 additions and 23 deletions

View File

@@ -85,6 +85,16 @@ class WcController extends Controller
public function destroy($id) public function destroy($id)
{ {
if (!auth('api')->check()) {
return response()->json(['message' => 'Nincs bejelentkezve'], 401);
}
$user = auth('api')->user();
if (!$user->is_admin) {
return response()->json(['message' => 'Nincs jogosultság'], 403);
}
$mosdo = WcAdatok::find($id); $mosdo = WcAdatok::find($id);
if ($mosdo) { if ($mosdo) {
@@ -94,4 +104,6 @@ class WcController extends Controller
return response()->json(['message' => 'Nem található'], 404); return response()->json(['message' => 'Nem található'], 404);
} }
} }
} }

View File

@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Keruletek extends Model class Keruletek extends Model
@@ -10,5 +11,9 @@ class Keruletek extends Model
return $this->hasMany(WcAdatok::class); return $this->hasMany(WcAdatok::class);
} }
use HasFactory;
protected $table = 'keruletek'; protected $table = 'keruletek';
public $timestamps = false;
} }

View File

@@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Kerulet>
*/
class KeruletekFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'kerulet_nev' => 'X. kerület'
];
}
}

View File

@@ -18,25 +18,11 @@ class UserFactory extends Factory
public function definition() public function definition()
{ {
return [ return [
'name' => $this->faker->name(), 'nev' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(), 'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(), 'felh_nev' => $this->faker->userName(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'jelszo' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10), 'is_admin' => false,
]; ];
} }
/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
} }

View File

@@ -0,0 +1,32 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\WcAdatok>
*/
class WcAdatokFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'nev' => $this->faker->name(),
'kerulet_id' => 1,
'kozeli_megall' => $this->faker->text(),
'akadalym' => false,
'ar' => 200,
'nyitva' => '0:00 - 24:00',
'hossz_koord' => 47.4979,
'szel_koord' => 19.0402,
'utvonal' => 'maps.google.com',
'felhasznalo_id' => 1
];
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Keruletek;
use App\Models\WcAdatok;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MosdoTorlesTest extends TestCase
{
use RefreshDatabase;
public function test_admin_torolhet_mosdot()
{
// 1. Admin felhasználó
$admin = User::factory()->create(['is_admin' => true]);
// 2. Teszt mosdó
$kerulet = Keruletek::factory()->create();
$mosdo = WcAdatok::factory()->create(['kerulet_id' => $kerulet->id, 'felhasznalo_id' => $admin->id]);
// 3. Token generálás és kérés küldés
$response = $this->actingAs($admin, 'api')
->deleteJson("/api/mosdotorles/{$mosdo->id}");
$response->assertStatus(200);
$this->assertDatabaseMissing('wc_adatok', ['id' => $mosdo->id]);
}
public function test_nem_admin_nem_torolhet()
{
$user = User::factory()->create(['is_admin' => false]);
$kerulet = Keruletek::factory()->create();
$mosdo = WcAdatok::factory()->create(['kerulet_id' => $kerulet->id, 'felhasznalo_id' => $user->id]);
$response = $this->actingAs($user, 'api')
->deleteJson("/api/mosdotorles/{$mosdo->id}");
$response->assertStatus(403);
$this->assertDatabaseHas('wc_adatok', ['id' => $mosdo->id]);
}
public function test_anonim_felhasznalo_nem_torolhet()
{
$kerulet = Keruletek::factory()->create();
$user = User::factory()->create(['is_admin' => false]);
$mosdo = WcAdatok::factory()->create(['kerulet_id' => $kerulet->id, 'felhasznalo_id' => $user->id]);
$response = $this->deleteJson("/api/mosdotorles/{$mosdo->id}");
$response->assertStatus(401);
}
}

View File

@@ -48,7 +48,10 @@ export default function Csempe() {
if (loading) { if (loading) {
return( return(
<div className="flex justify-center items-center h-32"> <div className="flex justify-center items-center h-32">
<div className="w-12 h-12 border-4 border-gray-300 border-t-yellow-500 rounded-full animate-spin"></div> <div
role="status"
className="w-12 h-12 border-4 border-gray-300 border-t-yellow-500 rounded-full animate-spin"
></div>
</div> </div>
); );
} }

View File

@@ -0,0 +1,76 @@
import React from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import Csempe from './Csempe';
import { useAuth } from '../Bejel_Regisz/AuthContext';
import * as fetchModule from '../../apiFetch';
// AuthContext mock
jest.mock('../Bejel_Regisz/AuthContext', () => ({
useAuth: jest.fn()
}));
// mosdokFetch mock
jest.mock('../../apiFetch', () => ({
mosdokFetch: jest.fn()
}));
describe('Csempe komponens', () => {
const mockMosdok = [
{
id: 1,
nev: 'Nyilvános WC',
kerulet: { kerulet_nev: 'V. kerület' },
kozeli_megall: 'Astoria',
ar: 200,
nyitva: '0-24',
akadalym: 1,
utvonal: 'https://maps.example.com/utvonal'
}
];
beforeEach(() => {
fetch.resetMocks?.(); // ha használsz jest-fetch-mock-ot
jest.clearAllMocks();
});
test('Adminnak megjelenik a Törlés gomb', async () => {
useAuth.mockReturnValue({ user: { is_admin: true } });
fetchModule.mosdokFetch.mockResolvedValue(mockMosdok);
render(<Csempe />);
await waitFor(() => {
expect(screen.getByText(/Törlés/i)).toBeInTheDocument();
});
});
test('Nem adminnak nem jelenik meg a Törlés gomb', async () => {
useAuth.mockReturnValue({ user: { is_admin: false } });
fetchModule.mosdokFetch.mockResolvedValue(mockMosdok);
render(<Csempe />);
await waitFor(() => {
expect(screen.queryByText(/Törlés/i)).not.toBeInTheDocument();
});
});
test('Hiba esetén megjelenik hibaüzenet', async () => {
useAuth.mockReturnValue({ user: { is_admin: true } });
fetchModule.mosdokFetch.mockResolvedValue(null);
render(<Csempe />);
await waitFor(() => {
expect(screen.getByText(/Nem sikerült betölteni/i)).toBeInTheDocument();
});
});
test('Betöltés alatt megjelenik spinner', async () => {
useAuth.mockReturnValue({ user: { is_admin: true } });
fetchModule.mosdokFetch.mockImplementation(() => new Promise(() => {})); // never resolves
render(<Csempe />);
expect(screen.getByRole('status')).toBeInTheDocument(); // a spinner div-ben legyen pl. role="status"
});
});