Refactor spaces acceptance tests

This commit is contained in:
Phil Davis committed 2021-10-28 13:27:41 +05:45
1 parent 55adc87bd2
commit 87997af0b6
3 files changed
+102 -95

No files matched your search

@@ -25,7 +25,7 @@ class RevaContext implements Context {
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function setUpScenario(BeforeScenarioScope $scope) { public function setUpScenario(BeforeScenarioScope $scope): void {
// Get the environment // Get the environment
$environment = $scope->getEnvironment(); $environment = $scope->getEnvironment();
// Get all the contexts you need in this context // Get all the contexts you need in this context
@@ -2,11 +2,11 @@
use Behat\Behat\Context\Context; use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use TestHelpers\HttpRequestHelper; use TestHelpers\HttpRequestHelper;
use TestHelpers\SetupHelper; use TestHelpers\SetupHelper;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use \Behat\Gherkin\Node\TableNode;
require_once 'bootstrap.php'; require_once 'bootstrap.php';
@@ -69,7 +69,7 @@ class SpacesContext implements Context {
* *
* @var string * @var string
*/ */
private $responseSpaceId; private string $responseSpaceId;
/** /**
* @param string $responseSpaceId * @param string $responseSpaceId
@@ -92,6 +92,7 @@ class SpacesContext implements Context {
* *
* @param $name string * @param $name string
* @return string * @return string
* @throws Exception
*/ */
public function getSpaceIdByName(string $name): string public function getSpaceIdByName(string $name): string
{ {
@@ -104,7 +105,7 @@ class SpacesContext implements Context {
return $spaceCandidate["id"]; return $spaceCandidate["id"];
} }
} }
return false; throw new Exception(__METHOD__ . " space with name $name not found");
} }
/** /**
@@ -153,29 +154,29 @@ class SpacesContext implements Context {
/** /**
* Send Graph List Spaces Request * Send Graph List Spaces Request
* *
* @param $baseUrl * @param string $baseUrl
* @param $user * @param string $user
* @param $password * @param string $password
* @param $arguments * @param string $urlArguments
* @param string $xRequestId * @param string $xRequestId
* @param array $body * @param array $body
* @param array $headers * @param array $headers
* @return ResponseInterface * @return ResponseInterface
*/ */
public function listSpacesRequest( public function listSpacesRequest(
$baseUrl, string $baseUrl,
$user, string $user,
$password, string $password,
$arguments, string $urlArguments,
string $xRequestId = '', string $xRequestId = '',
array $body = [], array $body = [],
array $headers = [] array $headers = []
) { ): ResponseInterface {
$fullUrl = $baseUrl; $fullUrl = $baseUrl;
if (!str_ends_with($fullUrl, '/')) { if (!str_ends_with($fullUrl, '/')) {
$fullUrl .= '/'; $fullUrl .= '/';
} }
$fullUrl .= "graph/v1.0/me/drives/" . $arguments; $fullUrl .= "graph/v1.0/me/drives/" . $urlArguments;
return HttpRequestHelper::get($fullUrl, $xRequestId, $user, $password, $headers, $body); return HttpRequestHelper::get($fullUrl, $xRequestId, $user, $password, $headers, $body);
} }
@@ -183,18 +184,18 @@ class SpacesContext implements Context {
/** /**
* Send Graph Create Space Request * Send Graph Create Space Request
* *
* @param $baseUrl * @param string $baseUrl
* @param $user * @param string $user
* @param $password * @param string $password
* @param string $body * @param string $body
* @param string $xRequestId * @param string $xRequestId
* @param array $headers * @param array $headers
* @return ResponseInterface * @return ResponseInterface
*/ */
public function sendCreateSpaceRequest( public function sendCreateSpaceRequest(
$baseUrl, string $baseUrl,
$user, string $user,
$password, string $password,
string $body, string $body,
string $xRequestId = '', string $xRequestId = '',
array $headers = [] array $headers = []
@@ -212,17 +213,17 @@ class SpacesContext implements Context {
/** /**
* Send Propfind Request to Url * Send Propfind Request to Url
* *
* @param $fullUrl * @param string $fullUrl
* @param $user * @param string $user
* @param $password * @param string $password
* @param string $xRequestId * @param string $xRequestId
* @param array $headers * @param array $headers
* @return ResponseInterface * @return ResponseInterface
*/ */
public function sendPropfindRequestToUrl( public function sendPropfindRequestToUrl(
$fullUrl, string $fullUrl,
$user, string $user,
$password, string $password,
string $xRequestId = '', string $xRequestId = '',
array $headers = [] array $headers = []
): ResponseInterface ): ResponseInterface
@@ -236,7 +237,7 @@ class SpacesContext implements Context {
* @param $user * @param $user
* @return void * @return void
*/ */
public function theUserListsAllHisAvailableSpacesUsingTheGraphApi($user): void public function theUserListsAllHisAvailableSpacesUsingTheGraphApi(string $user): void
{ {
$this->featureContext->setResponse( $this->featureContext->setResponse(
$this->listSpacesRequest( $this->listSpacesRequest(
@@ -253,13 +254,16 @@ class SpacesContext implements Context {
/** /**
* @When /^user "([^"]*)" creates a space "([^"]*)" of type "([^"]*)" with the default quota using the GraphApi$/ * @When /^user "([^"]*)" creates a space "([^"]*)" of type "([^"]*)" with the default quota using the GraphApi$/
* *
* @param $user string * @param string $user
* @param $spaceName string * @param string $spaceName
* @param $spaceType string * @param string $spaceType
* *
* @return void * @return void
*/ */
public function theUserCreatesASpaceUsingTheGraphApi($user, $spaceName, $spaceType): void public function theUserCreatesASpaceUsingTheGraphApi(
string $user,
string $spaceName,
string $spaceType): void
{ {
$space = ["Name" => $spaceName, "driveType" => $spaceType]; $space = ["Name" => $spaceName, "driveType" => $spaceType];
$body = json_encode($space); $body = json_encode($space);
@@ -277,17 +281,20 @@ class SpacesContext implements Context {
/** /**
* @When /^user "([^"]*)" creates a space "([^"]*)" of type "([^"]*)" with quota "([^"]*)" using the GraphApi$/ * @When /^user "([^"]*)" creates a space "([^"]*)" of type "([^"]*)" with quota "([^"]*)" using the GraphApi$/
* *
* @param $user string * @param string $user
* @param $spaceName string * @param string $spaceName
* @param $spaceType string * @param string $spaceType
* @param $quota int * @param int $quota
* *
* @return void * @return void
* @throws JsonException
*/ */
public function theUserCreatesASpaceWithQuotaUsingTheGraphApi($user, $spaceName, $spaceType, $quota): void public function theUserCreatesASpaceWithQuotaUsingTheGraphApi(
string $user,
string $spaceName,
string $spaceType,
int $quota): void
{ {
$space = ["Name" => $spaceName, "driveType" => $spaceType, "quota" => ["total" => (int) $quota]]; $space = ["Name" => $spaceName, "driveType" => $spaceType, "quota" => ["total" => $quota]];
$body = json_encode($space); $body = json_encode($space);
$this->featureContext->setResponse( $this->featureContext->setResponse(
$this->sendCreateSpaceRequest( $this->sendCreateSpaceRequest(
@@ -303,8 +310,8 @@ class SpacesContext implements Context {
/** /**
* @When /^the administrator gives "([^"]*)" the role "([^"]*)" using the settings api$/ * @When /^the administrator gives "([^"]*)" the role "([^"]*)" using the settings api$/
* *
* @param $user string * @param string $user
* @param $role string * @param string $role
* *
* @return void * @return void
*/ */
@@ -313,6 +320,9 @@ class SpacesContext implements Context {
$admin = $this->featureContext->getAdminUsername(); $admin = $this->featureContext->getAdminUsername();
$password = $this->featureContext->getAdminPassword(); $password = $this->featureContext->getAdminPassword();
$headers = []; $headers = [];
$bundles = [];
$accounts = [];
$assignment = [];
$baseUrl = $this->featureContext->getBaseUrl(); $baseUrl = $this->featureContext->getBaseUrl();
if (!str_ends_with($baseUrl, '/')) { if (!str_ends_with($baseUrl, '/')) {
@@ -323,13 +333,12 @@ class SpacesContext implements Context {
$this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, "{}")); $this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, "{}"));
if ($this->featureContext->getResponse()) { if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents(); $rawBody = $this->featureContext->getResponse()->getBody()->getContents();
} if (isset(\json_decode($rawBody, true)["bundles"])) {
$bundles = []; $bundles = \json_decode($rawBody, true)["bundles"];
if (isset(\json_decode($rawBody, true)["bundles"])) { }
$bundles = \json_decode($rawBody, true)["bundles"];
} }
$roleToAssign = ""; $roleToAssign = "";
foreach($bundles as $bundle => $value) { foreach($bundles as $value) {
// find the selected role // find the selected role
if ($value["displayName"] === $role) { if ($value["displayName"] === $role) {
$roleToAssign = $value; $roleToAssign = $value;
@@ -342,10 +351,9 @@ class SpacesContext implements Context {
$this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, "{}")); $this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, "{}"));
if ($this->featureContext->getResponse()) { if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents(); $rawBody = $this->featureContext->getResponse()->getBody()->getContents();
} if (isset(\json_decode($rawBody, true)["accounts"])) {
$accounts = []; $accounts = \json_decode($rawBody, true)["accounts"];
if (isset(\json_decode($rawBody, true)["accounts"])) { }
$accounts = \json_decode($rawBody, true)["accounts"];
} }
$accountToChange = ""; $accountToChange = "";
foreach($accounts as $account) { foreach($accounts as $account) {
@@ -354,7 +362,7 @@ class SpacesContext implements Context {
$accountToChange = $account; $accountToChange = $account;
} }
} }
Assert::assertNotEmpty($accountToChange, "The seleted account $user does not exist"); Assert::assertNotEmpty($accountToChange, "The selected account $user does not exist");
// set the new role // set the new role
$fullUrl = $baseUrl . "api/v0/settings/assignments-add"; $fullUrl = $baseUrl . "api/v0/settings/assignments-add";
@@ -363,11 +371,9 @@ class SpacesContext implements Context {
$this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, $body)); $this->featureContext->setResponse(HttpRequestHelper::post($fullUrl, "", $admin, $password, $headers, $body));
if ($this->featureContext->getResponse()) { if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents(); $rawBody = $this->featureContext->getResponse()->getBody()->getContents();
} if (isset(\json_decode($rawBody, true)["assignment"])) {
$assignment = \json_decode($rawBody, true)["assignment"];
$assignment = []; }
if (isset(\json_decode($rawBody, true)["assignment"])) {
$assignment = \json_decode($rawBody, true)["assignment"];
} }
Assert::assertEquals($accountToChange["id"], $assignment["accountUuid"]); Assert::assertEquals($accountToChange["id"], $assignment["accountUuid"]);
@@ -400,11 +406,14 @@ class SpacesContext implements Context {
/** /**
* @When /^user "([^"]*)" lists the content of the space with the name "([^"]*)" using the WebDav Api$/ * @When /^user "([^"]*)" lists the content of the space with the name "([^"]*)" using the WebDav Api$/
* *
* @param $user * @param string $user
* @param $name * @param string $name
* @return void * @return void
*/ */
public function theUserListsTheContentOfAPersonalSpaceRootUsingTheWebDAvApi($user, $name): void public function theUserListsTheContentOfAPersonalSpaceRootUsingTheWebDAvApi(
string $user,
string $name
): void
{ {
$spaceId = $this->getAvailableSpaces()[$name]["id"]; $spaceId = $this->getAvailableSpaces()[$name]["id"];
$spaceWebDavUrl = $this->getAvailableSpaces()[$name]["root"]["webDavUrl"]; $spaceWebDavUrl = $this->getAvailableSpaces()[$name]["root"]["webDavUrl"];
@@ -415,8 +424,6 @@ class SpacesContext implements Context {
$this->featureContext->getPasswordForUser($user), $this->featureContext->getPasswordForUser($user),
"", "",
[], [],
[],
[]
) )
); );
$this->setResponseSpaceId($spaceId); $this->setResponseSpaceId($spaceId);
@@ -427,7 +434,6 @@ class SpacesContext implements Context {
/** /**
* @Then /^the (?:propfind|search) result of the space should (not|)\s?contain these (?:files|entries):$/ * @Then /^the (?:propfind|search) result of the space should (not|)\s?contain these (?:files|entries):$/
* *
* @param string $user
* @param string $shouldOrNot (not|) * @param string $shouldOrNot (not|)
* @param TableNode $expectedFiles * @param TableNode $expectedFiles
* *
@@ -435,9 +441,9 @@ class SpacesContext implements Context {
* @throws Exception * @throws Exception
*/ */
public function thePropfindResultShouldContainEntries( public function thePropfindResultShouldContainEntries(
$shouldOrNot, string $shouldOrNot,
TableNode $expectedFiles TableNode $expectedFiles
) { ):void {
$this->propfindResultShouldContainEntries( $this->propfindResultShouldContainEntries(
$shouldOrNot, $shouldOrNot,
$expectedFiles, $expectedFiles,
@@ -452,7 +458,10 @@ class SpacesContext implements Context {
* *
* @return void * @return void
*/ */
public function jsonRespondedShouldContain($spaceName, TableNode $table) { public function jsonRespondedShouldContain(
string $spaceName,
TableNode $table
): void {
$this->featureContext->verifyTableNodeColumns($table, ['key', 'value']); $this->featureContext->verifyTableNodeColumns($table, ['key', 'value']);
Assert::assertIsArray($spaceAsArray = $this->getSpaceByName($spaceName), "No space with name $spaceName found"); Assert::assertIsArray($spaceAsArray = $this->getSpaceByName($spaceName), "No space with name $spaceName found");
foreach ($table->getHash() as $row) { foreach ($table->getHash() as $row) {
@@ -490,15 +499,14 @@ class SpacesContext implements Context {
/** /**
* @param string $shouldOrNot (not|) * @param string $shouldOrNot (not|)
* @param TableNode $expectedFiles * @param TableNode $expectedFiles
* @param string|null $user
* *
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function propfindResultShouldContainEntries( public function propfindResultShouldContainEntries(
$shouldOrNot, string $shouldOrNot,
TableNode $expectedFiles TableNode $expectedFiles
) { ): void {
$this->verifyTableNodeColumnsCount($expectedFiles, 1); $this->verifyTableNodeColumnsCount($expectedFiles, 1);
$elementRows = $expectedFiles->getRows(); $elementRows = $expectedFiles->getRows();
$should = ($shouldOrNot !== "not"); $should = ($shouldOrNot !== "not");
@@ -530,10 +538,10 @@ class SpacesContext implements Context {
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function verifyTableNodeColumnsCount($table, $count) { public function verifyTableNodeColumnsCount(
if (!($table instanceof TableNode)) { TableNode $table,
throw new Exception("TableNode expected but got " . \gettype($table)); int $count
} ): void {
if (\count($table->getRows()) < 1) { if (\count($table->getRows()) < 1) {
throw new Exception("Table should have at least one row."); throw new Exception("Table should have at least one row.");
} }
@@ -611,14 +619,16 @@ class SpacesContext implements Context {
* @param string $spaceName * @param string $spaceName
* *
* @return void * @return void
* @throws JsonException
*/ */
public function theUserCreatesAFolderUsingTheGraphApi($user, $folder, $spaceName): void public function theUserCreatesAFolderUsingTheGraphApi(
string $user,
string $folder,
string $spaceName
): void
{ {
$this->featureContext->setResponse( $this->featureContext->setResponse(
$this->sendCreateFolderRequest( $this->sendCreateFolderRequest(
$this->featureContext->getBaseUrl(), $this->featureContext->getBaseUrl(),
"",
"MKCOL", "MKCOL",
$user, $user,
$this->featureContext->getPasswordForUser($user), $this->featureContext->getPasswordForUser($user),
@@ -631,26 +641,25 @@ class SpacesContext implements Context {
/** /**
* Send Graph Create Space Request * Send Graph Create Space Request
* *
* @param $baseUrl * @param string $baseUrl
* @param $user
* @param $password
* @param string $method * @param string $method
* @param string $xRequestId * @param string $user
* @param array $headers * @param string $password
* @param string $folder * @param string $folder
* @param string $spaceName * @param string $spaceName
* @param string $xRequestId
* @param array $headers
* @return ResponseInterface * @return ResponseInterface
*/ */
public function sendCreateFolderRequest( public function sendCreateFolderRequest(
$baseUrl, string $baseUrl,
string $xRequestId = '',
string $method, string $method,
$user, string $user,
$password, string $password,
$folder, string $folder,
$spaceName, string $spaceName,
string $xRequestId = '',
array $headers = [] array $headers = []
): ResponseInterface ): ResponseInterface
{ {
$spaceId = $this->getAvailableSpaces()[$spaceName]["id"]; $spaceId = $this->getAvailableSpaces()[$spaceName]["id"];
+5 -7
View File
@@ -5,20 +5,18 @@
} }
}, },
"require": { "require": {
"behat/behat": "^3.8", "behat/behat": "^3.9",
"behat/gherkin": "4.7.1",
"behat/mink": "1.7.1", "behat/mink": "1.7.1",
"behat/mink-extension": "^2.3", "friends-of-behat/mink-extension": "^2.5",
"behat/mink-selenium2-driver": "^1.4",
"ciaranmcnulty/behat-stepthroughextension" : "dev-master", "ciaranmcnulty/behat-stepthroughextension" : "dev-master",
"jarnaiz/behat-junit-formatter": "^1.3", "jarnaiz/behat-junit-formatter": "^1.3",
"rdx/behat-variables": "^1.2", "rdx/behat-variables": "^1.2",
"sensiolabs/behat-page-object-extension": "^2.3", "sensiolabs/behat-page-object-extension": "^2.3",
"symfony/translation": "^4.4", "symfony/translation": "^4.4",
"sabre/xml": "^2.2", "sabre/xml": "^2.2",
"guzzlehttp/guzzle": "^7.2", "guzzlehttp/guzzle": "^7.4",
"phpunit/phpunit": "^9.4", "phpunit/phpunit": "^9.5",
"laminas/laminas-ldap": "^2.10", "laminas/laminas-ldap": "^2.10",
"ankitpokhrel/tus-php": "^2.1" "ankitpokhrel/tus-php": "^2.1"
} }
} }