From 1569da0db17606653ab00a7640cfbdf84988c42c Mon Sep 17 00:00:00 2001 From: amrita Date: Mon, 17 Jul 2023 12:47:45 +0545 Subject: [PATCH] Add tests for deleting specific notification --- .../deleteNotification.feature | 30 ++++ .../bootstrap/NotificationContext.php | 135 +++++++++++++++++- 2 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 tests/acceptance/features/apiNotification/deleteNotification.feature diff --git a/tests/acceptance/features/apiNotification/deleteNotification.feature b/tests/acceptance/features/apiNotification/deleteNotification.feature new file mode 100644 index 00000000000..bb32c544ea0 --- /dev/null +++ b/tests/acceptance/features/apiNotification/deleteNotification.feature @@ -0,0 +1,30 @@ +@api +Feature: Delete notification + As a user + I want to delete notifications + So that I can filter notifications + + Background: + Given these users have been created with default attributes and without skeleton files: + | username | + | Alice | + | Brian | + And user "Alice" has uploaded file with content "other data" to "/textfile1.txt" + And user "Alice" has created folder "my_data" + And user "Alice" has shared folder "my_data" with user "Brian" + And user "Alice" has shared file "/textfile1.txt" with user "Brian" + + + Scenario: delete a notification + When user "Brian" deletes a notification related to resource "my_data" with subject "Resource shared" + Then the HTTP status code should be "200" + And user "Brian" should have a notification with subject "Resource shared" and message: + | message | + | Alice Hansen shared textfile1.txt with you | + But user "Brian" should not have a notification related to resource "my_data" with subject "Resource shared" + + + Scenario: delete all notifications + When user "Brian" deletes all notifications + Then the HTTP status code should be "200" + And user "Brian" should not have any notification diff --git a/tests/acceptance/features/bootstrap/NotificationContext.php b/tests/acceptance/features/bootstrap/NotificationContext.php index d5c10602f16..ff4396b1b9b 100644 --- a/tests/acceptance/features/bootstrap/NotificationContext.php +++ b/tests/acceptance/features/bootstrap/NotificationContext.php @@ -14,6 +14,8 @@ use PHPUnit\Framework\Assert; use TestHelpers\GraphHelper; use Behat\Gherkin\Node\TableNode; +use GuzzleHttp\Exception\GuzzleException; +use Psr\Http\Message\ResponseInterface; require_once 'bootstrap.php'; @@ -104,6 +106,68 @@ public function userListAllNotifications(string $user):void { $this->featureContext->setResponse($response); } + /** + * @When user :user deletes all notifications + * + * @param string $user + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function userDeletesAllNotifications(string $user):void { + $this->userListAllNotifications($user); + if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { + $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; + foreach ($responseBody as $value) { + // set notificationId + $this->notificationIds[] = $value->notification_id; + } + } + $this->featureContext->setResponse($this->userDeletesNotification($user)); + } + + /** + * @When user :user deletes a notification related to resource :resource with subject :subject + * + * @param string $user + * @param string $resource + * @param string $subject + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function userDeletesNotificationOfResourceAndSubject(string $user, string $resource, string $subject):void { + $this->userListAllNotifications($user); + $this->filterResponseByNotificationSubjectAndResource($subject, $resource); + $this->featureContext->setResponse($this->userDeletesNotification($user)); + } + + /** + * deletes notification + * + * @param string $user + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function userDeletesNotification(string $user):ResponseInterface { + $this->setUserRecipient($user); + $payload["ids"] = $this->getNotificationIds(); + return OcsApiHelper::sendRequest( + $this->featureContext->getBaseUrl(), + $this->featureContext->getActualUsername($user), + $this->featureContext->getPasswordForUser($user), + 'DELETE', + $this->notificationEndpointPath, + $this->featureContext->getStepLineRef(), + \json_encode($payload), + 2 + ); + } + /** * @Then the notifications should be empty * @@ -111,10 +175,31 @@ public function userListAllNotifications(string $user):void { * @throws Exception */ public function theNotificationsShouldBeEmpty(): void { + $statusCode = $this->featureContext->getResponse()->getStatusCode(); + if ($statusCode !== 200) { + $response = $this->featureContext->getResponse()->getBody()->getContents(); + throw new \Exception( + __METHOD__ + . " Failed to get user notification list" . $response + ); + } $notifications = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; Assert::assertNull($notifications, "response should not contain any notification"); } + /** + * @Then user :user should not have any notification + * + * @param $user + * + * @return void + * @throws Exception + */ + public function userShouldNotHaveAnyNotification($user): void { + $this->userListAllNotifications($user); + $this->theNotificationsShouldBeEmpty(); + } + /** * @Then /^user "([^"]*)" should have "([^"]*)" notifications$/ * @@ -168,6 +253,8 @@ public function theJsonDataFromLastResponseShouldMatch( } /** + * filter notification according to subject + * * @param string $subject * * @return object @@ -191,7 +278,37 @@ public function filterResponseAccordingToNotificationSubject(string $subject): o } /** - * @Then user :user should get a notification with subject :subject and message: + * filter notification according to subject and resource + * + * @param string $subject + * @param string $resource + * + * @return array + */ + public function filterResponseByNotificationSubjectAndResource(string $subject, string $resource): array { + $responseBodyArray = []; + $statusCode = $this->featureContext->getResponse()->getStatusCode(); + if ($statusCode !== 200) { + $response = $this->featureContext->getResponse()->getBody()->getContents(); + Assert::fail($response . " Response should contain status code 200"); + } + if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { + $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; + foreach ($responseBody as $value) { + if (isset($value->subject) && $value->subject === $subject && isset($value->messageRichParameters->resource->name) && $value->messageRichParameters->resource->name === $resource) { + $this->notificationIds[] = $value->notification_id; + $responseBodyArray[] = $value; + } + } + } else { + $responseBodyArray[] = $this->featureContext->getJsonDecodedResponseBodyContent(); + Assert::fail("Response should contain notification but found: $responseBodyArray"); + } + return $responseBodyArray; + } + + /** + * @Then /^user "([^"]*)" should (?:get|have) a notification with subject "([^"]*)" and message:$/ * * @param string $user * @param string $subject @@ -201,7 +318,6 @@ public function filterResponseAccordingToNotificationSubject(string $subject): o */ public function userShouldGetANotificationWithMessage(string $user, string $subject, TableNode $table):void { $this->userListAllNotifications($user); - $this->featureContext->theHTTPStatusCodeShouldBe(200); $actualMessage = str_replace(["\r", "\n"], " ", $this->filterResponseAccordingToNotificationSubject($subject)->message); $expectedMessage = $table->getColumnsHash()[0]['message']; Assert::assertSame( @@ -211,6 +327,21 @@ public function userShouldGetANotificationWithMessage(string $user, string $subj ); } + /** + * @Then user :user should not have a notification related to resource :resource with subject :subject + * + * @param string $user + * @param string $resource + * @param string $subject + * + * @return void + */ + public function userShouldNotHaveANotificationRelatedToResourceWithSubject(string $user, string $resource, string $subject):void { + $this->userListAllNotifications($user); + $response = $this->filterResponseByNotificationSubjectAndResource($subject, $resource); + Assert::assertCount(0, $response, "Response should not contain notification related to resource '$resource' with subject '$subject' but found" . print_r($response, true)); + } + /** * @Then user :user should have received the following email from user :sender about the share of project space :spaceName *