diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index fc1bad4..0000000 --- a/.drone.yml +++ /dev/null @@ -1,15 +0,0 @@ -image: docker.vokal.io/web-testing -env: - - CVR_TOKEN=$$cvrToken - - GITHUB_CLIENTID=$$gitHubClientId - - GITHUB_CLIENTSECRET=$$gitHubClientSecret - - DB_CONN=$$mongoConn - - GITHUB_CALLBACKURL=https://cvr.vokal.io/auth/github/callback - - GITHUB_ORGS_WHITELIST=vokal - - HOST=https://cvr.vokal.io/ - - GITHUB_TESTING_AUTH_TOKEN=$$gitHubTestingAuthToken -script: - - npm install --silent -g istanbul - - npm install --silent - - chmod +x run-build.sh - - ./run-build.sh diff --git a/.gitignore b/.gitignore index 614240c..9b9b68a 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,5 @@ $RECYCLE.BIN/ local-settings.json tmp + +secrets.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ebd4f2a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: node_js +node_js: + - "node" +services: + - mongodb +env: + global: + - REPO_OWNER=vokal + - REPO_NAME=cvr-view + - GITHUB_CALLBACKURL=https://cvr.vokal.io/auth/github/callback + - GITHUB_ORGS_WHITELIST=vokal + - HOST=https://cvr.vokal.io/ + - GITHUB_CLIENTID=test + - GITHUB_CLIENTSECRET=test + - DB_CONN=mongodb://localhost +install: + - npm install -g istanbul + - npm install +script: + - chmod +x run-build.sh + - ./run-build.sh diff --git a/package.json b/package.json index 3ce2466..b5f6d35 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "devDependencies": { "mocha": "^2.3.4", + "nock": "^7.2.2", "supertest": "^1.1.0" } } diff --git a/routes/file.js b/routes/file.js index 61f7f89..3b0440f 100644 --- a/routes/file.js +++ b/routes/file.js @@ -80,7 +80,7 @@ module.exports = function ( req, res, next ) + ". Is the path correctly based from the project root?", 404 ) ); } - if( errMessage.indexOf( "No commit found for the ref" ) > -1 ) + if( errMessage.indexOf && errMessage.indexOf( "No commit found for the ref" ) > -1 ) { return next( createError( "The hash " + req.params.hash + " does not exist", 404 ) ); } diff --git a/run-build.sh b/run-build.sh index eb4c09b..c5a85d8 100644 --- a/run-build.sh +++ b/run-build.sh @@ -4,11 +4,13 @@ npm run testcover RESULT=$? +COMMIT="${TRAVIS_COMMIT_RANGE##*...}" + if [ $RESULT == 0 ]; then - GITHASH="$(git rev-parse HEAD)" - curl -F coverage=@coverage/lcov.info "https://cvr.vokal.io/coverage?token=$CVR_TOKEN&commit=$GITHASH&removepath=/var/cache/drone/src/github.com/vokal/cvr-view/&coveragetype=lcov" + echo "publish coverage for $COMMIT" + curl -F coverage=@coverage/lcov.info "https://cvr.vokal.io/coverage?owner=$REPO_OWNER&repo=$REPO_NAME&commit=$COMMIT&coveragetype=lcov" else - curl -X POST "https://cvr.vokal.io/coverage/abort?token=$CVR_TOKEN&commit=$GITHASH" + curl -X POST "https://cvr.vokal.io/coverage/abort?owner=$REPO_OWNER&repo=$REPO_NAME&commit=$COMMIT" fi exit $RESULT diff --git a/test/404.js b/test/404.js index f9e17ca..0c9f413 100644 --- a/test/404.js +++ b/test/404.js @@ -2,23 +2,20 @@ var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var app = require( "../app" ); module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should have a 404 page", function ( done ) { - request( server ) + agent .get( "/this/is/not/a/page" ) .expect( 404 ) .end( done ); diff --git a/test/assets/webhook-synchronize.json b/test/assets/webhook-synchronize.json index 5c91286..ea4a8d6 100644 --- a/test/assets/webhook-synchronize.json +++ b/test/assets/webhook-synchronize.json @@ -14,7 +14,7 @@ "type": "User", "site_admin": false }, - "body": "@foresmac pulling up new dep", + "body": "pulling up new dep", "created_at": "2015-09-10T17:51:48Z", "updated_at": "2015-09-10T18:18:21Z", "closed_at": null, @@ -25,7 +25,7 @@ "head": { "label": "jrit:master", "ref": "master", - "sha": "13a66297be32769cc2e2ecc8ca7146d717708b5f", + "sha": "558bc5aa45d591b3cdfea80af29e7ffb66ff55f1", "user": { "login": "jrit", "id": 1255823, @@ -35,8 +35,8 @@ }, "repo": { "id": 35064431, - "name": "cvr-view-seed", - "full_name": "jrit/cvr-view-seed", + "name": "cvr-view-test", + "full_name": "jrit/cvr-view-test", "owner": { "login": "jrit", "id": 1255823, @@ -59,8 +59,8 @@ }, "repo": { "id": 35064414, - "name": "cvr-view-seed", - "full_name": "vokal/cvr-view-seed", + "name": "cvr-view-test", + "full_name": "vokal/cvr-view-test", "owner": { "login": "vokal", "id": 375026, diff --git a/test/auth-success.js b/test/auth-success.js index 80a32de..d702d64 100644 --- a/test/auth-success.js +++ b/test/auth-success.js @@ -2,23 +2,21 @@ var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var nock = require( "nock" ); +var app = require( "../app" ); module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should redirect from the page", function ( done ) { - request( server ) + agent .get( "/auth/github/success" ) .expect( 302 ) .end( function ( err, res ) @@ -30,7 +28,11 @@ module.exports = function () it( "should fail token auth when invalid", function ( done ) { - request( server ) + nock( "https://api.github.com" ) + .get( "/user?access_token=123" ) + .reply( 401 ); + + agent .post( "/auth/github/token" ) .field( "token", "123" ) .expect( 401, done ); diff --git a/test/coverage.js b/test/coverage.js index e4cb6eb..2ca487e 100644 --- a/test/coverage.js +++ b/test/coverage.js @@ -1,24 +1,24 @@ "use strict"; +var util = require( "util" ); var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var app = require( "../app" ); +var nock = require( "nock" ); +var commit = "558bc5aa45d591b3cdfea80af29e7ffb66ff55f1"; module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should validate posting coverage", function ( done ) { - request( server ) + agent .post( "/coverage" ) .expect( 400 ) .end( function ( err, res ) @@ -30,7 +30,7 @@ module.exports = function () it( "should validate repo is required", function ( done ) { - request( server ) + agent .post( "/coverage" ) .field( "commit", "123" ) .expect( 400 ) @@ -43,7 +43,7 @@ module.exports = function () it( "should validate token", function ( done ) { - request( server ) + agent .post( "/coverage" ) .field( "commit", "123" ) .field( "token", "123" ) @@ -60,7 +60,7 @@ module.exports = function () { this.timeout( 5000 ); - request( server ) + agent .post( "/coverage" ) .field( "commit", "123" ) .field( "owner", "123" ) @@ -76,7 +76,7 @@ module.exports = function () it( "should validate coverage has content", function ( done ) { - request( server ) + agent .post( "/coverage" ) .field( "commit", "123" ) .field( "owner", "123" ) @@ -92,7 +92,7 @@ module.exports = function () it( "should validate coverage has a valid type", function ( done ) { - request( server ) + agent .post( "/coverage" ) .field( "commit", "123" ) .field( "owner", "123" ) @@ -109,13 +109,11 @@ module.exports = function () it( "should validate that commit exists on GitHub", function ( done ) { - this.timeout( 10000 ); - - request( server ) + agent .post( "/coverage" ) .field( "commit", "thisisnotacommit" ) .field( "owner", "vokal" ) - .field( "repo", "cvr-view-seed" ) + .field( "repo", "cvr-view-test" ) .attach( "coverage", "test/assets/lcov.info" ) .expect( 400 ) .end( function ( err, res ) @@ -127,13 +125,19 @@ module.exports = function () it( "should save coverage", function ( done ) { - this.timeout( 10000 ); + nock( "https://api.github.com" ) + .get( util.format( "/repos/vokal/cvr-view-test/commits?sha=%s&access_token=test", commit ) ) + .reply( 200 ) + .get( "/repos/vokal/cvr-view-test/pulls?access_token=test" ) + .reply( 200, [ { head: { sha: commit } } ] ) + .post( util.format( "/repos/vokal/cvr-view-test/statuses/%s?access_token=test", commit ) ) + .reply( 201 ); - request( server ) + agent .post( "/coverage" ) - .field( "commit", "13a66297be32769cc2e2ecc8ca7146d717708b5f" ) + .field( "commit", commit ) .field( "owner", "vokal" ) - .field( "repo", "cvr-view-seed" ) + .field( "repo", "cvr-view-test" ) .attach( "coverage", "test/assets/lcov.info" ) .expect( 201 ) .end( function ( err, res ) @@ -145,11 +149,15 @@ module.exports = function () it( "should abort pending coverage", function ( done ) { - request( server ) + nock( "https://api.github.com" ) + .post( util.format( "/repos/vokal/cvr-view-test/statuses/%s?access_token=test", commit ) ) + .reply( 200 ); + + agent .post( "/coverage/abort" ) - .field( "commit", "13a66297be32769cc2e2ecc8ca7146d717708b5f" ) + .field( "commit", "558bc5aa45d591b3cdfea80af29e7ffb66ff55f1" ) .field( "owner", "vokal" ) - .field( "repo", "cvr-view-seed" ) + .field( "repo", "cvr-view-test" ) .field( "reason", "This was a test" ) .expect( 201 ) .end( function ( err, res ) diff --git a/test/delete-repo.js b/test/delete-repo.js index 50a9cbe..d6b9f70 100644 --- a/test/delete-repo.js +++ b/test/delete-repo.js @@ -2,7 +2,9 @@ var assert = require( "assert" ); var request = require( "supertest" ); +var nock = require( "nock" ); var app = require( "../app" ); +var env = require( "../lib/env" ); module.exports = function () { @@ -15,16 +17,29 @@ module.exports = function () it( "should redirect on succesful login", function ( done ) { + nock( "https://api.github.com" ) + .get( "/user?access_token=test" ) + .reply( 200, { login: "cvr-view-test" }, { "x-oauth-scopes": "repo, user" } ); + agent .post( "/auth/github/token" ) - .field( "token", process.env.GITHUB_TESTING_AUTH_TOKEN ) + .field( "token", "test" ) .expect( 302, done ); } ); - it( "should delete the cvr-view-seed repo", function ( done ) + it( "should delete the cvr-view-test repo", function ( done ) { + nock( "https://api.github.com" ) + .get( "/repos/vokal/cvr-view-test/hooks" ) + .query( { access_token: "test", per_page: 100, page: 1 } ) + .reply( 200, [ { + config: { url: env.host + "webhook" } + } ] ) + .delete( "/repos/vokal/cvr-view-test/hooks?access_token=test" ) + .reply( 200 ); + agent - .post( "/repo/vokal/cvr-view-seed/delete" ) + .post( "/repo/vokal/cvr-view-test/delete" ) .expect( 302 ) .end( function ( err, res ) { diff --git a/test/new-token.js b/test/new-token.js index 4bf2eaa..541b484 100644 --- a/test/new-token.js +++ b/test/new-token.js @@ -3,6 +3,7 @@ var assert = require( "assert" ); var request = require( "supertest" ); var app = require( "../app" ); +var nock = require( "nock" ); module.exports = function () { @@ -15,20 +16,24 @@ module.exports = function () it( "should redirect on succesful login", function ( done ) { + nock( "https://api.github.com" ) + .get( "/user?access_token=test" ) + .reply( 200, { login: "cvr-view-test" }, { "x-oauth-scopes": "repo, user" } ); + agent .post( "/auth/github/token" ) - .field( "token", process.env.GITHUB_TESTING_AUTH_TOKEN ) + .field( "token", "test" ) .expect( 302, done ); } ); - it( "should create a new token for the cvr-view-seed repo", function ( done ) + it( "should create a new token for the cvr-view-test repo", function ( done ) { agent - .get( "/repo/vokal/cvr-view-seed/new-token" ) + .get( "/repo/vokal/cvr-view-test/new-token" ) .expect( 302 ) .end( function ( err, res ) { - assert.equal( res.headers.location, "/repo/vokal/cvr-view-seed" ); + assert.equal( res.headers.location, "/repo/vokal/cvr-view-test" ); done( err ); } ); } ); diff --git a/test/repo-details.js b/test/repo-details.js index cb6efcd..8959b86 100644 --- a/test/repo-details.js +++ b/test/repo-details.js @@ -3,6 +3,10 @@ var assert = require( "assert" ); var request = require( "supertest" ); var app = require( "../app" ); +var nock = require( "nock" ); +var fs = require( "fs" ); +var path = require( "path" ); +var env = require( "../lib/env" ); module.exports = function () { @@ -15,9 +19,13 @@ module.exports = function () it( "should redirect on succesful login", function ( done ) { + nock( "https://api.github.com" ) + .get( "/user?access_token=test" ) + .reply( 200, { login: "cvr-view-test" }, { "x-oauth-scopes": "repo, user" } ); + agent .post( "/auth/github/token" ) - .field( "token", process.env.GITHUB_TESTING_AUTH_TOKEN ) + .field( "token", "test" ) .expect( 302 ) .end( function ( err, res ) { @@ -26,41 +34,53 @@ module.exports = function () } ); } ); - it( "should load cvr-view-seed repo", function ( done ) + it( "should load cvr-view-test repo", function ( done ) { - this.timeout( 10000 ); + nock( "https://api.github.com" ) + .get( "/repos/vokal/cvr-view-test/hooks" ) + .query( { access_token: "test", per_page: 100, page: 1 } ) + .reply( 200, [ { + config: { url: env.host + "webhook" } + } ] ); agent - .get( "/repo/vokal/cvr-view-seed" ) + .get( "/repo/vokal/cvr-view-test" ) .expect( 200 ) .end( function ( err, res ) { app.repoToken = res.text.match( /\/coverage\?token=([0-9a-z]*)&commit=:commit_hash/ )[ 1 ]; - app.repoFile = res.text.match( /\/repo\/vokal\/cvr-view-seed\/[0-9a-z]*\/[^"]*/ )[ 0 ]; + app.repoFile = res.text.match( /\/repo\/vokal\/cvr-view-test\/[0-9a-z]*\/[^"]*/ )[ 0 ]; assert.equal( !!app.repoToken, true ); done( err ); } ); } ); - it( "should load cvr-view-seed repo settings", function ( done ) + it( "should load cvr-view-test repo settings", function ( done ) { agent - .get( "/repo/vokal/cvr-view-seed/settings" ) + .get( "/repo/vokal/cvr-view-test/settings" ) .expect( 200, done ); } ); - it( "should update cvr-view-seed repo settings", function ( done ) + it( "should update cvr-view-test repo settings", function ( done ) { agent - .post( "/repo/vokal/cvr-view-seed/settings" ) + .post( "/repo/vokal/cvr-view-test/settings" ) .field( "minPassingLinePercent", 50 ) .field( "removePath", "" ) .field( "prependPath", "" ) .expect( 200, done ); } ); - it( "should load a cvr-view-seed file", function ( done ) + it( "should load a cvr-view-test file", function ( done ) { + nock( "https://api.github.com" ) + .get( "/repos/vokal/cvr-view-test/contents/app.js?ref=558bc5aa45d591b3cdfea80af29e7ffb66ff55f1&access_token=test" ) + .reply( 200, { + encoding: "base64", + content: fs.readFileSync( path.resolve( process.cwd(), "app.js" ) ).toString() + } ); + agent .get( app.repoFile ) .expect( 200, done ); diff --git a/test/repos.js b/test/repos.js index 1ee5aef..d1a77af 100644 --- a/test/repos.js +++ b/test/repos.js @@ -3,6 +3,7 @@ var assert = require( "assert" ); var request = require( "supertest" ); var app = require( "../app" ); +var nock = require( "nock" ); module.exports = function () { @@ -30,11 +31,13 @@ module.exports = function () it( "should redirect to repos on succesful login", function ( done ) { - this.timeout( 10000 ); + nock( "https://api.github.com" ) + .get( "/user?access_token=test" ) + .reply( 200, { login: "cvr-view-test" }, { "x-oauth-scopes": "repo, user" } ); agent .post( "/auth/github/token" ) - .field( "token", process.env.GITHUB_TESTING_AUTH_TOKEN ) + .field( "token", "test" ) .expect( 302 ) .end( function ( err, res ) { @@ -45,19 +48,52 @@ module.exports = function () it( "should load repos page", function ( done ) { - this.timeout( 30000 ); // this takes too long on the first load + nock( "https://api.github.com" ) + .get( "/user/orgs" ) + .query( { per_page: "100", access_token: "test" } ) + .reply( 200, [ { + "login": "vokal" + } ] ); + + nock( "https://api.github.com" ) + .get( "/orgs/vokal/repos" ) + .query( { access_token: "test", per_page: 100, page: 1 } ) + .reply( 200, [ { + "owner": { + "login": "vokal" + }, + "name": "cvr-view-test", + "full_name": "vokal/cvr-view-test" + } ] ); + + nock( "https://api.github.com" ) + .get( "/user/repos" ) + .query( { access_token: "test", per_page: 100, page: 1 } ) + .reply( 200, [ { + "owner": { "login": "test" }, + "name": "cvr-view-test", + "full_name": "vokal/cvr-view-test", + "permissions": { "push": true } + } ] ); + + nock( "https://api.github.com" ) + .get( "/repos/vokal/cvr-view-test/hooks" ) + .query( { access_token: "test", per_page: 100, page: 1 } ) + .reply( 200, [] ) + .post( "/repos/vokal/cvr-view-test/hooks?access_token=test" ) + .reply( 201 ); agent .get( "/repos" ) .expect( 200, done ); } ); - it( "should activate the cvr-view-seed repo", function ( done ) + it( "should activate the cvr-view-test repo", function ( done ) { this.timeout( 10000 ); agent - .get( "/repo/vokal/cvr-view-seed" ) + .get( "/repo/vokal/cvr-view-test" ) .expect( 200 ) .end( function ( err, res ) { diff --git a/test/shields.js b/test/shields.js index f3a260f..782ba87 100644 --- a/test/shields.js +++ b/test/shields.js @@ -2,30 +2,27 @@ var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var app = require( "../app" ); module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should load a shield", function ( done ) { - request( server ) - .get( "/vokal/cvr-view-seed/shield.svg" ) + agent + .get( "/vokal/cvr-view-test/shield.svg" ) .expect( 200, done ); } ); it( "should 404 a shield on non-existing repo", function ( done ) { - request( server ) + agent .get( "/vokal/notthedroidsyouarelookingfor/shield.svg" ) .expect( 404, done ); } ); diff --git a/test/upload.js b/test/upload.js index b93b638..e727c75 100644 --- a/test/upload.js +++ b/test/upload.js @@ -2,23 +2,20 @@ var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var app = require( "../app" ); module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should exist", function ( done ) { - request( server ) + agent .get( "/upload" ) .expect( 200, done ); } ); diff --git a/test/webhook.js b/test/webhook.js index 4b2f381..fe3fef0 100644 --- a/test/webhook.js +++ b/test/webhook.js @@ -2,24 +2,22 @@ var assert = require( "assert" ); var request = require( "supertest" ); -var app = require( "./server" ); +var app = require( "../app" ); var fs = require( "fs" ); +var nock = require( "nock" ); module.exports = function () { - var server; + var agent; before( function ( done ) { - app( function ( err, res ) - { - server = res; - done( err ); - } ); + agent = request.agent( app ); + done(); } ); it( "should reject a non-PR request", function ( done ) { - request( server ) + agent .post( "/webhook" ) .expect( 202 ) .end( function ( err, res ) @@ -31,7 +29,11 @@ module.exports = function () it( "should accept a sync hook", function ( done ) { - request( server ) + nock( "https://api.github.com" ) + .post( "/repos/vokal/cvr-view-test/statuses/558bc5aa45d591b3cdfea80af29e7ffb66ff55f1?access_token=test" ) + .reply( 201 ); + + agent .post( "/webhook" ) .set( "Content-Type", "application/json" ) .send( fs.readFileSync( "./test/assets/webhook-synchronize.json" ).toString() ) @@ -48,7 +50,7 @@ module.exports = function () var hook = JSON.parse( fs.readFileSync( "./test/assets/webhook-synchronize.json" ).toString() ); hook.pull_request.title += "[ci skip]"; - request( server ) + agent .post( "/webhook" ) .set( "Content-Type", "application/json" ) .send( JSON.stringify( hook ) )