Skip to content

Commit

Permalink
bugfix: Request validity including request body failure
Browse files Browse the repository at this point in the history
  • Loading branch information
milenacrios committed Feb 27, 2024
1 parent 41e2777 commit 0158203
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/async-tokenize/async-tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AsyncTokenize {

removeFormElements(this.elements)
const tokenIdElement = createFormElement('tokenId', tokenId)

console.log(tokenIdElement)
form?.appendChild(tokenIdElement)

form?.submit()
Expand Down
88 changes: 87 additions & 1 deletion src/async-tokenize/tests/async-tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ describe('handle', () => {
expect(tokenIdInput).toBeInTheDocument()
expect(form).toContain(tokenIdInput)
expect(tokenIdInput).toBeTruthy()
expect(tokenIdInput?.value).toBe('sandbox-token-id')
})
})
test('should be possible to return a value in tokenIdElement and if the settings include the sandbox: true option, the value must be sandox-token-id ', async () => {
Expand Down Expand Up @@ -149,6 +148,7 @@ describe('handle', () => {
const tokenIdInput = document.querySelector<HTMLInputElement>(
'input[name="tokenId"]',
)
console.log('tokenId: ', tokenIdInput)
expect(tokenIdInput?.value).toBe('production-token-id')
})
})
Expand Down Expand Up @@ -239,4 +239,90 @@ describe('handle', () => {
expect(preventDefault).toBeCalled()
})
})
test('should be possible to return an error if empty apiKey and clientId are sent to the Malga constructor', async () => {
window.HTMLFormElement.prototype.submit = () => {}
onSubmit.mockImplementation((event) => {
event.preventDefault()
})

Form(onSubmit)

const MalgaConfigurationsEmpty = {
apiKey: '',
clientId: '',
}

const malga = new Malga(MalgaConfigurationsEmpty)

const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock)

expect(asyncTokenizeObject.handle).toThrowError()

const form = document.querySelector('form')
fireEvent.submit(form!)
})
test('should be possible to throw an error when the elements passed are incompatible with those in the DOM', async () => {
window.HTMLFormElement.prototype.submit = () => {}
onSubmit.mockImplementation((event) => {
event.preventDefault()
})

Form(onSubmit)

const malga = new Malga(MalgaConfigurations)

const asyncTokenizeObject = new AsyncTokenize(malga, {
form: 'data-form',
holderName: 'data-holder-name',
number: 'data-number',
expirationDate: 'data-expiration-date',
cvv: 'data-cvv',
})

expect(asyncTokenizeObject.handle).toThrowError(
"Cannot read properties of undefined (reading 'elements')",
)

const form = document.querySelector('form')
fireEvent.submit(form!)
})
test('asjnj', async () => {
window.HTMLFormElement.prototype.submit = () => {}
onSubmit.mockImplementation((event) => {
event.preventDefault()
})
const {
form,
holderNameInput,
cvvInput,
expirationDateInput,
numberInput,
} = handleFormMock()

form.setAttribute(formElementsMock.form, '')
form.onsubmit = onSubmit
form.id = 'form'
form.method = 'POST'
form.action = '/test'

holderNameInput.setAttribute(formElementsMock.holderName, '')
numberInput.setAttribute(formElementsMock.number, '')
cvvInput.setAttribute(formElementsMock.cvv, '')
expirationDateInput.setAttribute(formElementsMock.expirationDate, '')

document.body.appendChild(form)
form.appendChild(holderNameInput)
form.appendChild(numberInput)
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)

const malga = new Malga(MalgaConfigurations)

const asyncTokenizeObject = new AsyncTokenize(malga, formElementsMock)

expect(asyncTokenizeObject.handle).toThrowError()

const form2 = document.querySelector('form')
fireEvent.submit(form2!)
})
})
1 change: 1 addition & 0 deletions src/common/utils/form-elements/form-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function createFormElement(name: string, value: string) {
field.type = 'hidden'
field.name = name
field.value = value
console.log('token valor: ', field.value)

return field
}
9 changes: 3 additions & 6 deletions src/common/utils/tests/form-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
import { getFormValues } from '../form-values/form-values'

describe('getFormValues', () => {
beforeEach(() => {
document.body.innerHTML = ''
})
test('should be the values of elements equals to sended', () => {
const {
form,
Expand Down Expand Up @@ -67,12 +70,6 @@ describe('getFormValues', () => {
form.appendChild(expirationDateInput)
form.appendChild(cvvInput)

const inputs = document.querySelectorAll('input')
inputs[0].value = ''
inputs[1].value = ''
inputs[2].value = ''
inputs[3].value = ''

const formValue = getFormValues({
form: 'data-malga-tokenization-form',
holderName: 'data-malga-tokenization-holder-name',
Expand Down
6 changes: 3 additions & 3 deletions src/tokenize/tests/tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@ describe('handle', () => {
inputs[2].value = formValuesMock.expirationDate
inputs[3].value = formValuesMock.cvv

const MalgaConfigurations2 = {
const MalgaConfigurationsEmpty = {
apiKey: '',
clientId: '',
}

const malga = new Malga(MalgaConfigurations2)
const malga = new Malga(MalgaConfigurationsEmpty)

const tokenizeObject = new Tokenize(malga, formElementsMock)

await expect(tokenizeObject.handle()).rejects.toThrowError()
await expect(tokenizeObject.handle).rejects.toThrowError()
})
test('should be possible to perform the tokenization function and getFormValues with the respective elements correctly', async () => {
const {
Expand Down
10 changes: 5 additions & 5 deletions tests/mocks/request-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ export const handlers = [
http.post('https://api.malga.io/v1/tokens', async ({ request }) => {
const apiKey = request.headers.get('X-Api-Key')
const clientId = request.headers.get('X-Client-Id')
/**
* const data = await request.formData()

const data = await request.formData()

const cardHolderName = data.get('cardHolderName')
const cardNumber = data.get('cardNumber')
const cardCvv = data.get('cardCvv')
const cardExpirationDate = data.get('cardExpirationDate')
if (!cardHolderName && !cardNumber && !cardCvv && !cardExpirationDate) {
const cardCvv = data.get('cardCvv')

if (!cardHolderName || !cardNumber || !cardExpirationDate || !cardCvv) {
return new HttpResponse({ message: 'Forbidden' } as any, { status: 403 })
}
*/

if (!apiKey && !clientId) {
return new HttpResponse({ message: 'Forbidden' } as any, { status: 403 })
Expand Down

0 comments on commit 0158203

Please sign in to comment.