backend/src/tests/contacts/contact_add.spec.ts

215 lines
8.1 KiB
TypeScript

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")
});
});
// ---------------
describe('POST /api/contacts working (simple)', () => {
it('creating a new contact with only needed params should return 200', async () => {
const res = await axios.post(base + '/api/contacts', {
"firstname": "first",
"lastname": "last"
}, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('creating a new contact with all non-relationship optional params should return 200', async () => {
const res = await axios.post(base + '/api/contacts', {
"firstname": "first",
"middlename": "middle",
"lastname": "last",
"email": "testContact@lauf-fuer-kaya.de",
"phone": "+49017612345678",
"address": {
"address1": "test",
"address2": null,
"city": "herzogenaurach",
"country": "germany",
"postalcode": "91074",
}
}, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/contacts working (with group)', () => {
let added_org;
let added_team;
let added_contact;
it('creating a new org with just a name should return 200', async () => {
const res = await axios.post(base + '/api/organizations', {
"name": "test123"
}, axios_config);
delete res.data.contact;
delete res.data.teams;
added_org = res.data
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('creating a new team with a parent org should return 200', async () => {
const res = await axios.post(base + '/api/teams', {
"name": "test_team",
"parentGroup": added_org.id
}, axios_config);
delete res.data.contact;
added_team = res.data;
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('creating a new contact with a valid org should return 200', async () => {
const res = await axios.post(base + '/api/contacts', {
"firstname": "first",
"lastname": "last",
"groups": added_org.id
}, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json");
delete res.data.id;
expect(res.data).toEqual({
"firstname": "first",
"middlename": null,
"lastname": "last",
"phone": null,
"email": null,
"address": {
"address1": null,
"address2": null,
"postalcode": null,
"city": null,
"country": null
},
"groups": [added_org]
});
});
it('creating a new contact with a valid team should return 200', async () => {
const res = await axios.post(base + '/api/contacts', {
"firstname": "first",
"lastname": "last",
"groups": added_team.id
}, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json");
delete res.data.id;
expect(res.data).toEqual({
"firstname": "first",
"middlename": null,
"lastname": "last",
"phone": null,
"email": null,
"address": {
"address1": null,
"address2": null,
"postalcode": null,
"city": null,
"country": null
},
"groups": [added_team]
});
});
it('creating a new contact with a valid org and team should return 200', async () => {
const res = await axios.post(base + '/api/contacts', {
"firstname": "first",
"lastname": "last",
"groups": [added_org.id, added_team.id]
}, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json");
added_contact = res.data
delete res.data.id;
expect(res.data).toEqual({
"firstname": "first",
"middlename": null,
"lastname": "last",
"phone": null,
"email": null,
"address": {
"address1": null,
"address2": null,
"postalcode": null,
"city": null,
"country": null
},
"groups": [added_org, added_team]
});
});
it('checking if the added team\'s contact is the new contact should return 200', async () => {
const res = await axios.get(base + '/api/teams/' + added_team.id, axios_config);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json");
delete res.data.contact.groups;
delete res.data.contact.id;
delete added_contact.groups;
expect(res.data.contact).toEqual(added_contact);
});
});