Skip to content
Merged
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
187 changes: 37 additions & 150 deletions tests/Integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\TableNode;
use Behat\Hook\AfterScenario;
use Behat\Hook\BeforeScenario;
use Behat\Step\Given;
use Behat\Step\Then;
use Behat\Step\When;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ClientException;
Expand Down Expand Up @@ -62,34 +67,19 @@ public function __construct() {
$this->baseUrl = getenv('TEST_SERVER_URL');
}

/**
* @Given /^user "([^"]*)" has notifications$/
*
* @param string $user
*/
#[Given('/^user "([^"]*)" has notifications$/')]
public function hasNotifications(string $user) {
$response = $this->setTestingValue('POST', 'apps/notificationsintegrationtesting/notifications?userId=' . $user, null);
$this->assertStatusCode($response, 200);
}

/**
* @Given /^user "([^"]*)" receives notification with$/
*
* @param string $user
* @param TableNode|null $formData
*/
#[Given('/^user "([^"]*)" receives notification with$/')]
public function receiveNotification(string $user, TableNode $formData) {
$response = $this->setTestingValue('POST', 'apps/notificationsintegrationtesting/notifications?userId=' . $user, $formData);
$this->assertStatusCode($response, 200);
}

/**
* @Given /^user "([^"]*)" sends admin notification to "([^"]*)" with$/
*
* @param string $sender
* @param string $recipient
* @param TableNode|null $formData
*/
#[Given('/^user "([^"]*)" sends admin notification to "([^"]*)" with$/')]
public function sendAdminNotification(string $sender, string $recipient, TableNode $formData) {
$currentUser = $this->currentUser;
$this->setCurrentUser($sender);
Expand All @@ -98,11 +88,7 @@ public function sendAdminNotification(string $sender, string $recipient, TableNo
$this->setCurrentUser($currentUser);
}

/**
* @When /^getting notifications on (v\d+)(| with different etag| with matching etag)$/
* @param string $api
* @param string $eTag
*/
#[When('/^getting notifications on (v\d+)(| with different etag| with matching etag)$/')]
public function gettingNotifications(string $api, string $eTag) {
$headers = [];
if ($eTag === ' with different etag') {
Expand All @@ -116,18 +102,12 @@ public function gettingNotifications(string $api, string $eTag) {
$this->lastEtag = array_pop($etagHeaders);
}

/**
* @Then /^response body is empty$/
*/
#[Then('/^response body is empty$/')]
public function checkResponseBodyIsEmpty() {
Assert::assertSame('', $this->response->getBody()->getContents());
}

/**
* @Then /^list of notifications has (\d+) entries$/
*
* @param int $numNotifications
*/
#[Then('/^list of notifications has (\d+) entries$/')]
public function checkNumNotifications(int $numNotifications) {
$notifications = $this->getArrayOfNotificationsResponded($this->response);
Assert::assertCount((int)$numNotifications, $notifications);
Expand All @@ -140,9 +120,7 @@ public function checkNumNotifications(int $numNotifications) {
$this->notificationIds[] = $notificationIds;
}

/**
* @Then /^confirms previously fetched notification ids exist on (v\d+)$/
*/
#[Then('/^confirms previously fetched notification ids exist on (v\d+)$/')]
public function checkNotificationsExists(string $api) {
$notificationIds = end($this->notificationIds);

Expand Down Expand Up @@ -173,14 +151,7 @@ protected function getDataFromOCSResponse(ResponseInterface $response): array {
return $jsonBody['ocs']['data'];
}

/**
* @Then /^user "([^"]*)" has (\d+) notifications on (v\d+)(| missing the last one| missing the first one)$/
*
* @param string $user
* @param int $numNotifications
* @param string $api
* @param string $missingLast
*/
#[Then('/^user "([^"]*)" has (\d+) notifications on (v\d+)(| missing the last one| missing the first one)$/')]
public function userNumNotifications(string $user, int $numNotifications, string $api, string $missingLast) {
$this->sendingTo('GET', '/apps/notifications/api/' . $api . '/notifications?format=json');
$this->assertStatusCode($this->response, 200);
Expand All @@ -205,13 +176,7 @@ public function userNumNotifications(string $user, int $numNotifications, string
}
}

/**
* @Then /^(first|last) notification on (v\d+) matches$/
*
* @param string $notification
* @param string $api
* @param TableNode|null $formData
*/
#[Then('/^(first|last) notification on (v\d+) matches$/')]
public function matchNotification(string $notification, string $api, ?TableNode $formData = null) {
$lastNotifications = end($this->notificationIds);
if ($notification === 'first') {
Expand All @@ -230,12 +195,7 @@ public function matchNotification(string $notification, string $api, ?TableNode
}
}

/**
* @Then /^delete (first|last|same|faulty) notification on (v\d+)$/
*
* @param string $toDelete
* @param string $api
*/
#[Then('/^delete (first|last|same|faulty) notification on (v\d+)$/')]
public function deleteNotification(string $toDelete, string $api) {
Assert::assertNotEmpty($this->notificationIds);
$lastNotificationIds = end($this->notificationIds);
Expand All @@ -249,34 +209,21 @@ public function deleteNotification(string $toDelete, string $api) {
$this->sendingTo('DELETE', '/apps/notifications/api/' . $api . '/notifications/' . $this->deletedNotification);
}

/**
* @Then /^delete all notifications on (v\d+)$/
*
* @param string $api
*/
#[Then('/^delete all notifications on (v\d+)$/')]
public function deleteAllNotification($api) {
Assert::assertNotEmpty($this->notificationIds);
$this->sendingTo('DELETE', '/apps/notifications/api/' . $api . '/notifications');
}

/**
* @Then /^user "([^"]*)" unregisters from push notifications/
*
* @param string $user
*/
#[Then('/^user "([^"]*)" unregisters from push notifications/')]
public function unregisterForPushNotifications(string $user) {
$currentUser = $this->currentUser;
$this->setCurrentUser($user);
$this->sendingToWith('DELETE', '/apps/notifications/api/v2/push?format=json');
$this->setCurrentUser($currentUser);
}

/**
* @Then /^user "([^"]*)" registers for push notifications with$/
*
* @param string $user
* @param TableNode|null $formData
*/
#[Then('/^user "([^"]*)" registers for push notifications with$/')]
public function registerForPushNotifications(string $user, TableNode $formData) {
$data = $formData->getRowsHash();

Expand All @@ -299,9 +246,7 @@ public function registerForPushNotifications(string $user, TableNode $formData)
$this->setCurrentUser($currentUser);
}

/**
* @Then /^can validate the response and (skip verifying|verify) signature$/
*/
#[Then('/^can validate the response and (skip verifying|verify) signature$/')]
public function validateResponseAndSignature(string $verify): void {
$response = $this->getArrayOfNotificationsResponded($this->response);

Expand All @@ -324,11 +269,7 @@ public function validateResponseAndSignature(string $verify): void {
}
}

/**
* @Then /^user "([^"]*)" creates an app password$/
*
* @param string $user
*/
#[Then('/^user "([^"]*)" creates an app password$/')]
public function createAppPassword(string $user) {
$currentUser = $this->currentUser;
$this->setCurrentUser($user);
Expand All @@ -340,47 +281,30 @@ public function createAppPassword(string $user) {
$this->appPasswords[$user] = $response['apppassword'];
}

/**
* @Then /^user "([^"]*)" forgets the app password$/
*
* @param string $user
*/
#[Then('/^user "([^"]*)" forgets the app password$/')]
public function removeAppPassword(string $user) {
unset($this->appPasswords[$user]);
}

/**
* @Then /^error "([^"]*)" is expected with status code ([0-9]*)$/
*
* @param string $error
* @param int $statusCode
*/
#[Then('/^error "([^"]*)" is expected with status code ([0-9]*)$/')]
public function expectedErrorOnLastRequest(string $error, int $statusCode) {
$this->assertStatusCode($this->response, $statusCode);
$response = $this->getArrayOfNotificationsResponded($this->response);

Assert::assertEquals($error, $response['message']);
}

/**
* @Then /^status code is ([0-9]*)$/
*
* @param int $statusCode
*/
#[Then('/^status code is ([0-9]*)$/')]
public function isStatusCode(int $statusCode) {
$this->assertStatusCode($this->response, $statusCode);
}

/**
* @Given /^webpush is enabled$/
*/
#[Given('/^webpush is enabled$/')]
public function enableWebPush() {
$this->runOcc(['config:app:set', 'notifications', 'webpush_enabled', '--value=1']);
}

/**
* @Given /^webpush is disabled$/
*/
#[Given('/^webpush is disabled$/')]
public function disableWebPush() {
$this->runOcc(['config:app:set', 'notifications', 'webpush_enabled', '--value=0']);
}
Expand All @@ -406,27 +330,21 @@ protected function generateWebPushKeys(): void {
$this->webPushAuth = rtrim(strtr(base64_encode($authBytes), '+/', '-_'), '=');
}

/**
* @When /^user "([^"]*)" fetches the VAPID public key$/
*/
#[When('/^user "([^"]*)" fetches the VAPID public key$/')]
public function fetchVapidPublicKey(string $user) {
$currentUser = $this->currentUser;
$this->setCurrentUser($user);
$this->sendingToWith('GET', '/apps/notifications/api/v2/webpush/vapid?format=json');
$this->setCurrentUser($currentUser);
}

/**
* @Then /^the VAPID key is not empty$/
*/
#[Then('/^the VAPID key is not empty$/')]
public function vapidKeyIsNotEmpty() {
$response = $this->getDataFromOCSResponse($this->response);
Assert::assertNotEmpty($response['vapid'], 'VAPID public key should not be empty');
}

/**
* @Given /^user "([^"]*)" registers for webpush with$/
*/
#[Given('/^user "([^"]*)" registers for webpush with$/')]
public function registerForWebPush(string $user, TableNode $formData) {
$data = $formData->getRowsHash();

Expand All @@ -446,9 +364,7 @@ public function registerForWebPush(string $user, TableNode $formData) {
$this->setCurrentUser($currentUser);
}

/**
* @Given /^user "([^"]*)" activates webpush with the activation token$/
*/
#[Given('/^user "([^"]*)" activates webpush with the activation token$/')]
public function activateWebPushWithDbToken(string $user) {
// Fetch activation token from the database via the testing helper
$currentUser = $this->currentUser;
Expand All @@ -468,9 +384,7 @@ public function activateWebPushWithDbToken(string $user) {
$this->setCurrentUser($currentUser);
}

/**
* @Given /^user "([^"]*)" activates webpush with token "([^"]*)"$/
*/
#[Given('/^user "([^"]*)" activates webpush with token "([^"]*)"$/')]
public function activateWebPushWithToken(string $user, string $token) {
$currentUser = $this->currentUser;
$this->setCurrentUser($user);
Expand All @@ -480,31 +394,21 @@ public function activateWebPushWithToken(string $user, string $token) {
$this->setCurrentUser($currentUser);
}

/**
* @Given /^user "([^"]*)" removes webpush subscription$/
*/
#[Given('/^user "([^"]*)" removes webpush subscription$/')]
public function removeWebPushSubscription(string $user) {
$currentUser = $this->currentUser;
$this->setCurrentUser($user);
$this->sendingToWith('DELETE', '/apps/notifications/api/v2/webpush?format=json');
$this->setCurrentUser($currentUser);
}

/**
* @BeforeScenario
* @AfterScenario
*/
#[BeforeScenario]
#[AfterScenario]
public function clearNotifications() {
$response = $this->setTestingValue('DELETE', 'apps/notificationsintegrationtesting', null);
$this->assertStatusCode($response, 200);
}

/**
* @param string $verb
* @param string $url
* @param TableNode $body
* @return \GuzzleHttp\Message\FutureResponse|ResponseInterface|null
*/
protected function setTestingValue(string $verb, string $url, ?TableNode $body = null) {
$fullUrl = $this->baseUrl . 'ocs/v2.php/' . $url;
$client = new Client();
Expand All @@ -529,18 +433,12 @@ protected function setTestingValue(string $verb, string $url, ?TableNode $body =
* User management
*/

/**
* @Given /^as user "([^"]*)"$/
* @param string $user
*/
#[Given('/^as user "([^"]*)"$/')]
public function setCurrentUser(string $user) {
$this->currentUser = $user;
}

/**
* @Given /^user "([^"]*)" exists$/
* @param string $user
*/
#[Given('/^user "([^"]*)" exists$/')]
public function assureUserExists(string $user) {
try {
$this->userExists($user);
Expand Down Expand Up @@ -596,22 +494,15 @@ private function createUser(string $user) {
* Requests
*/

/**
* @When /^sending "([^"]*)" to "([^"]*)"$/
* @param string $verb
* @param string $url
*/
#[When('/^sending "([^"]*)" to "([^"]*)"$/')]
public function sendingTo(string $verb, string $url) {
$this->sendingToWith($verb, $url, null);
}

/**
* @When /^sending "([^"]*)" to "([^"]*)" with$/
* @param string $verb
* @param string $url
* @param TableNode|array|null $body
* @param array $headers
*/
#[When('/^sending "([^"]*)" to "([^"]*)" with$/')]
public function sendingToWith(string $verb, string $url, $body = null, array $headers = []) {
$fullUrl = $this->baseUrl . 'ocs/v2.php' . $url;
$client = new Client();
Expand Down Expand Up @@ -641,10 +532,6 @@ public function sendingToWith(string $verb, string $url, $body = null, array $he
}
}

/**
* @param ResponseInterface $response
* @param int $statusCode
*/
protected function assertStatusCode(ResponseInterface $response, int $statusCode) {
Assert::assertEquals($statusCode, $response->getStatusCode());
}
Expand Down
Loading