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

Feature/add unit test #2449

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8b4b1c3
meteor add dispatch:mocha
mauriciovieira Apr 5, 2017
453f9b0
Add npm scripts
mauriciovieira Apr 4, 2017
f4663dd
meteor add practicalmeteor:chai
mauriciovieira Apr 11, 2017
3fcffa9
Add a dummy test
mauriciovieira Apr 7, 2017
d09bc9b
Fix dummy test.
mauriciovieira Apr 7, 2017
42fa2af
Add npm test scripts
mauriciovieira Apr 11, 2017
8ae163d
Improve continuous integration
mauriciovieira Apr 11, 2017
22387ff
Wait tests to run for 30 minutes
mauriciovieira Apr 11, 2017
64a1f9d
Add chromedriver according to dispatch:mocha docs
mauriciovieira Apr 11, 2017
267b984
meteor add dispatch:mocha-browser
mauriciovieira Apr 11, 2017
490d661
Create a new server method userIsAdmin
marla-singer Apr 13, 2017
cce9b53
Remove part with FlorRouter.wait()
marla-singer Apr 13, 2017
dace893
Add route '/users' as "requireAdminRole"
marla-singer Apr 13, 2017
80dbfed
Import template to fix lint
mauriciovieira Apr 13, 2017
8f346d3
Revert "Remove part with FlorRouter.wait()"
mauriciovieira Apr 13, 2017
a8038af
Add copyright header
mauriciovieira Apr 13, 2017
35e39d2
Revert "Create a new server method userIsAdmin"
mauriciovieira Apr 13, 2017
69cb95c
Format code to improve readability
mauriciovieira Apr 13, 2017
4d2a7c8
Wrap FlowRouter initialization in meteor client env
mauriciovieira Apr 13, 2017
601a9bf
Workaround for FlowRouter error
mauriciovieira Apr 13, 2017
38c6d6e
meteor remove tap:i18n-ui
mauriciovieira Apr 13, 2017
272ed48
Revert "Add chromedriver according to dispatch:mocha docs"
mauriciovieira Apr 19, 2017
c40e6b6
Change npm scripts
mauriciovieira Apr 19, 2017
d97008c
Remove travis caching
mauriciovieira Apr 19, 2017
5b3782c
meteor add dburles:factory
mauriciovieira Apr 20, 2017
6fa44f3
Add faker npm package as dev-dependency
mauriciovieira Apr 20, 2017
2bd4030
Use yarn
mauriciovieira Apr 21, 2017
240526c
Test Branding rendering
mauriciovieira Apr 21, 2017
993ac91
Remove faker
mauriciovieira Apr 28, 2017
2a58e6d
meteor add practicalmeteor:mocha
mauriciovieira May 2, 2017
f71d722
meteor remove dispatch:mocha-browser
mauriciovieira May 2, 2017
737f0cf
WIP
mauriciovieira May 3, 2017
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
5 changes: 4 additions & 1 deletion .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ spiderable@1.0.14-release-testing.0
standard-minifier-css@1.3.3
standard-minifier-js@1.2.2
tap:i18n
tap:i18n-ui
tmeasday:publish-counts
todda00:friendly-slugs
tracker@1.1.2
Expand All @@ -89,3 +88,7 @@ mizzao:sharejs@=0.9.0
aramk:quill
softwarerero:accounts-t9n
raix:rssfeed
dispatch:mocha
practicalmeteor:chai
dburles:factory
practicalmeteor:mocha
10 changes: 9 additions & 1 deletion .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ accounts-ui@1.1.9
accounts-ui-unstyled@1.2.0
alanning:roles@1.2.15
aldeed:autoform@5.8.1
aldeed:browser-tests@0.1.0
aldeed:collection2@2.10.0
aldeed:collection2-core@1.2.0
aldeed:schema-deny@1.1.0
Expand Down Expand Up @@ -43,6 +44,7 @@ check@1.2.5
coffeescript@1.12.3_1
dandv:jquery-rateit@1.0.22_2
dburles:collection-helpers@1.1.0
dburles:factory@1.1.0
ddp@1.2.5
ddp-client@1.3.3
ddp-common@1.2.8
Expand All @@ -51,6 +53,7 @@ ddp-server@1.3.13
deepwell:bootstrap-datepicker2@1.3.0
deps@1.0.12
diff-sequence@1.0.7
dispatch:mocha@0.3.0
ecmascript@0.6.3
ecmascript-runtime@0.3.15
ejson@1.0.13
Expand Down Expand Up @@ -117,6 +120,11 @@ peppelg:bootstrap-3-modal@1.0.4
percolate:migrations@0.9.8
percolate:synced-cron@1.3.2
pfafman:filesaver@1.3.2
practicalmeteor:chai@2.1.0_1
practicalmeteor:loglevel@1.2.0_2
practicalmeteor:mocha@2.4.5_6
practicalmeteor:mocha-core@1.0.1
practicalmeteor:sinon@1.14.1_2
promise@0.8.8
raix:eventemitter@0.1.3
raix:handlebar-helpers@0.2.5
Expand Down Expand Up @@ -147,12 +155,12 @@ srp@1.0.10
standard-minifier-css@1.3.3
standard-minifier-js@1.2.2
tap:i18n@1.8.2
tap:i18n-ui@0.8.0
templating@1.3.0
templating-compiler@1.3.0
templating-runtime@1.3.0
templating-tools@1.1.0
tmeasday:publish-counts@0.8.0
tmeasday:test-reporter-helpers@0.2.1
todda00:friendly-slugs@0.6.0
tracker@1.1.2
trever:quill@0.0.5
Expand Down
25 changes: 19 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@ branches:
only:
- develop

services:
- docker

cache:
yarn: true
directories:
- "$HOME/.meteor"
- "$HOME/build/apinf/platform/node_modules"
- "$HOME/build/apinf/platform/.meteor/local"

before_cache:
- rm -rf $HOME/build/apinf/platform/.meteor/local/log
- rm -rf $HOME/build/apinf/platform/.meteor/local/run
- rm -rf $HOME/build/apinf/platform/.meteor/local/db

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH=$HOME/.yarn/bin:$PATH
- export PATH="$HOME/.yarn/bin:$HOME/.meteor:$PATH"
- if [ ! -e "$HOME/.meteor/meteor" ]; then curl https://install.meteor.com | sed s/--progress-bar/-sL/g | /bin/sh; fi

services:
- docker

before_script:
- yarn run lint
install:
- yarn

script:
- travis_wait 30 yarn test

after_success:
- ./.scripts/docker_build.sh
63 changes: 63 additions & 0 deletions branding/client/branding.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright 2017 Apinf Oy
This file is covered by the EUPL license.
You may obtain a copy of the licence at
https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11 */

/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback */

// Meteor packages imports
import { Template } from 'meteor/templating';
import { $ } from 'meteor/jquery';

// Collection imports
import Branding from '/branding/collection';

// Meteor contributed packages imports
import { Factory } from 'meteor/dburles:factory';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { chai } from 'meteor/practicalmeteor:chai';

// APINF imports
import withRenderedTemplate from '/tests/utils/test-helpers.js';

FlowRouter.route('/', {
name: 'hack route for tests',
action () {},
});

describe('Branding', function () {
// TODO: is there a way to register these two helpers automatically?
beforeEach(function () {
Template.registerHelper('_', key => { return key; });
});
afterEach(function () {
Template.deregisterHelper('_');
});

describe('rendering when creating a new Branding', function () {
it('should show expected fields', function () {
// Instantiate a Branding model
const branding = Factory.build('branding');
const data = {
// eslint-disable-next-line no-underscore-dangle
branding: Branding._transform(branding),
};
withRenderedTemplate('branding', data, (el) => {
chai.assert.equal($(el).find('.siteTitle').val(), branding.siteTitle);
chai.expect(sAlert.collection.findOne().message).to.equal('Test warning message...');
});
});
});

describe('autoform', function () {
// Instantiate brandingEdit autoform
// Mock the call so that the data is not going to be inserted
// Fill all fields
// Hit the button
// Check if sAlert was called
it('makes sure mocha is working multiply', function () {
chai.assert.equal(10 * 2, 20);
});
});
});
38 changes: 38 additions & 0 deletions branding/collection/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright 2017 Apinf Oy
This file is covered by the EUPL license.
You may obtain a copy of the licence at
https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11 */

// Meteor packages imports
import { Factory } from 'meteor/dburles:factory';

// Npm packages imports
import _ from 'lodash';

// Collection imports
import Branding from './';

// Define branding factory for tests and seeds
// Make sure it has the same fields as the Schema.
Factory.define('branding', Branding, {
projectLogoFileId: () => { return 'projectLogoFileId'; },
coverPhotoFileId: () => { return 'coverPhotoFileId'; },
colors: () => {},
'colors.primary': () => { return '#fff'; },
'colors.primaryText': () => { return 'black'; },
'colors.coverPhotoOverlay': () => { return 'blue'; },
'colors.overlayTransparency': () => { return _.random(0, 100); },
siteTitle: () => { return 'Site Title'; },
siteSlogan: () => { return 'Site slogan - catch phrase'; },
siteFooter: () => { return 'Copyright Site Title'; },
privacyPolicy: () => { return 'Privacy Policy'; },
termsOfUse: () => { return 'Terms of Use'; },
socialMedia: () => {
return [
{
name: ['Facebook', 'Twitter', 'Github'][_.random(0, 2)],
url: 'https://social.network/site',
},
];
},
});
37 changes: 21 additions & 16 deletions core/client/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Roles } from 'meteor/alanning:roles';

FlowRouter.wait();

Tracker.autorun(() => {
// The Roles package actually depends on a subscription.
// If the subscription is not ready the Roles.userIsInRole method will always return false.
// That is used for checking of does user have the admin role

// Make sure the roles subscription is ready & FlowRouter hasn't initialized already
if (Roles.subscription.ready() && !FlowRouter._initialized) {
// Start routing
return FlowRouter.initialize();
}
// Otherwise nothing
return undefined;
});
if (Meteor.isClient) {
FlowRouter.wait();

Tracker.autorun(() => {
// The Roles package actually depends on a subscription.
// If the subscription is not ready the Roles.userIsInRole method will always return false.
// That is used for checking of does user have the admin role

// Make sure the roles subscription is ready & FlowRouter hasn't initialized already
if (Roles.subscription.ready() && !FlowRouter._initialized) {
// Start routing
return FlowRouter.initialize();
}
// Otherwise nothing
return undefined;
});
}

// Define group for routes that require sign in
const signedIn = FlowRouter.group({
Expand Down Expand Up @@ -84,6 +86,9 @@ const requireAdminRole = function () {
FlowRouter.triggers.enter([redirectToCatalogue], { only: ['forgotPwd'] });

// Routes that require admin role
FlowRouter.triggers.enter([requireAdminRole], { only: ['settings', 'branding', 'proxies'] });
FlowRouter.triggers.enter(
[requireAdminRole],
{ only: ['settings', 'branding', 'proxies'] }
);

export default signedIn;
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@
"eslint-config-airbnb-base": "^5.0.3",
"eslint-import-resolver-meteor": "^0.3.3",
"eslint-plugin-import": "^1.14.0",
"eslint-plugin-meteor": "^4.0.0"
"eslint-plugin-meteor": "^4.0.0",
"faker": "^4.1.0",
"nightmare": "^2.10.0"
},
"scripts": {
"lint": "eslint .",
"lintfix": "eslint --fix .",
"test": "mocha ./.test/"
"pretest": "yarn run lint --silent",
"start": "meteor run",
"test-app-chrome": "meteor test --full-app --once --driver-package practicalmeteor:mocha",
"test-app-nightmare": "TEST_BROWSER_DRIVER=nightmare meteor test --full-app --once --driver-package dispatch:mocha",
"test-chrome": "meteor test --driver-package practicalmeteor:mocha",
"test-nightmare": "TEST_BROWSER_DRIVER=nightmare meteor test --once --driver-package dispatch:mocha",
"test": "yarn run test-nightmare"
},
"repository": {
"type": "git",
Expand Down
34 changes: 34 additions & 0 deletions tests/utils/test-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright 2017 Apinf Oy
This file is covered by the EUPL license.
You may obtain a copy of the licence at
https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11 */

// Meteor packages imports
import { Blaze } from 'meteor/blaze';
import { Template } from 'meteor/templating';
import { Tracker } from 'meteor/tracker';

// Npm packages imports
import _ from 'lodash';

// Helper function to dynamically create a div
function withDiv (callback) {
const el = document.createElement('div');
document.body.appendChild(el);
try {
callback(el);
} finally {
document.body.removeChild(el);
}
}

// Render a template in Blaze for testing purposes
export default function withRenderedTemplate (template, data, callback) {
withDiv((el) => {
const ourTemplate = _.isString(template) ? Template[template] : template;
console.log(ourTemplate);
Blaze.renderWithData(ourTemplate, data, el);
Tracker.flush();
callback(el);
});
}
25 changes: 1 addition & 24 deletions users/client/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,19 @@ This file is covered by the EUPL license.
You may obtain a copy of the licence at
https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11 */

// Meteor packages imports
import { Meteor } from 'meteor/meteor';

// Meteor contributed packages imports
import { Accounts } from 'meteor/accounts-base';
import { AccountsTemplates } from 'meteor/useraccounts:core';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Roles } from 'meteor/alanning:roles';
import { TAPi18n } from 'meteor/tap:i18n';
import { sAlert } from 'meteor/juliancwirko:s-alert';

// APINF imports
import signedIn from '/core/client/lib/router';

FlowRouter.route('/users', {
signedIn.route('/users', {
name: 'accountsAdmin',
triggersEnter: [
function (context, redirect) {
/*
Make sure user is authorized to access route (admin users only)
*/

// Get current User ID
const userId = Meteor.userId();

// Check if User is admin
const userIsAdmin = Roles.userIsInRole(userId, 'admin');

// If user is not an admin
if (!userIsAdmin) {
// Redirect to 'not authorized' route
redirect('/not-authorized');
}
},
],
action () {
BlazeLayout.render('masterLayout', { main: 'accountsAdmin' });
},
Expand Down
Loading