Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables #15

Closed
xlucas opened this issue Jan 29, 2016 · 2 comments
Closed

Environment variables #15

xlucas opened this issue Jan 29, 2016 · 2 comments

Comments

@xlucas
Copy link

xlucas commented Jan 29, 2016

Firsteval, congrats for the good work ! Great project !

I'm currently wondering how are environment variables supported. Typically say we have an authentication feature, and two scenari in it (authentication successfull and authentication failure) but It would be preferable to inject credentials from envrironment for the valid authentication scenario. What would be the best way to do this with godog ?

@l3pp4rd
Copy link
Member

l3pp4rd commented Jan 31, 2016

Hi, thanks ;)

It does not relate to godog and instead - is a design issue and you should manage your state and control it from tests the same as with godog. You can implement this in many ways, for example.

  1. in main func read used environment variables to config struct and in test case change it in before scenario hook. It would require to pass config values for functions which needs these parameters
  2. Another way, would be to export and unset these variables in after scenario hook.

The example for the second, which is easier but not better design solution, would be:

Feature: user authentication
  In order to access my private resources
  As an anonymous user
  I need to be able to authenticate

  Scenario: private resources are restricted from wrong credentials
    Given my credentials are "username" and "password"
    When I send "GET" request to "/v1/requires/auth"
    Then the response code should be 403

  Scenario: I should be able to access private resource with good credentials
    Given there is user "john" with password "secret"
    And my credentials are "john" and "secret"
    When I send "GET" request to "/v1/requires/auth"
    Then the response code should be 200

Where my credentials are step, could set environment variables:

func myCredentialsAre(user, passw string) error {
    os.Setenv("AUTH_USER", user)
    os.Setenv("AUTH_PASS", passw)
    return nil
}

Given there is user step, should allow to pass these credentials, some state in storage or memory to pass authentication with correct credentials.

After the scenario, you should unset the environment:

// s is godog suite
    s.AfterScenario(func(_ interface{}, _ error) {
        os.Unsetenv("AUTH_USER")
        os.Unsetenv("AUTH_PASS")
    })

Note, this solution won't work with concurrent tests. So better is to have something like configuration structure instead of os environment directly..

@xlucas
Copy link
Author

xlucas commented Jan 31, 2016

Totally makes sense, thanks alot for the details !

@l3pp4rd l3pp4rd closed this as completed Feb 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants