-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add payments by product backend validation (#7077)
* chore: scaffold controller code * chore: split into services * chore: update typeguard to check for _id and name * chore: validate product price * chore: update error message for payment definition failure * chore: add unit tests for isPaymentsProducts * chore: add tests for payments service * chore: remove stray comment * chore: check entire product definition * chore: convert mongoose doc to obj for comparison * chore: refine testcases --------- Co-authored-by: Ken <ken@open.gov.sg>
- Loading branch information
Showing
8 changed files
with
675 additions
and
4 deletions.
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,126 @@ | ||
import { ObjectId } from 'bson' | ||
import { isPaymentsProducts } from '../product' | ||
import { Product } from '../product' | ||
|
||
describe('Product validation', () => { | ||
it('should return false if products is not an array', () => { | ||
// Arrange | ||
const mockProductNonArray = 'some thing' | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductNonArray)).toBe(false) | ||
}) | ||
|
||
it('should return true if products is an empty array', () => { | ||
// Arrange | ||
const mockProductEmptyArray: Product[] = [] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductEmptyArray)).toBe(false) | ||
}) | ||
|
||
it('should return false if product has invalid object id', () => { | ||
// Arrange | ||
const mockProductInvalidId: any = [ | ||
{ | ||
name: 'some name', | ||
description: 'some description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: 'some id', | ||
}, | ||
] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductInvalidId)).toBe(false) | ||
}) | ||
|
||
it('should return false if product has no name', () => { | ||
// Arrange | ||
const mockProductWrongName = [ | ||
{ | ||
description: 'some description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
] as unknown as Product[] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductWrongName)).toBe(false) | ||
}) | ||
|
||
it('should return false if there are multiple products and at least one has no name', () => { | ||
// Arrange | ||
const mockMultipleProductOneNoName = [ | ||
{ | ||
description: 'some description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
{ | ||
name: 'has name', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
] as unknown as Product[] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockMultipleProductOneNoName)).toBe(false) | ||
}) | ||
|
||
it('should return true if product has valid object id and name', () => { | ||
// Arrange | ||
const mockProductsCorrectShape = [ | ||
{ | ||
name: 'some name', | ||
description: 'some description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
] as unknown as Product[] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductsCorrectShape)).toBe(true) | ||
}) | ||
|
||
it('should return true if multiple products have valid object id and name', () => { | ||
// Arrange | ||
const mockProductsCorrectShapeMultiple = [ | ||
{ | ||
name: 'some name', | ||
description: 'some description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
{ | ||
name: 'another name', | ||
description: 'another description', | ||
multi_qty: true, | ||
min_qty: 1, | ||
max_qty: 1, | ||
amount_cents: 1, | ||
_id: new ObjectId(), | ||
}, | ||
] as unknown as Product[] | ||
|
||
// Assert | ||
expect(isPaymentsProducts(mockProductsCorrectShapeMultiple)).toBe(true) | ||
}) | ||
}) |
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
Oops, something went wrong.