From a68400dd132cb9e6ab4de56a1207dfb1b764a738 Mon Sep 17 00:00:00 2001 From: Viktor Scharf Date: Wed, 29 Mar 2023 15:46:21 +0200 Subject: [PATCH 1/3] notification test --- tests/acceptance/config/behat.yml | 2 + .../features/apiSpaces/notification.feature | 126 ++++++++++++++ .../bootstrap/NotificationContext.php | 157 ++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 tests/acceptance/features/apiSpaces/notification.feature create mode 100644 tests/acceptance/features/bootstrap/NotificationContext.php diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 7b97371de48..73aa64c21ed 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -26,6 +26,7 @@ default: - ChecksumContext: - FavoritesContext: - FilesVersionsContext: + - NotificationContext: - OCSContext: - PublicWebDavContext: - TrashbinContext: @@ -43,6 +44,7 @@ default: - ChecksumContext: - FavoritesContext: - FilesVersionsContext: + - NotificationContext: - OCSContext: - PublicWebDavContext: - SearchContext: diff --git a/tests/acceptance/features/apiSpaces/notification.feature b/tests/acceptance/features/apiSpaces/notification.feature new file mode 100644 index 00000000000..a694aee4704 --- /dev/null +++ b/tests/acceptance/features/apiSpaces/notification.feature @@ -0,0 +1,126 @@ +@api @skipOnOcV10 +Feature: Notification + As a user + I want to be notified of actions related to me + + Background: + Given these users have been created with default attributes and without skeleton files: + | username | + | Alice | + | Brian | + | Carol | + And the administrator has given "Alice" the role "Space Admin" using the settings api + + + Scenario: user gets a notification of space sharing + Given user "Alice" has created a space "notificaton checking" with the default quota using the GraphApi + And user "Alice" has shared a space "notificaton checking" with settings: + | shareWith | Brian | + | role | editor | + When user "Brian" list all notifications + Then the HTTP status code should be "200" + And the JSON response should contain message with type "Space shared" and match + """ + { + "type": "object", + "required": [ + "app", + "datetime", + "message", + "messageRich", + "messageRichParameters", + "notification_id", + "object_id", + "object_type", + "subject", + "subjectRich", + "user" + ], + "properties": { + "app": { + "type": "string", + "enum": ["userlog"] + }, + "message": { + "type": "string", + "enum": ["Alice Hansen added you to Space notificaton checking"] + }, + "messageRich": { + "type": "string", + "enum": ["{user} added you to Space {space}"] + }, + "messageRichParameters": { + "type": "object", + "required": [ + "space", + "user" + ], + "properties": { + "space": { + "type": "object", + "required": [ + "id", + "name" + ], + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string", + "enum": ["notificaton checking"] + } + } + }, + "user": { + "type": "object", + "required": [ + "displayname", + "id", + "name" + ], + "properties": { + "displayname": { + "type": "string", + "enum": ["Alice Hansen"] + }, + "id": { + "type": "string", + "enim": ["%user_id%"] + }, + "name": { + "type": "string", + "enum": ["Alice"] + } + } + } + } + }, + "notification_id": { + "type": "string" + + }, + "object_id": { + "type": "string" + + }, + "object_type": { + "type": "string", + "enum": ["storagespace"] + }, + "subject": { + "type": "string", + "enum": ["Space shared"] + }, + "subjectRich": { + "type": "string", + "enum": ["Space shared"] + }, + "user": { + "type": "string", + "enum": ["Alice"] + } + } + } + """ + diff --git a/tests/acceptance/features/bootstrap/NotificationContext.php b/tests/acceptance/features/bootstrap/NotificationContext.php new file mode 100644 index 00000000000..3e329b0ac68 --- /dev/null +++ b/tests/acceptance/features/bootstrap/NotificationContext.php @@ -0,0 +1,157 @@ + + * @copyright Copyright (c) 2023 Viktor Scharf vscharf@owncloud.com + */ + +use Behat\Behat\Context\Context; +use Behat\Behat\Hook\Scope\BeforeScenarioScope; +use TestHelpers\OcsApiHelper; +use Behat\Gherkin\Node\PyStringNode; +use Helmich\JsonAssert\JsonAssertions; + +require_once 'bootstrap.php'; + +/** + * Defines application features from the specific context. + */ +class NotificationContext implements Context { + /** + * @var FeatureContext + */ + private $featureContext; + + /** + * @var string + */ + private string $notificationEndpointPath = '/apps/notifications/api/v1/notifications?format=json'; + + /** + * @var array[] + */ + private $notificationIds; + + /** + * @return array[] + */ + public function getNotificationIds():array { + return $this->notificationIds; + } + + /** + * @return array[] + */ + public function getLastNotificationIds():array { + return \end($this->notificationIds); + } + + /** + * @var string + */ + private string $userRecipient; + + /** + * @param string $userRecipient + * + * @return void + */ + public function setUserRecipient(string $userRecipient): void { + $this->userRecipient = $userRecipient; + } + + /** + * @return string + */ + public function getUserRecipient(): string { + return $this->userRecipient; + } + + /** + * @BeforeScenario + * + * @param BeforeScenarioScope $scope + * + * @return void + * @throws Exception + */ + public function setUpScenario(BeforeScenarioScope $scope):void { + // Get the environment + $environment = $scope->getEnvironment(); + // Get all the contexts you need in this context + $this->featureContext = $environment->getContext('FeatureContext'); + } + + /** + * @Then /^user "([^"]*)" list all notifications$/ + * + * @param string $user + * + * @return void + */ + public function userListAllNotifications(string $user):void { + $this->setUserRecipient($user); + $response = OcsApiHelper::sendRequest( + $this->featureContext->getBaseUrl(), + $this->featureContext->getActualUsername($user), + $this->featureContext->getPasswordForUser($user), + 'GET', + $this->notificationEndpointPath, + $this->featureContext->getStepLineRef() + ); + $this->featureContext->setResponse($response); + } + + /** + * @Then /^the JSON response should contain message with type "([^"]*)" and match$/ + * + * @param string $messageType + * @param string|null $spaceName + * @param PyStringNode $schemaString + * + * @return void + * @throws Exception + */ + public function theJsonDataFromLastResponseShouldMatch( + string $messageType, + ?string $spaceName = null, + PyStringNode $schemaString + ): void { + if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { + $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; + foreach ($responseBody as $value) { + if (isset($value->subject) && $value->subject === $messageType) { + $responseBody = $value; + // set notificationId + $this->notificationIds[] = $value->notification_id; + break; + } + } + } else { + $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent(); + } + + // substitute the value here + $schemaString = $schemaString->getRaw(); + $schemaString = $this->featureContext->substituteInLineCodes( + $schemaString, + $this->featureContext->getCurrentUser(), + [], + [ + [ + "code" => "%space_id%", + "function" => + [$this, "getSpaceIdByNameFromResponse"], + "parameter" => [$spaceName] + ] + ], + null, + $this->getUserRecipient(), + ); + JsonAssertions::assertJsonDocumentMatchesSchema( + $responseBody, + $this->featureContext->getJSONSchema($schemaString) + ); + } +} From ab6cdeadb214ab0ee4fcecd8f4602f4f18f578b4 Mon Sep 17 00:00:00 2001 From: Viktor Scharf Date: Thu, 30 Mar 2023 14:23:30 +0200 Subject: [PATCH 2/3] fix after review --- .../features/apiSpaces/notification.feature | 7 +++--- .../features/bootstrap/GraphContext.php | 2 +- .../bootstrap/NotificationContext.php | 23 ++++++------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/tests/acceptance/features/apiSpaces/notification.feature b/tests/acceptance/features/apiSpaces/notification.feature index a694aee4704..f88a72c3f80 100644 --- a/tests/acceptance/features/apiSpaces/notification.feature +++ b/tests/acceptance/features/apiSpaces/notification.feature @@ -17,9 +17,9 @@ Feature: Notification And user "Alice" has shared a space "notificaton checking" with settings: | shareWith | Brian | | role | editor | - When user "Brian" list all notifications + When user "Brian" lists all notifications Then the HTTP status code should be "200" - And the JSON response should contain message with type "Space shared" and match + And the JSON response should contain a notification message with the subject "Space shared" and the message-details should match """ { "type": "object", @@ -64,7 +64,8 @@ Feature: Notification ], "properties": { "id": { - "type": "string" + "type": "string", + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\\$[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}![a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" }, "name": { "type": "string", diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 56622a08811..e918589342c 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1688,7 +1688,7 @@ public function checkUserDriveInformation(array $expectedDriveInformation, array Assert::assertTrue(GraphHelper::isUUIDv4($actualKeyValue), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValue); break; case '%space_id%': - Assert::assertTrue(GraphHelper::isSpaceId($actualKeyValue), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue); + Assert::assertTrue(GraphHelper::isSpaceId($actualKeyValue), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue); break; default: $expectedDriveInformation[$keyName] = $this->featureContext->substituteInLineCodes( diff --git a/tests/acceptance/features/bootstrap/NotificationContext.php b/tests/acceptance/features/bootstrap/NotificationContext.php index 3e329b0ac68..1cd68b9905e 100644 --- a/tests/acceptance/features/bootstrap/NotificationContext.php +++ b/tests/acceptance/features/bootstrap/NotificationContext.php @@ -43,7 +43,7 @@ public function getNotificationIds():array { /** * @return array[] */ - public function getLastNotificationIds():array { + public function getLastNotificationId():array { return \end($this->notificationIds); } @@ -84,7 +84,7 @@ public function setUpScenario(BeforeScenarioScope $scope):void { } /** - * @Then /^user "([^"]*)" list all notifications$/ + * @Then /^user "([^"]*)" lists all notifications$/ * * @param string $user * @@ -104,24 +104,22 @@ public function userListAllNotifications(string $user):void { } /** - * @Then /^the JSON response should contain message with type "([^"]*)" and match$/ + * @Then /^the JSON response should contain a notification message with the subject "([^"]*)" and the message-details should match$/ * - * @param string $messageType - * @param string|null $spaceName + * @param string $subject * @param PyStringNode $schemaString * * @return void * @throws Exception */ public function theJsonDataFromLastResponseShouldMatch( - string $messageType, - ?string $spaceName = null, + string $subject, PyStringNode $schemaString ): void { if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; foreach ($responseBody as $value) { - if (isset($value->subject) && $value->subject === $messageType) { + if (isset($value->subject) && $value->subject === $subject) { $responseBody = $value; // set notificationId $this->notificationIds[] = $value->notification_id; @@ -138,14 +136,7 @@ public function theJsonDataFromLastResponseShouldMatch( $schemaString, $this->featureContext->getCurrentUser(), [], - [ - [ - "code" => "%space_id%", - "function" => - [$this, "getSpaceIdByNameFromResponse"], - "parameter" => [$spaceName] - ] - ], + [], null, $this->getUserRecipient(), ); From 4f5f100242ed0d38a7e85b474789fc6d2526884a Mon Sep 17 00:00:00 2001 From: Viktor Scharf Date: Thu, 30 Mar 2023 14:44:23 +0200 Subject: [PATCH 3/3] fix lint --- tests/acceptance/features/bootstrap/GraphContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index e918589342c..56622a08811 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1688,7 +1688,7 @@ public function checkUserDriveInformation(array $expectedDriveInformation, array Assert::assertTrue(GraphHelper::isUUIDv4($actualKeyValue), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValue); break; case '%space_id%': - Assert::assertTrue(GraphHelper::isSpaceId($actualKeyValue), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue); + Assert::assertTrue(GraphHelper::isSpaceId($actualKeyValue), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue); break; default: $expectedDriveInformation[$keyName] = $this->featureContext->substituteInLineCodes(