request-context.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { beforeAll, describe, expect, it } from 'vitest';
  3. import { CachedSession } from '../../config/session-cache/session-cache-strategy';
  4. import { Channel } from '../../entity/channel/channel.entity';
  5. import { Order } from '../../entity/order/order.entity';
  6. import { Zone } from '../../entity/zone/zone.entity';
  7. import { RequestContext, SerializedRequestContext } from './request-context';
  8. describe('RequestContext', () => {
  9. describe('serialize/deserialize', () => {
  10. let serializedCtx: SerializedRequestContext;
  11. let original: RequestContext;
  12. beforeAll(() => {
  13. original = createRequestContext();
  14. serializedCtx = original.serialize();
  15. });
  16. it('apiType', () => {
  17. const result = RequestContext.deserialize(serializedCtx);
  18. expect(result.apiType).toBe(original.apiType);
  19. });
  20. it('channelId', () => {
  21. const result = RequestContext.deserialize(serializedCtx);
  22. expect(result.channelId).toBe(original.channelId);
  23. });
  24. it('languageCode', () => {
  25. const result = RequestContext.deserialize(serializedCtx);
  26. expect(result.languageCode).toBe(original.languageCode);
  27. });
  28. it('activeUserId', () => {
  29. const result = RequestContext.deserialize(serializedCtx);
  30. expect(result.activeUserId).toBe(original.activeUserId);
  31. });
  32. it('isAuthorized', () => {
  33. const result = RequestContext.deserialize(serializedCtx);
  34. expect(result.isAuthorized).toBe(original.isAuthorized);
  35. });
  36. it('authorizedAsOwnerOnly', () => {
  37. const result = RequestContext.deserialize(serializedCtx);
  38. expect(result.authorizedAsOwnerOnly).toBe(original.authorizedAsOwnerOnly);
  39. });
  40. it('channel', () => {
  41. const result = RequestContext.deserialize(serializedCtx);
  42. expect(result.channel).toEqual(original.channel);
  43. });
  44. it('session', () => {
  45. const result = RequestContext.deserialize(serializedCtx);
  46. expect(result.session).toEqual(original.session);
  47. });
  48. // https://github.com/vendure-ecommerce/vendure/issues/864
  49. it('serialize request context with circular refs', () => {
  50. const cyclic: any = {};
  51. const cyclic1: any = {
  52. prop: cyclic,
  53. };
  54. cyclic.prop = cyclic1;
  55. const requestContext = createRequestContext({
  56. simple: 'foo',
  57. arr: [1, 2, 3],
  58. cycle: cyclic,
  59. cycleArr: [cyclic, cyclic],
  60. });
  61. const serialized = requestContext.serialize();
  62. expect(serialized._req).toEqual({
  63. simple: 'foo',
  64. arr: [1, 2, 3],
  65. cycle: {},
  66. cycleArr: [{}, {}],
  67. });
  68. });
  69. });
  70. describe('copy', () => {
  71. let original: RequestContext;
  72. beforeAll(() => {
  73. original = createRequestContext();
  74. });
  75. it('is a RequestContext instance', () => {
  76. const copy = original.copy();
  77. expect(copy instanceof RequestContext).toBe(true);
  78. });
  79. it('is not identical to original', () => {
  80. const copy = original.copy();
  81. expect(copy === original).toBe(false);
  82. });
  83. it('getters work', () => {
  84. const copy = original.copy();
  85. expect(copy.apiType).toEqual(original.apiType);
  86. expect(copy.channelId).toEqual(original.channelId);
  87. expect(copy.languageCode).toEqual(original.languageCode);
  88. expect(copy.activeUserId).toEqual(original.activeUserId);
  89. expect(copy.isAuthorized).toEqual(original.isAuthorized);
  90. expect(copy.authorizedAsOwnerOnly).toEqual(original.authorizedAsOwnerOnly);
  91. expect(copy.channel).toEqual(original.channel);
  92. expect(copy.session).toEqual(original.session);
  93. });
  94. it('mutating copy leaves original intact', () => {
  95. const copy = original.copy();
  96. (copy as any).foo = 'bar';
  97. expect((copy as any).foo).toBe('bar');
  98. expect((original as any).foo).toBeUndefined();
  99. });
  100. it('mutating deep property affects both', () => {
  101. const copy = original.copy();
  102. copy.channel.code = 'changed';
  103. expect(copy.channel.code).toBe('changed');
  104. expect(original.channel.code).toBe('changed');
  105. });
  106. });
  107. function createRequestContext(req?: any) {
  108. const activeOrder = new Order({
  109. id: '55555',
  110. active: true,
  111. code: 'ADAWDJAWD',
  112. });
  113. const session = {
  114. cacheExpiry: Number.MAX_SAFE_INTEGER,
  115. expires: new Date(),
  116. id: '1234',
  117. token: '2d37187e9e8fc47807fe4f58ca',
  118. activeOrderId: '123',
  119. user: {
  120. id: '8833774',
  121. identifier: 'user',
  122. verified: true,
  123. channelPermissions: [],
  124. },
  125. } satisfies CachedSession;
  126. const zone = new Zone({
  127. id: '62626',
  128. name: 'Europe',
  129. });
  130. const channel = new Channel({
  131. token: 'oiajwodij09au3r',
  132. id: '995859',
  133. code: '__default_channel__',
  134. defaultCurrencyCode: CurrencyCode.EUR,
  135. pricesIncludeTax: true,
  136. defaultLanguageCode: LanguageCode.en,
  137. defaultShippingZone: zone,
  138. defaultTaxZone: zone,
  139. });
  140. return new RequestContext({
  141. apiType: 'admin',
  142. languageCode: LanguageCode.en,
  143. channel,
  144. session,
  145. req: req ?? {},
  146. isAuthorized: true,
  147. authorizedAsOwnerOnly: false,
  148. });
  149. }
  150. });