diff --git a/modules/test/package-lock.json b/modules/test/package-lock.json index beef343..717f238 100644 --- a/modules/test/package-lock.json +++ b/modules/test/package-lock.json @@ -1,12 +1,12 @@ { "name": "@themost/test", - "version": "2.3.4", + "version": "2.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@themost/test", - "version": "2.3.4", + "version": "2.4.0", "license": "BSD-3-Clause", "dependencies": { "@themost/client": "^2", diff --git a/modules/test/package.json b/modules/test/package.json index 2e4de67..221e43d 100644 --- a/modules/test/package.json +++ b/modules/test/package.json @@ -1,6 +1,6 @@ { "name": "@themost/test", - "version": "2.3.4", + "version": "2.4.0", "description": "MOST Web Framework Test Api Server", "repository": "https://github.com/themost-framework/themost-test.git", "author": "Kyriakos Barbounakis", diff --git a/modules/test/server/app.js b/modules/test/server/app.js index d40df48..8f57829 100644 --- a/modules/test/server/app.js +++ b/modules/test/server/app.js @@ -1,24 +1,24 @@ import express from 'express'; -import {ViewEngine} from '@themost/ejs'; +import { ViewEngine } from '@themost/ejs'; import path from 'path'; import passport from 'passport'; -import {authRouter} from './routes/auth'; +import { authRouter } from './routes/auth'; // eslint-disable-next-line no-unused-vars -import { ExpressDataApplication, serviceRouter, dateReviver, ExpressDataContext } from '@themost/express'; -import {indexRouter} from './routes/index'; +import { ExpressDataApplication, serviceRouter, getMetadataDocument, dateReviver, ExpressDataContext } from '@themost/express'; +import { indexRouter } from './routes/index'; import temp from 'temp'; import fs from 'fs'; import cors from 'cors'; import { Authenticator } from './routes/auth'; import { docsRouter } from './routes/docs'; import createError from 'http-errors'; -import {HttpUnauthorizedError} from '@themost/common'; +import { HttpUnauthorizedError } from '@themost/common'; function getApplication() { const config = require('./config/app.json'); -// prepare temp database - const findAdapter = config.adapters.find( adapter => { + // prepare temp database + const findAdapter = config.adapters.find(adapter => { return adapter.name === 'test'; }); let testDatabase; @@ -47,14 +47,14 @@ function getApplication() { */ let app = express(); -// enable CORS -app.use(cors({ origin:true, credentials: true })); + // enable CORS + app.use(cors({ origin: true, credentials: true })); -app.use('/assets', express.static(path.join(__dirname, 'assets'))) + app.use('/assets', express.static(path.join(__dirname, 'assets'))) -// use ViewEngine for all ejs templates + // use ViewEngine for all ejs templates app.engine('ejs', ViewEngine.express()); -// view engine setup + // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); @@ -63,17 +63,17 @@ app.use('/assets', express.static(path.join(__dirname, 'assets'))) })); app.use(express.urlencoded({ extended: true })); -// @themost/data data application setup + // @themost/data data application setup const dataApplication = new ExpressDataApplication(path.resolve(__dirname, 'config')); if (dataApplication.hasService(Authenticator) === false) { dataApplication.useService(Authenticator); } -// hold data application + // hold data application app.set('ExpressDataApplication', dataApplication); -// use data middleware (register req.context) + // use data middleware (register req.context) app.use(dataApplication.middleware()); app.use('/', indexRouter); @@ -83,8 +83,9 @@ app.use('/assets', express.static(path.join(__dirname, 'assets'))) app.use('/api/docs', docsRouter); -// use @themost/express service router -// noinspection JSCheckFunctionSignatures + app.get('/api/\\$metadata', getMetadataDocument()); + // use @themost/express service router + // noinspection JSCheckFunctionSignatures app.use('/api/', (req, res, next) => { passport.authenticate('bearer', { session: false }, (err, user) => { if (err) { return next(err); } @@ -96,11 +97,11 @@ app.use('/assets', express.static(path.join(__dirname, 'assets'))) })(req, res, next); }, serviceRouter); -// catch 404 and forward to error handler + // catch 404 and forward to error handler app.use((_req, _res, next) => { next(createError(404)); }); -// error handler + // error handler app.use((err, req, res, next) => { if (res.headersSent) { return next(err) @@ -146,4 +147,4 @@ app.use('/assets', express.static(path.join(__dirname, 'assets'))) return app; } -export {getApplication}; +export { getApplication }; diff --git a/spec/app.spec.js b/spec/app.spec.js index 9a8a851..24c7632 100644 --- a/spec/app.spec.js +++ b/spec/app.spec.js @@ -1,197 +1,205 @@ -import {getApplication, serveApplication, getServerAddress, getToken, getTokenInfo} from '../modules/test/server'; -import {promisify} from 'es6-promisify'; -import {URL, URLSearchParams} from 'url'; +import { getApplication, serveApplication, getServerAddress, getToken, getTokenInfo } from '../modules/test/server'; +import { promisify } from 'es6-promisify'; +import { URL, URLSearchParams } from 'url'; import fetch from 'node-fetch'; +import { EdmSchema } from '@themost/client'; -describe('app', function () { +describe('app', function() { - it('should get app', () => { - expect(getApplication()).toBeTruthy(); + it('should get app', () => { + expect(getApplication()).toBeTruthy(); + }); + it('should serve app', async () => { + // serve + const server = await serveApplication(getApplication()); + expect(server).toBeTruthy(); + // get address info + const addressInfo = server.address(); + expect(addressInfo).toBeTruthy(); + expect(addressInfo.address).toBeTruthy(); + expect(addressInfo.port).toBeTruthy(); + // close server + await promisify(server.close).bind(server)(); + }); + + it('should get json error', async () => { + // serve + const server = await serveApplication(getApplication()); + const base = getServerAddress(server); + // get metadata + const response = await fetch(new URL('/missing', base), { + headers: { + 'Accept': 'application/json' + } }); - it('should serve app', async () => { - // serve - const server = await serveApplication(getApplication()); - expect(server).toBeTruthy(); - // get address info - const addressInfo = server.address(); - expect(addressInfo).toBeTruthy(); - expect(addressInfo.address).toBeTruthy(); - expect(addressInfo.port).toBeTruthy(); - // close server - await promisify(server.close).bind(server)(); + expect(response.ok).toBeFalsy(); + const body = await response.json(); + expect(body).toBeTruthy(); + // close server + await promisify(server.close).bind(server)(); + }); + + it('should post /auth/token', async () => { + // serve + const server = await serveApplication(getApplication()); + const base = getServerAddress(server); + // get metadata + const response = await fetch(new URL('/auth/token', base), { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({ + client_id: '9165351833584149', + client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', + username: 'alexis.rees@example.com', + password: 'secret', + grant_type: 'password', + scope: 'profile' + }).toString() }); - it('should get /api/$metadata error', async () => { - // serve - const server = await serveApplication(getApplication()); - const base = getServerAddress(server); - // get metadata - const response = await fetch(new URL('/api/$metadata', base)); - expect(response).toBeTruthy(); - expect(response.ok).toBeFalsy(); - // close server - await promisify(server.close).bind(server)(); + expect(response.ok).toBeTruthy(); + const body = await response.json(); + expect(body).toBeTruthy(); + // close server + await promisify(server.close).bind(server)(); + }); + it('should post /auth/token_info', async () => { + // serve + const liveServer = await serveApplication(getApplication()); + const base = getServerAddress(liveServer); + // get metadata + let response = await fetch(new URL('/auth/token', base), { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({ + client_id: '9165351833584149', + client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', + username: 'alexis.rees@example.com', + password: 'secret', + grant_type: 'password', + scope: 'profile' + }).toString() }); - - it('should get json error', async () => { - // serve - const server = await serveApplication(getApplication()); - const base = getServerAddress(server); - // get metadata - const response = await fetch(new URL('/missing', base), { - headers: { - 'Accept': 'application/json' - } - }); - expect(response.ok).toBeFalsy(); - const body = await response.json(); - expect(body).toBeTruthy(); - // close server - await promisify(server.close).bind(server)(); + expect(response.ok).toBeTruthy(); + const token = await response.json(); + expect(token).toBeTruthy(); + response = await fetch(new URL('/auth/token_info', base), { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Authorization': `Basic ${Buffer.from('9165351833584149:hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA').toString('base64')}`, + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({ + token: token.access_token + }).toString() }); + expect(response.ok).toBeTruthy(); + const token_info = await response.json(); + expect(token_info).toBeTruthy(); + expect(token_info.active).toBeTruthy(); + // close server + await promisify(liveServer.close).bind(liveServer)(); + }); - it('should post /auth/token', async () => { - // serve - const server = await serveApplication(getApplication()); - const base = getServerAddress(server); - // get metadata - const response = await fetch(new URL('/auth/token', base), { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - client_id: '9165351833584149', - client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', - username: 'alexis.rees@example.com', - password: 'secret', - grant_type: 'password', - scope: 'profile' - }).toString() - }); - expect(response.ok).toBeTruthy(); - const body = await response.json(); - expect(body).toBeTruthy(); - // close server - await promisify(server.close).bind(server)(); + it('should post /auth/me', async () => { + // serve + const server = await serveApplication(getApplication()); + const base = getServerAddress(server); + // get metadata + let response = await fetch(new URL('/auth/token', base), { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({ + client_id: '9165351833584149', + client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', + username: 'alexis.rees@example.com', + password: 'secret', + grant_type: 'password', + scope: 'profile' + }).toString() }); - it('should post /auth/token_info', async () => { - // serve - const liveServer = await serveApplication(getApplication()); - const base = getServerAddress(liveServer); - // get metadata - let response = await fetch(new URL('/auth/token', base), { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - client_id: '9165351833584149', - client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', - username: 'alexis.rees@example.com', - password: 'secret', - grant_type: 'password', - scope: 'profile' - }).toString() - }); - expect(response.ok).toBeTruthy(); - const token = await response.json(); - expect(token).toBeTruthy(); - response = await fetch(new URL('/auth/token_info', base), { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Authorization': `Basic ${Buffer.from('9165351833584149:hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA').toString('base64')}`, - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - token: token.access_token - }).toString() - }); - expect(response.ok).toBeTruthy(); - const token_info = await response.json(); - expect(token_info).toBeTruthy(); - expect(token_info.active).toBeTruthy(); - // close server - await promisify(liveServer.close).bind(liveServer)(); + expect(response.ok).toBeTruthy(); + const token = await response.json(); + expect(token).toBeTruthy(); + response = await fetch(new URL('/auth/me', base), { + method: 'GET', + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer ${token.access_token}`, + 'Content-Type': 'application/x-www-form-urlencoded' + } }); + expect(response.ok).toBeTruthy(); + const me = await response.json(); + expect(me).toBeTruthy(); + expect(me.name).toBe('alexis.rees@example.com'); + // close server + await promisify(server.close).bind(server)(); + }); - it('should post /auth/me', async () => { - // serve - const server = await serveApplication(getApplication()); - const base = getServerAddress(server); - // get metadata - let response = await fetch(new URL('/auth/token', base), { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - client_id: '9165351833584149', - client_secret: 'hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA', - username: 'alexis.rees@example.com', - password: 'secret', - grant_type: 'password', - scope: 'profile' - }).toString() - }); - expect(response.ok).toBeTruthy(); - const token = await response.json(); - expect(token).toBeTruthy(); - response = await fetch(new URL('/auth/me', base), { - method: 'GET', - headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer ${token.access_token}`, - 'Content-Type': 'application/x-www-form-urlencoded' - } - }); - expect(response.ok).toBeTruthy(); - const me = await response.json(); - expect(me).toBeTruthy(); - expect(me.name).toBe('alexis.rees@example.com'); - // close server - await promisify(server.close).bind(server)(); - }); + it('should use getToken()', async () => { + // serve + const server = await serveApplication(getApplication()); + const server_uri = getServerAddress(server); + // get token + let token = await getToken(server_uri, 'alexis.rees@example.com', 'secret'); + expect(token).toBeTruthy(); + expect(token.access_token).toBeTruthy(); + // unauthorized + try { + token = await getToken(server_uri, 'alexis.rees@example.com', 'test'); + expect(token).toBeFalsy(); + } + catch (err) { + expect(err).toBeTruthy(); + expect(err.statusCode).toBe(401); + } + // close server + await promisify(server.close).bind(server)(); + }); - it('should use getToken()', async () => { - // serve - const server = await serveApplication(getApplication()); - const server_uri = getServerAddress(server); - // get token - let token = await getToken(server_uri, 'alexis.rees@example.com', 'secret'); - expect(token).toBeTruthy(); - expect(token.access_token).toBeTruthy(); - // unauthorized - try { - token = await getToken(server_uri, 'alexis.rees@example.com', 'test'); - expect(token).toBeFalsy(); - } - catch (err) { - expect(err).toBeTruthy(); - expect(err.statusCode).toBe(401); - } - // close server - await promisify(server.close).bind(server)(); - }); + it('should use getTokenInfo()', async () => { + // serve + const server = await serveApplication(getApplication()); + const server_uri = getServerAddress(server); + // get token + let token = await getToken(server_uri, 'alexis.rees@example.com', 'secret'); + expect(token).toBeTruthy(); + let tokenInfo = await getTokenInfo(server_uri, token.access_token); + expect(tokenInfo).toBeTruthy(); + expect(tokenInfo.active).toBeTruthy(); + // active false + tokenInfo = await getTokenInfo(server_uri, 'test-token'); + expect(tokenInfo.active).toBeFalsy(); + // close server + await promisify(server.close).bind(server)(); + }); - it('should use getTokenInfo()', async () => { - // serve - const server = await serveApplication(getApplication()); - const server_uri = getServerAddress(server); - // get token - let token = await getToken(server_uri, 'alexis.rees@example.com', 'secret'); - expect(token).toBeTruthy(); - let tokenInfo = await getTokenInfo(server_uri, token.access_token); - expect(tokenInfo).toBeTruthy(); - expect(tokenInfo.active).toBeTruthy(); - // active false - tokenInfo = await getTokenInfo(server_uri, 'test-token'); - expect(tokenInfo.active).toBeFalsy(); - // close server - await promisify(server.close).bind(server)(); + it('should get metadata as anonymous', async () => { + const server = await serveApplication(getApplication(), 8080); + const server_uri = getServerAddress(server); + let response = await fetch(new URL('/api/$metadata', server_uri), { + method: 'GET', + headers: { + 'Accept': 'application/xml' + } }); + expect(response.ok).toBeTruthy(); + const body = await response.text(); + expect(body).toBeTruthy(); + const schema = EdmSchema.loadXML(body); + expect(schema).toBeTruthy(); + expect(schema.EntityType.length).toBeGreaterThan(0); + + }); });