Skip to content

Commit

Permalink
feat(s2s): unit tests
Browse files Browse the repository at this point in the history
Interim commit of unit tests.
  • Loading branch information
Daniel-Lam committed Jun 5, 2020
1 parent a914bec commit 520e27d
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/auth/s2s/s2s.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
// import axios from 'axios'
// import { AxiosInstance } from 'axios'
// import { AxiosResponse } from 'axios'
// import chai from 'chai'
// import { expect } from 'chai'
// // eslint-disable-next-line @typescript-eslint/camelcase
// import jwt_decode from 'jwt-decode'
// import sinon from 'sinon'
// import sinonChai from 'sinon-chai'
// import { mockReq, mockRes } from 'sinon-express-mock'
import { AxiosPromise } from 'axios'
// eslint-disable-next-line @typescript-eslint/camelcase
import jwt_decode from 'jwt-decode'
import * as nodeOtp from 'node-otp'
import { http } from '../../http/http'
import { DecodedJWT } from './decodedJwt.interface'
import { S2SConfig } from './s2sConfig.interface'
import { S2SAuth } from './s2s.class'
// import * as serviceAuth from './serviceAuth'

// chai.use(sinonChai)

describe('S2SAuth', () => {
let s2sAuth: S2SAuth
const s2sConfig = {
microservice: 'rpx-xui',
microservice: 'rpx_xui',
s2sEndpointUrl: 'http://s2s.local',
s2sSecret: 'topSecret',
} as S2SConfig
// const token = 'abc123'
const token = {
data: 'abc123',
}
const oneTimePassword = 'password'

beforeEach(() => {
s2sAuth = new S2SAuth()
s2sAuth.configure(s2sConfig)
// sinon.stub(serviceAuth, 'postS2SLease').resolves(token)
// eslint-disable-next-line @typescript-eslint/camelcase
// sinon.stub(jwt_decode.prototype).returns({ exp: Math.floor(Date.now() + 10000 / 1000) })

jest.spyOn(nodeOtp, 'totp').mockReturnValue(oneTimePassword)
jest.spyOn(http, 'post').mockImplementation(() => (Promise.resolve(token) as unknown) as AxiosPromise<string>)

jest.mock(
'jwt-decode',
() => jest.fn().mockReturnValue({ exp: Math.floor((Date.now() + 10000) / 1000) }),
// jest.fn().mockImplementation(() => ({ exp: Math.floor((Date.now() + 10000) / 1000) } as DecodedJWT)),
)
})

it('should be true that S2SAuth is defined', () => {
expect(S2SAuth).toBeDefined()
})

// xit('should return true that the cache is valid', () => {
xit('should generate an S2S token', async () => {
const s2sToken = await s2sAuth.serviceTokenGenerator()
expect(s2sToken).toEqual(token)
})
// it('should return true that the cache is valid', () => {
// const req = mockReq()
// const res = mockRes()
// // eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down

0 comments on commit 520e27d

Please sign in to comment.