3 var proxyquireNoCallThru = require('proxyquire').noCallThru();
4 var assert = require('assert');
5 var mockito = require('jsmockito').JsMockito;
6 var Promise = require('bluebird');
8 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
10 describe('room-context-service', function() {
11 var createPolicyForRoom, createPolicyForOneToOne, roomContextService, access;
13 var fixture = fixtureLoader.setup({
17 users: ['user1', 'user2']
22 beforeEach(function() {
23 createPolicyForRoom = mockito.mockFunction();
24 createPolicyForOneToOne = mockito.mockFunction();
28 .when(createPolicyForRoom)()
30 return Promise.resolve({
32 return Promise.resolve(access);
38 .when(createPolicyForOneToOne)()
40 return Promise.resolve({
42 return Promise.resolve(access);
45 return Promise.resolve(access);
50 roomContextService = proxyquireNoCallThru('../lib/room-context-service', {
51 'gitter-web-permissions/lib/policy-factory': {
52 createPolicyForRoom: createPolicyForRoom,
53 createPolicyForOneToOne: createPolicyForOneToOne
58 it('should generate context for non-members', function() {
60 return roomContextService
61 .findContextForUri(fixture.user1, fixture.troupe2.uri, {})
62 .then(function(roomContext) {
63 assert(!roomContext.roomMember);
67 it('should generate context for members', function() {
69 return roomContextService
70 .findContextForUri(fixture.user1, fixture.troupe1.uri, {})
71 .then(function(roomContext) {
72 assert(roomContext.roomMember);
76 it('should throw for users without access to the room', function() {
79 return roomContextService
80 .findContextForUri(fixture.user1, fixture.troupe2.uri, {})
81 .then(function(/*roomContext*/) {})
82 .catch(function(err) {
83 assert.strictEqual(err.status, 404);
87 it('should generate context for 1:1', function() {
90 return roomContextService
91 .findContextForUri(fixture.user1, fixture.user2.username, {})
92 .then(function(roomContext) {
93 assert(roomContext.roomMember);
97 it('should throw a redirect for 1:1 same user', function() {
98 return roomContextService
99 .findContextForUri(fixture.user1, fixture.user1.username, {})
100 .then(function(roomContext) {
101 assert.strictEqual(roomContext.ownUrl, true);
102 assert.strictEqual(roomContext.uri, fixture.user1.username);
106 it('should be logged in to see a 1:1', function() {
107 return roomContextService.findContextForUri(null, fixture.user1.username, {}).then(
108 function(/*roomContext*/) {
112 assert.strictEqual(err.status, 401);