Skip to content

Commit

Permalink
fix(bots): Add negative tests for setting up bots
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Aug 30, 2023
1 parent 857131f commit bf70a4e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/Command/Bot/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Talk\Model\BotConversationMapper;
use OCA\Talk\Model\BotServerMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->roomManager->getRoomByToken($token);
} catch (RoomNotFoundException) {
$output->writeln('<error>Conversation could not be found by token: ' . $token . '</error>');
return 1;
$returnCode = 2;
}

$bot = new BotConversation();
Expand All @@ -93,8 +94,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->botConversationMapper->insert($bot);
$output->writeln('<info>Successfully set up for conversation ' . $token . '</info>');
} catch (\Exception $e) {
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
$returnCode = 3;
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$output->writeln('<error>Bot is already set up for the conversation ' . $token . '</error>');
$returnCode = 3;
} else {
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
$returnCode = 4;
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/integration/features/bootstrap/CommandLineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ public function runOcc($args = [], $env = null) {
* @Given /^invoking occ with "([^"]*)"$/
*/
public function invokingTheCommand($cmd) {
// FIXME this way is deprecated
if (preg_match('/room-name:(?P<token>\w+)/', $cmd, $matches)) {
if (array_key_exists($matches['token'], self::$identifierToToken)) {
$cmd = preg_replace('/room-name:(\w+)/', self::$identifierToToken[$matches['token']], $cmd);
}
}

if (preg_match('/ROOM\((?P<name>\w+)\)/', $cmd, $matches)) {
if (array_key_exists($matches['name'], self::$identifierToToken)) {
$cmd = preg_replace('/ROOM\((\w+)\)/', self::$identifierToToken[$matches['name']], $cmd);
}
}
if (preg_match('/BOT\((?P<name>\w+)\)/', $cmd, $matches)) {
if (array_key_exists($matches['name'], self::$botNameToId)) {
$cmd = preg_replace('/BOT\((\w+)\)/', self::$botNameToId[$matches['name']], $cmd);
}
}

$args = explode(' ', $cmd);
$this->runOcc($args);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/features/chat/bots.feature
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,22 @@ Feature: chat/bots
When invoking occ with "talk:bot:install Bot Secret:1234567890123456789012345678901234567890 https://localhost/bot2"
Then the command failed with exit code 3
And the command output contains the text "Bot with the same secret is already registered"

Scenario: Set up conversation bot errors
Given invoking occ with "talk:bot:install ErrorBot Secret:1234567890123456789012345678901234567890 https://localhost/bot1"
And the command was successful
And read bot ids from OCC
And user "participant1" creates room "room" (v4)
| roomType | 2 |
| roomName | room |
When invoking occ with "talk:bot:setup 2147483647 invalid-token"
Then the command failed with exit code 1
And the command output contains the text "Bot could not be found by id: 2147483647"
When invoking occ with "talk:bot:setup BOT(ErrorBot) invalid-token"
Then the command failed with exit code 2
And the command output contains the text "Conversation could not be found by token: invalid-token"
When invoking occ with "talk:bot:setup BOT(ErrorBot) ROOM(room)"
And the command was successful
And invoking occ with "talk:bot:setup BOT(ErrorBot) ROOM(room)"
Then the command failed with exit code 3
And the command output contains the text "Bot is already set up for the conversation"

0 comments on commit bf70a4e

Please sign in to comment.