All files / app/routes/api/group/get get-groups.integration.spec.ts

100% Statements 41/41
100% Branches 0/0
100% Functions 9/9
100% Lines 37/37

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  1x 1x 1x 1x 1x 1x 1x   1x 1x         1x 2x 2x   2x   2x 2x 2x     1x 1x 1x               1x 1x 1x   1x   1x 5x           5x         1x           1x           1x     1x   1x   1x 1x   1x 5x   15x     5x        
/* eslint-disable @typescript-eslint/no-explicit-any */
import db, {syncPromise} from '../../../../db';
import {TestUtils} from '../../../../util/test-utils.spec';
import supertest from 'supertest';
import config from '../../../../config';
import app from '../../../../app';
import {Group, User} from '../../../../models';
import {expect} from 'chai';
 
describe('get /api/group', function() {
  const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
  let agent: supertest.SuperTest<supertest.Test>;
  let csrf: string;
  let user: any;
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
 
    agent = response.agent;
    csrf = response.csrf;
    user = response.user;
  });
 
  describe('if user not logged in', function() {
    it('responses with UnauthorizedError', function() {
      return supertest(app)
          .get('/api/group')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(401);
    });
  });
 
  describe('if user logged in', function() {
    it('responses with list of groups', async function() {
      const N = 5;
 
      const correctIds = [];
      // Create groups and memberships for user
      for (let i = 0; i < N; i++) {
        const group = await Group.create({
          name: 'test_group_1',
          description: 'test_group_1',
          ownerId: user.id,
        });
 
        correctIds.push(group.id);
      }
 
      // Create another user and another group
      // to check if only groups of currently logged in user are returned
      const otherUser = await User.create({
        username: 'other',
        password: 'other_password',
        email: 'other@mail.com',
      });
 
      const otherGroup = await Group.create({
        name: 'other_group',
        description: 'other_group',
        ownerId: otherUser.id,
      });
 
      const response = await agent.get('/api/group')
          .set(csrfHeaderName, csrf)
          .send()
          .then((res) => res.body);
 
      expect(response.groups).to.be.an('array');
 
      const groups = response.groups as any[];
      expect(groups).to.have.length(N);
 
      for (let i = 0; i < N; i++) {
        const id = correctIds[i];
 
        expect(groups.some((g) => g.id === id)).to.be.true;
      }
 
      expect(groups.every((g) => g.id !== otherGroup.id)).to.be.true;
    });
  });
});