All files / app/routes/api/group/group-id/member/get get-members-of-group.integration.spec.ts

100% Statements 44/44
100% Branches 0/0
100% Functions 12/12
100% Lines 43/43

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    1x 1x 1x 1x 1x 1x 1x   1x 1x         1x 3x 3x   3x 3x 3x 3x     1x 1x 1x               1x 1x 1x 1x           1x         1x   1x             1x                 1x 5x             5x           5x                   1x           1x       1x   1x 1x 1x 6x 21x   6x 6x 6x 6x          
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {expect} from 'chai';
import supertest from 'supertest';
import app from '../../../../../../app';
import config from '../../../../../../config';
import db, {syncPromise} from '../../../../../../db';
import {Group, Membership, User} from '../../../../../../models';
import {TestUtils} from '../../../../../../util/test-utils.spec';
 
describe('get /api/group/:groupId/member', function() {
  const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
  let agent: supertest.SuperTest<supertest.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;
    csrf = response.csrf;
    user = response.user;
  });
 
  describe('if user is not logged in', function() {
    it('responses with 401', function() {
      return supertest(app)
          .get('/api/group/1/member')
          .set(csrfHeaderName, csrf)
          .send()
          .expect(401);
    });
  });
 
  describe('if user is logged in', function() {
    describe('throws BadRequestError(400) if', function() {
      it('groupId is not numeric', function() {
        return agent
            .get('/api/group/test/member')
            .set(csrfHeaderName, csrf)
            .send()
            .expect(400)
            .then((res) => {
              expect(res.body.message).to.contain('groupId has to be a number');
            });
      });
    });
 
    it('responses with list of members', async function() {
      // Create group
      const group = await Group.create({
        name: 'group',
        description: 'description',
        ownerId: user.id,
      });
 
      // Add a few members
      const expectedMembers = [{
        isAdmin: true,
        groupId: group.id,
        userId: user.id,
        User: {
          id: user.id,
          username: user.username,
        },
      }];
      for (let i = 0; i < 5; i++) {
        const newUser = await User.create({
          username: `test-user-${i}`,
          email: `test-user-${i}@mail.com`,
          password: 'password',
        });
 
        // Add new user as member of group
        const member = await Membership.create({
          userId: newUser.id,
          groupId: group.id,
          isAdmin: i % 2 === 0,
        });
 
        expectedMembers.push({
          ...(member.get({plain: true}) as Membership),
          User: {
            id: newUser.id,
            username: newUser.username,
          },
        });
      }
 
      // Create other group
      await Group.create({
        name: 'other',
        description: 'other',
        ownerId: user.id,
      });
 
      const response = await agent
          .get(`/api/group/${group.id}/member`)
          .set(csrfHeaderName, csrf)
          .send()
          .then((res) => res.body);
 
      expect(response.members).to.be.an('array');
      expect(response.members).to.have.length(expectedMembers.length);
      response.members.forEach((member: any) => {
        const expectedMember = expectedMembers
            .find((el) => el.userId === member.userId);
 
        expect(expectedMember).to.exist;
        expect(member.User).to.eql(expectedMember!.User);
        expect(member.groupId).to.eql(expectedMember!.groupId);
        expect(member.isAdmin).to.eql(expectedMember!.isAdmin);
      });
    });
  });
});