@defra/bng-utils-lib 0.60.0 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.jest/setup.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
process.env.AZURE_STORAGE_ACCOUNT='devstoreaccount1'
|
|
2
|
+
process.env.AZURE_STORAGE_ACCESS_KEY='Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='
|
|
3
|
+
process.env.AZURE_BLOB_SERVICE_URL='http://127.0.0.1:10000/devstoreaccount1'
|
|
4
|
+
process.env.AZURE_QUEUE_SERVICE_URL='http://127.0.0.1:10001/devstoreaccount1'
|
|
5
|
+
process.env.POSTGRES_HOST='localhost'
|
|
6
|
+
process.env.POSTGRES_USER='postgres'
|
|
7
|
+
process.env.POSTGRES_PASSWORD='postgres'
|
|
8
|
+
process.env.POSTGRES_DATABASE='bng'
|
|
9
|
+
process.env.POSTGRES_PORT='5432'
|
|
10
|
+
process.env.POSTGRES_SSL_MODE=''
|
package/jest.config.cjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/bng-utils-lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.62.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "A library of utilities for the Biodiversity Net Gain Service",
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
"author": "Department for Environment, Food and Rural Affairs <https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs>",
|
|
17
17
|
"license": "OGL-UK-3.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@azure/identity": "^3.1.3",
|
|
20
|
+
"@defra/bng-connectors-lib": "0.62.0",
|
|
19
21
|
"axios": "^1.3.2"
|
|
20
22
|
},
|
|
21
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "f7e885a598331721c7333efd848eda6db0ac0605"
|
|
22
24
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
jest.mock('../db-config.js')
|
|
2
|
+
jest.mock('@azure/identity')
|
|
3
|
+
jest.mock('@defra/bng-connectors-lib')
|
|
4
|
+
|
|
5
|
+
describe('Database connection retrieval', () => {
|
|
6
|
+
it('Should request DB access token if no password supplied for database', done => {
|
|
7
|
+
jest.isolateModules(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const dbConfig = require('../db-config.js')
|
|
10
|
+
dbConfig.default = {
|
|
11
|
+
host: process.env.POSTGRES_HOST,
|
|
12
|
+
user: process.env.POSTGRES_USER,
|
|
13
|
+
database: process.env.POSTGRES_DATABASE,
|
|
14
|
+
port: process.env.POSTGRES_PORT,
|
|
15
|
+
ssl: !!process.env.POSTGRES_SSL_MODE
|
|
16
|
+
}
|
|
17
|
+
const getDBConnection = require('../get-db-connection.js').default
|
|
18
|
+
const { DefaultAzureCredential } = require('@azure/identity')
|
|
19
|
+
DefaultAzureCredential.prototype.getToken = jest.fn().mockImplementation(() => {
|
|
20
|
+
return {
|
|
21
|
+
token: 'test'
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
await getDBConnection()
|
|
25
|
+
expect(DefaultAzureCredential.prototype.getToken.mock.calls).toHaveLength(1)
|
|
26
|
+
done()
|
|
27
|
+
} catch (err) {
|
|
28
|
+
done(err)
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('Should not request DB access token if password supplied for database', done => {
|
|
34
|
+
jest.isolateModules(async () => {
|
|
35
|
+
const dbConfig = require('../db-config.js')
|
|
36
|
+
dbConfig.default = {
|
|
37
|
+
host: process.env.POSTGRES_HOST,
|
|
38
|
+
user: process.env.POSTGRES_USER,
|
|
39
|
+
password: process.env.POSTGRES_PASSWORD,
|
|
40
|
+
database: process.env.POSTGRES_DATABASE,
|
|
41
|
+
port: process.env.POSTGRES_PORT,
|
|
42
|
+
ssl: !!process.env.POSTGRES_SSL_MODE
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const getDBConnection = require('../get-db-connection.js').default
|
|
46
|
+
const { DefaultAzureCredential } = require('@azure/identity')
|
|
47
|
+
DefaultAzureCredential.prototype.getToken = jest.fn().mockImplementation(() => {
|
|
48
|
+
return {
|
|
49
|
+
token: 'test'
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
await getDBConnection()
|
|
53
|
+
expect(DefaultAzureCredential.prototype.getToken.mock.calls).toHaveLength(0)
|
|
54
|
+
done()
|
|
55
|
+
} catch (err) {
|
|
56
|
+
done(err)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const dbConfig = {
|
|
2
|
+
host: process.env.POSTGRES_HOST,
|
|
3
|
+
user: process.env.POSTGRES_USER,
|
|
4
|
+
password: process.env.POSTGRES_PASSWORD,
|
|
5
|
+
database: process.env.POSTGRES_DATABASE,
|
|
6
|
+
port: process.env.POSTGRES_PORT,
|
|
7
|
+
ssl: !!process.env.POSTGRES_SSL_MODE
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default dbConfig
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { postgresConnector } from '@defra/bng-connectors-lib'
|
|
2
|
+
import { DefaultAzureCredential } from '@azure/identity'
|
|
3
|
+
import DbConfig from './db-config.js'
|
|
4
|
+
|
|
5
|
+
const getDBConnection = async () => {
|
|
6
|
+
// Clone so that we don't store the token (which will expire)
|
|
7
|
+
const dbConfig = JSON.parse(JSON.stringify(DbConfig))
|
|
8
|
+
if (!dbConfig.password) {
|
|
9
|
+
const credential = new DefaultAzureCredential()
|
|
10
|
+
dbConfig.password = (await credential.getToken('https://ossrdbms-aad.database.windows.net')).token
|
|
11
|
+
}
|
|
12
|
+
return new postgresConnector.Db(dbConfig)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default getDBConnection
|
package/src/utils.js
CHANGED