Skip to content
Snippets Groups Projects
Commit 207d0bb7 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes
Browse files

test(auth-orcid): add integration tests for OAuth handler

parent e9ed3f5f
No related branches found
No related tags found
1 merge request!17ORCID auth server component
_build
coverage
node_modules
......@@ -49,7 +49,26 @@ test:
script:
- cd ${HOME}
# specify host here else it confuses the linked postgres image
- PGHOST=postgres npx testcafe 'chrome:headless --no-sandbox'
- PGHOST=postgres npm test
test:e2e:
image: $IMAGE_ORG/$IMAGE_NAME:$CI_COMMIT_SHA
stage: test
variables:
GIT_STRATEGY: none
# setup data for postgres image
POSTGRES_USER: test
POSTGRES_PASSWORD: pw
# connection details for tests
PGUSER: test
PGPASSWORD: pw
NODE_ENV: test
services:
- postgres
script:
- cd ${HOME}
# specify host here else it confuses the linked postgres image
- PGHOST=postgres npm run test:e2e:ci
push:latest:
image: docker:latest
......
......@@ -8,6 +8,7 @@ RUN echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/so
RUN apt-get update && apt-get install -y google-chrome-stable
COPY package.json yarn.lock ./
COPY server server
# We do a development install because react-styleguidist is a dev dependency and we want to run tests
RUN [ "yarn", "install", "--frozen-lockfile" ]
......@@ -20,6 +21,7 @@ COPY app.js .babelrc .eslintignore .eslintrc .prettierrc .stylelintignore .style
COPY app app
COPY config config
COPY scripts scripts
COPY static static
COPY test test
COPY webpack webpack
......
......@@ -14,6 +14,8 @@ module.exports = {
admin: true,
},
'auth-orcid': {
// get the oauth credentials from another developer or
// create new ones at https://orcid.org/content/register-client-application-sandbox
clientID: '',
clientSecret: '',
sandbox: true,
......
......@@ -2,13 +2,18 @@ const { deferConfig } = require('config/defer')
module.exports = {
'pubsweet-server': {
db: { database: 'test' },
db: { database: global.__testDbName || 'test' },
port: 4000,
baseUrl: deferConfig(
cfg => `http://localhost:${cfg['pubsweet-server'].port}`,
),
secret: 'test',
},
'auth-orcid': {
clientID: '123',
clientSecret: 'abc',
sandbox: true,
},
secret: 'test',
mailer: {
transport: {
sendmail: false,
......
......@@ -2,8 +2,11 @@ const Joi = require('joi')
module.exports = {
user: {
// make these core fields optional
email: Joi.string().email(),
passwordHash: Joi.string(),
// auth fields
orcid: Joi.string().required(),
oauth: Joi.object({
accessToken: Joi.string(),
......
......@@ -63,6 +63,7 @@
"fs-extra": "^4.0.2",
"html-webpack-plugin": "^2.24.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"joi-browser": "^10.0.6",
"lint-staged": "^4.1.3",
"prettier": "^1.8.2",
......@@ -84,7 +85,9 @@
"grid-styled/styled-components": "3.2.5"
},
"scripts": {
"test": "NODE_ENV=test testcafe chrome 'test/**/*.test.js'",
"test": "jest",
"test:e2e": "NODE_ENV=test testcafe chrome 'test/**/*.e2e.js'",
"test:e2e:ci": "testcafe 'chrome:headless --no-sandbox' 'test/**/*.e2e.js'",
"clean": "rm -rf node_modules",
"lint": "npm run lint:js && npm run lint:style",
"lint:js": "eslint .",
......@@ -94,22 +97,20 @@
"reset": "pubsweet setupdb --clobber",
"start": "docker-compose up",
"start:services": "docker-compose up postgres",
"start:styleguide": "docker-compose run --no-deps -p 6060:6060 app yarn run styleguide",
"start:styleguide":
"docker-compose run --no-deps -p 6060:6060 app yarn run styleguide",
"server": "pubsweet server",
"styleguide": "styleguidist server",
"build": "NODE_ENV=production pubsweet build",
"cz": "git-cz"
},
"jest": {
"testEnvironment": "jest-environment-db",
"testMatch": ["**/*.test.js"]
},
"lint-staged": {
"*.js": [
"prettier --write",
"eslint --fix",
"git add"
],
"*.{json,md}": [
"prettier --write",
"git add"
],
"*.js": ["prettier --write", "eslint --fix", "git add"],
"*.{json,md}": ["prettier --write", "git add"],
"*.css": "stylelint",
"*.scss": "stylelint"
},
......
module.exports = {
backend: () => require('./endpoint'),
backend: () => require('./orcid'),
typeDefs: `
extend type User {
orcid: String
......
File moved
const express = require('express')
const supertest = require('supertest')
const nock = require('nock')
const { createTables } = require('@pubsweet/db-manager')
const auth = require('./orcid')
const makeApp = () => {
const app = express()
auth(app)
return supertest(app)
}
describe('ORCID auth', () => {
beforeEach(() => createTables(true))
it('can log in', async () => {
// mock OAuth response
nock('https://sandbox.orcid.org')
.post('/oauth/token')
.reply(200, {
access_token: 'token456',
token_type: 'bearer',
refresh_token: 'token123',
expires_in: 631138518,
scope: '/read-limited',
name: 'Test User',
orcid: '0000-0003-3146-0256',
})
const app = makeApp()
const response = await app.get('/auth/orcid/callback?code=def')
expect(response.header.location).toMatch(/^\/login\?token/)
})
})
......@@ -3,14 +3,18 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"scripts": {},
"dependencies": {
"config": "^1.30.0",
"passport": "^0.4.0",
"passport-orcid": "^0.0.3"
},
"devDependencies": {
"nock": "^9.2.3",
"jest-environment-db": "^1.0.0",
"superagent": "^3.8.2",
"supertest": "^3.0.0"
},
"peerDependencies": {
"pubsweet-server": ">=2.0.0"
},
......
const NodeEnvironment = require('jest-environment-node')
const pg = require('pg')
const config = require('config')
class DatabaseTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup()
this.db = new pg.Client(
config['pubsweet-server'] && config['pubsweet-server'].db,
)
await this.db.connect()
// pass the test database name into the test environment as a global
this.global.__testDbName = `test_${Math.floor(Math.random() * 9999999)}`
await this.db.query(`CREATE DATABASE ${this.global.__testDbName}`)
}
async teardown() {
// terminate other connections from test before dropping db
await this.db.query(
`REVOKE CONNECT ON DATABASE ${this.global.__testDbName} FROM public`,
)
await this.db.query(`
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '${this.global.__testDbName}'`)
await this.db.query(`DROP DATABASE ${this.global.__testDbName}`)
await super.teardown()
await this.db.end()
}
}
module.exports = DatabaseTestEnvironment
{
"name": "jest-environment-db",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jest-environment-node": "^22.1.4"
}
}
......@@ -10,6 +10,7 @@ const admin = {
username: 'tester',
email: 'tester@example.com',
password: 'password',
orcid: '0000-0001',
admin: true,
}
......
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment