|
| 1 | +import supertest from 'supertest' |
| 2 | +import { web } from '../src/application/web' |
| 3 | +import { createContact } from './fixtures/contact' |
| 4 | +import { createAddressRequest } from './fixtures/address' |
| 5 | +import { createUser } from './fixtures/user' |
| 6 | +import { AddressResponse } from '../src/model/address-model' |
| 7 | +import { Response } from '../src/model/model' |
| 8 | +import { faker } from '@faker-js/faker' |
| 9 | + |
| 10 | +describe('POST /api/contacts/{:contactId}/addresses', () => { |
| 11 | + it('can create address by contact', async () => { |
| 12 | + const user = await createUser() |
| 13 | + |
| 14 | + const contact = await createContact(user) |
| 15 | + |
| 16 | + const request = createAddressRequest(contact) |
| 17 | + |
| 18 | + const response = await supertest(web) |
| 19 | + .post(`/api/contacts/${contact.id}/addresses`) |
| 20 | + .set('X-API-TOKEN', user.token!) |
| 21 | + .send(request) |
| 22 | + |
| 23 | + expect(response.status).toBe(200) |
| 24 | + |
| 25 | + const body: Response<AddressResponse> = response.body |
| 26 | + |
| 27 | + expect(body.data.id).not.toBeNull() |
| 28 | + expect(body.data.city).toBe(request.city) |
| 29 | + expect(body.data.province).toBe(request.province) |
| 30 | + expect(body.data.country).toBe(request.country) |
| 31 | + expect(body.data.postal_code).toBe(request.postal_code) |
| 32 | + expect(body.data.street).toBe(request.street) |
| 33 | + }) |
| 34 | + |
| 35 | + it('can create address partial by contact', async () => { |
| 36 | + const user = await createUser() |
| 37 | + |
| 38 | + const contact = await createContact(user) |
| 39 | + |
| 40 | + const request = createAddressRequest(contact) |
| 41 | + |
| 42 | + const response = await supertest(web) |
| 43 | + .post(`/api/contacts/${contact.id}/addresses`) |
| 44 | + .set('X-API-TOKEN', user.token!) |
| 45 | + .send({ |
| 46 | + street: request.street, |
| 47 | + country: request.country, |
| 48 | + postal_code: request.postal_code |
| 49 | + }) |
| 50 | + |
| 51 | + expect(response.status).toBe(200) |
| 52 | + |
| 53 | + const body: Response<AddressResponse> = response.body |
| 54 | + |
| 55 | + expect(body.data.id).not.toBeNull() |
| 56 | + expect(body.data.city).toBeNull() |
| 57 | + expect(body.data.province).toBeNull() |
| 58 | + expect(body.data.street).toBe(request.street) |
| 59 | + expect(body.data.country).toBe(request.country) |
| 60 | + expect(body.data.postal_code).toBe(request.postal_code) |
| 61 | + }) |
| 62 | + |
| 63 | + it('cant create address if payload is not desirable', async () => { |
| 64 | + const user = await createUser() |
| 65 | + |
| 66 | + const contact = await createContact(user) |
| 67 | + |
| 68 | + const response = await supertest(web) |
| 69 | + .post(`/api/contacts/${contact.id}/addresses`) |
| 70 | + .set('X-API-TOKEN', user.token!) |
| 71 | + .send({ |
| 72 | + street: faker.word.words(50), |
| 73 | + country: faker.word.words(50), |
| 74 | + postal_code: faker.word.words(50), |
| 75 | + province: faker.word.words(50), |
| 76 | + city: faker.word.words(50) |
| 77 | + }) |
| 78 | + |
| 79 | + expect(response.status).toBe(422) |
| 80 | + }) |
| 81 | + |
| 82 | + it('can create address if contact not available', async () => { |
| 83 | + const user = await createUser() |
| 84 | + |
| 85 | + const contact = await createContact(user) |
| 86 | + |
| 87 | + const response = await supertest(web) |
| 88 | + .post(`/api/contacts/0/addresses`) |
| 89 | + .set('X-API-TOKEN', user.token!) |
| 90 | + .send(createAddressRequest(contact)) |
| 91 | + |
| 92 | + expect(response.status).toBe(404) |
| 93 | + }) |
| 94 | +}) |
0 commit comments