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

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