-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(employeur): envoi formulaire contact poe
- Loading branch information
Showing
2 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { testApiHandler } from 'next-test-api-route-handler'; | ||
import nock from 'nock'; | ||
|
||
import { enregistrerContactPOEHandler } from '~/pages/api/contacts-poe/index.controller'; | ||
import { ErrorHttpResponse } from '~/server/errors/errorHttpResponse'; | ||
|
||
describe('enregistrerContactPOEHandler', () => { | ||
describe('quand la méthode n‘est pas un POST', () => { | ||
it('retourne une erreur 406', async () => { | ||
await testApiHandler<void | ErrorHttpResponse>({ | ||
handler: (req, res) => enregistrerContactPOEHandler(req, res), | ||
test: async ({ fetch }) => { | ||
const res = await fetch({ method: 'GET' }); | ||
expect(res.status).toEqual(406); | ||
}, | ||
url: '/contacts-poe', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('quand la méthode est un POST', () => { | ||
const jwt = '3456789098765RFVBGFDRTYHJNfKJHGV'; | ||
const identifier = '[email protected]'; // défini dans le fichier .env.test | ||
const password = 'monmotdepassesécurisé'; // défini dans le fichier .env.test | ||
|
||
describe('quand l‘appel est en succès', () => { | ||
it('retourne un status 200', async () => { | ||
let strapiReceivedBody: Record<string, string>; | ||
const strapiAuth = nock('http://localhost:1337/api') | ||
.post('/contacts-poe') | ||
.once() | ||
.reply(401, 'unauthorized') | ||
.post('/auth/local', { identifier, password }) | ||
.once() | ||
.reply(200, { jwt }); | ||
const strapiApi = nock('http://localhost:1337/api', { reqheaders: { Authorization: `Bearer ${jwt}` } }) | ||
.post('/contacts-poe', (body) => { | ||
strapiReceivedBody = body; | ||
return true; | ||
}) | ||
.once() | ||
.reply(201); | ||
|
||
await testApiHandler<void | ErrorHttpResponse>({ | ||
handler: (req, res) => enregistrerContactPOEHandler(req, res), | ||
test: async ({ fetch }) => { | ||
const res = await fetch({ | ||
body: JSON.stringify({ | ||
codePostal: '95000', | ||
commentaire: 'Places disponibles dans 2 mois', | ||
email: '[email protected]', | ||
nom: 'Mc Totface', | ||
nomSociété: 'OCTO Technology', | ||
nombreARecruter: '5', | ||
prénom: 'Toto', | ||
secteur: 'information-communication', | ||
siret: '41816609600069', | ||
taille: 'xxlarge', | ||
travail: 'consultant', | ||
téléphone: '0678954322', | ||
ville: 'Cergy', | ||
}), | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
method: 'POST', | ||
}); | ||
expect(res.status).toEqual(200); | ||
expect(strapiReceivedBody).toEqual({ | ||
data: { | ||
code_postal: '95000', | ||
commentaire: 'Places disponibles dans 2 mois', | ||
email: '[email protected]', | ||
nom: 'Mc Totface', | ||
nom_societe: 'OCTO Technology', | ||
nombreARecruter: '5', | ||
prenom: 'Toto', | ||
secteur: 'information-communication', | ||
siret: '41816609600069', | ||
taille: 'xxlarge', | ||
telephone: '+33678954322', | ||
travail: 'consultant', | ||
ville: 'Cergy', | ||
}, | ||
}); | ||
strapiAuth.done(); | ||
strapiApi.done(); | ||
}, | ||
url: '/contacts-poe', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('quand l‘appel est une demande incorrecte', () => { | ||
it('retourne un status 400', async () => { | ||
await testApiHandler<void | ErrorHttpResponse>({ | ||
handler: (req, res) => enregistrerContactPOEHandler(req, res), | ||
test: async ({ fetch }) => { | ||
const res = await fetch({ | ||
body: JSON.stringify({ | ||
age: 'not allowed', | ||
codePostal: '95000', | ||
commentaire: 'Places disponibles dans 2 mois', | ||
email: '[email protected]', | ||
nom: 'Mc Totface', | ||
nomSociété: 'OCTO Technology', | ||
nombreARecruter: '5', | ||
prénom: 'Toto', | ||
secteur: 'information-communication', | ||
siret: '41816609600069', | ||
taille: 'xxlarge', | ||
travail: 'consultant', | ||
téléphone: '0678954322', | ||
ville: 'Cergy', | ||
}), | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
method: 'POST', | ||
}); | ||
expect(res.status).toEqual(400); | ||
}, | ||
url: '/contacts-poe', | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters