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

100% Statements 52/52
100% Branches 0/0
100% Functions 14/14
100% Lines 51/51

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    1x 1x 1x 1x 1x 1x 1x   1x 1x           1x             1x 5x 5x   5x     5x     5x       5x           5x   5x   5x       1x 5x     1x 1x   1x     1x 1x         1x       1x   1x     1x   1x           1x           1x         1x         1x       1x 1x 1x 1x       1x   1x             1x                 1x 5x           5x                 5x             1x         1x         1x 1x        
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import config from '../../../../../config';
import db, {syncPromise} from '../../../../../db';
import app from '../../../../../app';
import request from 'supertest';
import {expect} from 'chai';
import {Invite, Group, User, Membership} from '../../../../../models';
import sinon from 'sinon';
 
describe('get /api/group/:groupId', function() {
  const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
 
  let csrf: string;
  let user: any;
  let agent: request.SuperTest<request.Test>;
 
  const signUpBody = {
    username: 'test',
    email: 'test@mail.com',
    password: 'password',
  };
 
  // Force sync database before each test
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    agent = request.agent(app);
 
    // Get csrf token
    csrf = await agent.head('/auth')
        .then((response) => {
        // Save jwt cookie
          return response.header[csrfHeaderName];
        });
 
    // Sign up to access api and set new jwt
    await agent
        .post('/auth/sign-up')
        .set(csrfHeaderName, csrf)
        .send(signUpBody)
        .expect(201)
        .then((response) => {
          user = response.body;
        });
    csrf = await agent.head('/auth')
        .then((response) => {
          return response.header[csrfHeaderName];
        });
  });
 
  afterEach(function() {
    sinon.restore();
  });
 
  it('only accessible if user is logged in', async function() {
    await agent.post('/auth/logout');
 
    await agent.get('/api/group/12').send().expect(401);
  });
 
  it('responses with 400 if groupId is not numeric', function() {
    return agent
        .get('/api/group/test')
        .send()
        .expect(400)
        .then((res) => {
          expect(res.body.message).to.contain('groupId has to be a number');
        });
  });
 
  it('responses with UnauthorizedError if user is ' +
      'not a member and has no invite', function() {
    return agent.get('/api/group/1').send().expect(401);
  });
 
  it('responses with simple version of group if user ' +
      'is no member but has invite', async function() {
    const owner = await User.create({
      username: 'OWNER',
      password: 'OWNER',
      email: 'OWNER@mail.com',
    });
 
    const group = await Group.create({
      name: 'TEST',
      description: 'TEST',
      ownerId: owner.id,
    });
 
    await Invite.create({
      groupId: group.id,
      userId: user.id,
    });
 
    await agent
        .get(`/api/group/${group.id}`)
        .send()
        .expect(200)
        .then((res) => {
          expect(res.body).to.include({
            name: group.name,
            description: (group as any).description,
          });
          expect(res.body).to.haveOwnProperty('ownerId');
          expect(res.body).to.not.have.property('members');
          expect(res.body).to.have.property('createdAt');
          expect(res.body).to.have.property('updatedAt');
        });
  });
 
  it('responses with group data and list of members if user ' +
  'is member', async function() {
    const group = await Group.create({
      name: 'TEST',
      description: 'TEST',
      ownerId: user.id,
    });
 
    // Create invite for other users
    const expectedMemberList: any = [{
      User: {
        username: user.username,
        id: user.id,
      },
      userId: user.id,
      isAdmin: true,
    }];
 
    for (let i = 0; i < 5; i++) {
      const member = await User.create({
        username: `test-${i}-name`,
        password: `test-${i}-password`,
        email: `test-${i}@mail.com`,
      });
 
      expectedMemberList.push({
        User: {
          username: member.username,
          id: member.id,
        },
        isAdmin: i % 2 === 0,
        userId: member.id,
      });
 
      await Membership.create({
        groupId: group.id,
        userId: member.id,
        isAdmin: i % 2 === 0,
      });
    }
 
    await agent
        .get(`/api/group/${group.id}`)
        .send()
        .expect(200)
        .then((res) => {
          expect(res.body).to.include({
            id: group.id,
            name: group.name,
            description: (group as any).description,
          });
          expect(res.body).to.haveOwnProperty('Owner');
          expect(res.body.members).to.be.undefined;
        });
  });
});