Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / invites / test / invite-validation-tests.js
blobe3c91c73bb1c365dd9b929997216f64d9bfcec42
1 'use strict';
3 var Promise = require('bluebird');
4 var assert = require('assert');
5 var StatusError = require('statuserror');
6 var inviteValidation = require('../lib/invite-validation');
7 var proxyquireNoCallThru = require('proxyquire').noCallThru();
9 describe('parseAndValidateInput', function() {
10 describe('maskEmail', function() {
11 var maskEmail = inviteValidation.maskEmail;
13 it('should mask the part to the left of the @', function() {
14 // strips the last (up to) 8 chars, replaces it will 4 *
15 assert.strictEqual(maskEmail('lerouxb@gmail.com'), '****@gmail.com');
16 assert.strictEqual(maskEmail('mike.bartlett@gmail.com'), 'mike.****@gmail.com');
17 });
19 it('should mask the part to the left of the +', function() {
20 // does the same as above, except to the left of + rather than @
21 assert.strictEqual(maskEmail('lerouxb+gitter@gmail.com'), '****@gmail.com');
22 assert.strictEqual(maskEmail('mike.bartlett+gitter@gmail.com'), 'mike.****@gmail.com');
23 });
24 });
26 describe('getAvatar', function() {
27 var getAvatar = proxyquireNoCallThru('../lib/invite-validation', {
28 // Just mock these out to make things synchronous as it just maps out to
29 // the avatars service anyway and that kind of thing should get tested in
30 // that module.
31 'gitter-web-avatars': {
32 getForGitHubUsername: function(username) {
33 return "getForGitHubUsername('" + username + "')";
35 getForGravatarEmail: function(email) {
36 return "getForGravatarEmail('" + email + "')";
38 getDefault: function() {
39 return 'getDefault()';
42 }).getAvatar;
44 it('should return an avatar for a github username', function() {
45 assert.strictEqual(getAvatar('github', 'lerouxb'), "getForGitHubUsername('lerouxb')");
46 });
48 it('should return a gravatar for an email address', function() {
49 assert.strictEqual(
50 getAvatar('twitter', '__leroux', 'lerouxb@gmail.com'),
51 "getForGravatarEmail('lerouxb@gmail.com')"
53 });
55 it("should return the default avatar if it isn't a github username or email address", function() {
56 assert.strictEqual(getAvatar('twitter', '__leroux'), 'getDefault()');
57 });
59 it('should return a gravatar for an email address', function() {
60 assert.strictEqual(
61 getAvatar('gitlab', 'MadLittleMods', 'contact@ericeastwood.com'),
62 "getForGravatarEmail('contact@ericeastwood.com')"
64 });
66 it("should return the default avatar if it isn't a github username or email address", function() {
67 assert.strictEqual(getAvatar('gitlab', 'MadLittleMods'), 'getDefault()');
68 });
69 });
71 describe('parseAndValidateInput', function() {
72 var parseAndValidateInput = inviteValidation.parseAndValidateInput;
74 describe('old method', function() {
75 it('should throw a 400 statuserror if no valid params are passed in', function() {
76 return Promise.try(function() {
77 return parseAndValidateInput({ foo: 'bar' });
79 .then(function() {
80 assert.ok(false, 'Expected error.');
82 .catch(StatusError, function(err) {
83 assert.strictEqual(err.status, 400);
84 });
85 });
87 it('should throw a 400 statuserror if a non-string value is passed in', function() {
88 return Promise.try(function() {
89 return parseAndValidateInput({ username: 1 });
91 .then(function() {
92 assert.ok(false, 'Expected error.');
94 .catch(StatusError, function(err) {
95 assert.strictEqual(err.status, 400);
96 });
97 });
99 it('should throw a 400 statuserror if multiple usernames are passed in', function() {
100 return Promise.try(function() {
101 return parseAndValidateInput({ username: 'gitter', githubUsername: 'github' });
103 .then(function() {
104 assert.ok(false, 'Expected error.');
106 .catch(StatusError, function(err) {
107 assert.strictEqual(err.status, 400);
111 it('should return type gitter if username was passed in', function() {
112 var result = parseAndValidateInput({ username: 'gitter' });
113 assert.deepStrictEqual(result, {
114 type: 'gitter',
115 externalId: 'gitter',
116 emailAddress: undefined
120 it('should return type github if githubUsername was passed in', function() {
121 var result = parseAndValidateInput({ githubUsername: 'github' });
122 assert.deepStrictEqual(result, {
123 type: 'github',
124 externalId: 'github',
125 emailAddress: undefined
129 it('should return type twitter if twitterUsername was passed in', function() {
130 var result = parseAndValidateInput({ twitterUsername: 'twitter' });
131 assert.deepStrictEqual(result, {
132 type: 'twitter',
133 externalId: 'twitter',
134 emailAddress: undefined
138 it('should return the email address if one was passed in with a username', function() {
139 var result = parseAndValidateInput({
140 twitterUsername: '__leroux',
141 email: 'lerouxb@gmail.com'
143 assert.deepStrictEqual(result, {
144 type: 'twitter',
145 externalId: '__leroux',
146 emailAddress: 'lerouxb@gmail.com'
150 it('should return type gitlab if gitlabUsername was passed in', function() {
151 var result = parseAndValidateInput({ gitlabUsername: 'MadLittleMods' });
152 assert.deepStrictEqual(result, {
153 type: 'gitlab',
154 externalId: 'MadLittleMods',
155 emailAddress: undefined
159 it('should return the email address if one was passed in with a username', function() {
160 var result = parseAndValidateInput({
161 gitlabUsername: 'MadLittleMods',
162 email: 'contact@ericeastwood.com'
164 assert.deepStrictEqual(result, {
165 type: 'gitlab',
166 externalId: 'MadLittleMods',
167 emailAddress: 'contact@ericeastwood.com'
172 describe('new method', function() {
173 it('should parse gitter invites', function() {
174 var invite = parseAndValidateInput({ type: 'gitter', externalId: 'suprememoocow' });
175 assert.deepEqual(invite, {
176 emailAddress: undefined,
177 externalId: 'suprememoocow',
178 type: 'gitter'
182 it('should parse github invites', function() {
183 var invite = parseAndValidateInput({ type: 'github', externalId: 'suprememoocow' });
184 assert.deepEqual(invite, {
185 emailAddress: undefined,
186 externalId: 'suprememoocow',
187 type: 'github'
191 it('should parse twitter invites', function() {
192 var invite = parseAndValidateInput({ type: 'twitter', externalId: 'suprememoocow' });
193 assert.deepEqual(invite, {
194 emailAddress: undefined,
195 externalId: 'suprememoocow',
196 type: 'twitter'
200 it('should parse gitlab invites', function() {
201 var invite = parseAndValidateInput({ type: 'gitlab', externalId: 'MadLittleMods' });
202 assert.deepEqual(invite, {
203 emailAddress: undefined,
204 externalId: 'MadLittleMods',
205 type: 'gitlab'
209 it('should throw a 400 statuserror if an externalId is not supplied', function() {
210 return Promise.try(function() {
211 return parseAndValidateInput({ type: 'gitter' });
213 .then(function() {
214 assert.ok(false, 'Expected error.');
216 .catch(StatusError, function(err) {
217 assert.strictEqual(err.status, 400);
221 it('should throw a 400 statuserror if an invalid type is supplied', function() {
222 return Promise.try(function() {
223 return parseAndValidateInput({ type: 'wombat', externalId: 'wombatz' });
225 .then(function() {
226 assert.ok(false, 'Expected error.');
228 .catch(StatusError, function(err) {
229 assert.strictEqual(err.status, 400);