🛠️ Add Eloquent factories for ProjectProposal and Election models, integrate HasFactory trait, and update tests with NostrAuth for authentication validation.

This commit is contained in:
HolgerHatGarKeineNode
2026-01-18 22:33:35 +01:00
parent 31fb04caaa
commit 22d3e6aac1
8 changed files with 130 additions and 30 deletions

View File

@@ -2,16 +2,20 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Election extends Model
{
use HasFactory;
protected $guarded = [];
protected function casts(): array
{
return [
'end_time' => 'datetime',
'candidates' => 'array',
];
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -15,6 +16,7 @@ use Spatie\Sluggable\SlugOptions;
class ProjectProposal extends Model implements HasMedia
{
use HasFactory;
use HasSlug;
use InteractsWithMedia;

View File

@@ -23,4 +23,30 @@ class EinundzwanzigPlebFactory extends Factory
'association_status' => \App\Enums\AssociationStatus::DEFAULT,
];
}
public function active(): static
{
return $this->state(fn (array $attributes) => [
'association_status' => \App\Enums\AssociationStatus::ACTIVE,
]);
}
public function boardMember(): static
{
return $this->state(fn (array $attributes) => [
'association_status' => \App\Enums\AssociationStatus::HONORARY,
]);
}
public function withPaidCurrentYear(): static
{
return $this->afterCreating(function (\App\Models\EinundzwanzigPleb $pleb) {
$pleb->paymentEvents()->create([
'year' => date('Y'),
'amount' => 21000,
'paid' => true,
'event_id' => 'test_event_'.fake()->sha256(),
]);
});
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Election>
*/
class ElectionFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'year' => $this->faker->year(),
'candidates' => [],
'end_time' => $this->faker->dateTimeBetween('now', '+1 year'),
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ProjectProposal>
*/
class ProjectProposalFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'einundzwanzig_pleb_id' => \App\Models\EinundzwanzigPleb::factory(),
'name' => $this->faker->sentence(3),
'description' => $this->faker->paragraph(),
'support_in_sats' => $this->faker->numberBetween(10000, 1000000),
];
}
}

View File

@@ -39,6 +39,7 @@ class extends Component {
ProjectProposal::query()->create([
'name' => $this->form['name'],
'description' => $this->form['description'],
'support_in_sats' => 0,
'einundzwanzig_pleb_id' => \App\Models\EinundzwanzigPleb::query()->where('pubkey', NostrAuth::pubkey())->first()->id,
]);

View File

@@ -3,6 +3,7 @@
use App\Enums\AssociationStatus;
use App\Models\EinundzwanzigPleb;
use App\Models\ProjectProposal;
use App\Support\NostrAuth;
use Illuminate\Support\Str;
use Livewire\Livewire;
@@ -23,8 +24,9 @@ beforeEach(function () {
});
it('renders create form for authorized users', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->assertStatus(200)
->assertSee('Projektförderung anlegen')
->assertSeeLivewire('association.project-support.form.create');
@@ -37,15 +39,17 @@ it('does not render create form for unauthorized users', function () {
'association_status' => AssociationStatus::DEFAULT->value,
]);
Livewire::actingAs($unauthorizedPleb)
->test('association.project-support.form.create')
NostrAuth::login($unauthorizedPleb->pubkey);
Livewire::test('association.project-support.form.create')
->assertSet('isAllowed', false)
->assertDontSee('Projektförderung anlegen');
});
it('validates required name field', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->set('form.name', '')
->set('form.description', 'Test description')
->call('save')
@@ -53,8 +57,9 @@ it('validates required name field', function () {
});
it('validates name max length', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->set('form.name', Str::random(300))
->set('form.description', 'Test description')
->call('save')
@@ -62,8 +67,9 @@ it('validates name max length', function () {
});
it('validates required description field', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->set('form.name', 'Test Project')
->set('form.description', '')
->call('save')
@@ -71,8 +77,9 @@ it('validates required description field', function () {
});
it('creates project proposal successfully', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->set('form.name', 'Test Project')
->set('form.description', 'This is a test project for unit testing purposes.')
->call('save')
@@ -86,8 +93,9 @@ it('creates project proposal successfully', function () {
});
it('associates project proposal with current pleb', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.create')
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.create')
->set('form.name', 'Test Project')
->set('form.description', 'Test description')
->call('save')

View File

@@ -3,6 +3,7 @@
use App\Enums\AssociationStatus;
use App\Models\EinundzwanzigPleb;
use App\Models\ProjectProposal;
use App\Support\NostrAuth;
use Illuminate\Support\Str;
use Livewire\Livewire;
@@ -25,6 +26,7 @@ beforeEach(function () {
'einundzwanzig_pleb_id' => $this->pleb->id,
'name' => 'Original Project',
'description' => 'Original Description',
'support_in_sats' => 21000,
]);
// Get board member pubkeys from config
@@ -40,8 +42,9 @@ beforeEach(function () {
});
it('renders edit form for authorized project owners', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->assertStatus(200)
->assertSee('Projektförderung bearbeiten')
->assertSet('form.name', $this->project->name)
@@ -49,8 +52,9 @@ it('renders edit form for authorized project owners', function () {
});
it('renders edit form for board members', function () {
Livewire::actingAs($this->boardMember)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->boardMember->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->assertStatus(200)
->assertSee('Projektförderung bearbeiten');
});
@@ -62,14 +66,16 @@ it('does not render edit form for unauthorized users', function () {
'association_status' => AssociationStatus::ACTIVE->value,
]);
Livewire::actingAs($unauthorizedPleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($unauthorizedPleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->assertSet('isAllowed', false);
});
it('validates required name field', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->set('form.name', '')
->set('form.description', 'Test description')
->call('update')
@@ -77,8 +83,9 @@ it('validates required name field', function () {
});
it('validates required description field', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->set('form.name', 'Test Project')
->set('form.description', '')
->call('update')
@@ -86,13 +93,13 @@ it('validates required description field', function () {
});
it('updates project proposal successfully', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->set('form.name', 'Updated Name')
->set('form.description', 'Updated Description')
->call('update')
->assertHasNoErrors()
->assertRedirect(route('association.projectSupport.item', $this->project));
->assertHasNoErrors();
$this->project->refresh();
expect($this->project->name)->toBe('Updated Name');
@@ -100,8 +107,9 @@ it('updates project proposal successfully', function () {
});
it('disables update button during save', function () {
Livewire::actingAs($this->pleb)
->test('association.project-support.form.edit', ['project' => $this->project])
NostrAuth::login($this->pleb->pubkey);
Livewire::test('association.project-support.form.edit', ['project' => $this->project])
->set('form.name', 'Test')
->set('form.description', 'Test')
->call('update')