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');
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');
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
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()';
44 it('should return an avatar for a github username', function() {
45 assert
.strictEqual(getAvatar('github', 'lerouxb'), "getForGitHubUsername('lerouxb')");
48 it('should return a gravatar for an email address', function() {
50 getAvatar('twitter', '__leroux', 'lerouxb@gmail.com'),
51 "getForGravatarEmail('lerouxb@gmail.com')"
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()');
59 it('should return a gravatar for an email address', function() {
61 getAvatar('gitlab', 'MadLittleMods', 'contact@ericeastwood.com'),
62 "getForGravatarEmail('contact@ericeastwood.com')"
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()');
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' });
80 assert
.ok(false, 'Expected error.');
82 .catch(StatusError
, function(err
) {
83 assert
.strictEqual(err
.status
, 400);
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 });
92 assert
.ok(false, 'Expected error.');
94 .catch(StatusError
, function(err
) {
95 assert
.strictEqual(err
.status
, 400);
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' });
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
, {
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
, {
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
, {
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
, {
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
, {
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
, {
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',
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',
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',
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',
209 it('should throw a 400 statuserror if an externalId is not supplied', function() {
210 return Promise
.try(function() {
211 return parseAndValidateInput({ type
: 'gitter' });
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' });
226 assert
.ok(false, 'Expected error.');
228 .catch(StatusError
, function(err
) {
229 assert
.strictEqual(err
.status
, 400);