Skip to content

Commit 0012143

Browse files
Add tests
1 parent d5f53f0 commit 0012143

File tree

6 files changed

+63
-17
lines changed

6 files changed

+63
-17
lines changed

lib/create-app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ function initAuthentication (argv, app) {
242242
let oidc = OidcManager.fromServerConfig(argv)
243243
app.locals.oidc = oidc
244244

245+
oidc.initialize()
246+
245247
// Initialize the WebId-OIDC authentication routes/api, including:
246248
// user-facing Solid endpoints (/login, /logout, /api/auth/select-provider)
247249
// and OIDC-specific ones
248250
app.use('/', API.oidc.middleware(oidc))
249251

250-
oidc.initialize()
251-
252252
// Enforce authentication with WebID-OIDC on all LDP routes
253253
app.use('/', oidc.rs.authenticate())
254254
break

lib/models/oidc-manager.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const url = require('url')
44
const path = require('path')
5-
const debug = require('./../debug').authentication
5+
const debug = require('../debug').authentication
66

77
const OidcManager = require('oidc-auth-manager')
88

@@ -25,17 +25,15 @@ const OidcManager = require('oidc-auth-manager')
2525
*/
2626
function fromServerConfig (argv) {
2727
let providerUri = argv.host.serverUri
28-
if (!providerUri) {
29-
throw new Error('Host with serverUri required for auth initialization')
30-
}
31-
3228
let authCallbackUri = url.resolve(providerUri, '/api/oidc/rp')
3329
let postLogoutUri = url.resolve(providerUri, '/goodbye')
3430

31+
let dbPath = path.join(argv.dbPath, 'oidc')
32+
3533
let options = {
3634
debug,
3735
providerUri,
38-
dbPath: path.join(argv.dbPath, 'oidc') || './db/oidc',
36+
dbPath,
3937
authCallbackUri,
4038
postLogoutUri,
4139
saltRounds: argv.saltRounds,

lib/requests/login-request.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const UserAccount = require('../models/user-account')
1515
class LoginByPasswordRequest {
1616
/**
1717
* @constructor
18-
* @param [options={}] {Object}
18+
* @param options {Object}
1919
*
2020
* @param [options.username] {string} Unique identifier submitted by user
2121
* from the Login form. Can be one of:
@@ -31,7 +31,7 @@ class LoginByPasswordRequest {
3131
* @param [options.authQueryParams] {Object} Key/value hashmap of parsed query
3232
* parameters that will be passed through to the /authorize endpoint.
3333
*/
34-
constructor (options = {}) {
34+
constructor (options) {
3535
this.username = options.username
3636
this.password = options.password
3737
this.response = options.response
@@ -82,12 +82,12 @@ class LoginByPasswordRequest {
8282
* Factory method, returns an initialized instance of LoginByPasswordRequest
8383
* from an incoming http request.
8484
*
85-
* @param [req={}] {IncomingRequest}
86-
* @param [res={}] {ServerResponse}
85+
* @param req {IncomingRequest}
86+
* @param res {ServerResponse}
8787
*
8888
* @return {LoginByPasswordRequest}
8989
*/
90-
static fromParams (req = {}, res = {}) {
90+
static fromParams (req, res) {
9191
let body = req.body || {}
9292

9393
let userStore, accountManager
@@ -299,9 +299,6 @@ class LoginByPasswordRequest {
299299
} else if (validUser) {
300300
// Login request is a user going to /login in browser
301301
uri = this.accountManager.accountUriFor(validUser.username)
302-
} else {
303-
let host = this.accountManager.host
304-
uri = host.serverUri
305302
}
306303

307304
return uri
@@ -314,6 +311,7 @@ class LoginByPasswordRequest {
314311
// Login/register request is part of an app's auth flow
315312
uri = this.authorizeUrl()
316313
} else {
314+
// User went to /register directly, not part of an auth flow
317315
let host = this.accountManager.host
318316
uri = host.serverUri
319317
}

test/integration/authentication-oidc.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ describe('Authentication API (OIDC)', () => {
122122
})
123123
})
124124

125-
describe('Login by Username and Password (POST /login)', done => {
125+
describe('Login page (GET /login)', () => {
126+
it('should load the user login form', () => {
127+
return alice.get('/login')
128+
.expect(200)
129+
})
130+
})
131+
132+
describe('Login by Username and Password (POST /login)', () => {
126133
// Logging in as alice, to alice's pod
127134
let aliceAccount = UserAccount.from({ webId: aliceWebId })
128135
let alicePassword = '12345'
@@ -204,4 +211,11 @@ describe('Authentication API (OIDC)', () => {
204211

205212
it('At /login, enter WebID & password -> redirect back to /foo')
206213
})
214+
215+
describe('Post-logout page (GET /goodbye)', () => {
216+
it('should load the post-logout page', () => {
217+
return alice.get('/goodbye')
218+
.expect(200)
219+
})
220+
})
207221
})

test/unit/login-by-password-request.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,36 @@ describe('LoginByPasswordRequest', () => {
454454
expect(res.statusCode).to.equal(302)
455455
expect(res._getRedirectUrl()).to.equal(expectedUri)
456456
})
457+
458+
describe('postRegisterUrl', () => {
459+
it('should return encoded /authorize url if redirect_uri is present', () => {
460+
let res = HttpMocks.createResponse()
461+
let authUrl = 'https://localhost/authorize?client_id=123'
462+
463+
let authQueryParams = {
464+
redirect_uri: 'https://app.example.com/callback'
465+
}
466+
467+
let options = { accountManager, authQueryParams, response: res }
468+
let request = new LoginByPasswordRequest(options)
469+
470+
request.authorizeUrl = sinon.stub().returns(authUrl)
471+
472+
let expectedAuthUrl = encodeURIComponent(authUrl)
473+
474+
expect(request.postRegisterUrl()).to.equal(expectedAuthUrl)
475+
})
476+
477+
it('should return encoded serverUri if not part of auth workflow', () => {
478+
let res = HttpMocks.createResponse()
479+
480+
let options = { accountManager, response: res }
481+
let request = new LoginByPasswordRequest(options)
482+
483+
let serverUri = 'https://localhost:8443'
484+
let encodedServerUri = encodeURIComponent(serverUri)
485+
486+
expect(request.postRegisterUrl()).to.equal(encodedServerUri)
487+
})
488+
})
457489
})

test/unit/oidc-manager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const SolidHost = require('../../lib/models/solid-host')
99

1010
describe('OidcManager', () => {
1111
describe('fromServerConfig()', () => {
12+
it('should error if no serverUri is provided in argv', () => {
13+
14+
})
15+
1216
it('should result in an initialized oidc object', () => {
1317
let serverUri = 'https://localhost:8443'
1418
let host = SolidHost.from({ serverUri })

0 commit comments

Comments
 (0)