All files / test IDTokenSpec.js

100% Statements 35/35
100% Branches 0/0
100% Functions 9/9
100% Lines 35/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78          1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x         1x 1x     1x 1x   1x   1x   1x           1x 1x 1x       1x   1x 1x   1x   1x 1x 1x     1x 1x   1x     1x 1x 1x 1x          
'use strict'
 
/**
 * Test dependencies
 */
const chai = require('chai')
const fs = require('fs')
const path = require('path')
const { JWT } = require('@trust/jose')
 
/**
 * Assertions
 */
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
chai.should()
let expect = chai.expect
 
/**
 * Code under test
 */
const Provider = require('../src/Provider')
const IDToken = require('../src/IDToken')
const IDTokenSchema = require('../src/schemas/IDTokenSchema')
 
/**
 * Tests
 */
describe('IDToken', () => {
  const providerUri = 'https://example.com'
  var provider
 
  before(() => {
    let configPath = path.join(__dirname, 'config', 'provider.json')
 
    let storedConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'))
 
    provider = new Provider(storedConfig)
 
    return provider.initializeKeyChain(provider.keys)
  })
 
  /**
   * Schema
   */
  describe('schema', () => {
    it('should reference the AccessToken Schema', () => {
      IDToken.schema.should.equal(IDTokenSchema)
    })
  })
 
  describe('issue()', () => {
    let code
    let subject = { _id: 'user123' }
    let client = { 'client_id': 'client123' }
    let request, response
    let params = {}
 
    beforeEach(() => {
      request = { params, code, provider, client, subject }
      response = {}
    })
 
    it('should issue an id token', () => {
      return IDToken.issue(request, response)
        .then(res => {
          return JWT.decode(res['id_token'])
        })
        .then(token => {
          expect(token.type).to.equal('JWS')
          expect(token.header.alg).to.equal('RS256')
          expect(token.payload.iss).to.equal(providerUri)
          expect(token.payload.sub).to.equal('user123')
        })
    })
  })
})