feat: Moderator able to ban users test

Signed-off-by: skalidindi53 <s.teja2004@gmail.com>
This commit is contained in:
skalidindi53 2024-06-26 16:44:39 -05:00 committed by Joas Schilling
parent 5ff594cbc9
commit 856fda5b49
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
6 changed files with 148 additions and 18 deletions

View file

@ -82,8 +82,8 @@ class BanController extends AEnvironmentAwareController {
*/
#[PublicPage]
#[RequireModeratorParticipant]
public function listBans(int $roomId): DataResponse {
$bans = $this->banService->getBansForRoom($roomId);
public function listBans(): DataResponse {
$bans = $this->banService->getBansForRoom($this->room->getId());
$result = array_map(function ($ban) {
return $ban->jsonSerialize();
}, $bans);
@ -104,8 +104,16 @@ class BanController extends AEnvironmentAwareController {
*/
#[PublicPage]
#[RequireModeratorParticipant]
public function unbanActor(int $banId): DataResponse {
$this->banService->findAndDeleteBanById($banId);
public function unbanActor(): DataResponse {
$banId = $this->request->getParam('id');
if ($banId === null) {
return new DataResponse([
'error' => 'Missing ban ID',
], Http::STATUS_BAD_REQUEST);
}
echo "Ban ID!!!: $banId\n";
$this->banService->findAndDeleteBanById((int)$banId);
echo "Unbanned successfully\n";
return new DataResponse([], Http::STATUS_OK);
}

View file

@ -44,18 +44,18 @@ class Version20000Date20240621150333 extends SimpleMigrationStep {
'notnull' => true,
'unsigned' => true,
]);
$table->addColumn('banned_by_actor_id', Types::STRING, [
$table->addColumn('banned_id', Types::STRING, [
'length' => 64,
'notnull' => true,
]);
$table->addColumn('banned_by_actor_type', Types::STRING, [
$table->addColumn('banned_type', Types::STRING, [
'length' => 64,
'notnull' => true,
]);
$table->addColumn('banned_at', Types::DATETIME, [
$table->addColumn('banned_time', Types::DATETIME, [
'notnull' => false,
]);
$table->addColumn('internalNote', Types::TEXT, [
$table->addColumn('internal_note', Types::TEXT, [
'notnull' => false,
]);

View file

@ -11,6 +11,8 @@ namespace OCA\Talk\Model;
use OCP\AppFramework\Db\Entity;
/**
* @method void setId(int $id)
* @method int getId()
* @method void setActorId(string $actorId)
* @method string getActorId()
* @method void setActorType(string $actorType)
@ -36,6 +38,7 @@ class Ban extends Entity implements \JsonSerializable {
protected ?string $internalNote = null;
public function __construct() {
$this->addType('id', 'int');
$this->addType('actorId', 'string');
$this->addType('actorType', 'string');
$this->addType('roomId', 'int');
@ -47,6 +50,7 @@ class Ban extends Entity implements \JsonSerializable {
public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'actorId' => $this->getActorId(),
'actorType' => $this->getActorType(),
'roomId' => $this->getRoomId(),

View file

@ -47,7 +47,7 @@ class BanService {
$ban->setRoomId($roomId);
$ban->setBannedId($bannedId);
$ban->setBannedType($bannedType);
$ban->setBannedTime($bannedTime ?? $this->timeFactory->getTime());
$ban->setBannedTime($bannedTime ?? new \DateTime());
$ban->setInternalNote($internalNote);
return $this->banMapper->insert($ban);

View file

@ -61,6 +61,8 @@ class FeatureContext implements Context, SnippetAcceptingContext {
protected static array $modifiedSince;
/** @var array<string, string> */
protected static array $createdTeams = [];
/** @var array<string, int> */
protected static array $userToBanId;
protected static array $permissionsMap = [
@ -149,6 +151,26 @@ class FeatureContext implements Context, SnippetAcceptingContext {
return self::$userToAttendeeId[$room][$type][$id];
}
public function getBanId(string $type, string $id, string $room, string $user = null) {
if (!isset(self::$userToBanId[$room][$type][$id])) {
if ($user !== null) {
echo "userLoadsBanIdsInRoom\n";
$this->userLoadsBanIdsInRoom($user, $room, 'v1');
} else {
throw new \Exception('Ban id unknown, please call userLoadsBanIdsInRoom with a user that has access before');
}
}
echo "userToBanId\n";
echo self::$userToBanId[$room][$type][$id];
if (!isset(self::$userToBanId[$room][$type][$id])) {
throw new \Exception('Ban id unknown, please call userLoadsBanIdsInRoom with a user that has access before 1');
}
echo "returned correct banId\n";
return self::$userToBanId[$room][$type][$id];
}
/**
* FeatureContext constructor.
*/
@ -869,6 +891,33 @@ class FeatureContext implements Context, SnippetAcceptingContext {
}
return $a1['actorId'] <=> $a2['actorId'];
}
/**
* @Then /^user "([^"]*)" loads ban ids in room "([^"]*)" \((v1)\)$/
*
* @param string $user
* @param string $identifier
* @param string $apiVersion
*/
public function userLoadsBanIdsInRoom(string $user, string $identifier, string $apiVersion = 'v1'): void {
$this->setCurrentUser($user);
echo "SENDING GET REQUEST\n";
echo "selftoken: " . self::$identifierToToken[$identifier] . "\n";
$this->sendRequest('GET', '/apps/spreed/api/' . $apiVersion . '/ban/' . self::$identifierToToken[$identifier]);
$this->assertStatusCode($this->response, 200);
$bans = $this->getDataFromResponse($this->response);
echo "BANS: " . print_r($bans, true) . "\n";
foreach ($bans as $ban) {
if (!isset(self::$userToBanId[$identifier][$ban['actorType']])) {
self::$userToBanId[$identifier][$ban['actorType']] = [];
}
self::$userToBanId[$identifier][$ban['actorType']][$ban['actorId']] = $ban['id'];
}
echo "print the user to ban id array\n";
echo print_r(self::$userToBanId, true) . "\n";
}
private function mapParticipantTypeTestInput($participantType) {
if (is_numeric($participantType)) {
@ -1456,7 +1505,7 @@ class FeatureContext implements Context, SnippetAcceptingContext {
}
/**
* @When /^user "([^"]*)" bans (user|group|email|remote) "([^"]*)" from room "([^"]*)" with (\d+) \((v4)\)$/
* @When /^user "([^"]*)" bans (user|group|email|remote) "([^"]*)" from room "([^"]*)" with (\d+) \((v1)\)$/
*
* @param string $user
* @param string $actorType
@ -1465,7 +1514,7 @@ class FeatureContext implements Context, SnippetAcceptingContext {
* @param int $statusCode
* @param string $apiVersion
*/
public function userBansUserFromRoom(string $user, string $actorType, string $actorId, string $identifier, int $statusCode, string $apiVersion): void {
public function userBansUserFromRoom(string $user, string $actorType, string $actorId, string $identifier, int $statusCode, string $apiVersion = 'v1', TableNode $internalNote): void {
if ($actorId === 'stranger') {
$actorId = '123456789';
} else {
@ -1476,16 +1525,68 @@ class FeatureContext implements Context, SnippetAcceptingContext {
}
$this->setCurrentUser($user);
$body = [
'actorType' => $actorType,
'actorId' => $actorId,
];
// Add the internal note if it exists
if ($internalNote !== null) {
$internalNoteData = $internalNote->getRowsHash();
if (isset($internalNoteData['internalNote'])) {
$body['internalNote'] = $internalNoteData['internalNote'];
}
}
//echo "Request Body: " . json_encode($body) . "\n";
echo "self" . self::$identifierToToken[$identifier] . "\n";
$this->sendRequest(
'POST', '/apps/spreed/api/' . $apiVersion . '/ban/' . self::$identifierToToken[$identifier], new TableNode([
['actorType', $actorType],
['actorId', $actorId],
])
'POST', '/apps/spreed/api/' . $apiVersion . '/ban/' . self::$identifierToToken[$identifier], $body
);
echo "Response: " . $this->response->getBody()->getContents() . "\n";
$this->assertStatusCode($this->response, $statusCode);
}
/**
* @When /^user "([^"]*)" unbans (user|group|email|remote) "([^"]*)" from room "([^"]*)" with (\d+) \((v1)\)$/
*
* @param string $user
* @param string $actorType
* @param string $actorId
* @param string $identifier
* @param int $statusCode
* @param string $apiVersion
*/
public function userUnbansUserFromRoom(string $user, string $actorType, string $actorId, string $identifier, int $statusCode, string $apiVersion = 'v1'): void {
if ($actorId === 'stranger') {
$actorId = '123456789';
} else {
if ($actorType === 'remote') {
$actorId .= '@' . rtrim($this->baseRemoteUrl, '/');
$actorType = 'federated_user';
}
echo "Getting banID\n";
$banId = $this->getBanId($actorType, $actorId, $identifier, $statusCode === 200 ? $user : null);
}
echo "starting to delete this ban\n";
$this->setCurrentUser($user);
echo "self: " . self::$identifierToToken[$identifier] . "\n";
echo "banId: " . $banId . "\n";
$this->sendRequest(
'DELETE', '/apps/spreed/api/' . $apiVersion . '/ban/' . self::$identifierToToken[$identifier], ['id' => $banId]
);
echo "Response Status Code: " . $this->response->getStatusCode() . "\n";
echo "Response Body: " . $this->response->getBody()->getContents() . "\n";
$this->assertStatusCode($this->response, $statusCode);
}
/**
* @Then /^user "([^"]*)" deletes room "([^"]*)" with (\d+) \((v4)\)$/

View file

@ -2,13 +2,30 @@ Feature: conversation/ban
Background:
Given user "participant1" exists
Given user "participant2" exists
Given user "participant3" exists
And guest accounts can be created
And user "user-guest@example.com" is a guest account user
Scenario: Ban a user from a room
Scenario: Moderator banning and unbanning multiple users
Given user "participant1" creates room "room" (v4)
| roomType | 3 |
| roomName | room |
And user "participant2" joins room "room" with 200 (v4)
When user "participant1" bans user "participant2" from room "room" with 200 (v4)
Then user "participant2" joins room "room" with 403 (v4)
And user "participant3" joins room "room" with 200 (v4)
And user "participant1" bans user "participant2" from room "room" with 200 (v1)
| internalNote | BannedP1 |
And user "participant1" bans user "participant3" from room "room" with 200 (v1)
| internalNote | BannedP2 |
#And user "participant2" joins room "room" with 403 (v4)
#And user "participant3" joins room "room" with 403 (v4)
And user "participant1" unbans user "participant2" from room "room" with 200 (v1)
And user "participant1" unbans user "participant3" from room "room" with 200 (v1)
# Scenario: Moderator banning and unbanning guest account
# Given user "participant1" creates room "room" (v4)
# | roomType | 3 |
# | roomName | room |
# And user "user-guest@example.com" joins room "room" with 200 (v4)
# And user "participant1" bans user "user-guest@example.com" from room "room" with 200 (v1)
# | internalNote | BannedG1 |
# And user "participant1" unbans user "user-guest@exmaple.com" from room "room" with 200 (v1)