forked from FAForever/website
-
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.
- Loading branch information
Showing
6 changed files
with
137 additions
and
13 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
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,96 @@ | ||
const express = require('express') | ||
const middlewares = require('../../routes/middleware') | ||
const passport = require("passport"); | ||
const appConfig = require("../../config/app"); | ||
const supertestSession = require('supertest-session'); | ||
const session = require('express-session'); | ||
|
||
let testApp = null | ||
let testSession = null | ||
|
||
beforeEach(() => { | ||
const app = new express() | ||
testSession = supertestSession(app) | ||
app.use(session({ | ||
resave: false, | ||
saveUninitialized: true, | ||
secret: appConfig.session.key, | ||
})); | ||
app.use(passport.initialize()) | ||
app.use(passport.session()) | ||
testApp = app | ||
}) | ||
|
||
describe('Authenticate Middleware', function () { | ||
|
||
test('route is protected and redirects to "/login"', async () => { | ||
testApp.get('/', middlewares.isAuthenticated(), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
const res = await testSession.get('/') | ||
|
||
expect(res.status).toBe(302) | ||
expect(res.headers['location']).toBe('/login') | ||
}) | ||
|
||
test('default url after login', async () => { | ||
testApp.get('/', middlewares.isAuthenticated(), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
testApp.get('/redirect-me', (req, res) => { | ||
res.redirect(req.session.returnTo) | ||
}) | ||
|
||
await testSession.get('/') | ||
const res = await testSession.get('/redirect-me') | ||
expect(res.status).toBe(302) | ||
expect(res.headers['location']).toBe('/') | ||
}) | ||
|
||
test('custom url after login', async () => { | ||
testApp.get('/', middlewares.isAuthenticated('/go-here'), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
testApp.get('/redirect-me', (req, res) => { | ||
res.redirect(req.session.returnTo) | ||
}) | ||
|
||
await testSession.get('/') | ||
const res = await testSession.get('/redirect-me') | ||
expect(res.status).toBe(302) | ||
expect(res.headers['location']).toBe('/go-here') | ||
}) | ||
|
||
test('api url flag return 401', async () => { | ||
testApp.get('/', middlewares.isAuthenticated(null, true), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
const res = await testSession.get('/') | ||
expect(res.status).toBe(401) | ||
expect(res.body).toEqual({error: 'Unauthorized'}) | ||
}) | ||
|
||
test('identifies xhr request', async () => { | ||
testApp.get('/', middlewares.isAuthenticated(), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
const res = await testSession.get('/').set('X-Requested-With', 'XMLHttpRequest') | ||
expect(res.status).toBe(401) | ||
expect(res.body).toEqual({error: 'Unauthorized'}) | ||
}) | ||
|
||
test('identifies json accept request', async () => { | ||
testApp.get('/', middlewares.isAuthenticated(), () => { | ||
fail('did not protect') | ||
}) | ||
|
||
const res = await testSession.get('/').set('Accept', 'application/json') | ||
expect(res.status).toBe(401) | ||
expect(res.body).toEqual({error: 'Unauthorized'}) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -1811,7 +1811,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b" | ||
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw== | ||
|
||
cookiejar@^2.1.4: | ||
cookiejar@^2.1.2, cookiejar@^2.1.4: | ||
version "2.1.4" | ||
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.4.tgz#ee669c1fea2cf42dc31585469d193fef0d65771b" | ||
integrity sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw== | ||
|
@@ -6705,6 +6705,15 @@ superagent@^8.0.5: | |
qs "^6.11.0" | ||
semver "^7.3.8" | ||
|
||
supertest-session@^5.0.1: | ||
version "5.0.1" | ||
resolved "https://registry.yarnpkg.com/supertest-session/-/supertest-session-5.0.1.tgz#046beefab6b5cc5c1f38e7ff9a532a341a2925e1" | ||
integrity sha512-RpR8tGQZGreQsOCiW3YMSPKMwPlAB8lA0Jyat+8VUSJaYvLHTMqhMW6gooJ2htzjr3w/kgqJTQDnmuFenzA9JA== | ||
dependencies: | ||
cookiejar "^2.1.2" | ||
methods "^1.1.2" | ||
object-assign "^4.0.1" | ||
|
||
supertest@^6.3.3: | ||
version "6.3.3" | ||
resolved "https://registry.yarnpkg.com/supertest/-/supertest-6.3.3.tgz#42f4da199fee656106fd422c094cf6c9578141db" | ||
|