adapt-authoring-config 1.2.0 → 1.3.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.
@@ -0,0 +1,15 @@
1
+ name: Tests
2
+ on: push
3
+ jobs:
4
+ default:
5
+ runs-on: ubuntu-latest
6
+ permissions:
7
+ contents: read
8
+ steps:
9
+ - uses: actions/checkout@master
10
+ - uses: actions/setup-node@master
11
+ with:
12
+ node-version: 'lts/*'
13
+ cache: 'npm'
14
+ - run: npm ci
15
+ - run: npm test
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-config",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "A configuration module for the Adapt authoring tool.",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-config",
6
6
  "license": "GPL-3.0",
@@ -9,6 +9,9 @@
9
9
  "bin": {
10
10
  "at-confgen": "./bin/confgen.js"
11
11
  },
12
+ "scripts": {
13
+ "test": "node --test tests/*.spec.js"
14
+ },
12
15
  "repository": "github:adapt-security/adapt-authoring-config",
13
16
  "dependencies": {
14
17
  "adapt-authoring-core": "^1.7.0",
@@ -1,55 +1,136 @@
1
- /* global before describe, it */
1
+ import { describe, it, before } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import ConfigModule from '../lib/ConfigModule.js'
2
4
 
3
- const Config = require('../lib/configUtils')
4
- const path = require('path')
5
- // const should = require('should')
5
+ describe('ConfigModule', () => {
6
+ let instance
6
7
 
7
- describe('Config module', function () {
8
- before(function () {
9
- this.config = new Config(global.ADAPT.app, {})
10
- this.configJson = require(path.join(process.cwd(), 'conf', 'testing.config.js'))
8
+ before(async () => {
9
+ const noopHook = { tap: () => {}, untap: () => {}, invoke: async () => {} }
10
+ const noopRouter = { path: '/', createChildRouter: () => noopRouter }
11
+ const mockApp = {
12
+ config: null,
13
+ rootDir: '/test',
14
+ name: 'test-app',
15
+ dependencies: {},
16
+ dependencyloader: { moduleLoadedHook: noopHook },
17
+ waitForModule: async (...names) => {
18
+ const mocks = {
19
+ errors: { LOAD_ERROR: new Error('load') },
20
+ auth: { unsecureRoute: () => {} },
21
+ server: { api: { createChildRouter: () => noopRouter } },
22
+ jsonschema: { createSchema: async () => ({ build: async () => ({}) }) }
23
+ }
24
+ const results = names.map(n => mocks[n] || {})
25
+ return results.length === 1 ? results[0] : results
26
+ },
27
+ errors: { LOAD_ERROR: new Error('load') }
28
+ }
29
+
30
+ instance = new ConfigModule(mockApp, {})
31
+
32
+ // Wait for the async init to settle (it will error in test mode, which is fine)
33
+ try { await instance.onReady() } catch (e) { /* expected */ }
34
+
35
+ // Ensure internal state is initialized for testing
36
+ if (!instance._config) instance._config = {}
37
+ if (!instance.publicAttributes) instance.publicAttributes = []
11
38
  })
12
- describe('#initialise()', function () {
13
- it('should error on missing required attribute', runConfigInitialise('required'))
14
- it('should error on incorrect attribute type', runConfigInitialise('incorrecttype'))
15
- it('should error on validator fail', runConfigInitialise('invalid'))
39
+
40
+ describe('#envVarToConfigKey()', () => {
41
+ it('should convert ADAPT_AUTHORING prefixed env vars to config keys', () => {
42
+ const result = instance.envVarToConfigKey('ADAPT_AUTHORING_SERVER__PORT')
43
+ assert.equal(result, 'adapt-authoring-server.PORT')
44
+ })
45
+
46
+ it('should convert underscores to hyphens in module prefix', () => {
47
+ const result = instance.envVarToConfigKey('ADAPT_AUTHORING_MY_MODULE__KEY')
48
+ assert.equal(result, 'adapt-authoring-my-module.KEY')
49
+ })
50
+
51
+ it('should prefix non-ADAPT_AUTHORING env vars with "env."', () => {
52
+ const result = instance.envVarToConfigKey('NODE_ENV')
53
+ assert.equal(result, 'env.NODE_ENV')
54
+ })
55
+
56
+ it('should handle env vars without double underscore', () => {
57
+ // When no __ separator exists, key will be undefined
58
+ // This documents the current behavior of the function
59
+ const result = instance.envVarToConfigKey('ADAPT_AUTHORING_TEST')
60
+ assert.equal(result, 'adapt-authoring-test.undefined')
61
+ })
16
62
  })
17
- describe('#has()', function () {
18
- it('should be able to verify a value exists', function () {
19
- const exists = this.config.has('adapt-authoring-testing.test')
20
- exists.should.be.true()
63
+
64
+ describe('#has()', () => {
65
+ it('should return true for existing config values', () => {
66
+ instance._config['test.key'] = 'value'
67
+ const exists = instance.has('test.key')
68
+ assert.equal(exists, true)
21
69
  })
22
- it('should be able to verify a value doesn\'t exist', function () {
23
- const exists = this.config.has('adapt-authoring-testing.nonono')
24
- exists.should.not.be.true()
70
+
71
+ it('should return false for non-existent config values', () => {
72
+ const exists = instance.has('nonexistent.key')
73
+ assert.equal(exists, false)
25
74
  })
26
75
  })
27
- describe('#get()', function () {
28
- it('should be able to retrieve a value', function () {
29
- const actualValue = this.config.get('adapt-authoring-testing.test')
30
- const expectedValue = this.configJson['adapt-authoring-testing'].test
31
- actualValue.should.equal(expectedValue)
76
+
77
+ describe('#get()', () => {
78
+ it('should retrieve a stored value', () => {
79
+ instance._config['test.value'] = 'expected'
80
+ const actual = instance.get('test.value')
81
+ assert.equal(actual, 'expected')
82
+ })
83
+
84
+ it('should return undefined for non-existent keys', () => {
85
+ const actual = instance.get('does.not.exist')
86
+ assert.equal(actual, undefined)
87
+ })
88
+
89
+ it('should retrieve different data types', () => {
90
+ instance._config['test.string'] = 'text'
91
+ instance._config['test.number'] = 42
92
+ instance._config['test.boolean'] = true
93
+ instance._config['test.object'] = { key: 'value' }
94
+
95
+ assert.equal(instance.get('test.string'), 'text')
96
+ assert.equal(instance.get('test.number'), 42)
97
+ assert.equal(instance.get('test.boolean'), true)
98
+ assert.deepEqual(instance.get('test.object'), { key: 'value' })
32
99
  })
33
100
  })
34
- describe('#getPublicConfig()', function () {
35
- it('should be able to retrieve values marked as public', function () {
36
- this.config.app.dependencies = [{ name: 'adapt-authoring-testing', dir: path.join(__dirname, 'data') }]
37
- this.config.initialise()
38
- const config = this.config.getPublicConfig()
39
- const value = config['adapt-authoring-testing.one']
40
- config.should.be.an.Object()
41
- value.should.equal('default')
101
+
102
+ describe('#getPublicConfig()', () => {
103
+ it('should return only public attributes', () => {
104
+ instance._config = {
105
+ 'module.public1': 'value1',
106
+ 'module.public2': 'value2',
107
+ 'module.private': 'secret'
108
+ }
109
+ instance.publicAttributes = ['module.public1', 'module.public2']
110
+
111
+ const config = instance.getPublicConfig()
112
+ assert.deepEqual(config, {
113
+ 'module.public1': 'value1',
114
+ 'module.public2': 'value2'
115
+ })
116
+ })
117
+
118
+ it('should return empty object when no public attributes exist', () => {
119
+ instance._config = { 'module.private': 'secret' }
120
+ instance.publicAttributes = []
121
+
122
+ const config = instance.getPublicConfig()
123
+ assert.deepEqual(config, {})
124
+ })
125
+
126
+ it('should handle undefined values for public attributes', () => {
127
+ instance._config = {}
128
+ instance.publicAttributes = ['module.missing']
129
+
130
+ const config = instance.getPublicConfig()
131
+ assert.deepEqual(config, {
132
+ 'module.missing': undefined
133
+ })
42
134
  })
43
135
  })
44
136
  })
45
- /**
46
- * Checks ConfigUtility#initialise
47
- * Loads the testing data in tests/data/dirname
48
- */
49
- function runConfigInitialise (dirname) {
50
- return function () {
51
- this.config.app.dependencies = [{ name: 'adapt-authoring-testing', dir: path.join(__dirname, 'data', dirname) }]
52
- this.config.initialise()
53
- this.config.errors.length.should.be.exactly(1)
54
- }
55
- }
@@ -1,36 +0,0 @@
1
- /* global describe, it */
2
-
3
- const path = require('path')
4
- const utils = require('../lib/ConfigUtils')
5
- const should = require('should')
6
-
7
- describe('Config utils', function () {
8
- describe('#loadFile()', function () {
9
- it('should be able to load a valid file', function () {
10
- const filepath = path.join(__dirname, 'data', 'testfile.json')
11
- const actualContents = utils.loadFile(filepath)
12
- const expectedContents = require(filepath)
13
- actualContents.should.deepEqual(expectedContents)
14
- })
15
- it('should not error on a missing file', function () {
16
- should.doesNotThrow(function () {
17
- const contents = utils.loadFile(path.join('this', 'path', 'does', 'not', 'exist.xyz'))
18
- should(contents).be.undefined()
19
- })
20
- })
21
- })
22
- describe('#loadConfigSchema()', function () {
23
- it('should be able to load a valid schema file', function () {
24
- const dir = path.join(__dirname, 'data')
25
- const actualContents = utils.loadConfigSchema(dir)
26
- const expectedContents = require(path.join(dir, 'conf', 'config.schema.js'))
27
- actualContents.should.deepEqual(expectedContents)
28
- })
29
- it('should not error on a missing schema file', function () {
30
- should.doesNotThrow(function () {
31
- const contents = utils.loadConfigSchema(path.join(__dirname, 'doesntexist'))
32
- should(contents).be.undefined()
33
- })
34
- })
35
- })
36
- })