-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from navikt/personlookup
person lookup, not hooked up to any routes yet
- Loading branch information
Showing
10 changed files
with
283 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
jest.genMockFromModule('request'); | ||
|
||
let stsStatusCode = 0; | ||
let stsResponseBody = {}; | ||
let personStatusCode = 0; | ||
let personResponseBody = {}; | ||
|
||
const setStsStatusCode = code => (stsStatusCode = code); | ||
const setStsResponseBody = body => (stsResponseBody = body); | ||
const setPersonStatusCode = code => (personStatusCode = code); | ||
const setPersonResponseBody = body => (personResponseBody = body); | ||
|
||
const get = (url, options, callback) => { | ||
if (url.includes('/sts/')) { | ||
callback(null, { statusCode: stsStatusCode }, stsResponseBody); | ||
} else { | ||
callback(null, { statusCode: personStatusCode }, personResponseBody); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
get: get, | ||
setStsStatusCode: setStsStatusCode, | ||
setStsResponseBody: setStsResponseBody, | ||
setPersonStatusCode: setPersonStatusCode, | ||
setPersonResponseBody: setPersonResponseBody | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
|
||
const request = require('request'); | ||
const authSupport = require('./authsupport'); | ||
|
||
let navConfig = null; | ||
let cachedAccessToken = null; | ||
|
||
const init = config => { | ||
navConfig = config; | ||
cachedAccessToken = null; | ||
}; | ||
|
||
const hentPerson = async aktørId => { | ||
return new Promise((resolve, reject) => { | ||
logonToNav() | ||
.then(jwt => { | ||
request.get( | ||
`http://sparkel.svc.nais.local/api/person/${aktørId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer: ${jwt}` | ||
} | ||
}, | ||
(error, response, body) => { | ||
if (error || response.statusCode !== 200) { | ||
reject( | ||
`Error during person lookup, got ${ | ||
response.statusCode | ||
} ${error || 'unknown error'}` | ||
); | ||
} else { | ||
resolve(body); | ||
} | ||
} | ||
); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
|
||
const logonToNav = async () => { | ||
return new Promise((resolve, reject) => { | ||
if ( | ||
cachedAccessToken && | ||
!authSupport.willExpireInLessThan(30, cachedAccessToken) | ||
) { | ||
resolve(cachedAccessToken); | ||
} else { | ||
request.get( | ||
`${navConfig.stsUrl}/rest/v1/sts/token?grant_type=client_credentials&scope=openid`, | ||
{ | ||
headers: { | ||
Authorization: | ||
'Basic ' + | ||
Buffer.from( | ||
`${navConfig.serviceUserName}:${navConfig.serviceUserPassword}` | ||
).toString('base64') | ||
} | ||
}, | ||
(error, response, body) => { | ||
if (error || response.statusCode !== 200) { | ||
reject( | ||
`Error during STS login, got ${ | ||
response.statusCode | ||
} ${error || 'unknown error'}` | ||
); | ||
} else { | ||
cachedAccessToken = body.access_token; | ||
resolve(body.access_token); | ||
} | ||
} | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
init: init, | ||
hentPerson: hentPerson | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const request = require('request'); | ||
|
||
const personLookup = require('../../src/server/personlookup'); | ||
|
||
beforeEach(() => { | ||
personLookup.init({ | ||
serviceUserName: 'da_usah', | ||
serviceUserPassword: 'pazzwd', | ||
stsUrl: 'http://localhost' | ||
}); | ||
}); | ||
|
||
test('successful person lookup resolves with person object', () => { | ||
request.setStsStatusCode(200); | ||
request.setStsResponseBody({ | ||
access_token: 'some_token' | ||
}); | ||
request.setPersonStatusCode(200); | ||
request.setPersonResponseBody({ navn: 'Navn Navnesen' }); | ||
|
||
expect(personLookup.hentPerson('12345')).resolves.toEqual({ | ||
navn: 'Navn Navnesen' | ||
}); | ||
}); | ||
|
||
test('logon failure -> rejection', () => { | ||
request.setStsStatusCode(500); | ||
|
||
expect(personLookup.hentPerson('12345')).rejects.toMatch( | ||
'Error during STS login' | ||
); | ||
}); | ||
|
||
test('lookup failure -> rejection', () => { | ||
request.setStsStatusCode(200); | ||
request.setStsResponseBody({ | ||
access_token: 'some_token' | ||
}); | ||
request.setPersonStatusCode(500); | ||
|
||
expect(personLookup.hentPerson('12345')).rejects.toMatch( | ||
'Error during person lookup' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters