Skip to content

Commit 4962f2b

Browse files
committed
feat: add setAgoraKey task and corresponding tests for API key management
1 parent 8d200f8 commit 4962f2b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const types = require('ethernaut-common/src/validation/types')
2+
const output = require('ethernaut-common/src/ui/output')
3+
const storage = require('ethernaut-common/src/io/storage')
4+
const { setEnvVar } = require('ethernaut-common/src/io/env')
5+
6+
require('../scopes/optigov')
7+
.task('agorakey', 'Sets the Agora API Key')
8+
.addParam('apiKey', 'The Agora API Key to use', undefined, types.string)
9+
.setAction(async ({ apiKey }, _hre) => {
10+
try {
11+
const config = storage.readConfig()
12+
13+
let summary = []
14+
15+
if (apiKey) {
16+
const currentKey = process.env.AGORA_API_KEY
17+
setEnvVar('AGORA_API_KEY', apiKey)
18+
summary.push(`- Agora API Key set to ${apiKey} (was ${currentKey})`)
19+
summary.push(
20+
'Please restart the tool for the new API key to take effect.',
21+
)
22+
}
23+
24+
storage.saveConfig(config)
25+
26+
if (summary.length === 0) {
27+
summary.push('No changes')
28+
}
29+
30+
return output.resultBox(summary.join('\n'))
31+
} catch (err) {
32+
return output.errorBox(err)
33+
}
34+
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const assert = require('assert')
2+
const hre = require('hardhat')
3+
const storage = require('ethernaut-common/src/io/storage')
4+
const { setEnvVar } = require('ethernaut-common/src/io/env')
5+
6+
describe('setAgoraKey task', function () {
7+
let originalEnv, originalSetEnvVar, originalSaveConfig, originalReadConfig
8+
9+
beforeEach(function () {
10+
// Save original environment variable value
11+
originalEnv = process.env.AGORA_API_KEY
12+
13+
// Stub setEnvVar to immediately update process.env
14+
originalSetEnvVar = setEnvVar
15+
require('ethernaut-common/src/io/env').setEnvVar = (key, value) => {
16+
process.env[key] = value
17+
}
18+
19+
// Stub storage functions to prevent file I/O
20+
originalSaveConfig = storage.saveConfig
21+
originalReadConfig = storage.readConfig
22+
storage.saveConfig = () => {} // no-op
23+
storage.readConfig = () => ({})
24+
})
25+
26+
afterEach(function () {
27+
// Restore original environment variable and function references
28+
process.env.AGORA_API_KEY = originalEnv
29+
require('ethernaut-common/src/io/env').setEnvVar = originalSetEnvVar
30+
storage.saveConfig = originalSaveConfig
31+
storage.readConfig = originalReadConfig
32+
})
33+
34+
it('should set the Agora API Key and instruct to restart', async function () {
35+
const testKey = 'TEST_API_KEY'
36+
37+
const result = await hre.run(
38+
{ scope: 'optigov', task: 'agorakey' },
39+
{ apiKey: testKey },
40+
)
41+
42+
// Verify the output contains the new key and the restart message.
43+
assert(result.includes(`- Agora API Key set to ${testKey}`))
44+
assert(
45+
result.includes(
46+
'Please restart the tool for the new API key to take effect.',
47+
),
48+
)
49+
// Also verify process.env was updated
50+
assert.strictEqual(process.env.AGORA_API_KEY, testKey)
51+
})
52+
})

0 commit comments

Comments
 (0)