75
									
								
								src/tests/contacts/contact_add.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								src/tests/contacts/contact_add.spec.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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")
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user