Test user creation

This commit is contained in:
Thomas Müller 2015-08-06 17:16:24 +02:00
parent cb6e1b115d
commit 925aa6c0f5
2 changed files with 108 additions and 43 deletions

View File

@ -11,11 +11,11 @@ require __DIR__ . '/../../vendor/autoload.php';
*/
class FeatureContext extends BehatContext {
/** @var string */
private $baseUrl = '';
/** @var string */
private $baseUrl = '';
/** @var ResponseInterface */
private $response = null;
/** @var ResponseInterface */
private $response = null;
/** @var string */
private $currentUser = '';
@ -23,51 +23,39 @@ class FeatureContext extends BehatContext {
/** @var int */
private $apiVersion = 1;
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters) {
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters) {
// Initialize your context here
// Initialize your context here
$this->baseUrl = $parameters['baseUrl'];
$this->adminUser = $parameters['admin'];
}
}
/**
* @When /^sending "([^"]*)" to "([^"]*)"$/
*/
public function sendingTo($verb, $url) {
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
$client = new Client();
// TODO: get admin user from config
$options = [];
if ($this->currentUser === 'admin') {
$options['auth'] = $this->adminUser;
}
/**
* @When /^sending "([^"]*)" to "([^"]*)"$/
*/
public function sendingTo($verb, $url) {
$this->sendingToWith($verb, $url, null);
}
try {
$this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
} catch (\GuzzleHttp\Exception\ClientException $ex) {
$this->response = $ex->getResponse();
}
}
/**
* @Then /^the status code should be "([^"]*)"$/
*/
public function theStatusCodeShouldBe($statusCode) {
PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
}
/**
* @Then /^the status code should be "([^"]*)"$/
*/
public function theStatusCodeShouldBe($statusCode) {
PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
}
/**
* @Given /^As an "([^"]*)"$/
*/
public function asAn($user) {
/**
* @Given /^As an "([^"]*)"$/
*/
public function asAn($user) {
$this->currentUser = $user;
}
}
/**
* @Given /^using api version "([^"]*)"$/
@ -80,7 +68,69 @@ class FeatureContext extends BehatContext {
* @Given /^user "([^"]*)" exists$/
*/
public function userExists($user) {
throw new \Behat\Behat\Exception\PendingException();
$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
$client = new Client();
$options = [];
if ($this->currentUser === 'admin') {
$options['auth'] = $this->adminUser;
}
$this->response = $client->get($fullUrl, $options);
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
}
/**
* @Given /^user "([^"]*)" does not exist$/
*/
public function userDoesNotExist($user) {
try {
$this->userExists($user);
} catch (\GuzzleHttp\Exception\ClientException $ex) {
PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
}
}
/**
* @When /^creating the user "([^"]*)r"$/
*/
public function creatingTheUser($user) {
$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
$client = new Client();
$options = [];
if ($this->currentUser === 'admin') {
$options['auth'] = $this->adminUser;
}
$this->response = $client->post($fullUrl, [
'form_params' => [
'userid' => $user,
'password' => '123456'
]
]);
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
}
/**
* @When /^sending "([^"]*)" to "([^"]*)" with$/
* @param \Behat\Gherkin\Node\TableNode|null $formData
*/
public function sendingToWith($verb, $url, $body) {
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
$client = new Client();
$options = [];
if ($this->currentUser === 'admin') {
$options['auth'] = $this->adminUser;
}
if ($body instanceof \Behat\Gherkin\Node\TableNode) {
$fd = $body->getRowsHash();
$options['body'] = $fd;
}
try {
$this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
} catch (\GuzzleHttp\Exception\ClientException $ex) {
$this->response = $ex->getResponse();
}
}
}

View File

@ -7,3 +7,18 @@ Feature: provisioning
When sending "GET" to "/cloud/users/test"
Then the status code should be "200"
Scenario: Listing all users
Given As an "admin"
When sending "GET" to "/cloud/users"
Then the status code should be "200"
Scenario: Create a user
Given As an "admin"
And user "brand-new-user" does not exist
When sending "POST" to "/cloud/users" with
| userid | brand-new-user |
| password | 123456 |
Then the status code should be "200"
And user "brand-new-user" exists