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

[tests-only][full-ci]Add tests for deleting notification #6735

Merged
merged 1 commit into from
Jul 18, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@api
Feature: Delete notification
As a user
I want to delete notifications
So that I can filter notifications
amrita-shrestha marked this conversation as resolved.
Show resolved Hide resolved

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:
amrita-shrestha marked this conversation as resolved.
Show resolved Hide resolved
| 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
135 changes: 133 additions & 2 deletions tests/acceptance/features/bootstrap/NotificationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -104,17 +106,100 @@ 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);
SagarGi marked this conversation as resolved.
Show resolved Hide resolved
$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
*
* @return 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$/
*
Expand Down Expand Up @@ -168,6 +253,8 @@ public function theJsonDataFromLastResponseShouldMatch(
}

/**
* filter notification according to subject
*
* @param string $subject
*
* @return object
Expand All @@ -191,7 +278,37 @@ public function filterResponseAccordingToNotificationSubject(string $subject): o
}

/**
* @Then user :user should get a notification with subject :subject and message:
KarunAtreya marked this conversation as resolved.
Show resolved Hide resolved
* 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:$/
amrita-shrestha marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $user
* @param string $subject
Expand All @@ -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(
Expand All @@ -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);
SagarGi marked this conversation as resolved.
Show resolved Hide resolved
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
*
Expand Down