159
									
								
								src/tests/donations/donations_update.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								src/tests/donations/donations_update.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,159 @@ | ||||
| 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('adding + updating fixed donation illegally', () => { | ||||
| 	let added_donor; | ||||
| 	let added_donation; | ||||
| 	it('creating a new donor with only needed params should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/donors', { | ||||
| 			"firstname": "first", | ||||
| 			"lastname": "last" | ||||
| 		}, axios_config); | ||||
| 		added_donor = res.data | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('creating a new fixed donation should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/donations/fixed', { | ||||
| 			"donor": added_donor.id, | ||||
| 			"amount": 1000 | ||||
| 		}, axios_config); | ||||
| 		added_donation = res.data; | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating empty should return 400', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/fixed/' + added_donation.id, null, axios_config); | ||||
| 		expect(res2.status).toEqual(400); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with wrong id should return 406', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/fixed/' + added_donation.id, { | ||||
| 			"id": added_donation.id + 1, | ||||
| 			"donor": added_donor.id, | ||||
| 			"amount": 100 | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(406); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with negative amount should return 400', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/fixed/' + added_donation.id, { | ||||
| 			"id": added_donation.id, | ||||
| 			"donor": added_donor.id, | ||||
| 			"amount": -1 | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(400); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with invalid donor should return 404', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/fixed/' + added_donation.id, { | ||||
| 			"id": added_donation.id, | ||||
| 			"donor": 9999999999999999999, | ||||
| 			"amount": 100 | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(404); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| }); | ||||
| // --------------- | ||||
| describe('adding + updating distance donation illegally', () => { | ||||
| 	let added_donor; | ||||
| 	let added_org; | ||||
| 	let added_runner; | ||||
| 	let added_donation; | ||||
| 	it('creating a new donor with only needed params should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/donors', { | ||||
| 			"firstname": "first", | ||||
| 			"lastname": "last" | ||||
| 		}, axios_config); | ||||
| 		added_donor = res.data | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('creating a new org with just a name should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/organisations', { | ||||
| 			"name": "test123" | ||||
| 		}, axios_config); | ||||
| 		added_org = res.data | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('creating a new runner with only needed params should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/runners', { | ||||
| 			"firstname": "first", | ||||
| 			"lastname": "last", | ||||
| 			"group": added_org.id | ||||
| 		}, axios_config); | ||||
| 		added_runner = res.data; | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('creating a new distance donation should return 200', async () => { | ||||
| 		const res = await axios.post(base + '/api/donations/distance', { | ||||
| 			"runner": added_runner.id, | ||||
| 			"amountPerDistance": 100, | ||||
| 			"donor": added_donor.id | ||||
| 		}, axios_config); | ||||
| 		added_donation = res.data; | ||||
| 		expect(res.status).toEqual(200); | ||||
| 		expect(res.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating empty should return 400', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/distance/' + added_donation.id, null, axios_config); | ||||
| 		expect(res2.status).toEqual(400); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with wrong id should return 406', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/distance/' + added_donation.id, { | ||||
| 			"id": added_donation.id + 1, | ||||
| 			"runner": added_runner.id, | ||||
| 			"amountPerDistance": 100, | ||||
| 			"donor": added_donor.id | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(406); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with negative amountPerDistance should return 400', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/distance/' + added_donation.id, { | ||||
| 			"id": added_donation.id, | ||||
| 			"runner": added_runner.id, | ||||
| 			"amountPerDistance": -1, | ||||
| 			"donor": added_donor.id | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(400); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with invalid donor should return 404', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/distance/' + added_donation.id, { | ||||
| 			"id": added_donation.id, | ||||
| 			"runner": added_runner.id, | ||||
| 			"amountPerDistance": 100, | ||||
| 			"donor": 9999999999999999999 | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(404); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| 	it('updating with invalid runner should return 404', async () => { | ||||
| 		const res2 = await axios.put(base + '/api/donations/distance/' + added_donation.id, { | ||||
| 			"id": added_donation.id, | ||||
| 			"runner": 9999999999999999999, | ||||
| 			"amountPerDistance": 100, | ||||
| 			"donor": added_donor.id | ||||
| 		}, axios_config); | ||||
| 		expect(res2.status).toEqual(404); | ||||
| 		expect(res2.headers['content-type']).toContain("application/json") | ||||
| 	}); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user