Skip to content

Commit

Permalink
makes e2e tests optionals and ease their setup (#67)
Browse files Browse the repository at this point in the history
* Skip tests requiring missing env. variables

E2E tests require secrets and tokens to be set. Skip those tests when
the environment variables are missing.

* Fix makefile

- `mocha` config moved to “test/mocha.opts”;
- `npm test` doesn’t required make;
- add coverage task;
- ensure “node_modules/“ is up to date.

* Set tests’ env. variable using `dotenv`

[dotenv] will append process.env variables set in the “.env” file. Note that it won’t override existing environment variable.


[dotenv]: https://github.com/motdotla/dotenv
  • Loading branch information
dinoboff authored and ddo committed Jan 4, 2018
1 parent 34f3a96 commit 712197e
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 77 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
coverage/
.env
41 changes: 35 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
test:
mocha --recursive --reporter spec --slow 1

coveralls:
istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec --recursive && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage

.PHONY: test
test: node_modules .env
npm test

coverage: coverage/lcov.info
node_modules/.bin/istanbul report text

coveralls: coverage/lcov.info
cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage

.PHONY: test coverage coveralls

.env:
@echo "BITBUCKET_CONSUMER_PUBLIC=" > $@
@echo "BITBUCKET_CONSUMER_SECRET=" >> $@
@echo "" >> $@
@echo "FLICKR_CONSUMER_key=" >> $@
@echo "FLICKR_CONSUMER_SECRET=" >> $@
@echo "" >> $@
@echo "LINKEDIN_CONSUMER_PUBLIC=" >> $@
@echo "LINKEDIN_CONSUMER_SECRET=" >> $@
@echo "" >> $@
@echo "OPENBANK_CONSUMER_PUBLIC=" >> $@
@echo "OPENBANK_CONSUMER_SECRET=" >> $@
@echo "" >> $@
@echo "TWITTER_CONSUMER_PUBLIC=" >> $@
@echo "TWITTER_CONSUMER_SECRET=" >> $@
@echo "TWITTER_TOKEN_PUBLIC=" >> $@
@echo "TWITTER_TOKEN_SECRET=" >> $@

coverage/lcov.info: node_modules package.json oauth-1.0a.js .env test/*.js test/**/*.js test/mocha.opts
node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly

node_modules: package.json
npm install
touch node_modules
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "2.2.3",
"description": "OAuth 1.0a Request Authorization for Node and Browser.",
"scripts": {
"test": "make test"
"test": "mocha"
},
"main": "oauth-1.0a.js",
"types": "oauth-1.0a.d.ts",
Expand All @@ -20,10 +20,11 @@
"author": "Ddo",
"license": "MIT",
"devDependencies": {
"mocha": "~1.17.0",
"chai": "~1.8.1",
"request": "~2.33.0",
"istanbul": "^0.2.7",
"coveralls": "^2.10.0"
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"dotenv": "^4.0.0",
"istanbul": "^0.4.5",
"mocha": "^4.0.1",
"request": "~2.33.0"
}
}
4 changes: 4 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--recursive
--reporter spec
--require dotenv/config
--slow 1
30 changes: 21 additions & 9 deletions test/services/bitbucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ var OAuth = require('../../oauth-1.0a');
var crypto = require('crypto');

describe("Bitbucket Personal Consumer", function() {
this.timeout(10000);
var oauth

var oauth = new OAuth({
consumer: {
key: process.env.BITBUCKET_CONSUMER_PUBLIC,
secret: process.env.BITBUCKET_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
beforeEach(function () {
if (
!process.env.BITBUCKET_CONSUMER_PUBLIC ||
!process.env.BITBUCKET_CONSUMER_SECRET
) {
this.skip('Bitbucket secret not set.');
return;
}

this.timeout(10000);

oauth = new OAuth({
consumer: {
key: process.env.BITBUCKET_CONSUMER_PUBLIC,
secret: process.env.BITBUCKET_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
});

describe("#Request Token", function() {
Expand Down
32 changes: 22 additions & 10 deletions test/services/flickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ var OAuth = require('../../oauth-1.0a');
var crypto = require('crypto');

describe("Flickr Personal Consumer", function() {
this.timeout(10000);

var oauth = new OAuth({
consumer: {
key: process.env.FLICKR_CONSUMER_key,
secret: process.env.FLICKR_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
var oauth;

beforeEach(function () {
if (
!process.env.FLICKR_CONSUMER_key ||
!process.env.FLICKR_CONSUMER_SECRET
) {
this.skip('Flickr secret not set.');
return;
}

this.timeout(10000);

oauth = new OAuth({
consumer: {
key: process.env.FLICKR_CONSUMER_key,
secret: process.env.FLICKR_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
});

describe.skip("#Request Token", function() {
Expand Down
32 changes: 22 additions & 10 deletions test/services/linkedin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@ var crypto = require('crypto');
Can not use Header
*/
describe("Linkedin Personal Consumer", function() {
this.timeout(10000);

var oauth = new OAuth({
consumer: {
key: process.env.LINKEDIN_CONSUMER_PUBLIC,
secret: process.env.LINKEDIN_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
var oauth;

beforeEach(function () {
if (
!process.env.LINKEDIN_CONSUMER_PUBLIC ||
!process.env.LINKEDIN_CONSUMER_SECRET
) {
this.skip('LinkedIn secret not set.');
return;
}

this.timeout(10000);

oauth = new OAuth({
consumer: {
key: process.env.LINKEDIN_CONSUMER_PUBLIC,
secret: process.env.LINKEDIN_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
});

describe("#Request Token", function() {
Expand Down
32 changes: 22 additions & 10 deletions test/services/openbank.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ var OAuth = require('../../oauth-1.0a');
var crypto = require('crypto');

describe.skip("Openbank Personal Consumer", function() {
this.timeout(10000);

var oauth = new OAuth({
consumer: {
key: process.env.OPENBANK_CONSUMER_PUBLIC,
secret: process.env.OPENBANK_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA256',
hash_function: function(base_string, key) {
return crypto.createHmac('sha256', key).update(base_string).digest('base64');
var oauth;

beforeEach(function () {
if (
!process.env.OPENBANK_CONSUMER_PUBLIC ||
!process.env.OPENBANK_CONSUMER_SECRET
) {
this.skip('Openbank secret not set.');
return;
}

this.timeout(10000);

oauth = new OAuth({
consumer: {
key: process.env.OPENBANK_CONSUMER_PUBLIC,
secret: process.env.OPENBANK_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA256',
hash_function: function(base_string, key) {
return crypto.createHmac('sha256', key).update(base_string).digest('base64');
}
});
});

//need to send as header
Expand Down
54 changes: 29 additions & 25 deletions test/services/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@ var OAuth = require('../../oauth-1.0a');
var crypto = require('crypto');

describe("Twitter Personal Consumer", function() {
var oauth = new OAuth({
consumer: {
key: process.env.TWITTER_CONSUMER_PUBLIC,
secret: process.env.TWITTER_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
var oauth, token;

beforeEach(function () {
if (
!process.env.TWITTER_CONSUMER_PUBLIC ||
!process.env.TWITTER_CONSUMER_SECRET ||
!process.env.TWITTER_TOKEN_PUBLIC ||
!process.env.TWITTER_TOKEN_SECRET
) {
this.skip('Twitter secret or token not set.');
return;
}
});

var token = {
key: process.env.TWITTER_TOKEN_PUBLIC,
secret: process.env.TWITTER_TOKEN_SECRET
};

describe("#Get user timeline", function() {
this.timeout(10000);

oauth = new OAuth({
consumer: {
key: process.env.TWITTER_CONSUMER_PUBLIC,
secret: process.env.TWITTER_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});

token = {
key: process.env.TWITTER_TOKEN_PUBLIC,
secret: process.env.TWITTER_TOKEN_SECRET
};
});

describe("#Get user timeline", function() {
var request = {
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
method: 'GET'
Expand All @@ -43,8 +57,6 @@ describe("Twitter Personal Consumer", function() {
});

describe("#Get user timeline limit 5", function() {
this.timeout(10000);

var request = {
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
method: 'GET',
Expand All @@ -69,8 +81,6 @@ describe("Twitter Personal Consumer", function() {
});

describe("#Get user timeline limit 5 by url", function() {
this.timeout(10000);

var request = {
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=5',
method: 'GET'
Expand All @@ -92,8 +102,6 @@ describe("Twitter Personal Consumer", function() {
});

describe("#Get user timeline by header", function() {
this.timeout(10000);

var request = {
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
method: 'GET'
Expand All @@ -115,8 +123,6 @@ describe("Twitter Personal Consumer", function() {
});

describe.skip("#Tweet", function() {
this.timeout(10000);

var text = 'Testing oauth-1.0a';

var request = {
Expand Down Expand Up @@ -146,8 +152,6 @@ describe("Twitter Personal Consumer", function() {
});

describe.skip("#Tweet by header", function() {
this.timeout(10000);

var text = 'Testing oauth-1.0a';

var request = {
Expand Down

0 comments on commit 712197e

Please sign in to comment.