Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Last active August 29, 2015 14:17
Show Gist options
  • Save dadamssg/a02dfac7797377632824 to your computer and use it in GitHub Desktop.
Save dadamssg/a02dfac7797377632824 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\AwesomeApp\Infrastructure\UserBundle\Features\Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Acme\AwesomeApp\Domain\User\Entity\User;
use Acme\AwesomeApp\Domain\User\Security\PasswordEncoder;
use Acme\AwesomeApp\Infrastructure\AppBundle\Features\Context\ApiContext;
use Symfony\Component\HttpFoundation\Response;
use Acme\AwesomeApp\Infrastructure\OAuthBundle\Features\Context\DataContext as OAuthContext;
class DomainContext implements Context, SnippetAcceptingContext
{
/**
* @var DataContext
*/
private $data;
/**
* @var ApiContext
*/
private $api;
/**
* @var OAuthContext
*/
private $oauth;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var User
*/
private $user;
/**
* @var PasswordEncoder
*/
private $encoder;
/**
* @param PasswordEncoder $encoder
*/
public function __construct(PasswordEncoder $encoder)
{
$this->encoder = $encoder;
}
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
/** @var InitializedContextEnvironment $environment */
$environment = $scope->getEnvironment();
$this->data = $environment->getContext(DataContext::CLASS);
$this->api = $environment->getContext(ApiContext::CLASS);
$this->oauth = $environment->getContext(OAuthContext::CLASS);
}
/**
* @Given user has email :email and password :password
*/
public function userHasEmailAndPassword($email, $password)
{
$this->email = $email;
$this->password = $password;
}
/**
* @When they register
*/
public function theyRegister()
{
$this->api->setPayload(
[
"register_user" => [
"email" => $this->email,
"password" => $this->password
]
]
);
$url = $this->api->generateUrl('awesomeapp_register_user');
$this->api->iRequest('POST', $url);
}
/**
* @Then they are put into the system
*/
public function theyArePutIntoTheSystem()
{
$this->api->iGetAResponse(Response::HTTP_CREATED);
$this->api->thePropertiesExist("message");
}
/**
* @Given an existing user with email :email and password :password
*/
public function anExistingUserWithEmailAndPassword($email, $password)
{
$this->email = $email;
$this->user = $this->data->createDisabledUser($email, $password);
}
/**
* @Given an existing enabled user with email :email and password :password
*/
public function anExistingEnabledUserWithEmailAndPassword($email, $password)
{
$this->email = $email;
$this->password = $password;
$this->user = $this->data->createEnabledUser($email, $password);
}
/**
* @When they confirm their email
*/
public function theyConfirmTheirEmail()
{
$url = $this->api->generateUrl('awesomeapp_confirm_user', ['token' => $this->user->getConfirmationToken()]);
$this->api->iRequest('GET', $url);
}
/**
* @Then they are enabled
*/
public function theyAreEnabled()
{
$user = $this->data->findRefreshedUserByEmail($this->email);
if (!$user->isEnabled()) {
throw new \Exception("The user should be enabled.");
}
}
/**
* @When they request to change their password
*/
public function theyRequestToChangeTheirPassword()
{
$this->api->setPayload(
[
"request_change_password" => [
"email" => $this->email
]
]
);
$url = $this->api->generateUrl('awesomeapp_request_change_password');
$this->api->iRequest('POST', $url);
}
/**
* @Then they are given instructions to complete the process
*/
public function theyAreGivenInstructionsToCompleteTheProcess()
{
$this->api->iGetAResponse(Response::HTTP_OK);
$this->api->thePropertiesExist("message");
}
/**
* @When they change their password to :password
*/
public function theyChangeTheirPasswordTo($password)
{
$this->password = $password;
$this->api->setPayload(
[
"change_password" => [
"password" => $this->password
]
]
);
$url = $this->api->generateUrl('awesomeapp_change_password', ["token" => $this->user->getConfirmationToken()]);
$this->api->iRequest('POST', $url);
}
/**
* @Then their password is changed and encrypted
*/
public function theirPasswordIsChangedAndEncrypted()
{
$user = $this->data->findRefreshedUserByEmail($this->email);
$expectedPassword = $this->encoder->encode($this->password, $user->getSalt());
if ($user->getPassword() !== $expectedPassword) {
throw new \Exception("Password was not changed correctly.");
}
}
/**
* @Then they are given a success message
*/
public function theyAreGivenASuccessMessage()
{
$this->api->iGetAResponse(Response::HTTP_OK);
$this->api->thePropertiesExist("message");
}
/**
* @Given they have an oauth access token for :email
*/
public function theyHaveAnOauthAccessTokenFor($email)
{
$user = $this->data->findUserByEmail($email);
$accessToken = $this->oauth->getAccessToken($user);
$accessToken = $accessToken->getToken();
$this->api->addHeader('Authorization', "Bearer $accessToken");
}
/**
* @When they request to view themselves
*/
public function theyRequestToViewThemselves()
{
$url = $this->api->generateUrl('awesomeapp_current_user');
$this->api->iRequest('GET', $url);
}
/**
* @Then they can see their user data
*/
public function theyCanSeeTheirUserData()
{
$this->api->iGetAResponse(Response::HTTP_OK);
$this->api->thePropertiesExist("user");
}
/**
* @Then the password isn't displayed
*/
public function thePasswordIsnTDisplayed()
{
$this->api->iScopeIntoTheProperty("user");
$this->api->thePropertiesDoNotExist(["password"]);
}
/**
* @When they request a password oauth access token
*/
public function theyRequestAPasswordOauthAccessToken()
{
$this->oauth->createPasswordClient();
$url = $this->api->generateUrl('awesomeapp_oauth_token');
$url .= sprintf("?grant_type=password&username=%s&password=%s", $this->email, $this->password);
$this->api->iRequest('GET', $url);
}
/**
* @Then they will get an access token
*/
public function theyWillGetAnAccessToken()
{
$this->api->iGetAResponse(Response::HTTP_OK);
$this->api->thePropertiesExist("access_token");
}
}
@user
Feature: User
In order to use AwesomeApp
As a user
They need to be able to manage their account
Scenario: User can register
Given user has email "user@awesomeapp.com" and password "awesomeapp"
When they register
Then they are put into the system
Scenario: User can confirm themselves
Given an existing user with email "user@awesomeapp.com" and password "awesomeapp"
When they confirm their email
Then they are enabled
Scenario: User can request to change their password
Given an existing user with email "user@awesomeapp.com" and password "awesomeapp"
When they request to change their password
Then they are given instructions to complete the process
Scenario: User can change their password
Given an existing user with email "user@awesomeapp.com" and password "awesomeapp"
When they change their password to "secret123"
Then their password is changed and encrypted
And they are given a success message
Scenario: User can view themselves
Given an existing enabled user with email "user@awesomeapp.com" and password "awesomeapp"
And they have an oauth access token for "user@awesomeapp.com"
When they request to view themselves
Then they can see their user data
And the password isn't displayed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment