Skip to content

Commit

Permalink
test: add tests for new package initialisation (#39)
Browse files Browse the repository at this point in the history
* test: add tests for package initialisation

* test: fix weird expected keys

* test: add better comments for publicKey test
  • Loading branch information
karrui committed Jun 8, 2020
1 parent 554afa3 commit 1e9a7e7
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions spec/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,87 @@
import formsg from '../src/index'
import {
getVerificationPublicKey,
getSigningPublicKey,
} from '../src/util/publicKey'
import { VERIFICATION_KEYS } from '../src/resource/verification-keys'
import { SIGNING_KEYS } from '../src/resource/signing-keys'
import Webhooks from '../src/webhooks'
import Crypto from '../src/crypto'
import Verification from '../src/verification'

const TEST_PUBLIC_KEY = SIGNING_KEYS.test.publicKey

describe('FormSG SDK', () => {
it('should be able to initialise without arguments', () => {
expect(() => formsg()).not.toThrow()
describe('Initialisation', () => {
it('should be able to initialise without arguments', () => {
expect(() => formsg()).not.toThrow()
})

it('should be able to initialise with valid verification options', () => {
// Arrange
const TEST_TRANSACTION_EXPIRY = 10000
const sdk = formsg({
mode: 'test',
verificationOptions: {
secretKey: VERIFICATION_KEYS.test.secretKey,
transactionExpiry: TEST_TRANSACTION_EXPIRY,
},
})

expect(sdk.verification.verificationPublicKey).toEqual(
VERIFICATION_KEYS.test.publicKey
)
expect(sdk.verification.verificationSecretKey).toEqual(
VERIFICATION_KEYS.test.secretKey
)
expect(sdk.verification.transactionExpiry).toEqual(
TEST_TRANSACTION_EXPIRY
)
})
})

describe('Public keys', () => {
it('should get the correct verification public key given a mode', () => {
expect(getVerificationPublicKey('test')).toBe(
VERIFICATION_KEYS.test.publicKey
)
expect(getVerificationPublicKey('staging')).toBe(
VERIFICATION_KEYS.staging.publicKey
)
expect(getVerificationPublicKey('development')).toBe(
VERIFICATION_KEYS.development.publicKey
)
expect(getVerificationPublicKey('production')).toBe(
VERIFICATION_KEYS.production.publicKey
)
expect(getVerificationPublicKey()).toBe(
VERIFICATION_KEYS.production.publicKey
)
})
})

it('should get the correct signing key given a mode', () => {
expect(getSigningPublicKey('test')).toBe(SIGNING_KEYS.test.publicKey)
expect(getSigningPublicKey('staging')).toBe(SIGNING_KEYS.staging.publicKey)
expect(getSigningPublicKey('development')).toBe(
SIGNING_KEYS.development.publicKey
)
expect(getSigningPublicKey('production')).toBe(
SIGNING_KEYS.production.publicKey
)
expect(getSigningPublicKey()).toBe(SIGNING_KEYS.production.publicKey)
})

it('should be able to initialise with given publicKey init param', () => {
// Act
const userSpecifiedKey = TEST_PUBLIC_KEY
// Create SDK with a specified public key
const sdk = formsg({ publicKey: userSpecifiedKey })

// Assert
// All various keys used by subpackages should be the given public key.
expect(sdk.crypto.publicSigningKey).toEqual(userSpecifiedKey)
expect(sdk.verification.verificationPublicKey).toEqual(userSpecifiedKey)
expect(sdk.webhooks.publicKey).toEqual(userSpecifiedKey)
})
})

0 comments on commit 1e9a7e7

Please sign in to comment.