From 94b2283af242fb27b533de5b52a3f66699dcf7ec Mon Sep 17 00:00:00 2001 From: Mosh Feu Date: Sat, 3 Nov 2018 01:18:01 +0200 Subject: [PATCH] #4 - continue main --- __mocks__/simple-git/promise.js | 37 ++++++++++ __tests__/server.spec.ts | 122 +++++++++++++++++++------------- jest.config.js | 5 +- package-lock.json | 30 ++++++++ package.json | 3 +- src/server.ts | 22 ++++-- 6 files changed, 161 insertions(+), 58 deletions(-) create mode 100644 __mocks__/simple-git/promise.js diff --git a/__mocks__/simple-git/promise.js b/__mocks__/simple-git/promise.js new file mode 100644 index 0000000..1bc9a02 --- /dev/null +++ b/__mocks__/simple-git/promise.js @@ -0,0 +1,37 @@ +const simpleGit = require('simple-git/promise'); +const {readdirSync} = require('fs'); + +let sg; +const listeners = []; + +module.exports = (localFolder) => { + if (localFolder) { + listeners.forEach(listener => { + listener(localFolder); + }); + sg = simpleGit(localFolder); + } + return { + clone: async () => { + await sg.init(); + }, + add: () => { + // console.log('mock add!!!'); + }, + addConfig: () => { + // console.log('mock addConfig!'); + }, + status: () => { + return sg.status(); + }, + commit: async () => { + // console.log('mock commit', readdirSync(localFolder)); + }, + push: () => { + // console.log('mock push'); + }, + _dispose: () => { + sg = null; + } + } +} \ No newline at end of file diff --git a/__tests__/server.spec.ts b/__tests__/server.spec.ts index 819e280..7ff4017 100644 --- a/__tests__/server.spec.ts +++ b/__tests__/server.spec.ts @@ -1,68 +1,90 @@ -import {get, Server} from 'http'; +// tslint:disable-next-line:no-implicit-dependencies +import axios from 'axios'; +// tslint:disable-next-line:no-submodule-imports +import * as simpleGit from 'simple-git/promise'; import {startServer} from '../src/server'; +jest.mock('simple-git/promise'); + describe('main', () => { - const port = 15523; - const baseUrl = `http://localhost:${port}`; - let server: Server; + let port; + let baseUrl; + let server; + let sg; - beforeEach(() => { + beforeEach(async () => { try { - server = startServer(port); + // tslint:disable + port = 3000 + Math.ceil(Math.random() * 1000); + // tslint:enable + process.env.PORT = port; + baseUrl = `http://localhost:${port}`; + server = await startServer(); + sg = simpleGit(); + + return server; } catch (error) { console.log(error, `can't start the server`); } }); - afterEach(() => { - server.close(() => { - console.log('server closed'); - }); + afterEach(async () => { + server.close(); + sg._dispose(); }) - it('GET /', done => { - get(baseUrl, res => { - expect(res.statusCode).toBe(200); + // tslint:disable:mocha-unneeded-done + it('GET /', async (done) => { + const res = await axios(baseUrl); + expect(res.status).toBe(200); + expect(res.data).toBe('Hello World!'); + done(); + }); + + describe('POST /commit', () => { + // tslint:disable-next-line:mocha-unneeded-done + it('complete payload', async (done) => { + const res = await axios({ + url: `http://localhost:${port}/commit`, + method: 'POST', + data: payload + }); + expect(res.status).toBe(200); + expect(res.data).toBe('done'); + const gitStatus = await sg.status(); + expect( + gitStatus.files.map(file => file.path) + ).toEqual( + payload.files.map(file => file.path) + ); done(); }); }); - // it('POST /commit', done => { - // // try { - - // const req = request({host: 'localhost', port: port, method: 'POST', path: '/commit'}, res => { - // console.log('res!!', res); - // res.on('data', chunk => { - // console.log('chunk', chunk); - // expect(chunk).toBe('done'); - // done(); - // }); - // }); - - // // console.log('req', req); - - // req.write(JSON.stringify({ - // "remoteRepo": "moshfeu/commit-bot-test", - // "token": "7cb950575b914d7c36a8de37d9db056671e92788", - // "commitMessage": "yet another commit", - // "files": [ - // { - // "path": "folder1/folder2/file3.js", - // "content": "alert('blabla123')" - // }, - // { - // "path": "folder1/folder3/file4.ts", - // "content": "import * as path from 'path';" - // } - // ] - // })); + it('not complete payload', async (done) => { + const res = await axios({ + url: `http://localhost:${port}/commit`, + method: 'POST', + validateStatus: () => true + }); - // // console.log('req done'); - // // } catch (error) { - // // console.log('error', error); - // // } + expect(res.status).toBe(500); + done(); + }); +}); - // // expect(1).toBe(1); - // // done(); - // }); -}); \ No newline at end of file +const payload = { + "remoteRepo": "moshfeu/commit-bot-test", + "token": "7cb950575b914d7c36a8de37d9db056671e92788", + "commitMessage": "yet another commit", + "files": [ + { + "path": "folder1/folder2/file3.js", + "content": "alert('blabla123')" + }, + { + "path": "folder1/folder3/file4.ts", + "content": "import * as path from 'path';" + } + ] +}; \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index 5cb22a7..7b87ac0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -12,7 +12,10 @@ module.exports = { testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(js)x?$', coverageDirectory: 'coverage', collectCoverageFrom: [ - 'src/**/*.{js,jsx}', + 'dist/src/**/*.{js,jsx}', '!src/**/*.d.ts', ], + moduleNameMapper: { + "simple-git/promise": "/__mocks__/simple-git/promise.js" + } }; diff --git a/package-lock.json b/package-lock.json index 14866d1..835aa63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -631,6 +631,16 @@ "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==", "dev": true }, + "axios": { + "version": "0.18.0", + "resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz", + "integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=", + "dev": true, + "requires": { + "follow-redirects": "^1.3.0", + "is-buffer": "^1.1.5" + } + }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -2045,6 +2055,26 @@ "integrity": "sha512-lAq4tLbm3sidmdCN8G3ExaxH7cUCtP5mgDvrYowsx84dcYkJJ4I28N7gkxA6+YlSXzaGLJYIDEi9WGfXzMiXdw==", "dev": true }, + "follow-redirects": { + "version": "1.5.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.9.tgz", + "integrity": "sha512-Bh65EZI/RU8nx0wbYF9shkFZlqLP+6WT/5FnA3cE/djNSuKNHJEinGGZgu/cQEkeeb2GdFOgenAmn8qaqYke2w==", + "dev": true, + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + } + } + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index 07e7f57..103c1af 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "tslint": "~5.11.0", "tslint-config-prettier": "1.15.0", "tslint-microsoft-contrib": "~5.2.1", - "tsutils": "~3.0.0" + "tsutils": "~3.0.0", + "axios": "^0.18.0" }, "scripts": { "debug": "tsc | nodemon dist/src/main.js --nolazy --debug-brk=5858", diff --git a/src/server.ts b/src/server.ts index c32c6fc..1f59cbb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,14 +1,13 @@ import * as bodyParser from 'body-parser'; import * as express from 'express'; -import { readdirSync } from 'fs'; import {Server} from 'http'; import {join} from 'path'; // tslint:disable-next-line:no-submodule-imports import * as simpleGit from 'simple-git/promise'; -import{directory} from 'tempy'; +import {directory} from 'tempy'; import {writeFileDeepSync} from './utils'; -export function startServer(port = process.env.PORT || 3000): Server { +export function startServer(port = process.env.PORT || 3000): Promise { const app = express(); app.use(bodyParser.json()); @@ -18,10 +17,16 @@ export function startServer(port = process.env.PORT || 3000): Server { app.post('/commit', async (req, res) => { const { remoteRepo, token, files, commitMessage } = req.body as {[key: string]: string | IFile[]}; - const tempFolder = directory(); + if (!remoteRepo || !token || !files || !commitMessage) { + res.status(500).send(`Payload isn't complete`); + + return; + } + const tempFolder = directory(); const sg = simpleGit(tempFolder); await sg.clone(`https://${token}@github.com/${remoteRepo}.git`, tempFolder); + (files).forEach((file: IFile) => { writeFileDeepSync(join(tempFolder, file.path), file.content); }); @@ -30,13 +35,18 @@ export function startServer(port = process.env.PORT || 3000): Server { await sg.addConfig('user.email', 'bot@spread-the-code.com'); await sg.addConfig('user.name', 'committer-bot'); - await sg.commit(commitMessage || 'unknow commit'); + await sg.commit(commitMessage); await sg.push(); res.send('done'); }); - return app.listen(port, () => console.log(`Example app listening on port ${port}!`)) + return new Promise((resolve) => { + const server = app.listen(port, () => { + console.log(`Example app listening on port ${port}!`); + resolve(server); + }); + }) } interface IFile {