Browse Source

tests(bots): Add tests for posting and reacting as a bot

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/10421/head
Joas Schilling 2 years ago
parent
commit
6cfa997125
No known key found for this signature in database GPG Key ID: 74434EFE0D2E2205
  1. 5
      tests/integration/features/bootstrap/CommandLineTrait.php
  2. 43
      tests/integration/features/bootstrap/FeatureContext.php
  3. 146
      tests/integration/features/chat/bots.feature

5
tests/integration/features/bootstrap/CommandLineTrait.php

@ -144,7 +144,10 @@ trait CommandLineTrait {
$msg = 'The command was not successful, exit code was ' . $this->lastCode . '.';
if (!empty($exceptions)) {
$msg .= ' Exceptions: ' . implode(', ', $exceptions);
$msg .= "\n" . ' Exceptions: ' . implode(', ', $exceptions);
} else {
$msg .= "\n" . ' ' . $this->lastStdOut;
$msg .= "\n" . ' ' . $this->lastStdErr;
}
throw new \Exception($msg);
} elseif (!empty($exceptions)) {

43
tests/integration/features/bootstrap/FeatureContext.php

@ -3654,6 +3654,49 @@ class FeatureContext implements Context, SnippetAcceptingContext {
$this->theCommandWasSuccessful();
}
/**
* @Then /^Bot "([^"]*)" (sends|removes) a (message|reaction) for room "([^"]*)" with (\d+)(?: \((v1)\))?$/
*/
public function botSendsRequest(string $botName, string $sends, string $action, string $identifier, int $status, string $apiVersion, TableNode $body): void {
$currentUser = $this->setCurrentUser(null);
$data = $body->getRowsHash();
$secret = $data['secret'];
unset($data['secret']);
if ($action === 'message') {
$url = '/message';
$toSign = $data['message'];
if (isset($data['replyTo'])) {
$data['replyTo'] = self::$textToMessageId[$data['replyTo']];
}
} else {
$url = '/reaction/' . self::$textToMessageId[$data['messageId']];
unset($data['messageId']);
$toSign = $data['reaction'];
}
$random = bin2hex(random_bytes(32));
$hash = hash_hmac('sha256', $random . $toSign, $secret);
$headers = [
'X-Nextcloud-Talk-Bot-Random' => $random,
'X-Nextcloud-Talk-Bot-Signature' => $hash,
];
$this->sendRequest(
$sends === 'sends' ? 'POST' : 'DELETE',
'/apps/spreed/api/' . $apiVersion . '/bot/' . self::$identifierToToken[$identifier] . $url,
$data,
$headers
);
$this->assertStatusCode($this->response, $status);
$this->setCurrentUser($currentUser);
}
/**
* @Then /^user "([^"]*)" (sets up|removes) bot "([^"]*)" for room "([^"]*)" with (\d+)(?: \((v1)\))?$/
*/

146
tests/integration/features/chat/bots.feature

@ -169,3 +169,149 @@ Feature: chat/bots
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"
Scenario: Bot with all features
Given invoking occ with "talk:bot:install Bot Secret1234567890123456789012345678901234567890 https://localhost/bot1"
And the command was successful
And read bot ids from OCC
And user "participant1" creates room "room1" (v4)
| roomType | 2 |
| roomName | room1 |
And invoking occ with "talk:bot:setup BOT(Bot) ROOM(room1)"
And the command was successful
And user "participant1" sends message "Message 1" to room "room1" with 201
When Bot "Bot" sends a message for room "room1" with 201 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| message | Response 1 |
| replyTo | Message 1 |
When Bot "Bot" sends a reaction for room "room1" with 201 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" sees the following messages in room "room1" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room1 | bots | BOT(Bot) | Bot (Bot) | Response 1 | [] | Message 1 |
| room1 | users | participant1 | participant1-displayname | Message 1 | [] | |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
| bots | BOT(Bot) | Bot (Bot) | 👍 |
When Bot "Bot" removes a reaction for room "room1" with 200 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
# Unchanged from above
Scenario: Bot with response only feature
Given invoking occ with "talk:bot:install Bot Secret1234567890123456789012345678901234567890 https://localhost/bot1 --feature=response"
And the command was successful
And read bot ids from OCC
And user "participant1" creates room "room1" (v4)
| roomType | 2 |
| roomName | room1 |
And invoking occ with "talk:bot:setup BOT(Bot) ROOM(room1)"
And the command was successful
And user "participant1" sends message "Message 1" to room "room1" with 201
When Bot "Bot" sends a message for room "room1" with 201 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| message | Response 1 |
| replyTo | Message 1 |
When Bot "Bot" sends a reaction for room "room1" with 201 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" sees the following messages in room "room1" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room1 | bots | BOT(Bot) | Bot (Bot) | Response 1 | [] | Message 1 |
| room1 | users | participant1 | participant1-displayname | Message 1 | [] | |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
| bots | BOT(Bot) | Bot (Bot) | 👍 |
When Bot "Bot" removes a reaction for room "room1" with 200 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
Scenario: Bot with receive-only feature
Given invoking occ with "talk:bot:install Bot Secret1234567890123456789012345678901234567890 https://localhost/bot1 --feature=webhook"
And the command was successful
And read bot ids from OCC
And user "participant1" creates room "room1" (v4)
| roomType | 2 |
| roomName | room1 |
And invoking occ with "talk:bot:setup BOT(Bot) ROOM(room1)"
And the command was successful
And user "participant1" sends message "Message 1" to room "room1" with 201
When Bot "Bot" sends a message for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| message | Response 1 |
| replyTo | Message 1 |
When Bot "Bot" sends a reaction for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" sees the following messages in room "room1" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room1 | users | participant1 | participant1-displayname | Message 1 | [] | |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
When Bot "Bot" removes a reaction for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Scenario: Bot from another room
Given invoking occ with "talk:bot:install Bot Secret1234567890123456789012345678901234567890 https://localhost/bot1 --feature=response"
And the command was successful
And read bot ids from OCC
And user "participant1" creates room "room1" (v4)
| roomType | 2 |
| roomName | room1 |
And user "participant1" creates room "room2" (v4)
| roomType | 2 |
| roomName | room2 |
And invoking occ with "talk:bot:setup BOT(Bot) ROOM(room2)"
And the command was successful
And user "participant1" sends message "Message 1" to room "room1" with 201
# Unauthenticated on actual room
When Bot "Bot" sends a message for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| message | Response 1 |
| replyTo | Message 1 |
# Bad requeston bot room
When Bot "Bot" sends a message for room "room2" with 400 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| message | Response 1 |
| replyTo | Message 1 |
# Unauthenticated on actual room
When Bot "Bot" sends a reaction for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
# Bad request on bot room
When Bot "Bot" sends a reaction for room "room2" with 400 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" sees the following messages in room "room1" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room1 | users | participant1 | participant1-displayname | Message 1 | [] | |
Then user "participant1" sees the following messages in room "room2" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
# Unauthenticated on actual room
When Bot "Bot" removes a reaction for room "room1" with 401 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
# Bad request on bot room
When Bot "Bot" removes a reaction for room "room2" with 404 (v1)
| secret | Secret1234567890123456789012345678901234567890 |
| messageId | Message 1 |
| reaction | 👍 |
Then user "participant1" retrieve reactions "👍" of message "Message 1" in room "room1" with 200
| actorType | actorId | actorDisplayName | reaction |
Loading…
Cancel
Save