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

100% Statements 35/35
100% Branches 0/0
100% Functions 10/10
100% Lines 35/35

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 1141x 1x   1x 1x 1x 1x 1x   1x     1x       1x 3x 3x   3x   3x 3x     1x 3x     1x     1x             1x     1x       1x           1x   1x             1x     1x               1x     1x             1x           1x     1x         1x           1x     1x     1x        
import {syncPromise} from '../../../../../db';
import {TestUtils} from '../../../../../util/test-utils.spec';
import request from 'supertest';
import sinon from 'sinon';
import db from '../../../../../db';
import config from '../../../../../config/config';
import {expect} from 'chai';
import {Membership} from '../../../../../models';
 
const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
 
 
describe('post /api/group/:groupId/leave', function() {
  let agent: request.SuperTest<request.Test>;
  let csrf: string;
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
 
    agent = response.agent;
    csrf = response.csrf;
  });
 
  afterEach(function() {
    sinon.restore();
  });
 
  it('responses with OwnerCannotLeaveError if owner ' +
  'of group tries to leave it', async function() {
    // Create group
    const group = await agent.post('/api/group')
        .set(csrfHeaderName, csrf)
        .send({
          name: 'NAME',
          description: 'DESC',
        })
        .expect(201)
        .then((res) => res.body);
 
    // Try to leave that group
    await agent.post(`/api/group/${group.id}/leave`)
        .set(csrfHeaderName, csrf)
        .expect(400)
        .then((res) => {
          expect(res.body.message).to
              .contain('An owner can\'t leave a group. ' +
              'Transfer your ownership to do so');
        });
  });
 
  it('leaves the group', async function() {
    // Create group
    const group = await agent.post('/api/group')
        .set(csrfHeaderName, csrf)
        .send({
          name: 'NAME',
          description: 'DESC',
        })
        .expect(201)
        .then((res) => res.body);
 
    // Create new user and log in with it
    const member = await agent.post('/auth/sign-up')
        .set(csrfHeaderName, csrf)
        .send({
          username: 'MEMBER',
          password: 'PASSWORD',
          email: 'MEMBER@mail.com',
        })
        .expect(201)
        .then((res) => res.body);
 
    // Assign user to group
    await Membership.create({
      userId: member.id,
      groupId: group.id,
      isAdmin: false,
    });
 
    // Check if membership exists now
    let memberships = await Membership.findAll({
      where: {
        userId: member.id,
      },
    });
 
    expect(memberships).to.have.length(1);
 
    // Try to leave that group
    await agent.post(`/api/group/${group.id}/leave`)
        .set(csrfHeaderName, csrf)
        .expect(204);
 
    // Check if membership was deleted
    memberships = await Membership.findAll({
      where: {
        userId: member.id,
      },
    });
 
    expect(memberships).to.have.length(0);
  });
 
  it('response with UnauthorizedError if user tries to leave ' +
  'a group he/she is not a member of', function() {
    // Try to leave that group
    return agent.post(`/api/group/1/leave`)
        .expect(401);
  });
});