From a30b0996c34f15550139c7ba09178da7439c2937 Mon Sep 17 00:00:00 2001 From: Jose Miguel Gallas Olmedo Date: Thu, 20 Jul 2017 20:09:02 +0200 Subject: [PATCH 1/4] adds travis.yaml --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fa230e3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - "4" +env: + - ENV_NODE="development" +before_script: + - npm install +script: + - node dist/src/server.js & + - npm test \ No newline at end of file From 15738c0c1802ce5364a039b973455b92a828cd40 Mon Sep 17 00:00:00 2001 From: Jose Miguel Gallas Olmedo Date: Fri, 21 Jul 2017 10:39:59 +0200 Subject: [PATCH 2/4] removes default parameter and adds some delay in spec for deleting object --- package-lock.json | 10 ++++------ package.json | 6 +++--- spec/routes/users.spec.ts | 5 ++++- src/repository/realm-helper.ts | 2 +- src/utils/sleep.ts | 13 +++++++++++++ tsconfig.json | 2 +- 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/utils/sleep.ts diff --git a/package-lock.json b/package-lock.json index 4468a53..7af56b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,6 @@ "version": "0.0.33", "resolved": "https://registry.npmjs.org/@types/form-data/-/form-data-0.0.33.tgz", "integrity": "sha1-yayFsqX9GENbjIXZ7LUObWyJP/g=", - "dev": true, "requires": { "@types/node": "8.0.13" } @@ -56,10 +55,9 @@ "dev": true }, "@types/request": { - "version": "0.0.47", - "resolved": "https://registry.npmjs.org/@types/request/-/request-0.0.47.tgz", - "integrity": "sha512-RAG3/f+dHwlC8R17loSB29zrKvEOQvTGUtgrNjKE8nXUkYxtV4IzEGO1ft+JMX0aa06pi1ed6b826e81eiwYdw==", - "dev": true, + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@types/request/-/request-2.0.0.tgz", + "integrity": "sha512-K/LAMXQn9HwiewHuNHHmwNrg4E8XUYqB99LSrObb3JArs3SHVzbzft113rbYcjPWStaqtpDpOhcjF3zCRqSvow==", "requires": { "@types/form-data": "0.0.33", "@types/node": "8.0.13" @@ -71,7 +69,7 @@ "integrity": "sha512-SbsRP/CnMW09mF8mIfOBgQzSJJKZkSbXIC3g1RSKgZSGNz5MK7dedxImr30ekjY71WzRRtQh4VwiAaHA3+Uu8w==", "dev": true, "requires": { - "@types/request": "0.0.47" + "@types/request": "2.0.0" } }, "@types/serve-static": { diff --git a/package.json b/package.json index cd29d8a..70914f3 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "users-api", "version": "0.1.0", - "description": "This API allows all RESTL operations over the dataset hosted in https://gist.githubusercontent.com/jasonmadigan/009c15b5dc4b4eccd32b/raw/34759c44e77d2f3515e20ed561cdd7a5e8345585/users.json.", + "description": "This API allows all CRUDL operations over the dataset hosted in https://gist.githubusercontent.com/jasonmadigan/009c15b5dc4b4eccd32b/raw/34759c44e77d2f3515e20ed561cdd7a5e8345585/users.json.", "scripts": { "start": "node dist/src/server.js", "postinstall": "node_modules/typescript/bin/tsc -p . && cp src/repository/seed.json dist/src/repository/", "test": "node_modules/.bin/jasmine", - "posttest": "rm -r database/test", - "clean": "rm -r node_modules database realm-object-server dist" + "clean": "rm -rf node_modules database realm-object-server dist" }, "repository": { "type": "git", @@ -17,6 +16,7 @@ "license": "ISC", "dependencies": { "@types/node": "^8.0.13", + "@types/request": "^2.0.0", "body-parser": "^1.17.2", "express": "^4.15.3", "realm": "^1.10.0", diff --git a/spec/routes/users.spec.ts b/spec/routes/users.spec.ts index 1e98207..4489877 100644 --- a/spec/routes/users.spec.ts +++ b/spec/routes/users.spec.ts @@ -1,4 +1,5 @@ import RealmHelper from "../../src/repository/realm-helper"; +import Sleep from "../../src/utils/sleep"; import User from "../../src/model/user"; import UserDateFilter from "../../src/model/user-date-filter"; @@ -504,9 +505,11 @@ describe("Route users", () => { ApiTestClient .deleteUser("josemigallas") - .then(res => { + .then(async res => { expect(res.statusCode).toEqual(200); + await Sleep.millis(200); + const user: User = RealmHelper.getUserByUsername("josemigallas"); expect(user).toBeFalsy(); diff --git a/src/repository/realm-helper.ts b/src/repository/realm-helper.ts index f1ff66d..091553b 100644 --- a/src/repository/realm-helper.ts +++ b/src/repository/realm-helper.ts @@ -34,7 +34,7 @@ export default class RealmHelper { return this._config; } - public static init(development = false): void { + public static init(development): void { if (development) { this._config.path = "database/test/users"; } diff --git a/src/utils/sleep.ts b/src/utils/sleep.ts new file mode 100644 index 0000000..b249a90 --- /dev/null +++ b/src/utils/sleep.ts @@ -0,0 +1,13 @@ +export default class Sleep { + + /** + * Waits a certain time of milliseconds. + * @param ms Milliseconds to wait + * @example + * await sleep(1000); + */ + public static millis(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + +} diff --git a/tsconfig.json b/tsconfig.json index d84080e..3eb45b9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "rootDir": ".", "sourceMap": true, "module": "commonjs", - "target": "es5" + "target": "es2015" }, "include": [ "./src/**/*", From 2617478b21fa65da1694e67b736e77a67f1c06cc Mon Sep 17 00:00:00 2001 From: Jose Miguel Gallas Olmedo Date: Fri, 21 Jul 2017 10:59:38 +0200 Subject: [PATCH 3/4] improves users specs --- spec/routes/users.spec.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/spec/routes/users.spec.ts b/spec/routes/users.spec.ts index 4489877..6dc99e9 100644 --- a/spec/routes/users.spec.ts +++ b/spec/routes/users.spec.ts @@ -391,13 +391,24 @@ describe("Route users", () => { const newUser: User = JSON.parse(JSON.stringify(TEST_USER)); newUser.username = "newUser"; - it("returns 201 if the user is succesfully created", done => { + it("returns 201 if the user is succesfully created", async done => { RealmHelper.deleteUser(newUser.username); + await Sleep.millis(200); + + const user: User = RealmHelper.getUserByUsername(newUser.username); + expect(user).toBeFalsy(); + ApiTestClient .createUser(newUser) - .then(res => { + .then(async res => { expect(res.statusCode).toEqual(201); + + await Sleep.millis(200); + + const createdUser: User = RealmHelper.getUserByUsername(newUser.username); + expect(createdUser).toBeTruthy(); + done(); }) .catch(err => { @@ -433,9 +444,11 @@ describe("Route users", () => { ApiTestClient .updateUser(userToUpdate) - .then(res => { + .then(async res => { expect(res.statusCode).toEqual(200); + await Sleep.millis(200); + const userAfterUpdating: User = RealmHelper.getUserByUsername(userToUpdate.username); expect(userAfterUpdating.email).toEqual(userToUpdate.email); @@ -462,9 +475,11 @@ describe("Route users", () => { ApiTestClient .updateUser(userToUpdate) - .then(res => { + .then(async res => { expect(res.statusCode).toEqual(200); + await Sleep.millis(200); + const userAfterUpdating: User = RealmHelper.getUserByUsername(userToUpdate.username); expect(userAfterUpdating.location.city).toEqual(userToUpdate.location.city); From 67ae4dcdac83def06f2ed8b465f056bee2d812d0 Mon Sep 17 00:00:00 2001 From: Jose Miguel Gallas Olmedo Date: Fri, 21 Jul 2017 11:14:10 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c72c471..2b9d8b1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ -# Users RESTful API[ ![Codeship Status for josemigallas/users-api](https://app.codeship.com/projects/017055e0-4b1b-0135-9499-6a83b1829c88/status?branch=master)](https://app.codeship.com/projects/232759) - -> :warning: :alien: Due to [this Request issue](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18074) transpilation is failing. It is necessary to fix the bug manually in node_modules/@types/request/typings.d.ts first time after doing an npm install. This is what's causing the failure at deployment on Heroku and testing with Codeship. +# Users RESTful API +[![Build Status](https://travis-ci.org/josemigallas/users-api.svg?branch=master)](https://travis-ci.org/josemigallas/users-api) This API allows all CRUDL operations over the dataset hosted in https://gist.githubusercontent.com/jasonmadigan/009c15b5dc4b4eccd32b/raw/34759c44e77d2f3515e20ed561cdd7a5e8345585/users.json.