Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/OpenViduTest.php
Show First 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | public function testJoinRoom(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$session_id = $room->fresh()->session_id; | $session_id = $room->fresh()->session_id; | ||||
$this->assertSame(Room::ROLE_SUBSCRIBER | Room::ROLE_MODERATOR | Room::ROLE_OWNER, $json['role']); | $this->assertSame(Room::ROLE_SUBSCRIBER | Room::ROLE_MODERATOR | Room::ROLE_OWNER, $json['role']); | ||||
$this->assertSame($session_id, $json['session']); | $this->assertSame($session_id, $json['session']); | ||||
$this->assertTrue(is_string($session_id) && !empty($session_id)); | $this->assertTrue(is_string($session_id) && !empty($session_id)); | ||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | $this->assertTrue(strpos($json['token'], 'wss://') === 0); | ||||
$this->assertTrue(!array_key_exists('shareToken', $json)); | |||||
$john_token = $json['token']; | $john_token = $json['token']; | ||||
// Non-owner, now the session exists, no 'init' argument | // Non-owner, now the session exists, no 'init' argument | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}"); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}"); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(322, $json['code']); | $this->assertSame(322, $json['code']); | ||||
$this->assertTrue(empty($json['token'])); | $this->assertTrue(empty($json['token'])); | ||||
$this->assertTrue(empty($json['shareToken'])); | |||||
// Non-owner, now the session exists, with 'init', but no 'canPublish' argument | // Non-owner, now the session exists, with 'init', but no 'canPublish' argument | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(Room::ROLE_SUBSCRIBER, $json['role']); | $this->assertSame(Room::ROLE_SUBSCRIBER, $json['role']); | ||||
$this->assertSame($session_id, $json['session']); | $this->assertSame($session_id, $json['session']); | ||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | $this->assertTrue(strpos($json['token'], 'wss://') === 0); | ||||
$this->assertTrue($json['token'] != $john_token); | $this->assertTrue($json['token'] != $john_token); | ||||
$this->assertTrue(empty($json['shareToken'])); | |||||
// Non-owner, now the session exists, with 'init', and with 'role=PUBLISHER' | // Non-owner, now the session exists, with 'init', and with 'role=PUBLISHER' | ||||
$post = ['canPublish' => true, 'init' => 1]; | $post = ['canPublish' => true, 'init' => 1]; | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", $post); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | $this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | ||||
$this->assertSame($session_id, $json['session']); | $this->assertSame($session_id, $json['session']); | ||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | $this->assertTrue(strpos($json['token'], 'wss://') === 0); | ||||
$this->assertTrue($json['token'] != $john_token); | $this->assertTrue($json['token'] != $john_token); | ||||
$this->assertTrue(!array_key_exists('shareToken', $json)); | |||||
$this->assertEmpty($json['config']['password']); | $this->assertEmpty($json['config']['password']); | ||||
$this->assertEmpty($json['config']['requires_password']); | $this->assertEmpty($json['config']['requires_password']); | ||||
// Non-owner, password protected room, password not provided | // Non-owner, password protected room, password not provided | ||||
$room->setSettings(['password' => 'pass']); | $room->setSettings(['password' => 'pass']); | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}"); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}"); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | public function testJoinRequests(): void | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", $post); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | $this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | ||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | $this->assertTrue(strpos($json['token'], 'wss://') === 0); | ||||
// TODO: Test a scenario where both password and lock are enabled | // TODO: Test a scenario where both password and lock are enabled | ||||
// TODO: Test accepting/denying as a non-owner moderator | |||||
} | } | ||||
/** | /** | ||||
* Test joining the room | * Test joining the room | ||||
* | * | ||||
* @group openvidu | * @group openvidu | ||||
* @depends testJoinRoom | * @depends testJoinRoom | ||||
*/ | */ | ||||
Show All 10 Lines | public function testJoinRoomGuest(): void | ||||
$response = $this->post("api/v4/openvidu/rooms/{$room->name}", $post); | $response = $this->post("api/v4/openvidu/rooms/{$room->name}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | $this->assertSame(Room::ROLE_PUBLISHER, $json['role']); | ||||
$this->assertSame($room->session_id, $json['session']); | $this->assertSame($room->session_id, $json['session']); | ||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | $this->assertTrue(strpos($json['token'], 'wss://') === 0); | ||||
$this->assertTrue(strpos($json['shareToken'], 'wss://') === 0); | |||||
$this->assertTrue($json['shareToken'] != $json['token']); | |||||
} | } | ||||
/** | /** | ||||
* Test closing the room (session) | * Test closing the room (session) | ||||
* | * | ||||
* @group openvidu | * @group openvidu | ||||
* @depends testJoinRoom | * @depends testJoinRoom | ||||
*/ | */ | ||||
Show All 40 Lines | public function testCloseRoom(): void | ||||
$this->assertSame('aaa', $room->fresh()->session_id); | $this->assertSame('aaa', $room->fresh()->session_id); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame("Failed to close the session.", $json['message']); | $this->assertSame("Failed to close the session.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
} | } | ||||
/** | /** | ||||
* Test creating an extra connection for screen sharing | |||||
* | |||||
* @group openvidu | |||||
*/ | |||||
public function testCreateConnection(): void | |||||
{ | |||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$jack = $this->getTestUser('jack@kolab.org'); | |||||
$room = Room::where('name', 'john')->first(); | |||||
$room->session_id = null; | |||||
$room->save(); | |||||
$this->assignMeetEntitlement($john); | |||||
// First we create the session | |||||
$post = ['init' => 1, 'canPublish' => 1]; | |||||
$response = $this->actingAs($john)->post("api/v4/openvidu/rooms/{$room->name}", $post); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$owner_auth_token = $json['authToken']; | |||||
// And the other user connection | |||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$conn_id = $json['connectionId']; | |||||
$auth_token = $json['authToken']; | |||||
// Non-existing room name | |||||
$response = $this->post("api/v4/openvidu/rooms/non-existing/connections", []); | |||||
$response->assertStatus(404); | |||||
// No connection token provided | |||||
$response = $this->post("api/v4/openvidu/rooms/{$room->name}/connections", []); | |||||
$response->assertStatus(403); | |||||
// Invalid token | |||||
$response = $this->actingAs($jack) | |||||
->withHeaders([OpenViduController::AUTH_HEADER => '123']) | |||||
->post("api/v4/openvidu/rooms/{$room->name}/connections", []); | |||||
$response->assertStatus(403); | |||||
// Subscriber can't get the screen-sharing connection | |||||
// Note: We're acting as Jack because there's no easy way to unset the 'actingAs' user | |||||
// throughout the test | |||||
$response = $this->actingAs($jack) | |||||
->withHeaders([OpenViduController::AUTH_HEADER => $auth_token]) | |||||
->post("api/v4/openvidu/rooms/{$room->name}/connections", []); | |||||
$response->assertStatus(403); | |||||
// Publisher can get the connection | |||||
$response = $this->actingAs($jack) | |||||
->withHeaders([OpenViduController::AUTH_HEADER => $owner_auth_token]) | |||||
->post("api/v4/openvidu/rooms/{$room->name}/connections", []); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertTrue(strpos($json['token'], 'wss://') === 0); | |||||
$this->assertTrue(strpos($json['token'], 'role=PUBLISHER') !== false); | |||||
} | |||||
/** | |||||
* Test dismissing a participant (closing a connection) | * Test dismissing a participant (closing a connection) | ||||
* | * | ||||
* @group openvidu | * @group openvidu | ||||
*/ | */ | ||||
public function testDismissConnection(): void | public function testDismissConnection(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | public function testDismissConnection(): void | ||||
// Expected success | // Expected success | ||||
$response = $this->actingAs($john)->post("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}/dismiss"); | $response = $this->actingAs($john)->post("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}/dismiss"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertNull($room->getOVConnection($conn_id)); | $this->assertNull($room->getOVConnection($conn_id)); | ||||
// Test acting as a moderator | |||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$conn_id = $json['connectionId']; | |||||
// Note: We're acting as Jack because there's no easy way to unset a 'actingAs' user | |||||
// throughout the test | |||||
$response = $this->actingAs($jack) | |||||
->withHeaders([OpenViduController::AUTH_HEADER => $this->getModeratorToken($room)]) | |||||
->post("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}/dismiss"); | |||||
$response->assertStatus(200); | |||||
} | } | ||||
/** | /** | ||||
* Test configuring the room (session) | * Test configuring the room (session) | ||||
* | * | ||||
* @group openvidu | * @group openvidu | ||||
*/ | */ | ||||
public function testSetRoomConfig(): void | public function testSetRoomConfig(): void | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | public function testUpdateConnection(): void | ||||
$this->assignMeetEntitlement($john); | $this->assignMeetEntitlement($john); | ||||
// First we create the session | // First we create the session | ||||
$response = $this->actingAs($john)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | $response = $this->actingAs($john)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$owner_conn_id = $json['connectionId']; | |||||
// And the other user connection | // And the other user connection | ||||
$response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | $response = $this->actingAs($jack)->post("api/v4/openvidu/rooms/{$room->name}", ['init' => 1]); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$conn_id = $json['connectionId']; | $conn_id = $json['connectionId']; | ||||
$auth_token = $json['authToken']; | |||||
$room->refresh(); | $room->refresh(); | ||||
$conn_data = $room->getOVConnection($conn_id); | $conn_data = $room->getOVConnection($conn_id); | ||||
$this->assertSame($conn_id, $conn_data['connectionId']); | $this->assertSame($conn_id, $conn_data['connectionId']); | ||||
// Non-existing room name | // Non-existing room name | ||||
$response = $this->actingAs($john)->put("api/v4/openvidu/rooms/non-existing/connections/{$conn_id}", []); | $response = $this->actingAs($john)->put("api/v4/openvidu/rooms/non-existing/connections/{$conn_id}", []); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
Show All 16 Lines | public function testUpdateConnection(): void | ||||
$post = ['role' => Room::ROLE_PUBLISHER | Room::ROLE_MODERATOR]; | $post = ['role' => Room::ROLE_PUBLISHER | Room::ROLE_MODERATOR]; | ||||
$response = $this->actingAs($john)->put("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}", $post); | $response = $this->actingAs($john)->put("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame($post['role'], Connection::find($conn_id)->role); | $this->assertSame($post['role'], Connection::find($conn_id)->role); | ||||
// Access as moderator | |||||
// Note: We're acting as Jack because there's no easy way to unset a 'actingAs' user | |||||
// throughout the test | |||||
$token = $this->getModeratorToken($room); | |||||
$post = ['role' => Room::ROLE_PUBLISHER]; | |||||
$response = $this->actingAs($jack)->withHeaders([OpenViduController::AUTH_HEADER => $token]) | |||||
->put("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}", $post); | |||||
$response->assertStatus(200); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame($post['role'], Connection::find($conn_id)->role); | |||||
// Assert that it's not possible to add/remove the 'owner' role | |||||
$post = ['role' => Room::ROLE_PUBLISHER | Room::ROLE_OWNER]; | |||||
$response = $this->actingAs($jack)->withHeaders([OpenViduController::AUTH_HEADER => $token]) | |||||
->put("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}", $post); | |||||
$response->assertStatus(403); | |||||
$post = ['role' => Room::ROLE_PUBLISHER]; | |||||
$response = $this->actingAs($jack)->withHeaders([OpenViduController::AUTH_HEADER => $token]) | |||||
->put("api/v4/openvidu/rooms/{$room->name}/connections/{$owner_conn_id}", $post); | |||||
$response->assertStatus(403); | |||||
// Assert that removing a 'moderator' role from the owner is not possible | |||||
$post = ['role' => Room::ROLE_PUBLISHER | Room::ROLE_OWNER]; | |||||
$response = $this->actingAs($jack)->withHeaders([OpenViduController::AUTH_HEADER => $token]) | |||||
->put("api/v4/openvidu/rooms/{$room->name}/connections/{$owner_conn_id}", $post); | |||||
$response->assertStatus(200); | |||||
$this->assertSame($post['role'] | Room::ROLE_MODERATOR, Connection::find($owner_conn_id)->role); | |||||
// Assert that non-moderator token does not allow access | |||||
$post = ['role' => Room::ROLE_SUBSCRIBER]; | |||||
$response = $this->actingAs($jack)->withHeaders([OpenViduController::AUTH_HEADER => $auth_token]) | |||||
->put("api/v4/openvidu/rooms/{$room->name}/connections/{$conn_id}", $post); | |||||
$response->assertStatus(403); | |||||
} | |||||
/** | |||||
* Create a moderator connection to the room session. | |||||
* | |||||
* @param \App\OpenVidu\Room $room The room | |||||
* | |||||
* @return string The connection authentication token | |||||
*/ | |||||
private function getModeratorToken(Room $room): string | |||||
{ | |||||
$result = $room->getSessionToken(Room::ROLE_MODERATOR); | |||||
return $result['authToken']; | |||||
} | } | ||||
} | } |