4

I am building a multi-tenant SaaS application which I am trying to write tests for with Behat, using Mink and the Behat Laravel Extension

When you register for an account, you get your own subdomain on the site {account}.tenancy.dev

my behat.yml file looks like so:

default:
    extensions:
        Laracasts\Behat:
            # env_path: .env.behat
        Behat\MinkExtension:
            default_session: laravel
            base_url: http://tenancy.dev
            laravel: ~

I am having problems straight off the bat as when I try to test my registration flow, I am getting a 404 error testing that the new subdomain is accessible, all of the data has been saved correctly, manually testing the process works and the subdomain routing works.

I was wondering if there was any way to do this using Behat and how I would go about setting Behat / Mink to use wildcard subdomains to test SaaS applications?

I am running the test inside the Homestead VM.

Community
  • 1
  • 1
Andrew Willis
  • 2,289
  • 3
  • 26
  • 53

3 Answers3

0

The base_url: http://tenancy.dev configuration is used to generate a fully qualified domain URL when you utilize relative path URL's in your mink steps (IE "/home").

When you want to hit a domain different from the domain specified in base_url, all you have to do is use the fully qualified domain URL in your step like "http://test.tenancy.dev/fully/qualified".

So use the base_url configuration to set what you will be using for the majority of your steps as relative url's and then explicitly specify the full domain for the exceptions.

When I create an account named foo And GET "http://foo.tenancy.dev/ping" Then I get a 200 response code When I GET "/home" Then the response contains "Sign Up"

If the majority of your testing will be against the sub domain, set that as your base_url and explicitly specify your top level domain when necessary.

Jeremy Giberson
  • 1,063
  • 8
  • 15
  • This is what I was doing and it gives me a 404 error because Behat runs the code in a sandbox and as such, the subdomains are never added to the sandbox. I think what I need is Behat to allow something like wildcard subdomain handling. – Andrew Willis Apr 28 '15 at 08:20
  • 1
    What you're describing isn't a Behat responsibility. Configuration to allow your sandbox host to respond to *.domain.com comes down to your web server (apache, nginx, etc) configuration. I suspect you just need to configure your sandbox web server to direct all traffic to *.tenancy.dev to your application dir. – Jeremy Giberson Apr 28 '15 at 15:59
0

You may resolve subdomains using xip.io, which is especially useful if you cannot access the /etc/hosts file on a CI server, for example.

To route {account}.tenancy.dev to your local webserver, you can use account.tenancy.dev.127.0.0.1.xip.io which resolves to 127.0.0.1.

Fabian Kleiser
  • 2,988
  • 3
  • 27
  • 44
0

After a short while I revisited this problem and found a rather simple solution to be used in my FeatureContext.php:

$this->setMinkParameter('base_url', $url);

This changes the base url for any scenario it is used in:

/**
 * @Given I visit the url :url
 */
public function visitDomain($url)
{
    $this->setMinkParameter('base_url', $url);
    $this->visit('/');
}

Which is used in the following way:

Scenario: Test Multi Tenancy
  Given I have a business "mttest"
  When I visit the url "http://mttest.example.com"
  Then I should see "mttest"

Obviously this is slightly contrived but does show that what I was intending to do is possible.

Andrew Willis
  • 2,289
  • 3
  • 26
  • 53