configuration.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. import type { AbstractDialect, DialectName } from '@sequelize/core';
  3. import {
  4. ConnectionRefusedError,
  5. HostNotReachableError,
  6. InvalidConnectionError,
  7. Sequelize,
  8. } from '@sequelize/core';
  9. import { OPEN_READONLY, OPEN_READWRITE, SqliteDialect } from '@sequelize/sqlite3';
  10. import { expect } from 'chai';
  11. import type { Class } from 'type-fest';
  12. import type { DialectConfigs } from '../config/config';
  13. import { CONFIG } from '../config/config';
  14. import {
  15. destroySequelizeAfterTest,
  16. getSqliteDatabasePath,
  17. getTestDialect,
  18. setResetMode,
  19. unlinkIfExists,
  20. } from './support';
  21. const dialectName = getTestDialect();
  22. describe('Configuration', () => {
  23. setResetMode('none');
  24. // See https://github.com/sequelize/sequelize/issues/17240
  25. it.skip(`throw HostNotReachableError when we don't have the correct server details`, async () => {
  26. const badHostConfigs: DialectConfigs = {
  27. mssql: {
  28. ...CONFIG.mssql,
  29. server: 'fhewiougjhewio.kiwi',
  30. connectTimeout: 100,
  31. },
  32. ibmi: {
  33. ...CONFIG.ibmi,
  34. dataSourceName: 'WRONG',
  35. },
  36. mysql: {
  37. ...CONFIG.mysql,
  38. port: 19_999,
  39. },
  40. mariadb: {
  41. ...CONFIG.mariadb,
  42. port: 19_999,
  43. },
  44. postgres: {
  45. ...CONFIG.postgres,
  46. port: 19_999,
  47. },
  48. snowflake: {
  49. ...CONFIG.snowflake,
  50. account: 'WRONG',
  51. },
  52. db2: {
  53. ...CONFIG.db2,
  54. port: 19_999,
  55. },
  56. sqlite3: {
  57. ...CONFIG.sqlite3,
  58. storage: '/path/to/no/where/land',
  59. mode: OPEN_READONLY,
  60. },
  61. };
  62. const errorByDialect: Record<DialectName, Class<Error>> = {
  63. mssql: ConnectionRefusedError,
  64. ibmi: ConnectionRefusedError,
  65. mysql: ConnectionRefusedError,
  66. mariadb: ConnectionRefusedError,
  67. postgres: ConnectionRefusedError,
  68. snowflake: HostNotReachableError,
  69. db2: ConnectionRefusedError,
  70. sqlite3: InvalidConnectionError,
  71. };
  72. const seq = new Sequelize<AbstractDialect>(badHostConfigs[dialectName]);
  73. destroySequelizeAfterTest(seq);
  74. await expect(seq.query('select 1 as hello')).to.be.rejectedWith(errorByDialect[dialectName]);
  75. });
  76. // See https://github.com/sequelize/sequelize/issues/17240
  77. it.skip('throws ConnectionRefusedError when we have the wrong credentials', async () => {
  78. // The following dialects do not have credentials
  79. if (dialectName === 'sqlite3') {
  80. return;
  81. }
  82. const config: Omit<DialectConfigs, 'sqlite3'> = {
  83. mssql: {
  84. ...CONFIG.mssql,
  85. authentication: {
  86. ...CONFIG.mssql.authentication,
  87. options: {
  88. ...CONFIG.mssql.authentication!.options,
  89. password: 'wrongpassword',
  90. },
  91. },
  92. },
  93. mysql: {
  94. ...CONFIG.mysql,
  95. password: 'wrongpassword',
  96. },
  97. mariadb: {
  98. ...CONFIG.mariadb,
  99. password: 'wrongpassword',
  100. },
  101. postgres: {
  102. ...CONFIG.postgres,
  103. password: 'wrongpassword',
  104. },
  105. snowflake: {
  106. ...CONFIG.snowflake,
  107. password: 'wrongpassword',
  108. },
  109. db2: {
  110. ...CONFIG.db2,
  111. password: 'wrongpassword',
  112. },
  113. ibmi: {
  114. ...CONFIG.ibmi,
  115. password: 'wrongpassword',
  116. },
  117. };
  118. const seq = new Sequelize<AbstractDialect>(config[dialectName]);
  119. destroySequelizeAfterTest(seq);
  120. const query =
  121. dialectName === 'ibmi' ? 'select 1 as hello from SYSIBM.SYSDUMMY1' : 'select 1 as hello';
  122. await expect(seq.query(query)).to.be.rejectedWith(ConnectionRefusedError);
  123. });
  124. it('[sqlite] respects READONLY / READWRITE connection modes', async () => {
  125. if (dialectName !== 'sqlite3') {
  126. return;
  127. }
  128. const dbPath = getSqliteDatabasePath('rw-options-test.sqlite');
  129. await unlinkIfExists(dbPath);
  130. const sequelizeReadOnly0 = new Sequelize({
  131. dialect: SqliteDialect,
  132. storage: dbPath,
  133. mode: OPEN_READONLY,
  134. });
  135. destroySequelizeAfterTest(sequelizeReadOnly0);
  136. const sequelizeReadWrite0 = new Sequelize({
  137. dialect: SqliteDialect,
  138. storage: dbPath,
  139. mode: OPEN_READWRITE,
  140. });
  141. destroySequelizeAfterTest(sequelizeReadWrite0);
  142. expect(sequelizeReadOnly0.options.replication.write.mode).to.equal(OPEN_READONLY);
  143. expect(sequelizeReadWrite0.options.replication.write.mode).to.equal(OPEN_READWRITE);
  144. const createTableFoo = 'CREATE TABLE foo (faz TEXT);';
  145. await Promise.all([
  146. sequelizeReadOnly0
  147. .query(createTableFoo)
  148. .should.be.rejectedWith(Error, 'SQLITE_CANTOPEN: unable to open database file'),
  149. sequelizeReadWrite0
  150. .query(createTableFoo)
  151. .should.be.rejectedWith(Error, 'SQLITE_CANTOPEN: unable to open database file'),
  152. ]);
  153. // By default, sqlite creates a connection that's READWRITE | CREATE
  154. // So this query will create a DB file
  155. const sequelize = new Sequelize({
  156. dialect: SqliteDialect,
  157. storage: dbPath,
  158. });
  159. destroySequelizeAfterTest(sequelize);
  160. await sequelize.query(createTableFoo);
  161. // await testAccess(roPath);
  162. const sequelizeReadOnly = new Sequelize({
  163. dialect: SqliteDialect,
  164. storage: dbPath,
  165. mode: OPEN_READONLY,
  166. });
  167. destroySequelizeAfterTest(sequelizeReadOnly);
  168. const sequelizeReadWrite = new Sequelize({
  169. dialect: SqliteDialect,
  170. storage: dbPath,
  171. mode: OPEN_READWRITE,
  172. });
  173. destroySequelizeAfterTest(sequelizeReadWrite);
  174. const createTableBar = 'CREATE TABLE bar (baz TEXT);';
  175. await Promise.all([
  176. sequelizeReadOnly
  177. .query(createTableBar)
  178. .should.be.rejectedWith(Error, 'SQLITE_READONLY: attempt to write a readonly database'),
  179. sequelizeReadWrite.query(createTableBar),
  180. ]);
  181. });
  182. });