|
13 | 13 | const Nano = require('..')
|
14 | 14 | const COUCH_URL = 'http://localhost:5984'
|
15 | 15 | const nano = Nano({ url: COUCH_URL, jar: true })
|
| 16 | +const nano2 = Nano({ url: COUCH_URL, jar: true }) |
16 | 17 | const nock = require('nock')
|
17 | 18 |
|
18 | 19 | afterEach(() => {
|
19 | 20 | nock.cleanAll()
|
20 | 21 | })
|
21 | 22 |
|
22 |
| -test('should be able to authenticate - POST /_session - nano.auth', async () => { |
| 23 | +test('should be able to authenticate multiple separate sessions - POST /_session - nano.auth', async () => { |
23 | 24 | // mocks
|
24 | 25 | const username = 'u'
|
| 26 | + const username2 = 'u2' |
25 | 27 | const password = 'p'
|
| 28 | + const password2 = 'p2' |
26 | 29 | const response = { ok: true, name: 'admin', roles: ['_admin', 'admin'] }
|
| 30 | + const response2 = { ok: true, name: 'operator', roles: ['_admin'] } |
27 | 31 | const authsession = 'AuthSession=YWRtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4fm5w;'
|
| 32 | + const authsession2 = 'AuthSession=XYZtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4123;' |
28 | 33 | const cookie = authsession + ' Version=1; Expires=Mon, 10-Feb-2050 09:03:21 GMT; Max-Age=600; Path=/; HttpOnly'
|
| 34 | + const cookie2 = authsession2 + ' Version=1; Expires=Fri Jun 10 2022 20:13:00 GMT; Max-Age=300; Path=/; HttpOnly' |
29 | 35 | const scope = nock(COUCH_URL)
|
30 | 36 | .post('/_session', 'name=u&password=p', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' })
|
31 | 37 | .reply(200, response, { 'Set-Cookie': cookie })
|
32 | 38 | .get('/_all_dbs')
|
33 | 39 | .reply(200, ['a'])
|
| 40 | + .post('/_session', 'name=u2&password=p2', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' }) |
| 41 | + .reply(200, response2, { 'Set-Cookie': cookie2 }) |
| 42 | + .get('/_all_dbs') |
| 43 | + .reply(200, ['a']) |
| 44 | + .get('/_all_dbs') |
| 45 | + .reply(200, ['a']) |
34 | 46 |
|
35 | 47 | // test POST /_session
|
36 | 48 | const p = await nano.auth(username, password)
|
37 | 49 | expect(p).toStrictEqual(response)
|
38 | 50 | await nano.db.list()
|
39 | 51 | expect(nano.config.cookies.length).toBe(1)
|
40 | 52 | expect(nano.config.cookies[0].toString().startsWith(authsession)).toBe(true)
|
| 53 | + |
| 54 | + // test POST /_session |
| 55 | + const p2 = await nano2.auth(username2, password2) |
| 56 | + expect(p2).toStrictEqual(response2) |
| 57 | + await nano2.db.list() |
| 58 | + expect(nano2.config.cookies.length).toBe(1) |
| 59 | + expect(nano2.config.cookies[0].toString()).toMatch(new RegExp('^' + authsession2)) |
| 60 | + |
| 61 | + await nano.db.list() |
| 62 | + expect(nano.config.cookies.length).toBe(1) |
| 63 | + expect(nano.config.cookies[0].toString()).toMatch(new RegExp('^' + authsession)) |
| 64 | + |
41 | 65 | expect(scope.isDone()).toBe(true)
|
42 | 66 | })
|
0 commit comments