Skip to content

Commit

Permalink
allow to set source Ip in BasicStructure steps
Browse files Browse the repository at this point in the history
  • Loading branch information
individual-it committed Aug 27, 2018
1 parent 20d073c commit a01a3ce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 28 deletions.
101 changes: 74 additions & 27 deletions tests/acceptance/features/bootstrap/BasicStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ trait BasicStructure {
*/
private $requestToken;

/**
* The local source IP address from which to initiate API actions.
* Defaults to system-selected address matching IP address family and scope.
*
* @var string|null
*/
private $sourceIpAddress = null;

/**
* BasicStructure constructor.
*
Expand Down Expand Up @@ -309,6 +317,20 @@ public function getOcsApiVersion() {
return $this->ocsApiVersion;
}

/**
* @return string|null
*/
public function getSourceIpAddress() {
return $this->sourceIpAddress;
}

/**
* @param string
*/
public function setSourceIpAddress($sourceIpAddress) {
$this->sourceIpAddress = $sourceIpAddress;
}

/**
* @Given /^using OCS API version "([^"]*)"$/
*
Expand Down Expand Up @@ -668,6 +690,14 @@ public function sendingToWithDirectUrl($user, $verb, $url, $body, $password = nu
$options['auth'] = [$user, $password];
}

if ($this->sourceIpAddress !== null) {
$options ['config'] = [
'curl' => [
CURLOPT_INTERFACE => $this->sourceIpAddress
]
];
}

if (!empty($this->cookieJar->toArray())) {
$options['cookies'] = $this->cookieJar;
}
Expand Down Expand Up @@ -825,28 +855,27 @@ public function userHasLoggedInToAWebStyleSessionUsingTheAPI($user) {
$loginUrl = $this->getBaseUrl() . '/login';
// Request a new session and extract CSRF token
$client = new Client();
$response = $client->get(
$loginUrl,
[
'cookies' => $this->cookieJar,
]
);
$options = [];
if ($this->sourceIpAddress !== null) {
$options ['config'] = [
'curl' => [
CURLOPT_INTERFACE => $this->sourceIpAddress
]
];
}
$options ['cookies'] = $this->cookieJar;
$response = $client->get($loginUrl, $options);
$this->extractRequestTokenFromResponse($response);

// Login and extract new token
$password = $this->getPasswordForUser($user);
$client = new Client();
$response = $client->post(
$loginUrl,
[
'body' => [
'user' => $user,
'password' => $password,
'requesttoken' => $this->requestToken,
],
'cookies' => $this->cookieJar,
]
);
$options ['body'] = [
'user' => $user,
'password' => $password,
'requesttoken' => $this->requestToken
];
$response = $client->post($loginUrl, $options);
$this->extractRequestTokenFromResponse($response);
}

Expand All @@ -861,12 +890,17 @@ public function userHasLoggedInToAWebStyleSessionUsingTheAPI($user) {
*/
public function sendingAToWithRequesttoken($method, $url) {
$client = new Client();
$options = [];
if ($this->sourceIpAddress !== null) {
$options ['config'] = [
'curl' => [
CURLOPT_INTERFACE => $this->sourceIpAddress
]
];
}
$options ['cookies'] = $this->cookieJar;
$request = $client->createRequest(
$method,
$this->getBaseUrl() . $url,
[
'cookies' => $this->cookieJar,
]
$method, $this->getBaseUrl() . $url, $options
);
$request->addHeader('requesttoken', $this->requestToken);
try {
Expand All @@ -887,12 +921,17 @@ public function sendingAToWithRequesttoken($method, $url) {
*/
public function sendingAToWithoutRequesttoken($method, $url) {
$client = new Client();
$options = [];
if ($this->sourceIpAddress !== null) {
$options ['config'] = [
'curl' => [
CURLOPT_INTERFACE => $this->sourceIpAddress
]
];
}
$options ['cookies'] = $this->cookieJar;
$request = $client->createRequest(
$method,
$this->getBaseUrl() . $url,
[
'cookies' => $this->cookieJar,
]
$method, $this->getBaseUrl() . $url, $options
);
try {
$this->response = $client->send($request);
Expand Down Expand Up @@ -1031,6 +1070,14 @@ public function getStatusPhp() {
$fullUrl = $this->getBaseUrl() . "/status.php";
$client = new Client();
$options = [];
if ($this->sourceIpAddress !== null) {
$options ['config'] = [
'curl' => [
CURLOPT_INTERFACE => $this->sourceIpAddress
]
];
}
$options ['cookies'] = $this->cookieJar;
$options['auth'] = $this->getAuthOptionForUser('admin');
try {
$this->response = $client->send(
Expand Down
4 changes: 3 additions & 1 deletion tests/acceptance/features/bootstrap/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait Ip {
* The local source IP address from which to initiate API actions.
* Defaults to system-selected address matching IP address family and scope.
*
* @var string
* @var string|null
*/
private $sourceIpAddress = null;

Expand Down Expand Up @@ -94,6 +94,8 @@ public function theClientAccessesTheServerFromIpAddress($sourceIpAddress) {
} else {
$this->baseUrlForSourceIp = $this->featureContext->getBaseUrl();
}

$this->featureContext->setSourceIpAddress($sourceIpAddress);
}

/**
Expand Down

0 comments on commit a01a3ce

Please sign in to comment.