82 lines
5.8 KiB
PHP
82 lines
5.8 KiB
PHP
<?php
|
|
namespace Tests\Feature\Plans;
|
|
use Tests\TestCase;
|
|
use App\Models\{Plan,User,Order};
|
|
use App\Services\PlanService;
|
|
use App\Exceptions\ApiException;
|
|
use App\Support\Setting;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Foundation\Bootstrap\RegisterProviders;
|
|
use Illuminate\Support\Facades\{Schema,Cache,Http};
|
|
use Illuminate\Http\Request;
|
|
class HnAdminPlanTest extends TestCase
|
|
{
|
|
public function createApplication()
|
|
{
|
|
$app = require dirname(__DIR__, 3).'/bootstrap/app.php';
|
|
$app->beforeBootstrapping(RegisterProviders::class, function ($app) {
|
|
$app['config']->set(['database.default'=>'sqlite', 'database.connections.sqlite.database'=>':memory:',
|
|
'cache.default'=>'array', 'cache.stores.redis'=>['driver'=>'array'], 'queue.default'=>'sync',
|
|
'app.key'=>'base64:'.base64_encode(str_repeat('t',32)), 'site.profile'=>'dpl']);
|
|
});
|
|
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
|
return $app;
|
|
}
|
|
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
Schema::create('v2_plugins',fn(Blueprint $t)=>[$t->id(),$t->boolean('is_enabled')->default(false)]);
|
|
Schema::create('v2_settings',fn(Blueprint $t)=>[$t->id(),$t->string('name'),$t->text('value')->nullable(),$t->timestamps()]);
|
|
Schema::create('v2_plan',function(Blueprint $t){$t->id();$t->string('name');$t->text('prices');foreach(['show','sell','renew'] as $f)$t->boolean($f)->default(true);$t->integer('capacity_limit')->nullable();$t->integer('transfer_enable')->default(110);$t->integer('sort')->default(1);$t->timestamps();});
|
|
Schema::create('v2_user',function(Blueprint $t){$t->id();$t->boolean('is_admin')->default(false);$t->integer('plan_id')->nullable();$t->timestamps();});
|
|
Schema::create('v2_order',fn(Blueprint $t)=>[$t->id(),$t->integer('user_id'),$t->integer('status')]);
|
|
Cache::store('redis')->flush();$this->app->forgetInstance(Setting::class);Http::preventStrayRequests();
|
|
config(['site.profile'=>'hn']);admin_setting(['hn_admin_only_plan_ids'=>[2]]);
|
|
Plan::create(['id'=>1,'name'=>'Standard','prices'=>['monthly'=>32]]);
|
|
Plan::create(['id'=>2,'name'=>'Administrator','show'=>false,'prices'=>['monthly'=>16]]);
|
|
\Illuminate\Support\Facades\DB::table('v2_user')->insert([['id'=>1,'is_admin'=>true],['id'=>2,'is_admin'=>false]]);
|
|
}
|
|
private function service():PlanService {return new PlanService(Plan::findOrFail(2));}
|
|
public function test_listing_is_admin_only_including_guest_and_accidentally_visible_plan():void {
|
|
$s=$this->service();$this->assertSame([1],$s->getAvailablePlans()->pluck('id')->values()->all());
|
|
$this->assertSame([1],$s->getAvailablePlans(User::find(2))->pluck('id')->values()->all());
|
|
$this->assertSame([1,2],$s->getAvailablePlans(User::find(1))->pluck('id')->values()->all());
|
|
Plan::where('id',2)->update(['show'=>true]);
|
|
$this->assertSame([1],$s->getAvailablePlans(User::find(2))->pluck('id')->values()->all());
|
|
}
|
|
public function test_direct_lookup_and_demoted_owner_are_denied():void {
|
|
$s=$this->service();$this->assertTrue((bool)$s->isPlanAvailableForUser($s->plan,User::find(1)));
|
|
$u=User::find(2);$u->plan_id=2;$this->assertFalse($s->isPlanAvailableForUser($s->plan,$u));
|
|
$request=Request::create('/api/v1/user/plan/fetch','GET',['id'=>2]);$request->setUserResolver(fn()=>$u);
|
|
$controller=$this->app->make(\App\Http\Controllers\V1\User\PlanController::class);
|
|
$json=json_decode($controller->fetch($request)->getContent(),true);$this->assertNotEquals(2,$json['data']['id']??null);
|
|
}
|
|
public function test_direct_order_controller_rejects_all_periods_without_writing_orders():void {
|
|
\Illuminate\Support\Facades\DB::table('v2_user')->where('id',2)->update(['plan_id'=>2]);
|
|
foreach(['month_price','reset_price','onetime_price']as$period){
|
|
$request=\App\Http\Requests\User\OrderSave::create('/api/v1/user/order/save','POST',['plan_id'=>2,'period'=>$period]);
|
|
$request->setContainer($this->app);$request->setRedirector($this->app->make('redirect'));$request->setUserResolver(fn()=>User::find(2));
|
|
try{$this->app->make(\App\Http\Controllers\V1\User\OrderController::class)->save($request);$this->fail('Ordinary user bypassed purchase guard');}catch(ApiException $e){$this->assertNotEmpty($e->getMessage());}
|
|
}
|
|
$this->assertSame(0,Order::count());
|
|
}
|
|
public function test_administrator_can_buy_hidden_monthly_plan_but_sale_and_capacity_still_apply():void {
|
|
$s=$this->service();$s->validatePurchase(User::find(1),'month_price');$this->assertTrue(true);
|
|
$s->plan->sell=false;
|
|
$this->expectException(ApiException::class);$s->validatePurchase(User::find(1),'month_price');
|
|
}
|
|
public function test_admin_cannot_bypass_capacity_or_disabled_renewal():void {
|
|
$s=$this->service();$s->plan->capacity_limit=0;
|
|
try{$s->validatePurchase(User::find(1),'month_price');$this->fail('Capacity bypassed');}catch(ApiException $e){$this->assertNotEmpty($e->getMessage());}
|
|
$s->plan->capacity_limit=null;$s->plan->renew=false;$u=User::find(1);$u->plan_id=2;
|
|
$this->expectException(ApiException::class);$s->validatePurchase($u,'month_price');
|
|
}
|
|
public function test_dpl_ignores_hn_private_configuration_and_retains_hidden_plan_rules():void {
|
|
config(['site.profile'=>'dpl']);$s=$this->service();
|
|
$this->assertSame([1],$s->getAvailablePlans(User::find(1))->pluck('id')->values()->all());
|
|
Plan::where('id',2)->update(['show'=>true]);$this->assertSame([1,2],$s->getAvailablePlans(User::find(2))->pluck('id')->values()->all());
|
|
$s->plan->show=false;$this->expectException(ApiException::class);$s->validatePurchase(User::find(1),'month_price');
|
|
}
|
|
}
|