All files / app/models/group group-notification-service.integration.spec.ts

80.43% Statements 74/92
100% Branches 0/0
85% Functions 17/20
80.43% Lines 74/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195    1x     1x 1x 1x 1x 1x 1x     1x 1x                 1x 1x 1x 1x 1x     1x 1x 1x     1x 4x 4x   4x 4x 4x 4x 4x     1x 1x   1x 1x 1x       1x   1x   1x         1x           1x 1x 1x 1x 1x 1x                       1x   1x 1x 1x           1x         1x           1x 1x 1x 1x 1x 1x                       1x   1x 1x 1x           1x 1x 1x 1x 1x 1x                         1x   1x 1x 1x         1x     1x 1x 1x     1x         1x         1x         1x                  
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {expect} from 'chai';
import {Server} from 'socket.io';
import supertest from 'supertest';
import config from '../../config';
import db, {syncPromise} from '../../db';
import {NotLoggedInError, NotMemberOfGroupError} from '../../errors';
import {TestUtils} from '../../util/test-utils.spec';
import {User} from '../user';
import Group from './group';
import http from 'http';
 
describe('GroupNotificationService', function() {
  const csrfName = config.jwt.securityOptions.tokenName.toLowerCase();
  let io: Server;
  let port: number;
  let csrf: string;
  let user: any;
  let agent: supertest.SuperTest<supertest.Test>;
  let jwtValue: string;
  let server: http.Server;
 
  before(async function() {
    const socketIo = await TestUtils.startSocketIo();
    io = socketIo.io;
    port = socketIo.port;
    server = socketIo.server;
  });
 
  after(function() {
    io.close();
    server.close();
  });
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
    agent = response.agent;
    csrf = response.csrf;
    user = response.user;
    jwtValue = response.jwtValue;
  });
 
  describe('can\'t connect to group namespace', function() {
    it('emits error event with NotLoggedInError if user ' +
    'is not logged in', function() {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await agent.put('/auth/logout')
              .set(csrfName, csrf)
              .send()
              .expect(204);
          const loggedOutJwtValue = TestUtils
              .extractJwtValue(res.header['set-cookie']);
          expect(loggedOutJwtValue).not.to.be.undefined;
 
          const group = await Group.create({
            name: 'group',
            ownerId: user.id,
          });
 
          const socket = TestUtils.createSocket(
              port,
              `/group/${group.id}`,
              loggedOutJwtValue,
          );
 
          socket.on('error', (e: any) => {
            try {
              expect(e).to.be.a('string');
              expect(e).to.equal(new NotLoggedInError().message);
              socket.close();
              resolve(true);
            } catch (e) {
              socket.close();
              reject(e);
            }
          });
        } catch (e) {
          reject(e);
        }
      });
    });
 
    it('and emits error event with NotMemberOfGroupError ' +
    'if user is logged in but not a member of the group', function() {
      return new Promise(async (resolve, reject) => {
        try {
          const owner = await User.create({
            username: 'owner',
            email: 'owner@mail.com',
            password: 'owner-password',
          });
 
          const group = await Group.create({
            name: 'group',
            ownerId: owner.id,
          });
 
          const socket = TestUtils.createSocket(
              port,
              `/group/${group.id}`,
              jwtValue,
          );
 
          socket.on('error', (e: any) => {
            try {
              expect(e).to.be.a('string');
              expect(e).to.equal(new NotMemberOfGroupError().message);
              socket.close();
              resolve(true);
            } catch (e) {
              socket.close();
              reject(e);
            }
          });
        } catch (e) {
          reject(e);
        }
      });
    });
 
    it('emits error event with NotMemberOfGroupError ' +
    'if user is logged in group doesn\'t exist', function() {
      return new Promise(async (resolve, reject) => {
        try {
          const socket = TestUtils.createSocket(
              port,
              '/group/5',
              jwtValue,
          );
 
          socket.on('error', (e: any) => {
            try {
              expect(e).to.be.a('string');
              expect(e).to.equal(new NotMemberOfGroupError().message);
              socket.close();
              resolve(true);
            } catch (e) {
              socket.close();
              reject(e);
            }
          });
        } catch (e) {
          reject(e);
        }
      });
    });
  });
 
  it('can connect to namespace if user is logged in ' +
  'and is member of group', function() {
    return new Promise(async (resolve, reject) => {
      try {
        const group = await Group.create({
          name: 'group',
          ownerId: user.id,
        });
 
        const socket = TestUtils
            .createSocket(port, `/group/${group.id}`, jwtValue);
 
        socket.on('connect', () => {
          socket.close();
          resolve(true);
        });
 
        socket.on('connect_error', (e: any) => {
          socket.close();
          reject(e);
        });
 
        socket.on('error', (e: any) => {
          socket.close();
          reject(e);
        });
 
        socket.on('connect_timeout', (e: any) => {
          socket.close();
          reject(e);
        });
 
        socket.open();
      } catch (e) {
        io.close();
        server.close();
        reject(e);
      }
    });
  });
});