Skip to content
Closed
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
Prev Previous commit
Next Next commit
Add integration test step to send requests as the logged in user
Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Jan 4, 2021
commit fcbf045ac38aa685fde07991c5588f9a57d0b242
53 changes: 48 additions & 5 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ class FeatureContext implements Context, SnippetAcceptingContext {
/** @var string */
protected $currentUser;

/** @var string */
protected $loggedInUser;

/** @var ResponseInterface */
private $response;

/** @var CookieJar[] */
private $cookieJars;

/** @var string */
private $requestToken;

/** @var string */
protected $baseUrl;

Expand Down Expand Up @@ -1752,7 +1758,7 @@ public function userLogsIn(string $user) {
]
);

$requestToken = $this->extractRequestTokenFromResponse($this->response);
$this->extractRequestTokenFromResponse($this->response);

// Login and extract new token
$password = ($user === 'admin') ? 'admin' : self::TEST_PASSWORD;
Expand All @@ -1763,21 +1769,58 @@ public function userLogsIn(string $user) {
'form_params' => [
'user' => $user,
'password' => $password,
'requesttoken' => $requestToken,
'requesttoken' => $this->requestToken,
],
'cookies' => $cookieJar,
]
);
$this->extractRequestTokenFromResponse($this->response);

$this->assertStatusCode($this->response, 200);

$this->loggedInUser = $user;
}

/**
* @param ResponseInterface $response
* @return string
*/
private function extractRequestTokenFromResponse(ResponseInterface $response): string {
return substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $response->getBody()->getContents()), 0, 89);
private function extractRequestTokenFromResponse(ResponseInterface $response): void {
$this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $response->getBody()->getContents()), 0, 89);
}

/**
* @When /^sending "([^"]*)" to "([^"]*)" with request token$/
* @param string $verb
* @param string $url
* @param TableNode|array|null $body
*/
public function sendingToWithRequestToken(string $verb, string $url, $body = null) {
$fullUrl = $this->baseUrl . $url;

$options = [
'cookies' => $this->getUserCookieJar($this->loggedInUser),
'headers' => [
'requesttoken' => $this->requestToken
],
];

if ($body instanceof TableNode) {
$fd = $body->getRowsHash();
$options['form_params'] = $fd;
} elseif ($body) {
$options = array_merge($options, $body);
}

$client = new Client();
try {
$this->response = $client->request(
$verb,
$fullUrl,
$options
);
} catch (ClientException $e) {
$this->response = $e->getResponse();
}
}

/**
Expand Down