Skip to content

Commit

Permalink
[api-test] first notification test (#5958)
Browse files Browse the repository at this point in the history
* notification test

* fix after review

* fix lint
  • Loading branch information
ScharfViktor authored Mar 31, 2023
1 parent 351ad9c commit e3c5466
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 0 deletions.
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
127 changes: 127 additions & 0 deletions tests/acceptance/features/apiSpaces/notification.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
@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" lists all notifications
Then the HTTP status code should be "200"
And the JSON response should contain a notification message with the subject "Space shared" and the message-details should 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",
"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",
"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"]
}
}
}
"""

148 changes: 148 additions & 0 deletions tests/acceptance/features/bootstrap/NotificationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?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 getLastNotificationId():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 "([^"]*)" lists 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 a notification message with the subject "([^"]*)" and the message-details should match$/
*
* @param string $subject
* @param PyStringNode $schemaString
*
* @return void
* @throws Exception
*/
public function theJsonDataFromLastResponseShouldMatch(
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 === $subject) {
$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(),
[],
[],
null,
$this->getUserRecipient(),
);
JsonAssertions::assertJsonDocumentMatchesSchema(
$responseBody,
$this->featureContext->getJSONSchema($schemaString)
);
}
}

0 comments on commit e3c5466

Please sign in to comment.