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

tests: Use a random test user when no env vars are present #584

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions .travis.yml

This file was deleted.

44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,48 +76,54 @@ If you're having any problem, please [raise an issue](https://github.com/resin-i
Tests
-----

In order to run the Resin SDK test suite, set the following environment variables from an account that exists and doesn't have a billing account code:
**WARNING: This will delete all applications and public keys of the test user**
Run the test suite by doing:

```sh
$ npm test
```

- `RESINTEST_EMAIL`: The main account email
- `RESINTEST_PASSWORD`: The main account password
- `RESINTEST_USERNAME`: The main account username.
### API Url

You also have to provide the following environment variables from an account that doesn't yet exists:
You can, optionally, set the `RESINTEST_API_URL` environment variable in order to run the tests using a different API backend (eg: `https://api.resinstaging.io`).

- `RESINTEST_REGISTER_EMAIL`: The email of the account to register.
- `RESINTEST_REGISTER_PASSWORD`: The password of the account to register.
- `RESINTEST_REGISTER_USERNAME`: The username of the account to register.
### Billing

In order to test the billing methods for a paid account, you also have to configure the following environment variables:
In order to test the billing methods for a paid account, you have to configure the following environment variables:

- `RESINTEST_PAID_EMAIL`: The email of the paid account.
- `RESINTEST_PAID_PASSWORD`: The password of the account.

*Note: The paid user's `account billing code` should be set to `testdev` so that it's tested against the test plan.*
*Note: The paid user's `account billing code` should be set to `testdev` so that it's tested against the proper plan.*

You can also, optionally, set the `RESINTEST_API_URL` environment variable in order to run the tests using a different API backend (eg: `https://api.resinstaging.io`).
### Avoiding rate limiting errors

While developing you might end up in a situation that the API returns rate limiting errors.
That's probably caused by the new user signups that each run of the test suit does.
You can avoid those errors to some extend by specifying a pre-existing **empty** user account to run the suit against.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

  • suit -> suite
  • extend -> extent

In order to do so, set the following environment variables for an account that exists and doesn't have a billing account code:
**WARNING: This will delete all applications and public keys of the test user**

- `RESINTEST_EMAIL`: The test account email
- `RESINTEST_PASSWORD`: The test account password
- `RESINTEST_USERNAME`: The test account username.

### Persisting ENV configuration

You can persist these settings by putting them all into a `.env` file in the root of this repo, in
[dotenv](https://www.npmjs.com/package/dotenv) format (`KEY=VALUE\n`). This will be automatically detected and used in the tests.
Make sure you don't accidentally commit this file (`.env` by default is gitignored, so hopefully this should be difficult).

Run the test suite by doing:

```sh
$ npm test
```

Contribute
----------

- Issue Tracker: [github.com/resin-io/resin-sdk/issues](https://github.com/resin-io/resin-sdk/issues)
- Source Code: [github.com/resin-io/resin-sdk](https://github.com/resin-io/resin-sdk)

Before submitting a PR, please make sure that you include tests, and that [coffeelint](http://www.coffeelint.org/) runs without any warning:
Before submitting a PR, please make sure that you include tests, and that the linter runs without any warning:

```sh
$ gulp lint
$ npm run lint
```

License
Expand Down
7 changes: 3 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ matrix:
environment:
matrix:
- nodejs_version: 6
RESINTEST_EMAIL: '[email protected]'
RESINTEST_USERNAME: 'test3_juan'
RESINTEST_REGISTER_EMAIL: '[email protected]'
RESINTEST_REGISTER_USERNAME: 'test3_register_juan'
RESINTEST_PAID_EMAIL: '[email protected]'
RESINTEST_PAID_PASSWORD:
secure: zqKu7kmQ2b53eRh4lPwKW1ht/Va/ykIuDwdkkT5cwgc=

install:
- ps: Install-Product node $env:nodejs_version x64
Expand Down
11 changes: 7 additions & 4 deletions karma.conf.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ module.exports = (config) ->
loadEnv()

karmaConfig.plugins.push(require('karma-chrome-launcher'))
karmaConfig.browsers = ['ChromeHeadless']
karmaConfig.browsers = ['ChromeHeadlessCustom']
karmaConfig.customLaunchers =
ChromeHeadlessCustom:
base: 'ChromeHeadless'
flags: [
'--no-sandbox'
]

karmaConfig.logLevel = config.LOG_INFO
karmaConfig.sauceLabs =
Expand Down Expand Up @@ -41,9 +47,6 @@ module.exports = (config) ->
'RESINTEST_USERNAME'
'RESINTEST_PAID_EMAIL'
'RESINTEST_PAID_PASSWORD'
'RESINTEST_REGISTER_EMAIL'
'RESINTEST_REGISTER_PASSWORD'
'RESINTEST_REGISTER_USERNAME'
]

config.set(karmaConfig)
2 changes: 1 addition & 1 deletion tests/integration/models/image.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
m = require('mochainon')
_ = require('lodash')

{ resin, credentials, givenLoggedInUser, givenMulticontainerApplication } = require('../setup')
{ resin, givenLoggedInUser, givenMulticontainerApplication } = require('../setup')

describe 'Image Model', ->

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/models/service.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
m = require('mochainon')
_ = require('lodash')

{ resin, credentials, givenLoggedInUser, givenMulticontainerApplication } = require('../setup')
{ resin, givenLoggedInUser, givenMulticontainerApplication } = require('../setup')

describe 'Service Model', ->

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/resin.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
m = require('mochainon')

{ resin, getSdk, sdkOpts, credentials, givenLoggedInUser } = require('./setup')
{ resin, getSdk, sdkOpts, givenLoggedInUser } = require('./setup')

describe 'Resin SDK', ->

Expand Down
99 changes: 69 additions & 30 deletions tests/integration/setup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ _ = require('lodash')
chai.use(require('chai-samsam'))

exports.IS_BROWSER = IS_BROWSER = window?
exports.credentials = {}

if IS_BROWSER
require('js-polyfills/es6')
Expand All @@ -30,38 +31,50 @@ _.assign opts,
isBrowser: IS_BROWSER,
retries: 3

buildCredentials = ->
if not env
throw new Error('Missing environment object?!')
exports.getSdk = getSdk
exports.sdkOpts = opts
exports.resin = resin = getSdk(opts)

shouldDeleteTestUser = false

credentials =
email: env.RESINTEST_EMAIL
password: env.RESINTEST_PASSWORD
username: env.RESINTEST_USERNAME
getCredentials = _.once Promise.method ->
username = "sdk_tests_#{Date.now()}"
register_username = "#{username}_reg"

getUserCredentials = (username) ->
username: username
email: "#{username}@resin.io"
password: "#{username}_!@#"

tmpEnv = env || {}
envTestUserCredentials =
username: tmpEnv.RESINTEST_USERNAME
email: tmpEnv.RESINTEST_EMAIL
password: tmpEnv.RESINTEST_PASSWORD

envPaidUserCredentials =
paid:
email: env.RESINTEST_PAID_EMAIL
password: env.RESINTEST_PAID_PASSWORD
register:
email: env.RESINTEST_REGISTER_EMAIL
password: env.RESINTEST_REGISTER_PASSWORD
username: env.RESINTEST_REGISTER_USERNAME

if not _.every [
credentials.email?
credentials.password?
credentials.username?
credentials.register.email?
credentials.register.password?
credentials.register.username?
]
throw new Error('Missing environment credentials')

return credentials
email: tmpEnv.RESINTEST_PAID_EMAIL
password: tmpEnv.RESINTEST_PAID_PASSWORD

exports.getSdk = getSdk
credentials = _.assign({},
getUserCredentials(username),
envPaidUserCredentials,
register: getUserCredentials(register_username)
)

exports.sdkOpts = opts
exports.resin = resin = getSdk(opts)
if _.every(envTestUserCredentials)
console.log('Using ENV test user credentials')
_.assign(credentials, envTestUserCredentials)
return credentials

resin.auth.register
email: credentials.email
password: credentials.password
.tap -> shouldDeleteTestUser = true
.return(credentials)
.tapCatch ->
console.error('Failed to autogenerate test credentials!')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should just throw an error here?


exports.resetUser = ->
return resin.auth.isLoggedIn().then (isLoggedIn) ->
Expand All @@ -82,8 +95,6 @@ exports.resetUser = ->
.catchReturn()
]

exports.credentials = buildCredentials()

exports.givenLoggedInUserWithApiKey = ->
beforeEach ->
resin.auth.login
Expand Down Expand Up @@ -325,3 +336,31 @@ exports.givenMulticontainerApplication = ->
@oldWebInstall = oldWebInstall
@newWebInstall = newWebInstall
@newDbInstall = newDbInstall

before ->
console.log('************ setup ************')
console.log("API Url: #{opts.apiUrl}")
getCredentials()
.then (result) ->
# use assign, since this runs asynchronously, after
# each test has imported the credentials object
_.assign(exports.credentials, result)
.finally -> console.log()

after ->
console.log('********** teardown **********')
if !shouldDeleteTestUser
return

resin.auth.login
email: exports.credentials.email
password: exports.credentials.password
.then(resin.auth.getUserId)
.then (userId) ->
return resin.request.send
method: 'DELETE'
url: "/v2/user(#{userId})"
baseUrl: exports.sdkOpts.apiUrl
.then -> console.log('Deleted autogenerated test user')
.then(resin.auth.logout)
.catch(message: 'Request error: Unauthorized', ->)