Skip to content

Commit

Permalink
Add integration testing (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
danabreo authored Aug 7, 2020
1 parent a2f2a92 commit 7af9c02
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
PASSPORT_CLIENT_ID=
PASSPORT_CLIENT_SECRET=
BASE_URL=
TEST_DATABASE_URL=
21 changes: 20 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ const handlebarsConfig = handlebars.create({
},
});

const testMiddleware = (req, res, next) => {
if (req && req.session && req.session.tmp_user) {
req.user = req.session.tmp_user;
}
if (next) next();
};

const testAccountInfo = require('./test/testAccountInfo.json');
const testIsLoggedIn = (req, res, next) => {
if (!req.user) req.user = {};
req.session.tmp_user = testAccountInfo;
next();
};

// Environments configs.
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
Expand Down Expand Up @@ -74,7 +88,12 @@ const moreInfo = require('./routes/moreInfo');
app.get('/discover/organization/:id', moreInfo.view);

const dashboard = require('./routes/dashboard');
app.get('/dashboard/:page?', isLoggedIn, dashboard.view);
if (process.env.NODE_ENV === 'testing') {
app.use(testMiddleware);
app.get('/dashboard/:page?', testIsLoggedIn, dashboard.view);
} else {
app.get('/dashboard/:page?', isLoggedIn, dashboard.view);
}

app.get('/auth/google', passport.authenticate('google', {scope: ['profile', 'email']}));

Expand Down
1 change: 1 addition & 0 deletions gcp/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ steps:
- 'PASSPORT_CLIENT_ID=${_PASSPORT_DEV_CLIENT_ID}'
- 'PASSPORT_CLIENT_SECRET=${_PASSPORT_DEV_CLIENT_SECRET}'
- 'BASE_URL=${_BASE_URL}'
- 'TEST_DATABASE_URL=${_TEST_DATABASE_URL}'
- name: node
entrypoint: npm
args: ['test']
1 change: 1 addition & 0 deletions gcp/clouddeploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ steps:
- 'PASSPORT_CLIENT_ID=${_PASSPORT_PROD_CLIENT_ID}'
- 'PASSPORT_CLIENT_SECRET=${_PASSPORT_PROD_CLIENT_SECRET}'
- 'BASE_URL=${_BASE_URL}'
- 'TEST_DATABASE_URL=${_TEST_DATABASE_URL}'
- name: 'gcr.io/cloud-builders/gcloud'
args: ['app', 'deploy']
timeout: 1800s
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"create-env-dev": "printenv > .env.dev",
"create-env-prod": "printenv > .env.prod",
"test": "NODE_ENV=development ./node_modules/.bin/mocha --timeout 60000 --exit",
"test": "NODE_ENV=testing ./node_modules/.bin/mocha --timeout 60000 --exit",
"start": "NODE_ENV=production node app.js",
"dev": "NODE_ENV=development node app.js",
"lint": "eslint --fix .",
Expand Down
43 changes: 43 additions & 0 deletions test/test-database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const expect = require('chai').expect;
const request = require('request');
require('../app');

const authUserKey = 'ZqbEncqt9EsJTLOcXRxt';
const authUserID = '7dJFX6hTPwVK9tLkXnrT';
it('Authenticated user found', function (done) {
request(`${process.env.TEST_DATABASE_URL}data/organization/member/${authUserKey}`, function (
error,
response,
body
) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain(authUserID);
done();
});
});

const authOrgKey = '7dJFX6hTPwVK9tLkXnrT';
it('Organization connection found', function (done) {
request(`${process.env.TEST_DATABASE_URL}data/member/organization/${authOrgKey}`, function (
error,
response,
body
) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain('Authenticated Account');
done();
});
});

const unAuthUserKey = 'HBfL3v5C19zRTMeMheOC';
it('Unauthenticated user detected', function (done) {
request(`${process.env.TEST_DATABASE_URL}data/organization/member/${unAuthUserKey}`, function (
error,
response,
body
) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain('{}');
done();
});
});
5 changes: 5 additions & 0 deletions test/test-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ it('Landing page status', function (done) {
it('Dashboard categories page', function (done) {
request(process.env.BASE_URL + 'dashboard/categories', function (error, response, body) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain('Add a category');
done();
});
});

it('Dashboard view page', function (done) {
request(process.env.BASE_URL + 'dashboard', function (error, response, body) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain('Edit');
expect(body).to.contain('Accepts drop off donations');
done();
});
});

it('Dashboard edit page', function (done) {
request(process.env.BASE_URL + 'dashboard/edit', function (error, response, body) {
expect(response.statusCode).to.equal(200);
expect(body).to.contain('Submit');
expect(body).to.contain('Accepts drop off donations');
done();
});
});
23 changes: 23 additions & 0 deletions test/testAccountInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "114671053640055693297",
"displayName": "Authenticated Account",
"name": {"familyName": "Account", "givenName": "Authenticated"},
"emails": [{"value": "[email protected]", "verified": true}],
"photos": [
{
"value": "https://lh5.googleusercontent.com/-kujBr31m3wk/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnfvVM5CsnovOAjMR1rVTOoteDA3A/photo.jpg"
}
],
"provider": "google",
"_raw": "{\n \"sub\": \"114671053640055693297\",\n \"name\": \"Authenticated Account\",\n \"given_name\": \"Authenticated\",\n \"family_name\": \"Account\",\n \"picture\": \"https://lh5.googleusercontent.com/-kujBr31m3wk/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnfvVM5CsnovOAjMR1rVTOoteDA3A/photo.jpg\",\n \"email\": \"[email protected]\",\n \"email_verified\": true,\n \"locale\": \"en\"\n}",
"_json": {
"sub": "114671053640055693297",
"name": "Authenticated Account",
"given_name": "Authenticated",
"family_name": "Account",
"picture": "https://lh5.googleusercontent.com/-kujBr31m3wk/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnfvVM5CsnovOAjMR1rVTOoteDA3A/photo.jpg",
"email": "[email protected]",
"email_verified": true,
"locale": "en"
}
}

0 comments on commit 7af9c02

Please sign in to comment.