Commit 45a628c0 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes Committed by Jennifer Spencer
Browse files

WIP feat: fetch email and institution from ORCID API

parent 34df2f1f
const superagent = require('superagent')
const apiRoot = 'https://api.sandbox.orcid.org/v2.1'
// request data from orcid API
const request = (user, endpoint) =>
superagent
.get(`${apiRoot}/${user.orcid}/${endpoint}`)
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${user.oauth.accessToken}`)
// convert API date object into Date
const toDate = date => {
if (date === null) return new Date()
const year = date.year.value
const month = parseInt((date.month && date.month.value) || '1', 10) - 1
const day = (date.day && date.day.value) || 1
return new Date(year, month, day)
}
module.exports = async user => {
const [emailResponse, empolymentsResponse] = await Promise.all([
request(user, 'email'),
request(user, 'employments'),
])
const emails = emailResponse.body.email
const email = emails.length ? emails[0].email : null
const employments = empolymentsResponse.body['employment-summary']
const institution = employments.length
? // sort by most recently ended
employments
.sort((a, b) => toDate(a['end-date']) - toDate(b['end-date']))
.pop().organization
: null
return {
email,
institution,
}
}
require('../../test/helpers/replay-setup')
const fetchDetails = require('./fetchDetails')
describe('Fetch ORCID author details', () => {
it('gets email', async () => {
const user = {
orcid: '0000-0003-3146-0256',
oauth: { accessToken: 'f7617529-f46a-40b1-99f4-4181859783ca' },
}
const response = await fetchDetails(user)
console.log(`response`, response)
})
})
const path = require('path')
const Replay = require('replay')
// Replay records outbound HTTP requests
//
// By default it replays existing ones and blocks new requests.
// When writing or updating tests you'll want to change the mode
// see https://github.com/assaf/node-replay#settings
Replay.fixtures = path.join(__dirname, '..', 'http-mocks')
GET /v2.1/0000-0003-3146-0256/employments
accept-encoding: gzip, deflate
accept: application/json
authorization: Bearer f7617529-f46a-40b1-99f4-4181859783ca
HTTP/1.1 200 OK
server: nginx/1.10.0
date: Tue, 01 May 2018 11:57:06 GMT
content-type: application/json;charset=UTF-8
transfer-encoding: chunked
connection: close
access-control-allow-origin: *
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
x-xss-protection: 1; mode=block
x-frame-options: DENY
x-content-type-options: nosniff
{"last-modified-date":{"value":1525174361842},"employment-summary":[{"created-date":{"value":1525104324077},"last-modified-date":{"value":1525172433904},"source":{"source-orcid":{"uri":"https://sandbox.orcid.org/0000-0003-3146-0256","path":"0000-0003-3146-0256","host":"sandbox.orcid.org"},"source-client-id":null,"source-name":{"value":"Test User"}},"department-name":null,"role-title":null,"start-date":{"year":{"value":"2017"},"month":{"value":"01"},"day":null},"end-date":null,"organization":{"name":"University of eLife","address":{"city":"Cambridge","region":null,"country":"GB"},"disambiguated-organization":null},"visibility":"PUBLIC","put-code":30483,"path":"/0000-0003-3146-0256/employment/30483"},{"created-date":{"value":1525104359269},"last-modified-date":{"value":1525174293172},"source":{"source-orcid":{"uri":"https://sandbox.orcid.org/0000-0003-3146-0256","path":"0000-0003-3146-0256","host":"sandbox.orcid.org"},"source-client-id":null,"source-name":{"value":"Test User"}},"department-name":null,"role-title":null,"start-date":{"year":{"value":"2016"},"month":{"value":"01"},"day":{"value":"01"}},"end-date":{"year":{"value":"2017"},"month":{"value":"01"},"day":null},"organization":{"name":"University of Manchester","address":{"city":"Manchester","region":null,"country":"GB"},"disambiguated-organization":{"disambiguated-organization-identifier":"5292","disambiguation-source":"RINGGOLD"}},"visibility":"LIMITED","put-code":30484,"path":"/0000-0003-3146-0256/employment/30484"},{"created-date":{"value":1525174361842},"last-modified-date":{"value":1525174361842},"source":{"source-orcid":{"uri":"https://sandbox.orcid.org/0000-0003-3146-0256","path":"0000-0003-3146-0256","host":"sandbox.orcid.org"},"source-client-id":null,"source-name":{"value":"Test User"}},"department-name":null,"role-title":null,"start-date":{"year":{"value":"2005"},"month":null,"day":null},"end-date":{"year":{"value":"2008"},"month":null,"day":null},"organization":{"name":"Royal Commission on the Ancient and Historical Monuments of Scotland","address":{"city":"Edinburgh","region":null,"country":"GB"},"disambiguated-organization":null},"visibility":"PUBLIC","put-code":30489,"path":"/0000-0003-3146-0256/employment/30489"}],"path":"/0000-0003-3146-0256/employments"}
\ No newline at end of file
GET /v2.1/0000-0003-3146-0256/email
accept-encoding: gzip, deflate
accept: application/json
authorization: Bearer f7617529-f46a-40b1-99f4-4181859783ca
HTTP/1.1 200 OK
server: nginx/1.10.0
date: Tue, 01 May 2018 11:57:06 GMT
content-type: application/json;charset=UTF-8
transfer-encoding: chunked
connection: close
access-control-allow-origin: *
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
x-xss-protection: 1; mode=block
x-frame-options: DENY
x-content-type-options: nosniff
{"last-modified-date":{"value":1525104549715},"email":[{"created-date":{"value":1523279444675},"last-modified-date":{"value":1525104549715},"source":{"source-orcid":{"uri":"https://sandbox.orcid.org/0000-0003-3146-0256","path":"0000-0003-3146-0256","host":"sandbox.orcid.org"},"source-client-id":null,"source-name":{"value":"Test User"}},"email":"elife@mailinator.com","path":null,"visibility":"PUBLIC","verified":true,"primary":true,"put-code":null}],"path":"/0000-0003-3146-0256/email"}
\ No newline at end of file
<<<<<<< HEAD
import replay from 'replay'
import { Selector } from 'testcafe'
import { dashboard, authorDetails } from './pageObjects'
......
Markdown is supported
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