3 var invitesService = require('../lib/invites-service');
4 var ObjectID = require('mongodb').ObjectID;
5 var assert = require('assert');
6 var StatusError = require('statuserror');
7 var TroupeInvite = require('gitter-web-persistence').TroupeInvite;
8 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
10 describe('invite-service', function() {
11 const fixture = fixtureLoader.setupEach({
18 describe('integration tests #slow', function() {
19 describe('createInvite', function() {
20 it('should create an invite', function() {
21 var roomId = new ObjectID();
24 .createInvite(roomId, {
26 externalId: 'gitterawesome',
27 invitedByUserId: fixture.userInvitedBy1.id,
28 emailAddress: 'test@gitter.im'
30 .then(function(invite) {
31 assert.strictEqual(invite.state, 'PENDING');
32 assert.strictEqual(String(invite.invitedByUserId), String(fixture.userInvitedBy1.id));
33 assert.strictEqual(String(invite.troupeId), String(roomId));
34 assert.strictEqual(invite.userId, null);
35 assert.strictEqual(invite.emailAddress, 'test@gitter.im');
36 assert.strictEqual(invite.externalId, 'gitterawesome');
37 assert.strictEqual(invite.type, 'github');
41 it('should not allow duplicate invites', function() {
42 var roomId = new ObjectID();
45 .createInvite(roomId, {
47 externalId: 'gitterawesome',
48 invitedByUserId: fixture.userInvitedBy1.id,
49 emailAddress: 'test@gitter.im'
52 return invitesService.createInvite(roomId, {
54 externalId: 'gitterawesome',
55 invitedByUserId: fixture.userInvitedBy1.id,
56 emailAddress: 'test@gitter.im'
60 assert.ok(false, 'Expected exception');
62 .catch(StatusError, function(err) {
63 assert.strictEqual(err.status, 409);
67 it('should not allow invites from hellbaned users', async () => {
68 var roomId = new ObjectID();
70 await invitesService.createInvite(roomId, {
72 externalId: 'gitterawesome',
73 invitedByUserId: fixture.userHellbanned1.id,
74 emailAddress: 'test@gitter.im'
77 const invitesInRoom = await TroupeInvite.find({
86 `Expected no invites in room but found ${
88 }\ninvitesInRoom=${JSON.stringify(invitesInRoom, null, 2)}`
93 describe('create-accept-complete flow', function() {
94 it('should accept an invite', function() {
95 var roomId = new ObjectID();
96 var userId = new ObjectID();
97 var userId2 = new ObjectID();
100 .createInvite(roomId, {
102 externalId: 'gitterawesome',
103 invitedByUserId: fixture.userInvitedBy1.id,
104 emailAddress: 'test@gitter.im'
109 .then(function(invite) {
110 this.invite = invite;
111 return invitesService.accept(userId, invite.secret);
113 .then(function(invite) {
114 assert.strictEqual(String(invite._id), String(this.invite._id));
115 return invitesService.markInviteAccepted(invite._id, userId);
118 // Attempt to reuse the invite, same user
119 return invitesService.accept(userId, this.invite.secret);
121 .then(function(invite) {
122 assert.strictEqual(String(invite._id), String(this.invite._id));
123 return invitesService
124 .accept(userId2, this.invite.secret)
126 assert.ok(false, 'Expected exception');
128 .catch(StatusError, function(err) {
129 assert.strictEqual(err.status, 404);
133 return TroupeInvite.findById(this.invite._id);
135 .then(function(invite) {
136 assert.strictEqual(invite.state, 'ACCEPTED');
137 assert.strictEqual(String(invite.userId), String(userId));
143 describe('findInvitesForReminder #slow', function() {
144 var fixture = fixtureLoader.setup({
149 it('should return invites', function() {
150 return invitesService
151 .createInvite(fixture.troupe1._id, {
153 emailAddress: fixtureLoader.generateEmail(),
154 invitedByUserId: fixture.user1._id
159 .then(function(invite) {
160 this.inviteId = invite._id;
161 return invitesService.findInvitesForReminder(-1);
163 .then(function(invites) {
164 assert(invites.length > 1);
165 var inviteId = this.inviteId;
166 var originalInvite = invites.filter(function(f) {
167 return String(inviteId) === String(f.invite._id);
170 assert(originalInvite);
171 assert(originalInvite.invite);
172 assert(originalInvite.invitedByUser);
173 assert(originalInvite.troupe);
175 assert.strictEqual(String(originalInvite.invite._id), String(inviteId));
176 assert.strictEqual(String(originalInvite.invitedByUser._id), String(fixture.user1._id));
177 assert.strictEqual(String(originalInvite.troupe._id), String(fixture.troupe1._id));