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

[api-test] first notification test #5958

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default:
- ChecksumContext:
- FavoritesContext:
- FilesVersionsContext:
- NotificationContext:
- OCSContext:
- PublicWebDavContext:
- TrashbinContext:
Expand All @@ -43,6 +44,7 @@ default:
- ChecksumContext:
- FavoritesContext:
- FilesVersionsContext:
- NotificationContext:
- OCSContext:
- PublicWebDavContext:
- SearchContext:
Expand Down
126 changes: 126 additions & 0 deletions tests/acceptance/features/apiSpaces/notification.feature
Original file line number Diff line number Diff line change
@@ -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"
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
},
"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"]
}
}
}
"""

157 changes: 157 additions & 0 deletions tests/acceptance/features/bootstrap/NotificationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Viktor Scharf <[email protected]>
* @copyright Copyright (c) 2023 Viktor Scharf [email protected]
*/

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 {
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
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$/
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
*
* @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$/
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
*
* @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]
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
]
],
null,
$this->getUserRecipient(),
);
JsonAssertions::assertJsonDocumentMatchesSchema(
$responseBody,
$this->featureContext->getJSONSchema($schemaString)
);
}
}