custom-fields.e2e-spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } 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. const customFields = {
  264. stringWithDefault: 'hello',
  265. localeStringWithDefault: 'hola',
  266. intWithDefault: 5,
  267. floatWithDefault: 5.5,
  268. booleanWithDefault: true,
  269. dateTimeWithDefault: '2019-04-30T12:59:16.415Z',
  270. // MySQL does not support defaults on TEXT fields, which is what "simple-json" uses
  271. // internally. See https://stackoverflow.com/q/3466872/772859
  272. stringListWithDefault: testConfig.dbConnectionOptions.type === 'mysql' ? null : ['cat'],
  273. };
  274. expect(product).toEqual({
  275. id: 'T_1',
  276. name: 'Laptop',
  277. customFields,
  278. });
  279. });
  280. it(
  281. 'update non-nullable field',
  282. assertThrowsWithMessage(async () => {
  283. await adminClient.query(gql`
  284. mutation {
  285. updateProduct(input: { id: "T_1", customFields: { notNullable: null } }) {
  286. id
  287. }
  288. }
  289. `);
  290. }, "The custom field 'notNullable' value cannot be set to null"),
  291. );
  292. it(
  293. 'throws on attempt to update readonly field',
  294. assertThrowsWithMessage(async () => {
  295. await adminClient.query(gql`
  296. mutation {
  297. updateProduct(input: { id: "T_1", customFields: { readonlyString: "hello" } }) {
  298. id
  299. }
  300. }
  301. `);
  302. }, `Field "readonlyString" is not defined by type UpdateProductCustomFieldsInput`),
  303. );
  304. it(
  305. 'throws on attempt to update readonly field when no other custom fields defined',
  306. assertThrowsWithMessage(async () => {
  307. await adminClient.query(gql`
  308. mutation {
  309. updateCustomer(input: { id: "T_1", customFields: { score: 5 } }) {
  310. id
  311. }
  312. }
  313. `);
  314. }, `The custom field 'score' is readonly`),
  315. );
  316. it(
  317. 'throws on attempt to create readonly field',
  318. assertThrowsWithMessage(async () => {
  319. await adminClient.query(gql`
  320. mutation {
  321. createProduct(
  322. input: {
  323. translations: [{ languageCode: en, name: "test" }]
  324. customFields: { readonlyString: "hello" }
  325. }
  326. ) {
  327. id
  328. }
  329. }
  330. `);
  331. }, `Field "readonlyString" is not defined by type CreateProductCustomFieldsInput`),
  332. );
  333. it('string length allows long strings', async () => {
  334. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  335. const result = await adminClient.query(
  336. gql`
  337. mutation($stringValue: String!) {
  338. updateProduct(input: { id: "T_1", customFields: { longString: $stringValue } }) {
  339. id
  340. customFields {
  341. longString
  342. }
  343. }
  344. }
  345. `,
  346. { stringValue: longString },
  347. );
  348. expect(result.updateProduct.customFields.longString).toBe(longString);
  349. });
  350. it('string length allows long localeStrings', async () => {
  351. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  352. const result = await adminClient.query(
  353. gql`
  354. mutation($stringValue: String!) {
  355. updateProduct(
  356. input: {
  357. id: "T_1"
  358. translations: [
  359. { languageCode: en, customFields: { longLocaleString: $stringValue } }
  360. ]
  361. }
  362. ) {
  363. id
  364. customFields {
  365. longLocaleString
  366. }
  367. }
  368. }
  369. `,
  370. { stringValue: longString },
  371. );
  372. expect(result.updateProduct.customFields.longLocaleString).toBe(longString);
  373. });
  374. describe('validation', () => {
  375. it(
  376. 'invalid string',
  377. assertThrowsWithMessage(async () => {
  378. await adminClient.query(gql`
  379. mutation {
  380. updateProduct(input: { id: "T_1", customFields: { validateString: "hello" } }) {
  381. id
  382. }
  383. }
  384. `);
  385. }, `The custom field 'validateString' value ['hello'] does not match the pattern [^[0-9][a-z]+$]`),
  386. );
  387. it(
  388. 'invalid string option',
  389. assertThrowsWithMessage(async () => {
  390. await adminClient.query(gql`
  391. mutation {
  392. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "tiny" } }) {
  393. id
  394. }
  395. }
  396. `);
  397. }, `The custom field 'stringWithOptions' value ['tiny'] is invalid. Valid options are ['small', 'medium', 'large']`),
  398. );
  399. it('valid string option', async () => {
  400. const { updateProduct } = await adminClient.query(gql`
  401. mutation {
  402. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "medium" } }) {
  403. id
  404. customFields {
  405. stringWithOptions
  406. }
  407. }
  408. }
  409. `);
  410. expect(updateProduct.customFields.stringWithOptions).toBe('medium');
  411. });
  412. it(
  413. 'invalid localeString',
  414. assertThrowsWithMessage(async () => {
  415. await adminClient.query(gql`
  416. mutation {
  417. updateProduct(
  418. input: {
  419. id: "T_1"
  420. translations: [
  421. {
  422. id: "T_1"
  423. languageCode: en
  424. customFields: { validateLocaleString: "servus" }
  425. }
  426. ]
  427. }
  428. ) {
  429. id
  430. }
  431. }
  432. `);
  433. }, `The custom field 'validateLocaleString' value ['servus'] does not match the pattern [^[0-9][a-z]+$]`),
  434. );
  435. it(
  436. 'invalid int',
  437. assertThrowsWithMessage(async () => {
  438. await adminClient.query(gql`
  439. mutation {
  440. updateProduct(input: { id: "T_1", customFields: { validateInt: 12 } }) {
  441. id
  442. }
  443. }
  444. `);
  445. }, `The custom field 'validateInt' value [12] is greater than the maximum [10]`),
  446. );
  447. it(
  448. 'invalid float',
  449. assertThrowsWithMessage(async () => {
  450. await adminClient.query(gql`
  451. mutation {
  452. updateProduct(input: { id: "T_1", customFields: { validateFloat: 10.6 } }) {
  453. id
  454. }
  455. }
  456. `);
  457. }, `The custom field 'validateFloat' value [10.6] is greater than the maximum [10.5]`),
  458. );
  459. it(
  460. 'invalid datetime',
  461. assertThrowsWithMessage(async () => {
  462. await adminClient.query(gql`
  463. mutation {
  464. updateProduct(
  465. input: {
  466. id: "T_1"
  467. customFields: { validateDateTime: "2019-01-01T05:25:00.000Z" }
  468. }
  469. ) {
  470. id
  471. }
  472. }
  473. `);
  474. }, `The custom field 'validateDateTime' value [2019-01-01T05:25:00.000Z] is less than the minimum [2019-01-01T08:30]`),
  475. );
  476. it(
  477. 'invalid validate function with string',
  478. assertThrowsWithMessage(async () => {
  479. await adminClient.query(gql`
  480. mutation {
  481. updateProduct(input: { id: "T_1", customFields: { validateFn1: "invalid" } }) {
  482. id
  483. }
  484. }
  485. `);
  486. }, `The value ['invalid'] is not valid`),
  487. );
  488. it(
  489. 'invalid validate function with localized string',
  490. assertThrowsWithMessage(async () => {
  491. await adminClient.query(gql`
  492. mutation {
  493. updateProduct(input: { id: "T_1", customFields: { validateFn2: "invalid" } }) {
  494. id
  495. }
  496. }
  497. `);
  498. }, `The value ['invalid'] is not valid`),
  499. );
  500. it(
  501. 'invalid list field',
  502. assertThrowsWithMessage(async () => {
  503. await adminClient.query(gql`
  504. mutation {
  505. updateProduct(
  506. input: { id: "T_1", customFields: { intListWithValidation: [1, 2, 3] } }
  507. ) {
  508. id
  509. }
  510. }
  511. `);
  512. }, `Must include the number 42!`),
  513. );
  514. it('valid list field', async () => {
  515. const { updateProduct } = await adminClient.query(gql`
  516. mutation {
  517. updateProduct(input: { id: "T_1", customFields: { intListWithValidation: [1, 42, 3] } }) {
  518. id
  519. customFields {
  520. intListWithValidation
  521. }
  522. }
  523. }
  524. `);
  525. expect(updateProduct.customFields.intListWithValidation).toEqual([1, 42, 3]);
  526. });
  527. });
  528. describe('public access', () => {
  529. it(
  530. 'non-public throws for Shop API',
  531. assertThrowsWithMessage(async () => {
  532. await shopClient.query(gql`
  533. query {
  534. product(id: "T_1") {
  535. id
  536. customFields {
  537. nonPublic
  538. }
  539. }
  540. }
  541. `);
  542. }, `Cannot query field "nonPublic" on type "ProductCustomFields"`),
  543. );
  544. it('publicly accessible via Shop API', async () => {
  545. const { product } = await shopClient.query(gql`
  546. query {
  547. product(id: "T_1") {
  548. id
  549. customFields {
  550. public
  551. }
  552. }
  553. }
  554. `);
  555. expect(product.customFields.public).toBe('ho!');
  556. });
  557. it(
  558. 'internal throws for Shop API',
  559. assertThrowsWithMessage(async () => {
  560. await shopClient.query(gql`
  561. query {
  562. product(id: "T_1") {
  563. id
  564. customFields {
  565. internalString
  566. }
  567. }
  568. }
  569. `);
  570. }, `Cannot query field "internalString" on type "ProductCustomFields"`),
  571. );
  572. it(
  573. 'internal throws for Admin API',
  574. assertThrowsWithMessage(async () => {
  575. await adminClient.query(gql`
  576. query {
  577. product(id: "T_1") {
  578. id
  579. customFields {
  580. internalString
  581. }
  582. }
  583. }
  584. `);
  585. }, `Cannot query field "internalString" on type "ProductCustomFields"`),
  586. );
  587. });
  588. describe('sort & filter', () => {
  589. it('can sort by custom fields', async () => {
  590. const { products } = await adminClient.query(gql`
  591. query {
  592. products(options: { sort: { nullable: ASC } }) {
  593. totalItems
  594. }
  595. }
  596. `);
  597. expect(products.totalItems).toBe(1);
  598. });
  599. it('can filter by custom fields', async () => {
  600. const { products } = await adminClient.query(gql`
  601. query {
  602. products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
  603. totalItems
  604. }
  605. }
  606. `);
  607. expect(products.totalItems).toBe(1);
  608. });
  609. it(
  610. 'cannot filter by internal field in Admin API',
  611. assertThrowsWithMessage(async () => {
  612. await adminClient.query(gql`
  613. query {
  614. products(options: { filter: { internalString: { contains: "hello" } } }) {
  615. totalItems
  616. }
  617. }
  618. `);
  619. }, `Field "internalString" is not defined by type ProductFilterParameter`),
  620. );
  621. it(
  622. 'cannot filter by internal field in Shop API',
  623. assertThrowsWithMessage(async () => {
  624. await shopClient.query(gql`
  625. query {
  626. products(options: { filter: { internalString: { contains: "hello" } } }) {
  627. totalItems
  628. }
  629. }
  630. `);
  631. }, `Field "internalString" is not defined by type ProductFilterParameter`),
  632. );
  633. });
  634. });