Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-11682 Fix admin client PACT test #3843

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 71 additions & 67 deletions test/unit/clients/adminusers-client-find-service.pact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,24 @@ const path = require('path')

// NPM dependencies
const { Pact } = require('@pact-foundation/pact')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const { expect } = require('chai')

// Constants
const SERVICES_PATH = '/v1/api/services'
const port = Math.floor(Math.random() * 48127) + 1024
const PORT = Math.floor(Math.random() * 48127) + 1024
const BASE_URL = `http://127.0.0.1:${PORT}`

// Custom dependencies
const getAdminUsersClient = require('../../../app/services/clients/adminusers.client')
const adminUsersClient = require('../../../app/services/clients/adminusers.client')
const serviceFixtures = require('../../fixtures/service.fixtures')
const PactInteractionBuilder = require('../../test-helpers/pact/pact-interaction-builder').PactInteractionBuilder
const { pactify } = require('../../test-helpers/pact/pact-base')()

// Global setup
const expect = chai.expect
chai.use(chaiAsPromised)
const adminusersClient = getAdminUsersClient({ baseUrl: `http://127.0.0.1:${port}` })

describe('adminusers client - services API', function () {
const provider = new Pact({
consumer: 'frontend-to-be',
provider: 'adminusers',
port: port,
port: PORT,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
Expand All @@ -40,77 +35,86 @@ describe('adminusers client - services API', function () {
describe('FIND service by gateway account id', function () {
describe('success', function () {
const gatewayAccountId = '101'
const getServiceResponse = serviceFixtures.validServiceResponse({ gateway_account_ids: [gatewayAccountId] })
before((done) => {
provider.addInteraction(
new PactInteractionBuilder(`${SERVICES_PATH}`)
.withQuery({ gatewayAccountId: gatewayAccountId })
.withState('a service exists with the given gateway account id association')
.withUponReceiving('a valid find service request')
.withResponseBody(pactify(getServiceResponse))
.withStatusCode(200)
.build()
).then(() => done())

before(() => {
const getServiceResponse = serviceFixtures.validServiceResponse({ gateway_account_ids: [gatewayAccountId] })

const builder = new PactInteractionBuilder(`${SERVICES_PATH}`)
.withMethod('GET')
.withQuery({ gatewayAccountId: gatewayAccountId })
.withState('a service exists with the given gateway account id association')
.withUponReceiving('a valid find service request')
.withResponseBody(pactify(getServiceResponse))
.withStatusCode(200)
.build()

return provider.addInteraction(builder)
})

afterEach(() => provider.verify())
setTimeout(() => {
it('should return service successfully', function (done) {
adminusersClient.findServiceBy({ gatewayAccountId: gatewayAccountId }).then(service => {
expect(service.gatewayAccountIds[0]).to.be.equal(gatewayAccountId)
done()
}).catch((err) => done('should not be hit: ' + JSON.stringify(err)))
})
}, 2000)

it('should return service successfully', async function () {
const res = await adminUsersClient({ baseUrl: BASE_URL }).findServiceBy(
{ gatewayAccountId: gatewayAccountId }
)

expect(res.gatewayAccountIds[0]).to.be.equal(gatewayAccountId)
})
})

describe('bad request', function () {
const invalidGatewayAccountId = 'not-a-number'
beforeEach((done) => {
provider.addInteraction(
new PactInteractionBuilder(`${SERVICES_PATH}`)
.withQuery({ gatewayAccountId: invalidGatewayAccountId })
.withState('a service exists with the given gateway account id association')
.withUponReceiving('an invalid find service request')
.withStatusCode(400)
.build()
).then(() => done())
beforeEach(() => {
const builder = new PactInteractionBuilder(`${SERVICES_PATH}`)
.withMethod('GET')
.withQuery({ gatewayAccountId: invalidGatewayAccountId })
.withState('a service exists with the given gateway account id association')
.withUponReceiving('an invalid find service request')
.withStatusCode(400)
.build()

return provider.addInteraction(builder)
})

afterEach(() => provider.verify())
setTimeout(() => {
it('error 400', function (done) {
adminusersClient.findServiceBy({ gatewayAccountId: invalidGatewayAccountId })
.then(() => done('should not be hit'))
.catch(response => {
expect(response.errorCode).to.be.equal(400)
done()
})
})
}, 3000)

it('error 400', async function () {
try {
await adminUsersClient({ baseUrl: BASE_URL }).findServiceBy(
{ gatewayAccountId: invalidGatewayAccountId }
)
throw new Error('should not be hit')
} catch (error) {
expect(error.errorCode).to.be.equal(400)
}
})
})

describe('not found', function () {
const nonAssociatedGatewayAccountId = '999'
beforeEach((done) => {
provider.addInteraction(
new PactInteractionBuilder(`${SERVICES_PATH}`)
.withQuery({ gatewayAccountId: nonAssociatedGatewayAccountId })
.withState('a service with given gateway account id does not exist')
.withUponReceiving('a valid find service request')
.withStatusCode(404)
.build()
).then(() => done())
beforeEach(() => {
const builder = new PactInteractionBuilder(`${SERVICES_PATH}`)
.withMethod('GET')
.withQuery({ gatewayAccountId: nonAssociatedGatewayAccountId })
.withState('a service with given gateway account id does not exist')
.withUponReceiving('a valid find service request')
.withStatusCode(404)
.build()

return provider.addInteraction(builder)
})
afterEach(() => provider.verify())
setTimeout(() => {
it('error 400', function (done) {
adminusersClient.findServiceBy({ gatewayAccountId: nonAssociatedGatewayAccountId })
.then(() => done('should not be hit'))
.catch(response => {
expect(response.errorCode).to.be.equal(404)
done()
})
})
}, 4000)

it('error 404', async function () {
try {
await adminUsersClient({ baseUrl: BASE_URL }).findServiceBy(
{ gatewayAccountId: nonAssociatedGatewayAccountId }
)
throw new Error('should not be hit')
} catch (error) {
expect(error.errorCode).to.be.equal(404)
}
})
})
})
})
Loading