feat: integrate login/registration into PurchaseWizard
This commit is contained in:
@@ -14,8 +14,12 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Mockery;
|
||||
use PayPal\PayPalHttp\Client;
|
||||
use PayPalHttp\Client;
|
||||
use PayPal\Checkout\Orders\OrdersCreateRequest;
|
||||
use PayPal\Checkout\Orders\OrdersCaptureRequest;
|
||||
use PayPal\Checkout\Orders\Order;
|
||||
use App\PayPal\PayPalClient; // Assuming custom PayPalClient in App namespace
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PurchaseTest extends TestCase
|
||||
{
|
||||
@@ -88,16 +92,24 @@ class PurchaseTest extends TestCase
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
$mockClient = Mockery::mock('PayPal\PayPalHttp\Client');
|
||||
$mockOrders = Mockery::mock();
|
||||
$mockOrders->shouldReceive('createOrder')->andReturn(new \stdClass()); // Simplified mock
|
||||
$mockClient->shouldReceive('orders')->andReturn($mockOrders);
|
||||
$this->app->instance('PayPal\PayPalHttp\Client', $mockClient);
|
||||
$mockHttpClient = Mockery::mock(Client::class);
|
||||
$mockResponse = Mockery::mock();
|
||||
$mockOrder = Mockery::mock(Order::class);
|
||||
$mockOrder->id = 'test-order-id';
|
||||
$mockResponse->result = $mockOrder;
|
||||
$mockHttpClient->shouldReceive('execute')->andReturn($mockResponse);
|
||||
|
||||
$response = $this->get(route('buy.packages', $paidPackage->id) . '?provider=paypal');
|
||||
$mockPayPalClient = Mockery::mock(PayPalClient::class);
|
||||
$mockPayPalClient->shouldReceive('client')->andReturn($mockHttpClient);
|
||||
|
||||
$response->assertStatus(302);
|
||||
$this->assertNotNull(session('paypal_order_id'));
|
||||
$this->app->instance(PayPalClient::class, $mockPayPalClient);
|
||||
|
||||
$response = $this->postJson(route('api.packages.paypal-create'), [
|
||||
'package_id' => $paidPackage->id,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['orderID' => 'test-order-id']);
|
||||
}
|
||||
|
||||
public function test_paypal_success_captures_and_activates_package()
|
||||
@@ -114,22 +126,25 @@ class PurchaseTest extends TestCase
|
||||
'type' => $paidPackage->type,
|
||||
]);
|
||||
|
||||
$mockClient = Mockery::mock('PayPal\PayPalHttp\Client');
|
||||
$mockOrders = Mockery::mock();
|
||||
$mockCapture = new \stdClass();
|
||||
$mockHttpClient = Mockery::mock(Client::class);
|
||||
$mockResponse = Mockery::mock();
|
||||
$mockCapture = Mockery::mock(Order::class);
|
||||
$mockCapture->status = 'COMPLETED';
|
||||
$mockCapture->purchaseUnits = [(object)['custom_id' => $metadata]];
|
||||
$mockResponse = new \stdClass();
|
||||
$mockCapture->purchaseUnits = [ (object) ['custom_id' => $metadata] ];
|
||||
$mockResponse->result = $mockCapture;
|
||||
$mockOrders->shouldReceive('captureOrder')->andReturn($mockResponse);
|
||||
$mockClient->shouldReceive('orders')->andReturn($mockOrders);
|
||||
$this->app->instance('PayPal\PayPalHttp\Client', $mockClient);
|
||||
$mockHttpClient->shouldReceive('execute')->andReturn($mockResponse);
|
||||
|
||||
session(['paypal_order_id' => 'test-order-id']);
|
||||
$mockPayPalClient = Mockery::mock(PayPalClient::class);
|
||||
$mockPayPalClient->shouldReceive('client')->andReturn($mockHttpClient);
|
||||
|
||||
$response = $this->get(route('marketing.success', $paidPackage->id));
|
||||
$this->app->instance(PayPalClient::class, $mockPayPalClient);
|
||||
|
||||
$response->assertRedirect('/admin');
|
||||
$response = $this->postJson(route('api.packages.paypal-capture'), [
|
||||
'order_id' => 'test-order-id',
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['success' => true]);
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
@@ -138,12 +153,158 @@ class PurchaseTest extends TestCase
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
'provider_id' => 'paypal',
|
||||
'provider_id' => 'test-order-id',
|
||||
]);
|
||||
$this->assertNull(session('paypal_order_id'));
|
||||
$this->assertEquals('active', $tenant->fresh()->subscription_status);
|
||||
}
|
||||
|
||||
public function test_wizard_complete_purchase_handles_stripe_success()
|
||||
{
|
||||
$package = Package::factory()->create(['price' => 10]);
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
$response = $this->postJson(route('api.packages.purchase'), [
|
||||
'package_id' => $package->id,
|
||||
'type' => 'endcustomer_event',
|
||||
'payment_method' => 'stripe',
|
||||
'payment_method_id' => 'pm_test_success',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'pm_test_success',
|
||||
'price' => 10,
|
||||
]);
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_wizard_complete_purchase_handles_paypal_success()
|
||||
{
|
||||
$package = Package::factory()->create(['price' => 10]);
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
$response = $this->postJson(route('api.packages.purchase'), [
|
||||
'package_id' => $package->id,
|
||||
'type' => 'endcustomer_event',
|
||||
'payment_method' => 'paypal',
|
||||
'paypal_order_id' => 'order_test_success',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'order_test_success',
|
||||
'price' => 10,
|
||||
]);
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_wizard_auth_error_handling_in_registration()
|
||||
{
|
||||
$package = Package::factory()->create(['price' => 0]);
|
||||
$existingUser = User::factory()->create(['email' => 'duplicate@example.com']);
|
||||
|
||||
$response = $this->post('/de/register', [
|
||||
'name' => 'Duplicate User',
|
||||
'username' => 'duplicate',
|
||||
'email' => 'duplicate@example.com',
|
||||
'password' => 'Password123!',
|
||||
'password_confirmation' => 'Password123!',
|
||||
'first_name' => 'Max',
|
||||
'last_name' => 'Mustermann',
|
||||
'address' => 'Musterstr. 1',
|
||||
'phone' => '+49123456789',
|
||||
'privacy_consent' => true,
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHasErrors(['email']);
|
||||
$this->assertDatabaseMissing('users', ['email' => 'duplicate@example.com']); // No duplicate created
|
||||
}
|
||||
|
||||
public function test_wizard_login_error_handling()
|
||||
{
|
||||
$user = User::factory()->create(['email' => 'test@example.com', 'password' => bcrypt('wrongpass')]);
|
||||
|
||||
$response = $this->post('/de/login', [
|
||||
'email' => 'test@example.com',
|
||||
'password' => 'wrongpass',
|
||||
]);
|
||||
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHasErrors(['email']); // Or custom message
|
||||
$this->assertGuest(); // Not logged in
|
||||
}
|
||||
|
||||
public function test_wizard_trial_activation_for_first_reseller()
|
||||
{
|
||||
$resellerPackage = Package::factory()->create(['price' => 10, 'type' => 'reseller_subscription']);
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
// No active packages yet
|
||||
$this->assertEquals(0, TenantPackage::where('tenant_id', $tenant->id)->where('active', true)->count());
|
||||
|
||||
$response = $this->postJson(route('api.packages.purchase'), [
|
||||
'package_id' => $resellerPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'payment_method' => 'paypal',
|
||||
'paypal_order_id' => 'trial_order_id',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)->where('package_id', $resellerPackage->id)->first();
|
||||
$this->assertNotNull($tenantPackage->expires_at);
|
||||
$this->assertTrue($tenantPackage->expires_at->isFuture());
|
||||
$this->assertTrue($tenantPackage->expires_at->diffInDays(now()) === 14); // Trial period
|
||||
}
|
||||
|
||||
public function test_wizard_reseller_renewal_no_trial()
|
||||
{
|
||||
$resellerPackage = Package::factory()->create(['price' => 10, 'type' => 'reseller_subscription']);
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
// Existing active package
|
||||
TenantPackage::create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $resellerPackage->id,
|
||||
'active' => true,
|
||||
'expires_at' => \Carbon\Carbon::now()->addYear(),
|
||||
]);
|
||||
|
||||
$response = $this->postJson(route('api.packages.purchase'), [
|
||||
'package_id' => $resellerPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'payment_method' => 'stripe',
|
||||
'payment_method_id' => 'pm_renewal',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)->where('package_id', $resellerPackage->id)->first();
|
||||
$this->assertTrue($tenantPackage->expires_at->diffInDays(now()) === 365); // Full year, no trial
|
||||
}
|
||||
|
||||
public function test_purchase_fails_when_package_limit_reached()
|
||||
{
|
||||
$package = Package::factory()->create(['price' => 0, 'max_events' => 1]); // Assume max_events field
|
||||
@@ -201,20 +362,20 @@ class PurchaseTest extends TestCase
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
Auth::login($user);
|
||||
|
||||
$mockClient = Mockery::mock('PayPal\PayPalHttp\Client');
|
||||
$mockOrders = Mockery::mock();
|
||||
$mockResponse = new \stdClass();
|
||||
$mockResponse->statusCode = 400;
|
||||
$mockOrders->shouldReceive('captureOrder')->andThrow(new \Exception('Capture failed'));
|
||||
$mockClient->shouldReceive('orders')->andReturn($mockOrders);
|
||||
$this->app->instance('PayPal\PayPalHttp\Client', $mockClient);
|
||||
$mockHttpClient = Mockery::mock(Client::class);
|
||||
$mockHttpClient->shouldReceive('execute')->andThrow(new \Exception('Capture failed'));
|
||||
|
||||
session(['paypal_order_id' => 'failed-order-id']);
|
||||
$mockPayPalClient = Mockery::mock(PayPalClient::class);
|
||||
$mockPayPalClient->shouldReceive('client')->andReturn($mockHttpClient);
|
||||
|
||||
$response = $this->get(route('marketing.success', $paidPackage->id));
|
||||
$this->app->instance(PayPalClient::class, $mockPayPalClient);
|
||||
|
||||
$response = $this->postJson(route('api.packages.paypal-capture'), [
|
||||
'order_id' => 'failed-order-id',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertSee('Payment capture failed');
|
||||
$response->assertJson(['success' => false]);
|
||||
$this->assertDatabaseMissing('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
|
||||
Reference in New Issue
Block a user