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

[NEW] Start running unit tests #6605

Merged
merged 20 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9e47952
first test
ggazzo Mar 30, 2017
58399a3
Update chimp-config.js
ggazzo Apr 5, 2017
8305dbd
Update package.json
rodrigok Apr 5, 2017
810c03b
phase 2
ggazzo Apr 6, 2017
bf67081
Merge branch 'develop' into unit-test-mentions
engelgabriel Apr 7, 2017
2191c0b
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo Apr 10, 2017
2cd0751
ajust pattern
ggazzo Apr 10, 2017
328920e
Merge branch 'unit-test-mentions' of https://github.com/ggazzo/Rocket…
ggazzo Apr 10, 2017
6f4206c
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 10, 2017
0448acb
fix Sam's review
ggazzo May 10, 2017
cdad257
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 10, 2017
d0c6c92
updated travis
ggazzo May 12, 2017
77c766e
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 10, 2017
ce26020
Merge branch 'develop' into unit-test-mentions
ggazzo May 16, 2017
6e765fa
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 17, 2017
304aa56
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 17, 2017
08f0ec0
Merge branch 'unit-test-mentions' of https://github.com/RocketChat/Ro…
ggazzo May 17, 2017
c8849da
Merge branch 'unit-test-mentions' of https://github.com/ggazzo/Rocket…
ggazzo May 17, 2017
c3d3e27
deleted babelrc
ggazzo May 17, 2017
53f3d82
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
ggazzo May 17, 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ before_script:
- |-
mongo --eval 'rs.initiate({_id:"rs0", members: [{"_id":1, "host":"localhost:27017"}]})'
- meteor npm run lint
- meteor npm run testunit
- meteor npm run stylelint
- travis_retry meteor build --headless /tmp/build
- mkdir /tmp/build-test
Expand Down
3 changes: 3 additions & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--compilers js:babel-mocha-es6-compiler
--reporter spec
--ui bdd
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
"name": "Sing Li",
"email": "[email protected]"
}],
"mocha": {
"tests": [
"packages/**/*.tests.js"
],
"files": [
"packages/**/*.js",
"!packages/**/*.tests.js"
]
},
"keywords": [
"rocketchat",
"rocket",
Expand All @@ -43,6 +52,9 @@
"chimp-watch": "chimp --ddp=http://localhost:3000 --watch --mocha --path=tests/end-to-end",
"chimp-test": "chimp tests/chimp-config.js",
"postinstall": "cd packages/rocketchat-katex && npm i",
"testunit-watch": "mocha --watch --opts ./mocha.opts \"`node -e \"console.log(require('./package.json').mocha.tests.join(' '))\"`\"",
"coverage": "nyc -r html mocha --opts ./mocha.opts \"`node -e \"console.log(require('./package.json').mocha.tests.join(' '))\"`\"",
"testunit": "mocha --opts ./mocha.opts \"`node -e \"console.log(require('./package.json').mocha.tests.join(' '))\"`\"",
"version": "node .scripts/version.js",
"set-version": "node .scripts/set-version.js",
"release": "npm run set-version --silent"
Expand All @@ -57,11 +69,13 @@
"email": "[email protected]"
},
"devDependencies": {
"babel-mocha-es6-compiler": "^0.1.0",
"babel-plugin-array-includes": "^2.0.3",
"chimp": "^0.49.0",
"conventional-changelog": "^1.1.3",
"eslint": "^3.19.0",
"stylelint": "^7.10.1",
"supertest": "^3.0.0",
"conventional-changelog": "^1.1.3"
"supertest": "^3.0.0"
},
"dependencies": {
"babel-runtime": "^6.23.0",
Expand Down
65 changes: 65 additions & 0 deletions packages/rocketchat-mentions/Mentions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import _ from 'underscore';
export default class {
constructor({pattern, me}) {
this.pattern = pattern;
this.me = me;
}
set me(m) {
this._me = m;
}
get me() {
return typeof this._me === 'function' ? this._me() : this._me;
}
set pattern(p) {
this._pattern = p;
}
get pattern() {
return typeof this._pattern === 'function' ? this._pattern() : this._pattern;
}
get userMentionRegex() {
return new RegExp(`@(${ this.pattern })`, 'gm');
}
get channelMentionRegex() {
return new RegExp(`#(${ this.pattern })`, 'gm');
}
replaceUsers(str, message, me) {
return str.replace(this.userMentionRegex, (match, username) => {
if (['all', 'here'].includes(username)) {
return `<a class="mention-link mention-link-me mention-link-all background-attention-color">${ match }</a>`;
}

if (message.temp == null && _.findWhere(message.mentions, {username}) == null) {
return match;
}
return `<a class="mention-link ${ username === me ? 'mention-link-me background-primary-action-color':'' }" data-username="${ username }">${ match }</a>`;
});
}
replaceChannels(str, message) {
return str.replace(this.channelMentionRegex, (match, name) => {
if (message.temp == null && _.findWhere(message.channels, {name}) == null) {
return match;
}
return `<a class="mention-link" data-channel="${ name }">${ match }</a>`;
});
}
getUserMentions(str) {
return str.match(this.userMentionRegex) || [];
}
getChannelMentions(str) {
return str.match(this.channelMentionRegex) || [];
}
parse(message) {
let msg = (message && message.html) || '';
if (!msg.trim()) {
return message;
}
msg = this.replaceUsers(msg, message, this.me);
msg = this.replaceChannels(msg, message, this.me);
message.html = msg;
return message;
}
}
76 changes: 76 additions & 0 deletions packages/rocketchat-mentions/MentionsServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import Mentions from './Mentions';
export default class MentionsServer extends Mentions {
constructor(args) {
super(args);
this.messageMaxAll = args.messageMaxAll;
this.getChannel = args.getChannel;
this.getChannels = args.getChannels;
this.getUsers = args.getUsers;
}
set getUsers(m) {
this._getUsers = m;
}
get getUsers() {
return typeof this._getUsers === 'function' ? this._getUsers : () => this._getUsers;
}
set getChannels(m) {
this._getChannels = m;
}
get getChannels() {
return typeof this._getChannels === 'function' ? this._getChannels : () => this._getChannels;
}
set getChannel(m) {
this._getChannel = m;
}
get getChannel() {
return typeof this._getChannel === 'function' ? this._getChannel : () => this._getChannel;
}
set messageMaxAll(m) {
this._messageMaxAll = m;
}
get messageMaxAll() {
return typeof this._messageMaxAll === 'function' ? this._messageMaxAll() : this._messageMaxAll;
}
getUsersByMentions({msg, rid}) {
let mentions = this.getUserMentions(msg);
const mentionsAll = [];
const userMentions = [];

mentions.forEach((m) => {
const mention = m.trim().substr(1);
if (mention !== 'all' && mention !== 'here') {
return userMentions.push(mention);
}
if (mention === 'all') {
const messageMaxAll = this.messageMaxAll;
const allChannel = this.getChannel(rid);
if (messageMaxAll !== 0 && allChannel.usernames.length >= messageMaxAll) {
return;
}
}
mentionsAll.push({
_id: mention,
username: mention
});
});
mentions = userMentions.length ? this.getUsers(userMentions) : [];
return [...mentionsAll, ...mentions];
}
getChannelbyMentions({msg}) {
const channels = this.getChannelMentions(msg);
return this.getChannels(channels.map(c => c.trim().substr(1)));
}
execute(message) {
const mentionsAll = this.getUsersByMentions(message);
const channels = this.getChannelbyMentions(message);

message.mentions = mentionsAll;

message.channels = channels;
return message;
}
}
51 changes: 11 additions & 40 deletions packages/rocketchat-mentions/client.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/

function MentionsClient(message) {
let msg = (message && message.html) || '';
if (!msg.trim()) {
return message;
import Mentions from './Mentions';
const MentionsClient = new Mentions({
pattern() {
return RocketChat.settings.get('UTF8_Names_Validation');
},
me() {
const me = Meteor.user();
return me && me.username;
}
const msgMentionRegex = new RegExp(`(?:^|\\s|\\n)(@(${ RocketChat.settings.get('UTF8_Names_Validation') }):?)[:.,\s]?`, 'g');

let me = Meteor.user();
me = me ? me.username : null;

msg = msg.replace(msgMentionRegex, function(match, mention, username) {
if (['all', 'here'].includes(username)) {
return match.replace(mention, `<a class="mention-link mention-link-me mention-link-all background-attention-color">${ mention }</a>`);
}
if (message.temp == null && _.findWhere(message.mentions, {username}) == null) {
return match;
}
return match.replace(mention, `<a class="mention-link ${ username === me ? 'mention-link-me background-primary-action-color':'' }" data-username="${ username }">${ mention }</a>`);
});

const msgChannelRegex = new RegExp(`(?:^|\\s|\\n)(#(${ RocketChat.settings.get('UTF8_Names_Validation') }))[:.,\s]?`, 'g');

msg = msg.replace(msgChannelRegex, function(match, mention, name) {
if (message.temp == null && _.findWhere(message.channels, {name}) == null) {
return match;
}
return match.replace(mention, `<a class="mention-link" data-channel="${ name }">${ mention }</a>`);
});
message.html = msg;
return message;
}


RocketChat.callbacks.add('renderMessage', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-message');

RocketChat.callbacks.add('renderMentions', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-mentions');
});
RocketChat.callbacks.add('renderMessage', (message) => MentionsClient.parse(message), RocketChat.callbacks.priority.MEDIUM, 'mentions-message');
RocketChat.callbacks.add('renderMentions', (message) => MentionsClient.parse(message), RocketChat.callbacks.priority.MEDIUM, 'mentions-mentions');
4 changes: 3 additions & 1 deletion packages/rocketchat-mentions/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'ecmascript',
'rocketchat:lib'
'rocketchat:lib',
'underscore'
]);

api.addFiles('server.js', 'server');
api.addFiles('client.js', 'client');
// api.('mentions.js', 'client');
});
58 changes: 9 additions & 49 deletions packages/rocketchat-mentions/server.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/

function MentionsServer(message) {
const msgMentionRegex = new RegExp(`(?:^|\\s|\\n)(?:@)(${ RocketChat.settings.get('UTF8_Names_Validation') })`, 'g');
const mentionsAll = [];
const userMentions = [];
let mentions = message.msg.match(msgMentionRegex);
if (mentions) {
mentions.forEach((m) => {
const mention = m.trim().substr(1);
if (mention !== 'all' && mention !== 'here') {
return userMentions.push(mention);
}
if (mention === 'all') {
const messageMaxAll = RocketChat.settings.get('Message_MaxAll');
const allChannel = RocketChat.models.Rooms.findOneById(message.rid);
if (messageMaxAll !== 0 && allChannel.usernames.length >= messageMaxAll) {
return;
}
}
mentionsAll.push({
_id: mention,
username: mention
});
});
mentions = userMentions.length ? Meteor.users.find({ username: {$in: _.unique(userMentions)}}, { fields: {_id: true, username: true }}).fetch() : [];

const verifiedMentions = [...mentionsAll, ...mentions];
if (verifiedMentions.length !== 0) {
message.mentions = verifiedMentions;
}
}

const msgChannelRegex = new RegExp(`(?:^|\\s|\\n)(?:#)(${ RocketChat.settings.get('UTF8_Names_Validation') })`, 'g');
let channels = message.msg.match(msgChannelRegex);
if (channels) {
channels = channels.map(c => c.trim().substr(1));
const verifiedChannels = RocketChat.models.Rooms.find({ name: {$in: _.unique(channels)}, t: 'c' }, { fields: {_id: 1, name: 1 }}).fetch();
if (verifiedChannels.length !== 0) {
message.channels = verifiedChannels;
}
}
return message;
}

RocketChat.callbacks.add('beforeSaveMessage', MentionsServer, RocketChat.callbacks.priority.HIGH, 'mentions');
import MentionsServer from './MentionsServer';
const mention = new MentionsServer({
pattern: () => RocketChat.settings.get('UTF8_Names_Validation'),
messageMaxAll: () => RocketChat.settings.get('Message_MaxAll'),
getUsers: (usernames) => Meteor.users.find({ username: {$in: _.unique(usernames)}}, { fields: {_id: true, username: true }}).fetch(),
getChannel: (rid) => RocketChat.models.Rooms.findOneById(rid),
getChannels: (channels) => RocketChat.models.Rooms.find({ name: {$in: _.unique(channels)}, t: 'c' }, { fields: {_id: 1, name: 1 }}).fetch()
});
RocketChat.callbacks.add('beforeSaveMessage', (message) => mention.execute(message), RocketChat.callbacks.priority.HIGH, 'mentions');
Loading