File size: 6,137 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
  At the end
    2 Clients: [
      {
        email: "[email protected]",
          lastname: "Client",
          firstname: "Updated",
          phone: "5146669999",
          cell: "4389996666",
          title: "IT manager",
          company: {name: 'New Updated Company'}
      },
      {
        email: "[email protected]",
          lastname: "Client",
          firstname: "User",
          phone: "5146669999",
          cell: "4389996666",
          title: "IT manager",
          company: {name: 'Company 1'}
      }
    ]

    2 more Companies created: 
      'New Company'
      'New Updated Company'
*/

module.exports = function (request, app) {
  describe('Client Suite Tests', () => {
    var userToken = '';
    beforeAll(async () => {
      var response = await request(app)
        .post('/api/users/token')
        .send({ username: 'admin', password: 'Admin123' });
      userToken = response.body.datas.token;
    });

    describe('Client CRUD operations', () => {
      var client1Id = '';
      var client2Id = '';
      var client3Id = '';
      it('Get clients (no existing clients in db)', async () => {
        var response = await request(app)
          .get('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`]);

        expect(response.status).toBe(200);
        expect(response.body.datas).toHaveLength(0);
      });

      it('Create client with email only', async () => {
        var client = { email: '[email protected]' };
        var response = await request(app)
          .post('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);
        expect(response.status).toBe(201);
        client1Id = response.body.datas._id;
      });

      it('Create client with all information and existing company', async () => {
        var client = {
          email: '[email protected]',
          lastname: 'Client',
          firstname: 'User',
          phone: '5146669999',
          cell: '4389996666',
          title: 'IT manager',
          company: { name: 'Company 1' },
        };
        var response = await request(app)
          .post('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);
        expect(response.status).toBe(201);
        client2Id = response.body.datas._id;
      });

      it('Create client with nonexistent company', async () => {
        var client = {
          email: '[email protected]',
          company: { name: 'New Company' },
        };
        var response = await request(app)
          .post('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);
        expect(response.status).toBe(201);
        client3Id = response.body.datas._id;
      });

      it('Should not create client with existing email', async () => {
        var client = { email: '[email protected]' };
        var response = await request(app)
          .post('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);

        expect(response.status).toBe(422);
      });

      it('Should not create client without email', async () => {
        var client = { firstname: 'firstname', lastname: 'lastname' };
        var response = await request(app)
          .post('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);

        expect(response.status).toBe(422);
      });

      it('Get clients (existing clients in db)', async () => {
        const expected = [
          { email: '[email protected]' },
          {
            email: '[email protected]',
            lastname: 'Client',
            firstname: 'User',
            phone: '5146669999',
            cell: '4389996666',
            title: 'IT manager',
            company: { name: 'Company 1' },
          },
          {
            email: '[email protected]',
            company: { name: 'New Company' },
          },
        ];
        var response = await request(app)
          .get('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`]);

        expect(response.status).toBe(200);
        expect(
          response.body.datas.map(t => {
            return {
              email: t.email,
              lastname: t.lastname,
              firstname: t.firstname,
              phone: t.phone,
              cell: t.cell,
              title: t.title,
              company: t.company,
            };
          }),
        ).toEqual(expect.arrayContaining(expected));
      });

      it('Update client', async () => {
        var client = {
          email: '[email protected]',
          lastname: 'Client',
          firstname: 'Updated',
          phone: '5146669999',
          cell: '4389996666',
          title: 'IT manager',
          company: { name: 'New Updated Company' },
        };

        var response = await request(app)
          .put(`/api/clients/${client1Id}`)
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);
        expect(response.status).toBe(200);
      });

      it('Update client with nonexistent id', async () => {
        var client = { firstname: 'Client' };

        var response = await request(app)
          .put(`/api/clients/deadbeefdeadbeefdeadbeef`)
          .set('Cookie', [`token=JWT ${userToken}`])
          .send(client);
        expect(response.status).toBe(404);
      });

      it('Delete client', async () => {
        var response = await request(app)
          .delete(`/api/clients/${client3Id}`)
          .set('Cookie', [`token=JWT ${userToken}`]);
        expect(response.status).toBe(200);

        response = await request(app)
          .get('/api/clients')
          .set('Cookie', [`token=JWT ${userToken}`]);
        expect(response.body.datas).toHaveLength(2);
      });

      it('Delete client with nonexistent email', async () => {
        var response = await request(app)
          .delete(`/api/clients/deadbeefdeadbeefdeadbeef`)
          .set('Cookie', [`token=JWT ${userToken}`]);
        expect(response.status).toBe(404);
      });
    });
  });
};