it('should retun 400 if request is invalid', async () => { const response = await request(app) .post('/api/dios') .set('Accept', 'application/json') .set('x-access-token', user.token) .send({ dioId: 1, name: 'test_post', roomId: '0fc1d78e-fd1c-4717-b610-65d2fa3d01b2', admin: true, }); expect(response.status).toBeBadRequest(); });
it('should retun 200 when user is authenticated', async () => { const response = await request(app) .get('/api/users') .set('Accept', 'application/json') .set('x-access-token', user.token) .send(); expect(response.body).toEqual([ { username: user.username, userId: user.userId, }, ]); });
it('should delete a dio', async () => { const response = await request(app) .delete('/api/dios/1') .set('Accept', 'application/json') .set('x-access-token', user.token); expect(response.status).toHaveStatusOk(); const dio = await knex(Dio.TABLE) .select() .where('roomId', 1); expect(dio).toHaveLength(0); });
it('should do not update username', async () => { const response = await request(app) .patch(`/api/users/${user.userId}`) .set('Accept', 'application/json') .set('x-access-token', user.token) .send({ username: '******', password: '******' }); expect(response.status).toHaveStatusOk(); const users = await knex(User.TABLE) .first() .where('userId', user.userId); expect(users).toHaveProperty('username', user.username); });
it('should run the routine', async () => { const runSpy = jest.spyOn(Routine, 'run'); const response = await request(app) .get(`/api/routines/${initRoutines[0].routineId}/action`) .set('Accept', 'application/json') .set('x-access-token', user.token); expect(response.status).toHaveStatusOk(); expect(response.body).toEqual({}); expect(Routine.run).toHaveBeenCalled(); expect(runSpy.mock.calls).toMatchSnapshot(); runSpy.mockRestore(); });
it('should return 401 when user is not authenticated', async () => { const response = await request(app) .patch(`/api/routines/${initRoutines[0].routineId}`) .set('Accept', 'application/json') .set('x-access-token', 'fake') .send({ sceneId: 'faaed78e-fd1c-4717-b610-65d2fa3d01b2', name: 'room_updated', interval: '* * * * * *', }); expect(response.status).toBeUnauthorized(); expect(response.body).toEqual({}); });
it('should return 404 if routineId is unknow', async () => { const response = await request(app) .patch('/api/routines/faaed78e-fd1c-4717-b610-65d2fa3d01b2') .set('Accept', 'application/json') .set('x-access-token', user.token) .send({ sceneId: 'faaed78e-fd1c-4717-b610-65d2fa3d01b2', name: 'room_updated', interval: '* * * * * *', }); expect(response.status).toBe(404); expect(response.body).toEqual({}); });
it('can handle a basic implicit GET request', () => { app = createApp(); const expected = { testString: 'it works', }; const query = { query: '{ testString }', }; const req = request(app) .get('/graphql').query(query); return req.then((res) => { expect(res.status).to.equal(200); return expect(res.body.data).to.deep.equal(expected); }); });
it('throws an error if options promise is rejected', () => { app = createApp({ graphqlOptions: () => { return Promise.reject({}) as any as GraphQLOptions; }}); const expected = 'Invalid options'; const req = request(app) .post('/graphql') .send({ query: 'query test{ testString }', }); return req.then((res) => { expect(res.status).to.equal(500); return expect(res.error.text).to.contain(expected); }); });
it('can be called with an options function', () => { app = createApp({graphqlOptions: (): GraphQLOptions => ({schema})}); const expected = { testString: 'it works', }; const req = request(app) .post('/graphql') .send({ query: 'query test{ testString }', }); return req.then((res) => { expect(res.status).to.equal(200); return expect(res.body.data).to.deep.equal(expected); }); });