const assert = require('assert');
const hook = require('../../src/custom/rfx-create-room-on-activate');
const sinon = require('sinon');
const Promise = require('bluebird');

describe('Hook `rfx-create-room-on-activate`', function suite() {
  before(function start() {
    return global.startService.call(this, {
      hooks: { 'users:activate': hook },
    });
  });
  after(global.clearRedis);

  it('should be able to create room after register', function test() {
    const amqpStub = sinon.stub(this.users.amqp, 'publishAndWait');
    const room = {
      id: 'ee39afd6-b99a-47d0-a43b-a942cfd5451f',
      name: 'Bar Station | Foo Scool',
      createdBy: 'foo@gmail.com',
    };

    amqpStub.withArgs('chat.internal.rooms.create')
      .returns(Promise.resolve(room));

    return this.dispatch('users.invite', {
        email: 'foo@gmail.com',
        ctx: {
          firstName: 'Foo',
          lastName: 'Bar',
        },
        metadata: {
          '*.localhost': {
            scool: 'Foo Scool',
            station: 'Bar Station',
          },
        },
      })
      .reflect()
      .then(inspectPromise())
      .then(result => {
        assert.ok(result.context.token.secret);

        return this.dispatch('users.register', {
          username: 'foo@gmail.com',
          password: '123',
          inviteToken: result.context.token.secret,
          audience: '*.localhost',
        });
      })
      .reflect()
      .then(inspectPromise())
      .then(result => {
        const stubArgs = amqpStub.args[0];
        assert.equal(stubArgs[0], 'chat.internal.rooms.create');
        assert.deepEqual(stubArgs[1], { name: room.name, createdBy: room.createdBy });

        assert.equal(result.user.username, 'foo@gmail.com');
        assert.equal(
          result.user.metadata['*.localhost'].roomId,
          'ee39afd6-b99a-47d0-a43b-a942cfd5451f'
        );

        amqpStub.restore();
      });
  });
});

@@ -1,12 +1,70 @@
+/* global inspectPromise */%0A%0Aconst _ = require('lodash');%0A
 const assert
@@ -304,76 +304,40 @@
 ore(
-function start() %7B%0A    return global.startService.call(this, %7B%0A     
+_.partial(global.startService, %7B
  hoo
@@ -370,21 +370,11 @@
 ok %7D
-,%0A    %7D);%0A 
  %7D
+)
 );%0A 
@@ -831,18 +831,16 @@
 %7B%0A      
-  
 email: '
@@ -851,26 +851,24 @@
 gmail.com',%0A
-  
       ctx: %7B
@@ -876,18 +876,16 @@
         
-  
 firstNam
@@ -902,18 +902,16 @@
         
- 
- 
 lastName
@@ -919,18 +919,16 @@
  'Bar',%0A
-  
       %7D,
@@ -934,18 +934,16 @@
 ,%0A      
-  
 metadata
@@ -954,18 +954,16 @@
         
- 
- 
 '*.local
@@ -985,12 +985,18 @@
     
-  sc
+stationSch
 ool:
@@ -1019,25 +1019,27 @@
         
- 
- 
 station
+Name
 : 'Bar S
@@ -1047,18 +1047,16 @@
 ation',%0A
-  
         
@@ -1068,24 +1068,18 @@
     
-  
 %7D,%0A    
- 
- 
 %7D)%0A
-  
     
@@ -1085,34 +1085,32 @@
  .reflect()%0A    
-  
 .then(inspectPro
@@ -1113,34 +1113,32 @@
 tPromise())%0A    
-  
 .then(result =%3E 
@@ -1135,26 +1135,24 @@
 result =%3E %7B%0A
-  
       assert
@@ -1192,18 +1192,16 @@
 %0A%0A      
-  
 return t
@@ -1241,18 +1241,16 @@
         
-  
 username
@@ -1264,26 +1264,24 @@
 gmail.com',%0A
-  
         pass
@@ -1301,18 +1301,16 @@
         
-  
 inviteTo
@@ -1351,18 +1351,16 @@
         
-  
 audience
@@ -1386,18 +1386,16 @@
     
-  
 %7D);%0A    
   %7D)
@@ -1390,23 +1390,19 @@
 %7D);%0A    
- 
- 
 %7D)%0A
-  
     .ref
@@ -1408,18 +1408,16 @@
 flect()%0A
-  
     .the
@@ -1440,18 +1440,16 @@
 ())%0A    
- 
- 
 .then(re
@@ -1454,26 +1454,24 @@
 result =%3E %7B%0A
-  
       const 
@@ -1497,34 +1497,32 @@
 .args%5B0%5D;%0A      
-  
 assert.equal(stu
@@ -1564,26 +1564,24 @@
 te');%0A      
- 
- 
 assert.deepE
@@ -1650,26 +1650,24 @@
  %7D);%0A%0A      
-  
 assert.equal
@@ -1709,26 +1709,24 @@
 om');%0A      
-  
 assert.equal
@@ -1735,18 +1735,16 @@
         
- 
- 
 result.u
@@ -1787,18 +1787,16 @@
         
-  
 'ee39afd
@@ -1836,16 +1836,12 @@
     
- 
- 
 );%0A%0A
-  
     
@@ -1862,18 +1862,36 @@
 tore();%0A
+%0A
   
+    return null;%0A
     %7D);%0A

fix rfx hook