|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE) |
| 4 | + */ |
| 5 | +/* global describe, it, afterEach, expect, pryv */ |
| 6 | + |
| 7 | +const AuthController = require('../src/Auth/AuthController'); |
| 8 | +const AuthStates = require('../src/Auth/AuthStates'); |
| 9 | +const utils = require('../src/utils'); |
| 10 | + |
| 11 | +/** |
| 12 | + * [RQLX] The `requesterDisplay` of an auth request travels untouched. |
| 13 | + * |
| 14 | + * It is how an app names itself on the auth page when the platform's app |
| 15 | + * catalog does not know it. This library posts it as given and hands back |
| 16 | + * the echo of a core that stored it; the core validates it and the auth |
| 17 | + * page decides how to show it. Driven through stubbed fetches, no platform. |
| 18 | + */ |
| 19 | +describe('[RQLX] auth-request requesterDisplay', function () { |
| 20 | + const SERVICE_INFO = { |
| 21 | + register: 'https://reg.test.local/', |
| 22 | + access: 'https://reg.test.local/access', |
| 23 | + api: 'https://{username}.test.local/', |
| 24 | + name: 'Test' |
| 25 | + }; |
| 26 | + const DISPLAY = { |
| 27 | + name: { en: 'Cycle Intelligence', fr: 'Intelligence du cycle' }, |
| 28 | + logoUri: 'https://cdn.test.local/logo.png', |
| 29 | + purpose: 'Tracks your cycle' |
| 30 | + }; |
| 31 | + const REQUEST = { |
| 32 | + requestingAppId: 'cycle-intelligence', |
| 33 | + requestedPermissions: [{ streamId: 'diary', level: 'read', defaultName: 'Journal' }], |
| 34 | + requesterDisplay: DISPLAY |
| 35 | + }; |
| 36 | + |
| 37 | + let realFetch = null; |
| 38 | + let saved = null; |
| 39 | + let posted; |
| 40 | + |
| 41 | + /** Stub fetch: serve service-info, capture the access POST, answer it. */ |
| 42 | + function stubFetch (accessResponse) { |
| 43 | + if (realFetch == null) realFetch = global.fetch; |
| 44 | + posted = []; |
| 45 | + global.fetch = async function (url, options) { |
| 46 | + if (String(url).includes('/access')) { |
| 47 | + posted.push(JSON.parse(options.body)); |
| 48 | + return { ok: true, status: 201, json: async () => accessResponse }; |
| 49 | + } |
| 50 | + return { ok: true, status: 200, json: async () => SERVICE_INFO }; |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + afterEach(function () { |
| 55 | + if (realFetch != null) global.fetch = realFetch; |
| 56 | + realFetch = null; |
| 57 | + if (saved != null) { |
| 58 | + utils.fetchPost = saved.fetchPost; |
| 59 | + utils.fetchGet = saved.fetchGet; |
| 60 | + } |
| 61 | + saved = null; |
| 62 | + }); |
| 63 | + |
| 64 | + it('[RQLA] startAccessRequest posts requesterDisplay verbatim and returns the echo of the core', async function () { |
| 65 | + // The core echoes its stored form: here the logo host is not allowed, so |
| 66 | + // the echo comes back without `logoUri`, which is what the app reads. |
| 67 | + const echo = { name: DISPLAY.name, purpose: DISPLAY.purpose }; |
| 68 | + stubFetch({ key: 'k1', authUrl: 'https://auth/', poll: 'https://poll/k1', poll_rate_ms: 1000, requesterDisplay: echo }); |
| 69 | + const service = new pryv.Service('https://reg.test.local/service/info'); |
| 70 | + const env = await service.startAccessRequest(REQUEST); |
| 71 | + expect(posted).to.have.lengthOf(1); |
| 72 | + expect(posted[0].requesterDisplay).to.deep.equal(DISPLAY); |
| 73 | + expect(env.requesterDisplay).to.deep.equal(echo); |
| 74 | + }); |
| 75 | + |
| 76 | + it('[RQLB] no echo (an older core, or a platform that does not show declared names) leaves the envelope without the key', async function () { |
| 77 | + stubFetch({ key: 'k2', authUrl: 'https://auth/', poll: 'https://poll/k2', poll_rate_ms: 1000 }); |
| 78 | + const service = new pryv.Service('https://reg.test.local/service/info'); |
| 79 | + const env = await service.startAccessRequest(REQUEST); |
| 80 | + expect(posted[0].requesterDisplay).to.deep.equal(DISPLAY); |
| 81 | + expect(env.key).to.equal('k2'); |
| 82 | + expect(env).to.not.have.property('requesterDisplay'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('[RQLC] the sign-in button posts requesterDisplay as given and its NEED_SIGNIN state carries the echo', async function () { |
| 86 | + const echo = { name: 'Cycle Intelligence' }; |
| 87 | + const bodies = []; |
| 88 | + saved = { fetchPost: utils.fetchPost, fetchGet: utils.fetchGet }; |
| 89 | + utils.fetchPost = async (url, body) => { |
| 90 | + bodies.push(body); |
| 91 | + return { |
| 92 | + response: { ok: true, status: 201 }, |
| 93 | + body: { status: 'NEED_SIGNIN', key: 'rk1', poll: 'https://core.test.local/reg/access/rk1', poll_rate_ms: 5, authUrl: 'https://ui.test.local/auth?key=rk1', requesterDisplay: echo } |
| 94 | + }; |
| 95 | + }; |
| 96 | + utils.fetchGet = async () => ({ response: { ok: false, status: 403 }, body: { status: 'REFUSED', reasonId: 'REFUSED_BY_USER' } }); |
| 97 | + const seen = []; |
| 98 | + const auth = new AuthController({ |
| 99 | + onStateChange: (s) => seen.push(s), |
| 100 | + authRequest: { requestingAppId: 'cycle-intelligence', requestedPermissions: [], credentialHandoff: 'inline', requesterDisplay: DISPLAY } |
| 101 | + }, { infoSync: () => SERVICE_INFO }); |
| 102 | + auth.serviceInfo = SERVICE_INFO; |
| 103 | + auth.state = { status: AuthStates.INITIALIZED, serviceInfo: SERVICE_INFO }; |
| 104 | + await auth.startAuthRequest(); |
| 105 | + |
| 106 | + expect(bodies).to.have.lengthOf(1); |
| 107 | + expect(bodies[0].requesterDisplay).to.deep.equal(DISPLAY); |
| 108 | + const needSignin = seen.find((s) => s.status === 'NEED_SIGNIN'); |
| 109 | + expect(needSignin.requesterDisplay).to.deep.equal(echo); |
| 110 | + // not carried past the decision |
| 111 | + const refused = seen.find((s) => s.status === 'REFUSED'); |
| 112 | + expect(refused).to.not.have.property('requesterDisplay'); |
| 113 | + }); |
| 114 | +}); |
0 commit comments