Skip to content

Instantly share code, notes, and snippets.

@slav
Created September 14, 2012 20:17
Show Gist options
  • Save slav/3724463 to your computer and use it in GitHub Desktop.
Save slav/3724463 to your computer and use it in GitHub Desktop.
Specflow with Lokad CQRS sample
Feature: Registration
Registering new accounts with the system
allows users to create account, register a company (tenant)
and the first user (admin) for the company are created.
In order to use the system
As a visitor
I want to register and create a new account
Scenario: Fresh registration
When I register for new account
| Field | Value |
| Company Name | Agile Harbor |
| Company Phone | (502) 123-12345 |
| Company Website | http://www.agileharbor.com |
| User Name | Pal Palych |
| User Email | palych@agileharbor.com |
| User Password | test123 |
Then registration succeeds
And I can login with 'palych@agileharbor.com' and password 'test123'
And user 'palych@agileharbor.com' exists as 'admin'
And company 'Agile Harbor' exists
Scenario: User already exists
Given account registered
| Field | Value |
| Company Name | Agile Harbor |
| Company Phone | (502) 123-12345 |
| Company Url | http://www.agileharbor.com |
| User Name | Pal Palych |
| User Email | palych@agileharbor.com |
| User Password | test123 |
When I register for new account
| Field | Value |
| Company Name | Street Moda |
| Company Phone | (502) 123-12345 |
| Company Url | http://www.agileharbor.com |
| User Name | Vasya Palych |
| User Email | palych@agileharbor.com |
| User Password | test123 |
Then registration fails with message "'palych@agileharbor.com' email is already taken"
Scenario: Company already exists
Given account registered
| Field | Value |
| Company Name | Agile Harbor |
| Company Phone | (502) 123-12345 |
| Company Url | http://www.agileharbor.com |
| User Name | Pal Palych |
| User Email | palych@agileharbor.com |
| User Password | test123 |
When I register for new account
| Field | Value |
| Company Name | Agile Harbor |
| Company Phone | (502) 123-12345 |
| Company Url | http://www.agileharbor.com |
| User Name | Vasya Pupkin |
| User Email | pupkin@agileharbor.com |
| User Password | test123 |
Then registration fails with message "'Agile Harbor' company name is already taken"
[ Binding ]
public class RegistrationSteps
{
private RegistrationId _regId;
[ Given( @"account registered" ) ]
public void GivenAccountRegistered( RegistrationInfo regInfo )
{
var regId = new RegistrationId( Guid.NewGuid() );
Sp.SendToCore( new CreateRegistration( regId, regInfo ) );
Sp.Ap.Do( () =>
{
// Wait for registration to complete
var registration = Sp.Support.GetView< RegistrationView >( regId );
registration.Value.Completed.Should().BeTrue();
} );
}
[ When( @"I register for new account" ) ]
public void WhenIRegisterForNewAccount( RegistrationInfo regInfo )
{
this._regId = new RegistrationId( Guid.NewGuid() );
Sp.SendToCore( new CreateRegistration( this._regId, regInfo ) );
}
[ Then( @"registration succeeds" ) ]
public void ThenRegistrationSucceeds()
{
Sp.Ap.Do( () =>
{
var view = Sp.Support.GetView< RegistrationView >( this._regId );
view.HasValue.Should().BeTrue();
var registration = view.Value;
registration.HasProblems.Should().BeFalse();
registration.Status.Should().Be( "Registration completed" );
} );
}
[ Then( @"I can login with '(.*)' and password '(.*)'" ) ]
public void ThenICanLoginWithAndPassword( string email, string password )
{
Sp.Ap.Do( () =>
{
var loginsIndex = Sp.Support.GetSingleton< LoginsIndex >();
var userId = loginsIndex.Logins[ Email.From( email ) ];
var user = Sp.Support.GetView< LoginView >( new UserId( userId ) );
var passwordHash = user.Value.PasswordHash;
passwordHash.Should().Be( PasswordHash.Generate( password, passwordHash.Salt, new PasswordGenerator() ) );
} );
}
[ Then( @"user '(.*)' exists as '(.*)'" ) ]
public void ThenUserExistsAs( string email, string role )
{
Sp.Ap.Do( () =>
{
var loginsIndex = Sp.Support.GetSingleton< LoginsIndex >();
var userId = loginsIndex.Logins[ Email.From( email ) ];
var user = Sp.Support.GetView< LoginView >( new UserId( userId ) ).Value;
user.UserRole.Should().Be( UserRole.Admin );
} );
}
[ Then( @"company '(.*)' exists" ) ]
public void ThenCompanyExists( string tenantName )
{
Sp.Ap.Do( () =>
{
var tenantsIndex = Sp.Support.GetSingleton< TenantsIndex >();
var tenantId = tenantsIndex.Names[ new TenantName( tenantName ) ];
var tenant = Sp.Support.GetView< TenantView >( new TenantId( tenantId ) ).Value;
tenant.Name.Should().Be( new TenantName( tenantName ) );
} );
}
[ Then( @"registration fails with message ""(.*)""" ) ]
public void ThenRegistrationFailsWithMessage( string errorMessage )
{
Sp.Ap.Do( () =>
{
var registration = Sp.Support.GetView< RegistrationView >( this._regId ).Value;
registration.HasProblems.Should().BeTrue( "failed registration should show it has problems" );
registration.Problem.Should().Be( errorMessage );
} );
}
[ StepArgumentTransformation ]
public RegistrationInfo RegistrationInfoTransform( Table regTable )
{
var regInfoBuilder = new RegistrationInfoBuilder( regTable.GetValue( "User Email" ), regTable.GetValue( "User Name" ),
regTable.GetValue( "User Password" ), regTable.GetValue( "Company Name" ) )
{
OptionalTenantPhone = regTable.GetValue( "Company Phone" ),
OptionalTenantUrl = regTable.GetValue( "Company Website" )
};
return regInfoBuilder.Build( new PasswordGenerator() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment