完善职务礼包红包默认配置
This commit is contained in:
@@ -64,9 +64,9 @@ class RedPacketControllerTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证站长可以成功发出礼包并写入 Redis 拆包结果。
|
||||
* 方法功能:验证站长没有在职职务时也不能绕过礼包权限。
|
||||
*/
|
||||
public function test_superadmin_can_send_red_packet(): void
|
||||
public function test_site_owner_without_position_cannot_send_red_packet(): void
|
||||
{
|
||||
$admin = $this->createSiteOwner();
|
||||
|
||||
@@ -75,19 +75,72 @@ class RedPacketControllerTest extends TestCase
|
||||
'type' => 'gold',
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['status' => 'success']);
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('red_packet_envelopes', [
|
||||
'sender_id' => $admin->id,
|
||||
/**
|
||||
* 方法功能:验证礼包弹窗配置接口会读取当前职务的数据库配置。
|
||||
*/
|
||||
public function test_red_packet_config_uses_position_packet_config(): void
|
||||
{
|
||||
$user = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
], redPacketAmount: 2468, redPacketCount: 8);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'status' => 'success',
|
||||
'amount' => 2468,
|
||||
'count' => 8,
|
||||
'expire_seconds' => 300,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证没有礼包权限时不能读取发包弹窗配置。
|
||||
*/
|
||||
public function test_user_without_red_packet_permission_cannot_read_packet_config(): void
|
||||
{
|
||||
$user = $this->createUserWithPermissions([]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
|
||||
|
||||
$response->assertStatus(403)
|
||||
->assertJson(['status' => 'error']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证拥有礼包权限的职务用户会按职务配置发出礼包。
|
||||
*/
|
||||
public function test_position_user_with_red_packet_permission_uses_position_packet_config(): void
|
||||
{
|
||||
$user = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
], redPacketAmount: 1234, redPacketCount: 7);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
|
||||
'room_id' => 1,
|
||||
'type' => 'gold',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('status', 'success');
|
||||
|
||||
$this->assertDatabaseHas('red_packet_envelopes', [
|
||||
'sender_id' => $user->id,
|
||||
'room_id' => 1,
|
||||
'type' => 'gold',
|
||||
'total_amount' => 1234,
|
||||
'total_count' => 7,
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$envelope = RedPacketEnvelope::first();
|
||||
// Check Redis for parts
|
||||
$this->assertEquals(10, Redis::llen("red_packet:{$envelope->id}:amounts"));
|
||||
$amounts = array_map('intval', Redis::lrange("red_packet:{$envelope->id}:amounts", 0, -1));
|
||||
$this->assertCount(7, $amounts);
|
||||
$this->assertSame(1234, array_sum($amounts));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +150,7 @@ class RedPacketControllerTest extends TestCase
|
||||
{
|
||||
$user = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
], redPacketAmount: 4321, redPacketCount: 6);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
|
||||
'room_id' => 1,
|
||||
@@ -107,6 +160,13 @@ class RedPacketControllerTest extends TestCase
|
||||
$response->assertOk()->assertJson([
|
||||
'status' => 'success',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('red_packet_envelopes', [
|
||||
'sender_id' => $user->id,
|
||||
'type' => 'exp',
|
||||
'total_amount' => 4321,
|
||||
'total_count' => 6,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +174,9 @@ class RedPacketControllerTest extends TestCase
|
||||
*/
|
||||
public function test_cannot_send_multiple_active_packets_in_same_room(): void
|
||||
{
|
||||
$admin = $this->createSiteOwner();
|
||||
$admin = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
|
||||
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
|
||||
'room_id' => 1,
|
||||
@@ -134,7 +196,9 @@ class RedPacketControllerTest extends TestCase
|
||||
*/
|
||||
public function test_user_can_claim_red_packet(): void
|
||||
{
|
||||
$admin = $this->createSiteOwner();
|
||||
$admin = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
$user = User::factory()->create(['jjb' => 100]);
|
||||
|
||||
// Send packet
|
||||
@@ -169,7 +233,9 @@ class RedPacketControllerTest extends TestCase
|
||||
{
|
||||
Event::fake([RedPacketClaimed::class]);
|
||||
|
||||
$admin = $this->createSiteOwner();
|
||||
$admin = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
$user = User::factory()->create(['jjb' => 100]);
|
||||
|
||||
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
|
||||
@@ -203,7 +269,9 @@ class RedPacketControllerTest extends TestCase
|
||||
*/
|
||||
public function test_user_cannot_claim_same_packet_twice(): void
|
||||
{
|
||||
$admin = $this->createSiteOwner();
|
||||
$admin = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
|
||||
@@ -232,7 +300,9 @@ class RedPacketControllerTest extends TestCase
|
||||
*/
|
||||
public function test_can_check_packet_status(): void
|
||||
{
|
||||
$admin = $this->createSiteOwner();
|
||||
$admin = $this->createUserWithPermissions([
|
||||
PositionPermissionRegistry::ROOM_RED_PACKET,
|
||||
]);
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
|
||||
@@ -279,7 +349,7 @@ class RedPacketControllerTest extends TestCase
|
||||
*
|
||||
* @param list<string> $permissions
|
||||
*/
|
||||
private function createUserWithPermissions(array $permissions): User
|
||||
private function createUserWithPermissions(array $permissions, int $redPacketAmount = 8888, int $redPacketCount = 10): User
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
'user_level' => 80,
|
||||
@@ -301,6 +371,8 @@ class RedPacketControllerTest extends TestCase
|
||||
'level' => 80,
|
||||
'sort_order' => 1,
|
||||
'permissions' => $permissions,
|
||||
'red_packet_amount' => $redPacketAmount,
|
||||
'red_packet_count' => $redPacketCount,
|
||||
]);
|
||||
|
||||
UserPosition::create([
|
||||
|
||||
Reference in New Issue
Block a user