diff --git a/controllers/auth.js b/controllers/auth.js index ab02c80b..27e11f43 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -11,7 +11,7 @@ module.exports = (req, res) => { let requestAccessToken - switch (req.params.service) { + switch (req.query.provider) { case 'gitlab': requestAccessToken = siteConfig => oauth.requestGitLabAccessToken( @@ -34,12 +34,12 @@ module.exports = (req, res) => { return staticman.getSiteConfig() .then(requestAccessToken) .then((accessToken) => { - const git = gitFactory.create(req.params.service, { + const git = gitFactory.create(req.query.provider, { oauthToken: accessToken }) // TODO: Simplify this when v2 support is dropped. - const getUser = req.params.version === '2' && req.params.service === 'github' + const getUser = req.params.version === '2' && req.query.provider === 'github' ? git.api.users.get({}).then(({data}) => data) : git.getCurrentUser() diff --git a/coverage/cobertura-coverage.xml b/coverage/cobertura-coverage.xml index 9fb167bb..09ec016a 100644 --- a/coverage/cobertura-coverage.xml +++ b/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + /home/nick/Development/Javascript/staticman @@ -669,9 +669,9 @@ - + - + @@ -778,12 +778,12 @@ - - - + + + - - + + @@ -815,9 +815,9 @@ - + - + @@ -928,11 +928,11 @@ - - + + - - + + @@ -1093,8 +1093,8 @@ - - + + diff --git a/test/unit/controllers/auth.test.js b/test/unit/controllers/auth.test.js index 61e2fdfe..18f25743 100644 --- a/test/unit/controllers/auth.test.js +++ b/test/unit/controllers/auth.test.js @@ -18,17 +18,10 @@ beforeEach(() => { describe('Auth controller', () => { describe('GitHub', () => { - test('authenticates to GitHub with the given code and returns the authenticated user', () => { - const mockAccessToken = 'qwertyuiop' - const mockCode = '1q2w3e4r' - const mockUser = { - login: 'johndoe', - name: 'John Doe', - email: 'johndoe@test.com' - } - - const siteConfig = helpers.getConfig() + const siteConfig = helpers.getConfig() + const mockCode = '1q2w3e4r' + const oauthRequest = nock(/github\.com/) .post('/login/oauth/access_token') .query({ @@ -37,6 +30,16 @@ describe('Auth controller', () => { code: mockCode, redirect_uri: siteConfig.get('githubAuth.redirectUri') }) + + test('authenticates to GitHub with the given code and returns the authenticated user', () => { + const mockAccessToken = 'qwertyuiop' + const mockUser = { + login: 'johndoe', + name: 'John Doe', + email: 'johndoe@test.com' + } + + oauthRequest .reply(200, { access_token: mockAccessToken }) @@ -50,7 +53,8 @@ describe('Auth controller', () => { const reqWithQuery = Object.assign({}, req, { query: { - code: mockCode + code: mockCode, + provider: 'github' } }) @@ -64,21 +68,11 @@ describe('Auth controller', () => { test('authenticates to GitHub with the given code and returns the original GitHub user when using v2 API', () => { const mockAccessToken = 'qwertyuiop' - const mockCode = '1q2w3e4r' const mockUser = { login: 'johndoe' } - const siteConfig = helpers.getConfig() - - nock(/github\.com/) - .post('/login/oauth/access_token') - .query({ - client_id: siteConfig.get('githubAuth.clientId'), - client_secret: siteConfig.get('githubAuth.clientSecret'), - code: mockCode, - redirect_uri: siteConfig.get('githubAuth.redirectUri') - }) + oauthRequest .reply(200, { access_token: mockAccessToken }) @@ -92,11 +86,11 @@ describe('Auth controller', () => { const reqWithQuery = Object.assign({}, req, { params: { - service: 'github', version: '2' }, query: { - code: mockCode + code: mockCode, + provider: 'github' } }) @@ -108,25 +102,12 @@ describe('Auth controller', () => { }) test('returns a 401 response when unable to get an access token from GitHub', () => { - const mockCode = '1q2w3e4r' - const siteConfig = helpers.getConfig() - - nock(/github\.com/) - .post('/login/oauth/access_token') - .query({ - client_id: siteConfig.get('githubAuth.clientId'), - client_secret: siteConfig.get('githubAuth.clientSecret'), - code: mockCode, - redirect_uri: siteConfig.get('githubAuth.redirectUri') - }) + oauthRequest .reply(401, { error: 'invalid_code' }) const reqWithQuery = Object.assign({}, req, { - params: { - service: 'github' - }, query: { code: mockCode } @@ -167,7 +148,8 @@ describe('Auth controller', () => { const reqWithQuery = Object.assign({}, req, { query: { - code: mockCode + code: mockCode, + provider: 'github' } }) @@ -180,17 +162,10 @@ describe('Auth controller', () => { }) describe('GitLab', () => { - test('authenticates to GitLab with the given code and returns the authenticated user', () => { - const mockAccessToken = 'qwertyuiop' - const mockCode = '1q2w3e4r' - const mockUser = { - username: 'johndoe', - name: 'John Doe', - email: 'johndoe@test.com' - } - - const siteConfig = helpers.getConfig() + const siteConfig = helpers.getConfig() + const mockCode = '1q2w3e4r' + const oauthRequest = nock(/gitlab\.com/) .post('/oauth/token') .query({ @@ -200,6 +175,16 @@ describe('Auth controller', () => { grant_type: 'authorization_code', redirect_uri: siteConfig.get('gitlabAuth.redirectUri') }) + + test('authenticates to GitLab with the given code and returns the authenticated user', () => { + const mockAccessToken = 'qwertyuiop' + const mockUser = { + username: 'johndoe', + name: 'John Doe', + email: 'johndoe@test.com' + } + + oauthRequest .reply(200, { access_token: mockAccessToken }) @@ -213,11 +198,9 @@ describe('Auth controller', () => { .reply(200, mockUser) const reqWithQuery = Object.assign({}, req, { - params: { - service: 'gitlab' - }, query: { - code: mockCode + code: mockCode, + provider: 'gitlab' } }) @@ -230,28 +213,15 @@ describe('Auth controller', () => { }) test('returns a 401 response when unable to get an access token from GitLab', () => { - const mockCode = '1q2w3e4r' - const siteConfig = helpers.getConfig() - - nock(/gitlab\.com/) - .post('/oauth/token') - .query({ - client_id: siteConfig.get('gitlabAuth.clientId'), - client_secret: siteConfig.get('gitlabAuth.clientSecret'), - code: mockCode, - grant_type: 'authorization_code', - redirect_uri: siteConfig.get('gitlabAuth.redirectUri') - }) + oauthRequest .reply(401, { error: 'invalid_code' }) const reqWithQuery = Object.assign({}, req, { - params: { - service: 'gitlab' - }, query: { - code: mockCode + code: mockCode, + provider: 'gitlab' } }) @@ -264,19 +234,8 @@ describe('Auth controller', () => { test('returns a 401 response when an incorrect access token is used for the GitLab API', () => { const mockAccessToken = 'qwertyuiop' - const mockCode = '1q2w3e4r' - - const siteConfig = helpers.getConfig() - nock(/gitlab\.com/) - .post('/oauth/token') - .query({ - client_id: siteConfig.get('gitlabAuth.clientId'), - client_secret: siteConfig.get('gitlabAuth.clientSecret'), - code: mockCode, - grant_type: 'authorization_code', - redirect_uri: siteConfig.get('gitlabAuth.redirectUri') - }) + oauthRequest .reply(200, { access_token: mockAccessToken }) @@ -292,11 +251,9 @@ describe('Auth controller', () => { }) const reqWithQuery = Object.assign({}, req, { - params: { - service: 'gitlab' - }, query: { - code: mockCode + code: mockCode, + provider: 'gitlab' } })