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

100% Statements 80/80
100% Branches 0/0
100% Functions 32/32
100% Lines 80/80

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249  1x 1x 1x 1x 1x 1x 1x 1x     1x 1x       1x 1x 1x   1x     1x 1x       1x 1x           1x             1x 9x 9x   9x     9x     9x       9x           9x   9x   9x       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     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';
import {Group, Membership, User} from '../../../../../models';
import sinon from 'sinon';
import Bluebird from 'bluebird';
 
 
describe('put /api/group/:groupId', function() {
  describe('user is not logged in:', function() {
    let agent: request.SuperTest<request.Test>;
 
    // Force sync database before each test
    beforeEach(async function() {
      await syncPromise;
      await db.sync({force: true});
 
      agent = request.agent(app);
    });
 
    it('responses with 401', function() {
      return agent.put('/api/group/10').expect(401);
    });
  });
 
  describe('user is logged in:', 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 401 if user not member of group', function() {
      return agent.put('/api/group/1').expect(401);
    });
 
    it('responses with 401 if user not admin of group', function() {
      // Create owner of group
      const ownerData = {
        username: 'OWNER',
        password: 'OWNER PASSWORD',
        email: 'OWNER@OWNER.com',
      };
      // Create the database entries manually and then send request
      return User.create(ownerData).then((owner) => {
        expect(owner).to.be.not.null;
        const groupData = {
          name: 'TEST',
          description: 'DESC',
          ownerId: owner.id,
        };
        return Group.create(groupData);
      }).then((group) => {
        expect(group).to.be.not.null;
        return Membership.create({
          groupId: group.id,
          userId: user.id,
          isAdmin: false,
        }).then(() => group);
      }).then((group) =>
        agent.put(`/api/group/${group.id}`)
            .set(csrfHeaderName, csrf).expect(401));
    });
 
    it('responses with 404 if user is admin of group but ' +
        'group doesn\'t exist', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
 
      // Stub Group.findByPk to simulate that the group somehow doesn't
      // exist. Could be an extreme edge case
      const groupFindByPkStub: any = sinon.stub(Group, 'update')
          .usingPromise(Bluebird).resolves([0, []]);
 
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .expect(404))
          .then(() => {
            sinon.assert.calledOnce(groupFindByPkStub);
            sinon.restore();
          });
    });
 
    it('responses with 400 if name if not a string', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        name: 10,
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(400));
    });
 
    it('responses with 400 if name is an empty string', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        name: '',
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(400));
    });
 
    it('changes name field of group successfully', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        name: 'new name',
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(200).then((response) => {
                  expect(response.body).to.include({
                    name: requestBody.name,
                    description: groupData.description,
                    ownerId: groupData.ownerId,
                  });
                }));
    });
 
    it('responses with 400 if descriptions is not a string', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        description: 14,
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(400));
    });
 
    it('changes descriptions field successfully', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        description: 'new desc',
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(200).then((response) => {
                  expect(response.body).to.include({
                    name: groupData.name,
                    description: requestBody.description,
                    ownerId: groupData.ownerId,
                  });
                }));
    });
 
    it('cannot changes ownerId of group', function() {
      // Create the database entries manually and then send request
      const groupData = {
        name: 'name',
        description: 'desc',
        ownerId: user.id,
      };
      const requestBody = {
        ownerId: 6,
      };
      return Group.create(groupData)
          .then((group) =>
            agent.put(`/api/group/${group.id}`)
                .set(csrfHeaderName, csrf)
                .send(requestBody)
                .expect(400));
    });
  });
});