stripe-metadata-sanitize.e2e-spec.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { sanitizeMetadata } from '../src/stripe/metadata-sanitize';
  2. describe('Stripe Metadata Sanitize', () => {
  3. const metadata = {
  4. customerEmail: 'test@gmail.com',
  5. };
  6. it('should sanitize and create new object metadata', () => {
  7. const newMetadata = sanitizeMetadata(metadata);
  8. expect(newMetadata).toEqual(metadata);
  9. expect(newMetadata).not.toBe(metadata);
  10. });
  11. it('should omit fields that have key length exceed 40 characters', () => {
  12. const newMetadata = sanitizeMetadata({
  13. ...metadata,
  14. reallylongkey_reallylongkey_reallylongkey_reallylongkey_reallylongkey: 1,
  15. });
  16. expect(newMetadata).toEqual(metadata);
  17. });
  18. it('should omit fields that have value length exceed 500 characters', () => {
  19. const reallyLongText = Array(501).fill('a').join();
  20. const newMetadata = sanitizeMetadata({
  21. ...metadata,
  22. complexField: reallyLongText,
  23. });
  24. expect(newMetadata).toEqual(metadata);
  25. });
  26. it('should truncate metadata that have more than 50 keys', () => {
  27. const moreThan50KeysMetadata = Array(51)
  28. .fill('a')
  29. .reduce((obj, val, idx) => {
  30. obj[idx] = val;
  31. return obj;
  32. }, {});
  33. const newMetadata = sanitizeMetadata(moreThan50KeysMetadata);
  34. expect(Object.keys(newMetadata).length).toEqual(50);
  35. delete moreThan50KeysMetadata['50'];
  36. expect(newMetadata).toEqual(moreThan50KeysMetadata);
  37. });
  38. });