forked from nytimes/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] slack oauth support (nytimes#245)
- Loading branch information
1 parent
af1a418
commit 6f74845
Showing
7 changed files
with
169 additions
and
20 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
'use strict' | ||
|
||
const request = require('supertest') | ||
const {assert} = require('chai') | ||
const sinon = require('sinon') | ||
const express = require('express') | ||
|
||
const log = require('../../server/logger') | ||
const app = require('../../server/index') | ||
|
||
/* | ||
|
@@ -24,18 +23,78 @@ const regexUser = { | |
} | ||
|
||
const specificUser = { | ||
emails: [{ value: '[email protected]' }], | ||
emails: [{value: '[email protected]'}], | ||
id: '12', | ||
userId: '12' | ||
} | ||
|
||
const unauthorizedUser = { | ||
emails: [{ value: '[email protected]' }], | ||
emails: [{value: '[email protected]'}], | ||
id: '13', | ||
userId: '13' | ||
} | ||
|
||
describe('Authentication', () => { | ||
describe('.env specified oauth strategy', () => { | ||
const sandbox = sinon.createSandbox() | ||
beforeEach(() => { | ||
jest.resetModules() | ||
sandbox.stub(express.request, 'isAuthenticated').returns(false) | ||
}) | ||
|
||
afterEach(() => { | ||
sandbox.restore() | ||
}) | ||
|
||
it('should warn if there is an invalid strategy specified', () => { | ||
process.env.OAUTH_STRATEGY = 'fakjkjfdz' | ||
const spy = sandbox.spy(log, 'warn') | ||
const appWithInvalidOauth = require('../../server/index') // need to redo app setup | ||
return request(appWithInvalidOauth) | ||
.get('/login') | ||
.expect(302) | ||
.then((res) => { | ||
assert.isTrue(spy.called, 'warn was not called') | ||
assert.match(res.headers.location, /google/) | ||
}) | ||
}) | ||
|
||
it('should default to google if there is no auth strategy specified', () => { | ||
process.env.OAUTH_STRATEGY = undefined | ||
const appWithoutOauth = require('../../server/index') // need to redo app setup | ||
return request(appWithoutOauth) | ||
.get('/login') | ||
.expect(302) | ||
.then((res) => { | ||
assert.match(res.headers.location, /google/) | ||
}) | ||
}) | ||
|
||
it('should use slack strategy if slack is specified', () => { | ||
process.env.OAUTH_STRATEGY = 'Slack' | ||
process.env.SLACK_CLIENT_ID = '1234567890' | ||
process.env.SLACK_CLIENT_SECRET = '1234567890' | ||
const appWithSlackAuth = require('../../server/index') // need to redo app setup | ||
return request(appWithSlackAuth) | ||
.get('/login') | ||
.expect(302) | ||
.then((res) => { | ||
assert.match(res.headers.location, /slack/) | ||
}) | ||
}) | ||
|
||
it('Slack has to be capitalized, sorry', () => { | ||
process.env.OAUTH_STRATEGY = 'slack' | ||
const appWithSlackAuth = require('../../server/index') // need to redo app setup | ||
return request(appWithSlackAuth) | ||
.get('/login') | ||
.expect(302) | ||
.then((res) => { | ||
assert.match(res.headers.location, /google/) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when not logged in', () => { | ||
beforeAll(() => sinon.stub(express.request, 'isAuthenticated').returns(false)) | ||
afterAll(() => sinon.restore()) | ||
|
@@ -63,7 +122,7 @@ describe('Authentication', () => { | |
|
||
describe('when logging in with regex-approved domain', () => { | ||
beforeAll(() => { | ||
sinon.stub(app.request, 'session').value({ passport: { user: regexUser } }) | ||
sinon.stub(app.request, 'session').value({passport: {user: regexUser}}) | ||
sinon.stub(express.request, 'user').value(regexUser) | ||
sinon.stub(express.request, 'userInfo').value(regexUser) | ||
}) | ||
|
@@ -78,7 +137,7 @@ describe('Authentication', () => { | |
|
||
describe('when logging in with specified email address', () => { | ||
beforeAll(() => { | ||
sinon.stub(app.request, 'session').value({ passport: { user: specificUser } }) | ||
sinon.stub(app.request, 'session').value({passport: {user: specificUser}}) | ||
sinon.stub(express.request, 'user').value(specificUser) | ||
sinon.stub(express.request, 'userInfo').value(specificUser) | ||
}) | ||
|