All files / app/routes/api/user/invite/get-all get-all-invites.integration.spec.ts

100% Statements 67/67
100% Branches 0/0
100% Functions 14/14
100% Lines 65/65

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  1x 1x   1x 1x 1x   1x   1x         1x 5x 5x   5x   5x 5x 5x     1x 1x 1x     1x               1x 1x 1x     1x           1x   1x 1x 5x           5x           5x         5x     1x           1x 1x   1x 1x 15x     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-explicit-any */
import db, {syncPromise} from '../../../../../db';
import {TestUtils} from '../../../../../util/test-utils.spec';
import request from 'supertest';
import {expect, assert} from 'chai';
import config from '../../../../../config';
import {User, Group, Invite} from '../../../../../models';
 
const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
 
describe('get /api/user/invite', function() {
  let agent: request.SuperTest<request.Test>;
  let user: any;
  let csrf: string;
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
 
    agent = response.agent;
    user = response.user;
    csrf = response.csrf;
  });
 
  describe('if not logged in', function() {
    it('responses with 401', async function() {
      await agent.put('/auth/logout')
          .set(csrfHeaderName, csrf)
          .send();
      return agent
          .get('/api/user/invite')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(401);
    });
  });
 
  describe('if logged in', function() {
    it('responses with empty list if user has no invites', function() {
      return agent.get('/api/user/invite')
          .expect(200)
          .then((response) => {
            expect(response.body).to.eql({
              invites: [],
            });
          });
    });
 
    it('responses with correct invites', async function() {
      // Creates users and groups for which the invites can be generated
      const groupIds = [];
      for (let i = 0; i < 5; i++) {
        const testUser = await User.create({
          username: `test-user-${i}-name`,
          password: `test-user-${i}-pass`,
          email: `test-user-${i}-email@mail.com`,
        });
 
        const group = await Group.create({
          name: `test-group-${i}-name`,
          description: `test-group-${i}-desc`,
          ownerId: testUser.id,
        });
 
        await Invite.create({
          userId: user.id,
          groupId: group.id,
        });
 
        groupIds.push(group.id);
      }
 
      const response = await agent.get('/api/user/invite')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(200);
 
      // Check if all invites exists
      expect(response.body.invites).to.exist.and.to.be.an('array');
      expect(response.body.invites).to.have.length(groupIds.length);
 
      const invites = response.body.invites as Array<any>;
      groupIds.forEach((id) => {
        assert(invites.some((invite) => invite.Group.id === id));
      });
 
      invites.every((invite) => {
        expect(invite.userId).to.equal(user.id);
      });
    });
 
    it('only responses with invites of logged in user', async function() {
      // Create group an invite for logged in user
      const owner = await User.create({
        username: `owner-name`,
        password: `owner-pass`,
        email: `owner-email@mail.com`,
      });
      const group = await Group.create({
        name: `test-group-name`,
        description: `test-group-desc`,
        ownerId: owner.id,
      });
      await Invite.create({
        userId: user.id,
        groupId: group.id,
      });
 
      // Create other user and invite for him/her
      const otherUser = await User.create({
        username: `other-name`,
        password: `other-pass`,
        email: `other-email@mail.com`,
      });
      await Invite.create({
        userId: otherUser.id,
        groupId: group.id,
      });
 
      const response = await agent.get('/api/user/invite')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(200);
 
      // Check if all invites exists
      expect(response.body.invites).to.exist.and.to.be.an('array');
      expect(response.body.invites).to.have.length(1);
 
      const invites = response.body.invites as Array<any>;
 
      invites.every((invite) => {
        expect(invite.userId).to.equal(user.id);
      });
    });
 
    it('only provides detailed information about ' +
    'group and invite sender', async function() {
      // Create group an invite for logged in user
      const owner = await User.create({
        username: `owner-name`,
        password: `owner-pass`,
        email: `owner-email@mail.com`,
      });
      const group = await Group.create({
        name: `test-group-name`,
        description: `test-group-desc`,
        ownerId: owner.id,
      });
      await Invite.create({
        userId: user.id,
        groupId: group.id,
        invitedBy: owner.id,
      });
 
      const response = await agent.get('/api/user/invite')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(200);
 
      // Check if all invites exists
      expect(response.body.invites).to.exist.and.to.be.an('array');
      expect(response.body.invites).to.have.length(1);
 
      const invite = (response.body.invites as Array<any>)[0];
 
      expect(invite.Group).to.exist;
      expect(invite.Group.Owner).to.exist;
      expect(owner).to.include(invite.Group.Owner);
      expect(invite.Group.id).to.be.equal(group.id);
      expect(invite.Group.name).to.be.equal(group.name);
      expect(invite.Group.description).to.be.equal((group as any).description);
 
      expect(invite.InviteSender).to.exist;
      expect(owner).to.include(invite.InviteSender);
    });
  });
});