From 940d62cde4cf7be7780904d681a5e4c9efaa2ba5 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 19 Jan 2021 18:14:09 +0100 Subject: [PATCH] Added contact add invalid tests ref #104 --- src/tests/contacts/contact_add.spec.ts | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/tests/contacts/contact_add.spec.ts diff --git a/src/tests/contacts/contact_add.spec.ts b/src/tests/contacts/contact_add.spec.ts new file mode 100644 index 0000000..2136aa3 --- /dev/null +++ b/src/tests/contacts/contact_add.spec.ts @@ -0,0 +1,75 @@ +import axios from 'axios'; +import { config } from '../../config'; +const base = "http://localhost:" + config.internal_port + +let access_token; +let axios_config; + +beforeAll(async () => { + const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" }); + access_token = res.data["access_token"]; + axios_config = { + headers: { "authorization": "Bearer " + access_token }, + validateStatus: undefined + }; +}); + +// --------------- +describe('POST /api/contacts with errors', () => { + it('creating a new contact without any parameters should return 400', async () => { + const res = await axios.post(base + '/api/contacts', null, axios_config); + expect(res.status).toEqual(400); + expect(res.headers['content-type']).toContain("application/json") + }); + it('creating a new contact without a last name should return 400', async () => { + const res = await axios.post(base + '/api/contacts', { + "firstname": "first", + "middlename": "middle" + }, axios_config); + expect(res.status).toEqual(400); + expect(res.headers['content-type']).toContain("application/json") + }); + it('creating a new contact with a invalid phone number should return 400', async () => { + const res = await axios.post(base + '/api/contacts', { + "firstname": "first", + "middlename": "middle", + "lastname": "last", + "phone": "123" + }, axios_config); + expect(res.status).toEqual(400); + expect(res.headers['content-type']).toContain("application/json") + }); + it('creating a new contact with a invalid mail address should return 400', async () => { + const res = await axios.post(base + '/api/contacts', { + "firstname": "string", + "middlename": "string", + "lastname": "string", + "phone": null, + "email": "123", + }, axios_config); + expect(res.status).toEqual(400); + expect(res.headers['content-type']).toContain("application/json") + }); + it('creating a new contact with an invalid address 400', async () => { + const res = await axios.post(base + '/api/contacts', { + "firstname": "string", + "middlename": "string", + "lastname": "string", + "address": { + "city": "Testcity" + } + }, axios_config); + expect(res.status).toEqual(400); + expect(res.headers['content-type']).toContain("application/json") + }); + it('creating a new contact with a invalid group should return 404', async () => { + const res = await axios.post(base + '/api/contacts', { + "firstname": "string", + "middlename": "string", + "lastname": "string", + "groups": 9999999999999 + }, axios_config); + expect(res.status).toEqual(404); + expect(res.headers['content-type']).toContain("application/json") + }); +});