Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(chat): Add a test that editing old messages is blocked #12244

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/integration-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=mysql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ config:system:set debug --value=true --type=boolean
./occ config:system:set hashing_default_password --value=true --type=boolean
./occ app:enable --force ${{ env.APP_NAME }}
./occ app:enable --force call_summary_bot
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/integration-oci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=oci --database-name=XE --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=autotest --database-pass=owncloud --admin-user admin --admin-pass admin
./occ config:system:set debug --value=true --type=boolean
./occ config:system:set hashing_default_password --value=true --type=boolean
./occ app:enable --force ${{ env.APP_NAME }}
./occ app:enable --force call_summary_bot
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/integration-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=pgsql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ config:system:set debug --value=true --type=boolean
./occ config:system:set hashing_default_password --value=true --type=boolean
./occ config:system:set memcache.local --value="\\OC\\Memcache\\APCu"
./occ config:system:set memcache.distributed --value="\\OC\\Memcache\\APCu"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/integration-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=sqlite --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ config:system:set debug --value=true --type=boolean
./occ config:system:set hashing_default_password --value=true --type=boolean
./occ app:enable --force ${{ env.APP_NAME }}
./occ app:enable --force call_summary_bot
Expand Down
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<command>OCA\Talk\Command\Bot\State</command>
<command>OCA\Talk\Command\Bot\Setup</command>
<command>OCA\Talk\Command\Bot\Uninstall</command>
<command>OCA\Talk\Command\Developer\AgeChatMessages</command>
<command>OCA\Talk\Command\Developer\UpdateDocs</command>
<command>OCA\Talk\Command\Monitor\Calls</command>
<command>OCA\Talk\Command\Monitor\HasActiveCalls</command>
Expand Down
112 changes: 112 additions & 0 deletions lib/Command/Developer/AgeChatMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\Command\Developer;

use OC\Core\Command\Base;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class AgeChatMessages extends Base {
public function __construct(
private readonly IConfig $config,
private readonly IDBConnection $connection,
private readonly Manager $manager,
) {
parent::__construct();
}

public function isEnabled(): bool {
return $this->config->getSystemValue('debug', false) === true;
}

protected function configure(): void {
$this
->setName('talk:developer:age-chat-messages')
->setDescription('Artificially ages chat messages in the given conversation, so deletion and other things can be tested')
->addArgument(
'token',
InputArgument::REQUIRED,
'Token of the room to manipulate'
)
->addOption(
'hours',
null,
InputOption::VALUE_REQUIRED,
'Number of hours to age all chat messages',
24
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$token = $input->getArgument('token');
$hours = (int) $input->getOption('hours');
if ($hours < 1) {
$output->writeln('<error>Invalid age: ' . $hours . '</error>');
return 1;
}

try {
$room = $this->manager->getRoomByToken($token);
} catch (RoomNotFoundException) {
$output->writeln('<error>Room not found: ' . $token . '</error>');
return 1;
}

$update = $this->connection->getQueryBuilder();
$update->update('comments')
->set('creation_timestamp', $update->createParameter('creation_timestamp'))
->set('expire_date', $update->createParameter('expire_date'))
->set('meta_data', $update->createParameter('meta_data'))
->where($update->expr()->eq('id', $update->createParameter('id')));

$query = $this->connection->getQueryBuilder();
$query->select('id', 'creation_timestamp', 'expire_date', 'meta_data')
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter('chat')))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($room->getId())));

$result = $query->executeQuery();
while ($row = $result->fetch()) {
$creationTimestamp = new \DateTime($row['creation_timestamp']);
$creationTimestamp->sub(new \DateInterval('PT' . $hours . 'H'));

$expireDate = null;
if ($row['expire_date']) {
$expireDate = new \DateTime($row['expire_date']);
$expireDate->sub(new \DateInterval('PT' . $hours . 'H'));
}

$metaData = 'null';
if ($row['meta_data'] !== 'null') {
$metaData = json_decode($row['meta_data'], true);
if (isset($metaData['last_edited_time'])) {
$metaData['last_edited_time'] -= $hours * 3600;
}
$metaData = json_encode($metaData);
}

$update->setParameter('id', $row['id']);
$update->setParameter('creation_timestamp', $creationTimestamp, IQueryBuilder::PARAM_DATE);
$update->setParameter('expire_date', $expireDate, IQueryBuilder::PARAM_DATE);
$update->setParameter('meta_data', $metaData);
$update->executeStatement();
}
$result->closeCursor();

return 0;
}
}
3 changes: 2 additions & 1 deletion lib/Command/Developer/UpdateDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$info = $this->appManager->getAppInfo('spreed');
$documentation = "# Talk occ commands\n\n";
foreach ($info['commands'] as $namespace) {
if ($namespace === self::class) {
if ($namespace === self::class
|| $namespace === AgeChatMessages::class) {
continue;
}

Expand Down
21 changes: 18 additions & 3 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ public function userSendsMessageToRoom(string $user, string $sendingMode, string
* @param string $statusCode
* @param string $apiVersion
*/
public function userEditsMessageToRoom(string $user, string $oldMessage, string $identifier, string $newMessage, string $statusCode, string $apiVersion = 'v1') {
public function userEditsMessageToRoom(string $user, string $oldMessage, string $identifier, string $newMessage, int $statusCode, string $apiVersion = 'v1', ?TableNode $formData = null) {
$oldMessage = substr($oldMessage, 1, -1);
$oldMessage = str_replace('\n', "\n", $oldMessage);
$messageId = self::$textToMessageId[$oldMessage];
Expand All @@ -2020,8 +2020,15 @@ public function userEditsMessageToRoom(string $user, string $oldMessage, string
$this->assertStatusCode($this->response, $statusCode);
sleep(1); // make sure Postgres manages the order of the messages

self::$textToMessageId[$newMessage] = $messageId;
self::$messageIdToText[$messageId] = $newMessage;
if ($statusCode === 200 || $statusCode === 202) {
self::$textToMessageId[$newMessage] = $messageId;
self::$messageIdToText[$messageId] = $newMessage;
} elseif ($formData instanceof TableNode) {
Assert::assertEquals(
$formData->getRowsHash(),
$this->getDataFromResponse($this->response),
);
}
}

/**
Expand Down Expand Up @@ -3876,6 +3883,14 @@ public function userSetsTheRecordingConsentToXWithStatusCode(string $user, int $
$this->assertStatusCode($this->response, $statusCode);
}

/**
* @Given /^aging messages (\d+) hours in room "([^"]*)"$/
*/
public function occAgeChatMessages(int $hours, string $identifier): void {
$this->runOcc(['talk:developer:age-chat-messages', '--hours', $hours, self::$identifierToToken[$identifier]]);
$this->theCommandWasSuccessful();
}

/**
* @Given /^the following recording consent is recorded for (room|user) "([^"]*)"$/
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/features/chat-1/edit-message.feature
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ Feature: chat-1/edit-message
Then user "participant1" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage | lastEditActorType | lastEditActorId | lastEditActorDisplayName |
| room | deleted_users | deleted_users | | Message 1 - Edit 2 | [] | | deleted_users | deleted_users | |
When aging messages 6 hours in room "room"
And user "participant1" edits message "Message 1 - Edit 1" in room "room" to "Message 1 - Edit 2" with 200
When aging messages 24 hours in room "room"
And user "participant1" edits message "Message 1 - Edit 2" in room "room" to "Message 1 - Edit Too old" with 400
| error | age |

Scenario: Editing a caption
Given user "participant1" creates room "room" (v4)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/spreedcheats/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
return [
'ocs' => [
['name' => 'Api#resetSpreed', 'url' => '/', 'verb' => 'DELETE'],
['name' => 'Api#ageChat', 'url' => '/age', 'verb' => 'POST'],
],
];
73 changes: 62 additions & 11 deletions tests/integration/spreedcheats/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\SpreedCheats\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -16,22 +17,14 @@
use OCP\Share\IShare;

class ApiController extends OCSController {
/** @var IDBConnection */
private $db;

public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
IDBConnection $db
protected IDBConnection $db,
) {
parent::__construct($appName, $request);
$this->db = $db;
}

/**
* @NoCSRFRequired
*
* @return DataResponse
*/
public function resetSpreed(): DataResponse {
$delete = $this->db->getQueryBuilder();
$delete->delete('talk_attachments')->executeStatement();
Expand Down Expand Up @@ -110,4 +103,62 @@ public function resetSpreed(): DataResponse {

return new DataResponse();
}

public function ageChat(string $token, int $hours): DataResponse {
$query = $this->db->getQueryBuilder();
$query->select('id')
->from('talk_rooms')
->where($query->expr()->eq('token', $query->createNamedParameter($token)));

$result = $query->executeQuery();
$roomId = (int) $result->fetchOne();
$result->closeCursor();

if (!$roomId) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

$update = $this->db->getQueryBuilder();
$update->update('comments')
->set('creation_timestamp', $update->createParameter('creation_timestamp'))
->set('expire_date', $update->createParameter('expire_date'))
->set('meta_data', $update->createParameter('meta_data'))
->where($update->expr()->eq('id', $update->createParameter('id')));

$query = $this->db->getQueryBuilder();
$query->select('id', 'creation_timestamp', 'expire_date', 'meta_data')
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter('chat')))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($roomId)));

$result = $query->executeQuery();
while ($row = $result->fetch()) {
$creationTimestamp = new \DateTime($row['creation_timestamp']);
$creationTimestamp->sub(new \DateInterval('PT' . $hours . 'H'));

$expireDate = null;
if ($row['expire_date']) {
$expireDate = new \DateTime($row['expire_date']);
$expireDate->sub(new \DateInterval('PT' . $hours . 'H'));
}

$metaData = 'null';
if ($row['meta_data'] !== 'null') {
$metaData = json_decode($row['meta_data'], true);
if (isset($metaData['last_edited_time'])) {
$metaData['last_edited_time'] -= $hours * 3600;
}
$metaData = json_encode($metaData);
}

$update->setParameter('id', $row['id']);
$update->setParameter('creation_timestamp', $creationTimestamp, IQueryBuilder::PARAM_DATE);
$update->setParameter('expire_date', $expireDate, IQueryBuilder::PARAM_DATE);
$update->setParameter('meta_data', $metaData);
$update->executeStatement();
}
$result->closeCursor();

return new DataResponse();
}
}
Loading