custom-fields.e2e-spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { CustomFields, mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  8. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  9. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  10. fixPostgresTimezone();
  11. // tslint:disable:no-non-null-assertion
  12. const customConfig = mergeConfig(testConfig, {
  13. dbConnectionOptions: {
  14. timezone: 'Z',
  15. },
  16. customFields: {
  17. Product: [
  18. { name: 'nullable', type: 'string' },
  19. { name: 'notNullable', type: 'string', nullable: false, defaultValue: '' },
  20. { name: 'stringWithDefault', type: 'string', defaultValue: 'hello' },
  21. { name: 'localeStringWithDefault', type: 'localeString', defaultValue: 'hola' },
  22. { name: 'intWithDefault', type: 'int', defaultValue: 5 },
  23. { name: 'floatWithDefault', type: 'float', defaultValue: 5.5 },
  24. { name: 'booleanWithDefault', type: 'boolean', defaultValue: true },
  25. {
  26. name: 'dateTimeWithDefault',
  27. type: 'datetime',
  28. defaultValue: new Date('2019-04-30T12:59:16.4158386Z'),
  29. },
  30. { name: 'validateString', type: 'string', pattern: '^[0-9][a-z]+$' },
  31. { name: 'validateLocaleString', type: 'localeString', pattern: '^[0-9][a-z]+$' },
  32. { name: 'validateInt', type: 'int', min: 0, max: 10 },
  33. { name: 'validateFloat', type: 'float', min: 0.5, max: 10.5 },
  34. {
  35. name: 'validateDateTime',
  36. type: 'datetime',
  37. min: '2019-01-01T08:30',
  38. max: '2019-06-01T08:30',
  39. },
  40. {
  41. name: 'validateFn1',
  42. type: 'string',
  43. validate: value => {
  44. if (value !== 'valid') {
  45. return `The value ['${value}'] is not valid`;
  46. }
  47. },
  48. },
  49. {
  50. name: 'validateFn2',
  51. type: 'string',
  52. validate: value => {
  53. if (value !== 'valid') {
  54. return [
  55. {
  56. languageCode: LanguageCode.en,
  57. value: `The value ['${value}'] is not valid`,
  58. },
  59. ];
  60. }
  61. },
  62. },
  63. {
  64. name: 'stringWithOptions',
  65. type: 'string',
  66. options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
  67. },
  68. {
  69. name: 'nonPublic',
  70. type: 'string',
  71. defaultValue: 'hi!',
  72. public: false,
  73. },
  74. {
  75. name: 'public',
  76. type: 'string',
  77. defaultValue: 'ho!',
  78. public: true,
  79. },
  80. {
  81. name: 'longString',
  82. type: 'string',
  83. length: 10000,
  84. },
  85. {
  86. name: 'longLocaleString',
  87. type: 'localeString',
  88. length: 10000,
  89. },
  90. {
  91. name: 'readonlyString',
  92. type: 'string',
  93. readonly: true,
  94. },
  95. {
  96. name: 'internalString',
  97. type: 'string',
  98. internal: true,
  99. },
  100. {
  101. name: 'stringList',
  102. type: 'string',
  103. list: true,
  104. },
  105. {
  106. name: 'localeStringList',
  107. type: 'localeString',
  108. list: true,
  109. },
  110. {
  111. name: 'stringListWithDefault',
  112. type: 'string',
  113. list: true,
  114. defaultValue: ['cat'],
  115. },
  116. {
  117. name: 'intListWithValidation',
  118. type: 'int',
  119. list: true,
  120. validate: value => {
  121. if (!value.includes(42)) {
  122. return `Must include the number 42!`;
  123. }
  124. },
  125. },
  126. ],
  127. Facet: [
  128. {
  129. name: 'translated',
  130. type: 'localeString',
  131. },
  132. ],
  133. Customer: [
  134. {
  135. name: 'score',
  136. type: 'int',
  137. readonly: true,
  138. },
  139. ],
  140. } as CustomFields,
  141. });
  142. describe('Custom fields', () => {
  143. const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
  144. beforeAll(async () => {
  145. await server.init({
  146. initialData,
  147. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  148. customerCount: 1,
  149. });
  150. await adminClient.asSuperAdmin();
  151. }, TEST_SETUP_TIMEOUT_MS);
  152. afterAll(async () => {
  153. await server.destroy();
  154. });
  155. it('globalSettings.serverConfig.customFieldConfig', async () => {
  156. const { globalSettings } = await adminClient.query(gql`
  157. query {
  158. globalSettings {
  159. serverConfig {
  160. customFieldConfig {
  161. Product {
  162. ... on CustomField {
  163. name
  164. type
  165. list
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. `);
  173. expect(globalSettings.serverConfig.customFieldConfig).toEqual({
  174. Product: [
  175. { name: 'nullable', type: 'string', list: false },
  176. { name: 'notNullable', type: 'string', list: false },
  177. { name: 'stringWithDefault', type: 'string', list: false },
  178. { name: 'localeStringWithDefault', type: 'localeString', list: false },
  179. { name: 'intWithDefault', type: 'int', list: false },
  180. { name: 'floatWithDefault', type: 'float', list: false },
  181. { name: 'booleanWithDefault', type: 'boolean', list: false },
  182. { name: 'dateTimeWithDefault', type: 'datetime', list: false },
  183. { name: 'validateString', type: 'string', list: false },
  184. { name: 'validateLocaleString', type: 'localeString', list: false },
  185. { name: 'validateInt', type: 'int', list: false },
  186. { name: 'validateFloat', type: 'float', list: false },
  187. { name: 'validateDateTime', type: 'datetime', list: false },
  188. { name: 'validateFn1', type: 'string', list: false },
  189. { name: 'validateFn2', type: 'string', list: false },
  190. { name: 'stringWithOptions', type: 'string', list: false },
  191. { name: 'nonPublic', type: 'string', list: false },
  192. { name: 'public', type: 'string', list: false },
  193. { name: 'longString', type: 'string', list: false },
  194. { name: 'longLocaleString', type: 'localeString', list: false },
  195. { name: 'readonlyString', type: 'string', list: false },
  196. { name: 'stringList', type: 'string', list: true },
  197. { name: 'localeStringList', type: 'localeString', list: true },
  198. { name: 'stringListWithDefault', type: 'string', list: true },
  199. { name: 'intListWithValidation', type: 'int', list: true },
  200. // The internal type should not be exposed at all
  201. // { name: 'internalString', type: 'string' },
  202. ],
  203. });
  204. });
  205. it('get nullable with no default', async () => {
  206. const { product } = await adminClient.query(gql`
  207. query {
  208. product(id: "T_1") {
  209. id
  210. name
  211. customFields {
  212. nullable
  213. }
  214. }
  215. }
  216. `);
  217. expect(product).toEqual({
  218. id: 'T_1',
  219. name: 'Laptop',
  220. customFields: {
  221. nullable: null,
  222. },
  223. });
  224. });
  225. it('get entity with localeString only', async () => {
  226. const { facet } = await adminClient.query(gql`
  227. query {
  228. facet(id: "T_1") {
  229. id
  230. name
  231. customFields {
  232. translated
  233. }
  234. }
  235. }
  236. `);
  237. expect(facet).toEqual({
  238. id: 'T_1',
  239. name: 'category',
  240. customFields: {
  241. translated: null,
  242. },
  243. });
  244. });
  245. it('get fields with default values', async () => {
  246. const { product } = await adminClient.query(gql`
  247. query {
  248. product(id: "T_1") {
  249. id
  250. name
  251. customFields {
  252. stringWithDefault
  253. localeStringWithDefault
  254. intWithDefault
  255. floatWithDefault
  256. booleanWithDefault
  257. dateTimeWithDefault
  258. stringListWithDefault
  259. }
  260. }
  261. }
  262. `);
  263. expect(product).toEqual({
  264. id: 'T_1',
  265. name: 'Laptop',
  266. customFields: {
  267. stringWithDefault: 'hello',
  268. localeStringWithDefault: 'hola',
  269. intWithDefault: 5,
  270. floatWithDefault: 5.5,
  271. booleanWithDefault: true,
  272. dateTimeWithDefault: '2019-04-30T12:59:16.415Z',
  273. stringListWithDefault: ['cat'],
  274. },
  275. });
  276. });
  277. it(
  278. 'update non-nullable field',
  279. assertThrowsWithMessage(async () => {
  280. await adminClient.query(gql`
  281. mutation {
  282. updateProduct(input: { id: "T_1", customFields: { notNullable: null } }) {
  283. id
  284. }
  285. }
  286. `);
  287. }, "The custom field 'notNullable' value cannot be set to null"),
  288. );
  289. it(
  290. 'throws on attempt to update readonly field',
  291. assertThrowsWithMessage(async () => {
  292. await adminClient.query(gql`
  293. mutation {
  294. updateProduct(input: { id: "T_1", customFields: { readonlyString: "hello" } }) {
  295. id
  296. }
  297. }
  298. `);
  299. }, `Field "readonlyString" is not defined by type UpdateProductCustomFieldsInput`),
  300. );
  301. it(
  302. 'throws on attempt to update readonly field when no other custom fields defined',
  303. assertThrowsWithMessage(async () => {
  304. await adminClient.query(gql`
  305. mutation {
  306. updateCustomer(input: { id: "T_1", customFields: { score: 5 } }) {
  307. id
  308. }
  309. }
  310. `);
  311. }, `The custom field 'score' is readonly`),
  312. );
  313. it(
  314. 'throws on attempt to create readonly field',
  315. assertThrowsWithMessage(async () => {
  316. await adminClient.query(gql`
  317. mutation {
  318. createProduct(
  319. input: {
  320. translations: [{ languageCode: en, name: "test" }]
  321. customFields: { readonlyString: "hello" }
  322. }
  323. ) {
  324. id
  325. }
  326. }
  327. `);
  328. }, `Field "readonlyString" is not defined by type CreateProductCustomFieldsInput`),
  329. );
  330. it('string length allows long strings', async () => {
  331. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  332. const result = await adminClient.query(
  333. gql`
  334. mutation($stringValue: String!) {
  335. updateProduct(input: { id: "T_1", customFields: { longString: $stringValue } }) {
  336. id
  337. customFields {
  338. longString
  339. }
  340. }
  341. }
  342. `,
  343. { stringValue: longString },
  344. );
  345. expect(result.updateProduct.customFields.longString).toBe(longString);
  346. });
  347. it('string length allows long localeStrings', async () => {
  348. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  349. const result = await adminClient.query(
  350. gql`
  351. mutation($stringValue: String!) {
  352. updateProduct(
  353. input: {
  354. id: "T_1"
  355. translations: [
  356. { languageCode: en, customFields: { longLocaleString: $stringValue } }
  357. ]
  358. }
  359. ) {
  360. id
  361. customFields {
  362. longLocaleString
  363. }
  364. }
  365. }
  366. `,
  367. { stringValue: longString },
  368. );
  369. expect(result.updateProduct.customFields.longLocaleString).toBe(longString);
  370. });
  371. describe('validation', () => {
  372. it(
  373. 'invalid string',
  374. assertThrowsWithMessage(async () => {
  375. await adminClient.query(gql`
  376. mutation {
  377. updateProduct(input: { id: "T_1", customFields: { validateString: "hello" } }) {
  378. id
  379. }
  380. }
  381. `);
  382. }, `The custom field 'validateString' value ['hello'] does not match the pattern [^[0-9][a-z]+$]`),
  383. );
  384. it(
  385. 'invalid string option',
  386. assertThrowsWithMessage(async () => {
  387. await adminClient.query(gql`
  388. mutation {
  389. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "tiny" } }) {
  390. id
  391. }
  392. }
  393. `);
  394. }, `The custom field 'stringWithOptions' value ['tiny'] is invalid. Valid options are ['small', 'medium', 'large']`),
  395. );
  396. it('valid string option', async () => {
  397. const { updateProduct } = await adminClient.query(gql`
  398. mutation {
  399. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "medium" } }) {
  400. id
  401. customFields {
  402. stringWithOptions
  403. }
  404. }
  405. }
  406. `);
  407. expect(updateProduct.customFields.stringWithOptions).toBe('medium');
  408. });
  409. it(
  410. 'invalid localeString',
  411. assertThrowsWithMessage(async () => {
  412. await adminClient.query(gql`
  413. mutation {
  414. updateProduct(
  415. input: {
  416. id: "T_1"
  417. translations: [
  418. {
  419. id: "T_1"
  420. languageCode: en
  421. customFields: { validateLocaleString: "servus" }
  422. }
  423. ]
  424. }
  425. ) {
  426. id
  427. }
  428. }
  429. `);
  430. }, `The custom field 'validateLocaleString' value ['servus'] does not match the pattern [^[0-9][a-z]+$]`),
  431. );
  432. it(
  433. 'invalid int',
  434. assertThrowsWithMessage(async () => {
  435. await adminClient.query(gql`
  436. mutation {
  437. updateProduct(input: { id: "T_1", customFields: { validateInt: 12 } }) {
  438. id
  439. }
  440. }
  441. `);
  442. }, `The custom field 'validateInt' value [12] is greater than the maximum [10]`),
  443. );
  444. it(
  445. 'invalid float',
  446. assertThrowsWithMessage(async () => {
  447. await adminClient.query(gql`
  448. mutation {
  449. updateProduct(input: { id: "T_1", customFields: { validateFloat: 10.6 } }) {
  450. id
  451. }
  452. }
  453. `);
  454. }, `The custom field 'validateFloat' value [10.6] is greater than the maximum [10.5]`),
  455. );
  456. it(
  457. 'invalid datetime',
  458. assertThrowsWithMessage(async () => {
  459. await adminClient.query(gql`
  460. mutation {
  461. updateProduct(
  462. input: {
  463. id: "T_1"
  464. customFields: { validateDateTime: "2019-01-01T05:25:00.000Z" }
  465. }
  466. ) {
  467. id
  468. }
  469. }
  470. `);
  471. }, `The custom field 'validateDateTime' value [2019-01-01T05:25:00.000Z] is less than the minimum [2019-01-01T08:30]`),
  472. );
  473. it(
  474. 'invalid validate function with string',
  475. assertThrowsWithMessage(async () => {
  476. await adminClient.query(gql`
  477. mutation {
  478. updateProduct(input: { id: "T_1", customFields: { validateFn1: "invalid" } }) {
  479. id
  480. }
  481. }
  482. `);
  483. }, `The value ['invalid'] is not valid`),
  484. );
  485. it(
  486. 'invalid validate function with localized string',
  487. assertThrowsWithMessage(async () => {
  488. await adminClient.query(gql`
  489. mutation {
  490. updateProduct(input: { id: "T_1", customFields: { validateFn2: "invalid" } }) {
  491. id
  492. }
  493. }
  494. `);
  495. }, `The value ['invalid'] is not valid`),
  496. );
  497. it(
  498. 'invalid list field',
  499. assertThrowsWithMessage(async () => {
  500. await adminClient.query(gql`
  501. mutation {
  502. updateProduct(
  503. input: { id: "T_1", customFields: { intListWithValidation: [1, 2, 3] } }
  504. ) {
  505. id
  506. }
  507. }
  508. `);
  509. }, `Must include the number 42!`),
  510. );
  511. it('valid list field', async () => {
  512. const { updateProduct } = await adminClient.query(gql`
  513. mutation {
  514. updateProduct(input: { id: "T_1", customFields: { intListWithValidation: [1, 42, 3] } }) {
  515. id
  516. customFields {
  517. intListWithValidation
  518. }
  519. }
  520. }
  521. `);
  522. expect(updateProduct.customFields.intListWithValidation).toEqual([1, 42, 3]);
  523. });
  524. });
  525. describe('public access', () => {
  526. it(
  527. 'non-public throws for Shop API',
  528. assertThrowsWithMessage(async () => {
  529. await shopClient.query(gql`
  530. query {
  531. product(id: "T_1") {
  532. id
  533. customFields {
  534. nonPublic
  535. }
  536. }
  537. }
  538. `);
  539. }, `Cannot query field "nonPublic" on type "ProductCustomFields"`),
  540. );
  541. it('publicly accessible via Shop API', async () => {
  542. const { product } = await shopClient.query(gql`
  543. query {
  544. product(id: "T_1") {
  545. id
  546. customFields {
  547. public
  548. }
  549. }
  550. }
  551. `);
  552. expect(product.customFields.public).toBe('ho!');
  553. });
  554. it(
  555. 'internal throws for Shop API',
  556. assertThrowsWithMessage(async () => {
  557. await shopClient.query(gql`
  558. query {
  559. product(id: "T_1") {
  560. id
  561. customFields {
  562. internalString
  563. }
  564. }
  565. }
  566. `);
  567. }, `Cannot query field "internalString" on type "ProductCustomFields"`),
  568. );
  569. it(
  570. 'internal throws for Admin API',
  571. assertThrowsWithMessage(async () => {
  572. await adminClient.query(gql`
  573. query {
  574. product(id: "T_1") {
  575. id
  576. customFields {
  577. internalString
  578. }
  579. }
  580. }
  581. `);
  582. }, `Cannot query field "internalString" on type "ProductCustomFields"`),
  583. );
  584. });
  585. describe('sort & filter', () => {
  586. it('can sort by custom fields', async () => {
  587. const { products } = await adminClient.query(gql`
  588. query {
  589. products(options: { sort: { nullable: ASC } }) {
  590. totalItems
  591. }
  592. }
  593. `);
  594. expect(products.totalItems).toBe(1);
  595. });
  596. it('can filter by custom fields', async () => {
  597. const { products } = await adminClient.query(gql`
  598. query {
  599. products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
  600. totalItems
  601. }
  602. }
  603. `);
  604. expect(products.totalItems).toBe(1);
  605. });
  606. it(
  607. 'cannot filter by internal field in Admin API',
  608. assertThrowsWithMessage(async () => {
  609. await adminClient.query(gql`
  610. query {
  611. products(options: { filter: { internalString: { contains: "hello" } } }) {
  612. totalItems
  613. }
  614. }
  615. `);
  616. }, `Field "internalString" is not defined by type ProductFilterParameter`),
  617. );
  618. it(
  619. 'cannot filter by internal field in Shop API',
  620. assertThrowsWithMessage(async () => {
  621. await shopClient.query(gql`
  622. query {
  623. products(options: { filter: { internalString: { contains: "hello" } } }) {
  624. totalItems
  625. }
  626. }
  627. `);
  628. }, `Field "internalString" is not defined by type ProductFilterParameter`),
  629. );
  630. });
  631. });