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

100% Statements 50/50
100% Branches 0/0
100% Functions 19/19
100% Lines 50/50

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  1x 1x 1x 1x 1x     1x 1x           1x             1x 7x 7x   7x     7x     7x       7x           7x   7x   7x       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 1x 1x        
/* eslint-disable @typescript-eslint/no-explicit-any */
import config from '../../../../config';
import request from 'supertest';
import app from '../../../../app';
import db, {syncPromise} from '../../../../db';
import {expect} from 'chai';
 
 
describe('post /api/group', 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];
        });
  });
 
  it('responses with 400 if name is missing', function() {
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .expect(400)
        .then((response) => {
          expect(response.body.message).to.include('name');
        });
  });
 
  it('responses with 400 if name is not a string', function() {
    const body = {
      name: 12,
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(400)
        .then((response) => {
          expect(response.body.message).to.include('name');
        });
  });
 
  it('responses with 400 if name is an empty string', function() {
    const body = {
      name: '',
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(400)
        .then((response) => {
          expect(response.body.message).to.include('name');
        });
  });
 
  it('responses with 201 if name is correct but description ' +
      'is missing', function() {
    const body = {
      name: 'NAME',
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(201)
        .then((response) => {
          expect(response.body.ownerId).to.equal(user.id);
          expect(response.body.name).to.equal(body.name);
        });
  });
 
  it('responses with 400 if name is correct but description ' +
      'is not a string', function() {
    const body = {
      name: 'NAME',
      description: 11,
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(400)
        .then((response) => {
          expect(response.body.message).to.include('description');
        });
  });
 
  it('responses with 201 if name is correct but description ' +
      'is an empty string', function() {
    const body = {
      name: 'NAME',
      description: '',
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(201)
        .then((response) => {
          expect(response.body.ownerId).to.equal(user.id);
          expect(response.body.name).to.equal(body.name);
          expect(response.body.description).to.equal(body.description);
        });
  });
 
  it('responses with 201 if name is correct but description ' +
      'is non empty string', function() {
    const body = {
      name: 'NAME',
      description: 'DESC',
    };
 
    return agent
        .post('/api/group')
        .set(csrfHeaderName, csrf)
        .send(body)
        .expect(201)
        .then((response) => {
          expect(response.body.ownerId).to.equal(user.id);
          expect(response.body.name).to.equal(body.name);
          expect(response.body.description).to.equal(body.description);
        });
  });
});