Skip to content

Commit

Permalink
[tests-only][full-ci] Backport implementing code refactoring (#6650)
Browse files Browse the repository at this point in the history
* implement DRY principle

* added response check after file download
  • Loading branch information
PrajwolAmatya authored Jun 28, 2023
1 parent fd4ca45 commit da2f8b2
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 603 deletions.
49 changes: 30 additions & 19 deletions tests/acceptance/features/bootstrap/ArchiverContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use TestHelpers\SetupHelper;
use wapmorgan\UnifiedArchive\UnifiedArchive;
use PHPUnit\Framework\Assert;
use \Psr\Http\Message\ResponseInterface;

require_once 'bootstrap.php';

Expand Down Expand Up @@ -118,18 +119,7 @@ public function userDownloadsTheArchive(
foreach ($headersTable as $row) {
$headers[$row['header']] = $row ['value'];
}

$user = $this->featureContext->getActualUsername($user);
$queryString = $this->getArchiverQueryString($user, $resource, $addressType);
$this->featureContext->setResponse(
HttpRequestHelper::get(
$this->featureContext->getBaseUrl() . '/archiver?' . $queryString,
'',
$user,
$this->featureContext->getPasswordForUser($user),
$headers
)
);
$this->featureContext->setResponse($this->downloadArchive($user, $resource, $addressType, null, $headers));
}

/**
Expand All @@ -151,15 +141,36 @@ public function userDownloadsTheArchiveOfItemOfUser(
string $owner,
string $addressType
): void {
$this->featureContext->setResponse($this->downloadArchive($downloader, $resource, $addressType, $owner));
}

/**
* @param string $downloader
* @param string $resource
* @param string $addressType
* @param string|null $owner
* @param array|null $headers
*
* @return ResponseInterface
* @throws GuzzleException
* @throws JsonException
*/
public function downloadArchive(
string $downloader,
string $resource,
string $addressType,
?string $owner = null,
?array $headers = null
): ResponseInterface {
$owner = $owner ?? $downloader;
$downloader = $this->featureContext->getActualUsername($downloader);
$queryString = $this->getArchiverQueryString($owner, $resource, $addressType);
$this->featureContext->setResponse(
HttpRequestHelper::get(
$this->featureContext->getBaseUrl() . '/archiver?' . $queryString,
'',
$downloader,
$this->featureContext->getPasswordForUser($downloader),
)
return HttpRequestHelper::get(
$this->featureContext->getBaseUrl() . '/archiver?' . $queryString,
'',
$downloader,
$this->featureContext->getPasswordForUser($downloader),
$headers
);
}

Expand Down
41 changes: 23 additions & 18 deletions tests/acceptance/features/bootstrap/FilesVersionsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PHPUnit\Framework\Assert;
use TestHelpers\HttpRequestHelper;
use TestHelpers\WebDavHelper;
use Psr\Http\Message\ResponseInterface;

require_once 'bootstrap.php';

Expand Down Expand Up @@ -55,20 +56,7 @@ private function getVersionsPathForFileId(string $fileId):string {
* @throws Exception
*/
public function userTriesToGetFileVersions(string $user, string $file, string $fileOwner):void {
$user = $this->featureContext->getActualUsername($user);
$fileOwner = $this->featureContext->getActualUsername($fileOwner);
$fileId = $this->featureContext->getFileIdForPath($fileOwner, $file);
Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $file user $fileOwner not found (the file may not exist)");
$response = $this->featureContext->makeDavRequest(
$user,
"PROPFIND",
$this->getVersionsPathForFileId($fileId),
null,
null,
null,
'2'
);
$this->featureContext->setResponse($response, $user);
$this->featureContext->setResponse($this->getFileVersions($user, $file, $fileOwner));
}

/**
Expand All @@ -81,10 +69,28 @@ public function userTriesToGetFileVersions(string $user, string $file, string $f
* @throws Exception
*/
public function userGetsFileVersions(string $user, string $file):void {
$this->featureContext->setResponse($this->getFileVersions($user, $file));
}

/**
* @param string $user
* @param string $file
* @param string|null $fileOwner
*
* @return ResponseInterface
* @throws JsonException
* @throws GuzzleException
*/
public function getFileVersions(
string $user,
string $file,
?string $fileOwner = null
): ResponseInterface {
$user = $this->featureContext->getActualUsername($user);
$fileId = $this->featureContext->getFileIdForPath($user, $file);
Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $file user $user not found (the file may not exist)");
$response = $this->featureContext->makeDavRequest(
$fileOwner = $fileOwner ? $this->featureContext->getActualUsername($fileOwner) : $user;
$fileId = $this->featureContext->getFileIdForPath($fileOwner, $file);
Assert::assertNotNull($fileId, __METHOD__ . " fileid of file $file user $fileOwner not found (the file may not exist)");
return $this->featureContext->makeDavRequest(
$user,
"PROPFIND",
$this->getVersionsPathForFileId($fileId),
Expand All @@ -93,7 +99,6 @@ public function userGetsFileVersions(string $user, string $file):void {
null,
'2'
);
$this->featureContext->setResponse($response, $user);
}

/**
Expand Down
81 changes: 23 additions & 58 deletions tests/acceptance/features/bootstrap/GraphContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,33 +263,6 @@ public function adminHasRetrievedUserUsingTheGraphApi(string $user): void {
$this->featureContext->thenTheHTTPStatusCodeShouldBe(200);
}

/**
* @param $requestingUser
* @param $targetUser
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
public function userHasRetrievedUserUsingTheGraphApi(
$requestingUser,
$targetUser
): void {
$requester = $this->featureContext->getActualUsername($requestingUser);
$requesterPassword = $this->featureContext->getPasswordForUser($requestingUser);
$user = $this->featureContext->getActualUsername($targetUser);
$userId = $this->featureContext->getAttributeOfCreatedUser($user, "id");
$response = GraphHelper::getUser(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$requester,
$requesterPassword,
$userId
);
$this->featureContext->setResponse($response);
$this->featureContext->thenTheHTTPStatusCodeShouldBe(200);
}

/**
* @param string $groupId
* @param string|null $user
Expand Down Expand Up @@ -2423,8 +2396,26 @@ public function userTriesToExportGdprReportOfAnotherUserUsingGraphApi(string $us
}

/**
* @When user :user unassigns the role of user :ofUser using the Graph API
* @When user :user tries to unassign the role of user :ofUser using the Graph API
* @param string $user
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function getAssignedRole(string $user) {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
return (
GraphHelper::getAssignedRole(
$this->featureContext->getBAseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$userId
)
);
}

/**
* @When /^user "([^"]*)" (?:unassigns|tries to unassign) the role of user "([^"]*)" using the Graph API$/
*
* @param string $user
* @param string $ofUser
Expand All @@ -2437,15 +2428,7 @@ public function userTriesToExportGdprReportOfAnotherUserUsingGraphApi(string $us
public function theUserUnassignsTheRoleOfUserUsingTheGraphApi(string $user, string $ofUser): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($ofUser, 'id') ?? $ofUser;
$credentials = $this->getAdminOrUserCredentials($user);

$response = GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$userId
);
$appRoleAssignmentId = $this->featureContext->getJsonDecodedResponse($response)["value"][0]["id"];
$appRoleAssignmentId = $this->featureContext->getJsonDecodedResponse($this->getAssignedRole($ofUser))["value"][0]["id"];

$this->featureContext->setResponse(
GraphHelper::unassignRole(
Expand All @@ -2470,16 +2453,7 @@ public function theUserUnassignsTheRoleOfUserUsingTheGraphApi(string $user, stri
* @throws Exception
*/
public function userShouldHaveTheRoleAssigned(string $user, string $role): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
$response = GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUserName(),
$this->featureContext->getAdminPassword(),
$userId
);

$jsonDecodedResponse = $this->featureContext->getJsonDecodedResponse($response)['value'][0];
$jsonDecodedResponse = $this->featureContext->getJsonDecodedResponse($this->getAssignedRole($user))['value'][0];
if (empty($this->appEntity)) {
$this->setApplicationEntity();
}
Expand All @@ -2502,16 +2476,7 @@ public function userShouldHaveTheRoleAssigned(string $user, string $role): void
* @throws Exception
*/
public function userShouldNotHaveAnyRoleAssigned(string $user): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
$response = GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUserName(),
$this->featureContext->getAdminPassword(),
$userId
);

$jsonDecodedResponse = $this->featureContext->getJsonDecodedResponse($response)['value'];
$jsonDecodedResponse = $this->featureContext->getJsonDecodedResponse($this->getAssignedRole($user))['value'];
Assert::assertEmpty(
$jsonDecodedResponse,
__METHOD__
Expand Down
Loading

0 comments on commit da2f8b2

Please sign in to comment.