Guest User

Untitled

a guest
Nov 3rd, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. {
  2. "configs": [
  3. {
  4. "automock": false,
  5. "browser": false,
  6. "cache": true,
  7. "cacheDirectory": "/tmp/jest_rs",
  8. "clearMocks": false,
  9. "coveragePathIgnorePatterns": [
  10. "/node_modules/"
  11. ],
  12. "detectLeaks": false,
  13. "detectOpenHandles": false,
  14. "errorOnDeprecated": false,
  15. "filter": null,
  16. "forceCoverageMatch": [],
  17. "globals": {},
  18. "haste": {
  19. "providesModuleNodeModules": []
  20. },
  21. "moduleDirectories": [
  22. "node_modules"
  23. ],
  24. "moduleFileExtensions": [
  25. "js",
  26. "json",
  27. "jsx",
  28. "node"
  29. ],
  30. "moduleNameMapper": {},
  31. "modulePathIgnorePatterns": [],
  32. "name": "9f5155d702743ad8d949d6d219c1bc22",
  33. "prettierPath": null,
  34. "resetMocks": false,
  35. "resetModules": false,
  36. "resolver": null,
  37. "restoreMocks": false,
  38. "rootDir": "/home/mauricio/work/growthbond/gbnd1",
  39. "roots": [
  40. "/home/mauricio/work/growthbond/gbnd1"
  41. ],
  42. "runner": "jest-runner",
  43. "setupFiles": [
  44. "/home/mauricio/work/growthbond/gbnd1/node_modules/regenerator-runtime/runtime.js"
  45. ],
  46. "setupTestFrameworkScriptFile": "/home/mauricio/work/growthbond/gbnd1/testConfig/setupScript.js",
  47. "skipFilter": false,
  48. "snapshotSerializers": [],
  49. "testEnvironment": "/home/mauricio/work/growthbond/gbnd1/testConfig/mongo-environment.js",
  50. "testEnvironmentOptions": {},
  51. "testLocationInResults": false,
  52. "testMatch": [
  53. "**/__tests__/**/*.js?(x)",
  54. "**/?(*.)+(spec|test).js?(x)"
  55. ],
  56. "testPathIgnorePatterns": [
  57. "/node_modules/"
  58. ],
  59. "testRegex": "",
  60. "testRunner": "/home/mauricio/work/growthbond/gbnd1/node_modules/jest-jasmine2/build/index.js",
  61. "testURL": "http://localhost",
  62. "timers": "real",
  63. "transform": [
  64. [
  65. "^.+\.jsx?$",
  66. "/home/mauricio/work/growthbond/gbnd1/node_modules/babel-jest/build/index.js"
  67. ]
  68. ],
  69. "transformIgnorePatterns": [
  70. "/node_modules/"
  71. ],
  72. "watchPathIgnorePatterns": []
  73. }
  74. ],
  75. "globalConfig": {
  76. "bail": false,
  77. "changedFilesWithAncestor": false,
  78. "collectCoverage": false,
  79. "collectCoverageFrom": null,
  80. "coverageDirectory": "/home/mauricio/work/growthbond/gbnd1/coverage",
  81. "coverageReporters": [
  82. "json",
  83. "text",
  84. "lcov",
  85. "clover"
  86. ],
  87. "coverageThreshold": null,
  88. "detectLeaks": false,
  89. "detectOpenHandles": false,
  90. "errorOnDeprecated": false,
  91. "expand": false,
  92. "filter": null,
  93. "globalSetup": "/home/mauricio/work/growthbond/gbnd1/testConfig/setup.js",
  94. "globalTeardown": "/home/mauricio/work/growthbond/gbnd1/testConfig/teardown.js",
  95. "listTests": false,
  96. "maxWorkers": 3,
  97. "noStackTrace": false,
  98. "nonFlagArgs": [],
  99. "notify": false,
  100. "notifyMode": "always",
  101. "passWithNoTests": false,
  102. "projects": null,
  103. "rootDir": "/home/mauricio/work/growthbond/gbnd1",
  104. "runTestsByPath": false,
  105. "skipFilter": false,
  106. "testFailureExitCode": 1,
  107. "testPathPattern": "",
  108. "testResultsProcessor": null,
  109. "updateSnapshot": "new",
  110. "useStderr": false,
  111. "verbose": true,
  112. "watch": false,
  113. "watchman": true
  114. },
  115. "version": "23.6.0"
  116. }
  117.  
  118. "testMatch": [
  119. "**/__tests__/**/*.js?(x)",
  120. "**/?(*.)+(spec|test).js?(x)"
  121. ],
  122.  
  123. module.exports = {
  124. globalSetup: './testConfig/setup.js',
  125. globalTeardown: './testConfig/teardown.js',
  126. testEnvironment: './testConfig/mongo-environment.js',
  127. setupTestFrameworkScriptFile: './testConfig/setupScript.js',
  128. verbose: true
  129. }
  130.  
  131. const path = require('path');
  132. const fs = require('fs');
  133. const MongodbMemoryServer = require('mongodb-memory-server');
  134. const globalConfigPath = path.join(__dirname, 'globalConfig.json');
  135.  
  136. const mongod = new MongodbMemoryServer.default({
  137. instance: {
  138. dbName: 'jest'
  139. },
  140. binary: {
  141. version: '3.2.18'
  142. },
  143. autoStart: false,
  144. });
  145.  
  146. module.exports = async () => {
  147. if (!mongod.isRunning) {
  148. await mongod.start();
  149. }
  150.  
  151. const mongoConfig = {
  152. mongoDBName: 'jest',
  153. mongoUri: await mongod.getConnectionString()
  154. };
  155.  
  156. // Write global config to disk because all tests run in different contexts.
  157. fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
  158. console.log('Config is written');
  159.  
  160. // Set reference to mongod in order to close the server during teardown.
  161. global.__MONGOD__ = mongod;
  162. process.env.MONGO_URL = mongoConfig.mongoUri;
  163. };
  164.  
  165. module.exports = async function() {
  166. await global.__MONGOD__.stop();
  167. };
  168.  
  169. const NodeEnvironment = require('jest-environment-node');
  170. const path = require('path');
  171. const fs = require('fs');
  172. const globalConfigPath = path.join(__dirname, 'globalConfig.json');
  173.  
  174. module.exports = class MongoEnvironment extends NodeEnvironment {
  175. constructor(config) {
  176. super(config);
  177. }
  178.  
  179. async setup() {
  180. console.log('Setup MongoDB Test Environment');
  181.  
  182. const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));
  183.  
  184. this.global.__MONGO_URI__ = globalConfig.mongoUri;
  185. this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName;
  186.  
  187. await super.setup();
  188. }
  189.  
  190. async teardown() {
  191. console.log('Teardown MongoDB Test Environment');
  192.  
  193. await super.teardown();
  194. }
  195.  
  196. runScript(script) {
  197. return super.runScript(script);
  198. }
  199. };
  200.  
  201. const MongoClient= require('mongodb');
  202. const User = require('../../db/models/user');
  203. let connection;
  204. let db;
  205.  
  206. beforeAll(async () => {
  207. connection = await MongoClient.connect(global.__MONGO_URI__);
  208. db = await connection.db(global.__MONGO_DB_NAME__);
  209. });
  210.  
  211. afterAll(async () => {
  212. await connection.close();
  213. await db.close();
  214. });
  215.  
  216.  
  217. describe('Password Encription', async () => {
  218.  
  219. const uEmail = 'test@a.com';
  220. const uPass = '123test';
  221. var testUser = new User({
  222. email:uEmail ,
  223. password: uPass
  224. });
  225.  
  226. await testUser.save()
  227.  
  228. test('Encripted password string is different to plain password', async () => {
  229. user = await User.findOne({ email: uEmail });
  230. expect(user.password).not.toEqual(uPass);
  231. });
  232.  
  233. test('ComparePassword method verify that plain password is the same that encrypted password', async () => {
  234. rightPassword = await user.comparePassword(uPass);
  235. expect(rightPassword).toBeTrue();
  236. });
  237.  
  238. test('ComparePassword method verify that altered plain password is not the same that encrypted password', async () => {
  239. wrongPassword = await user.comparePassword(uPass+'random');
  240. expect(rightPassword).not.toBeTrue();
  241. });
  242.  
  243.  
  244. });
  245.  
  246. require('dotenv').config()
  247. const authS = require('../../services/authService');
  248. const jwt = require('jsonwebtoken');
  249.  
  250. describe('Auth Services',()=>{
  251. const payload = {test:'This is a test'}
  252. const user = {id:101}
  253. const mockSecret = 'SECRET123HAAJAHJSoafdafda'
  254. const token = authS.jwtSign(user,payload)
  255. test('JWT sign', () => {
  256. expect(authS.jwtSign(user,payload)).toBeString();
  257. });
  258.  
  259. test('JWT verify different secret', ()=>{
  260. badToken = jwt.sign(
  261. payload,
  262. mockSecret,
  263. { subject:String(user.id),
  264. expiresIn:'1h'
  265. }
  266. );
  267. expect(()=>authS.jwtVerify(badToken)).toThrowError(jwt.JsonWebTokenError)
  268. })
  269.  
  270. test('JWT verify payload', ()=>{
  271. expect(authS.jwtVerify(authS.jwtSign(user,payload))).toMatchObject(payload)
  272. })
  273.  
  274. })
  275.  
  276. globalSetup: './testConfig/setup.js',
  277. globalTeardown: './testConfig/teardown.js',
  278. testEnvironment: './testConfig/mongo-environment.js',
Add Comment
Please, Sign In to add comment