Skip to content

Commit 9247f33

Browse files
committed
feat: account provided but no password, prompt user for password
1 parent 914eaa2 commit 9247f33

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-command",
3-
"version": "0.1.51-beta.0",
3+
"version": "0.1.52",
44
"description": "A CLI tools for AElf",
55
"main": "src/index.js",
66
"type": "module",

src/command/call.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,18 @@ class CallCommand extends BaseSubCommand {
9696
try {
9797
let { contractAddress, method, params } = subOptions;
9898
let wallet;
99-
if (!account || !password) {
100-
// no need to provide account and password
99+
if (!account) {
100+
// No account and password provided, create a new wallet
101101
wallet = AElf.wallet.createNewWallet();
102+
} else if (account && !password) {
103+
// Account provided but no password, prompt user for password
104+
const passwordPrompt = await inquirer.prompt({
105+
type: 'password',
106+
name: 'password',
107+
message: 'Please enter your password:',
108+
mask: '*'
109+
});
110+
wallet = getWallet(datadir, account, passwordPrompt.password);
102111
} else {
103112
wallet = getWallet(datadir, account, password);
104113
}

test/command/call.test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Command } from 'commander';
2-
import path from 'path';
32
import AElf from 'aelf-sdk';
43
import inquirer from 'inquirer';
54
import { CallCommand } from '../../src/command';
@@ -196,3 +195,64 @@ describe('CallCommand', () => {
196195
inquirer.prompt = backup;
197196
});
198197
});
198+
199+
describe('run call method when only account is provided', () => {
200+
let callCommand;
201+
let mockCommander;
202+
let mockOraInstance;
203+
let mockInquirer;
204+
let getWallet;
205+
let AElf;
206+
beforeEach(() => {
207+
jest.resetModules();
208+
jest.mock('inquirer');
209+
jest.mock('../../src/utils/wallet.js');
210+
jest.mock('aelf-sdk');
211+
mockInquirer = require('inquirer');
212+
mockOraInstance = {
213+
start: jest.fn(),
214+
succeed: jest.fn(),
215+
fail: jest.fn()
216+
};
217+
getWallet = require('../../src/utils/wallet.js').getWallet;
218+
AElf = require('aelf-sdk');
219+
mockCommander = {
220+
name: 'call',
221+
opts: jest.fn(() => ({
222+
account,
223+
endpoint: endPoint,
224+
datadir: dataDir,
225+
password: null
226+
}))
227+
};
228+
229+
callCommand = new CallCommand(sampleRc, 'call', 'Test description', [], [], []);
230+
callCommand.oraInstance = mockOraInstance;
231+
});
232+
afterEach(() => {
233+
jest.resetAllMocks();
234+
});
235+
test('should prompt for password when only account is provided', async () => {
236+
// Mock getWallet to ensure it's called correctly
237+
getWallet.mockReturnValueOnce({
238+
address: 'testAddress'
239+
});
240+
241+
// Mock AElf instance creation
242+
AElf.providers.HttpProvider.mockImplementation(() => ({
243+
send: jest.fn()
244+
}));
245+
inquirer.prompt = jest.fn();
246+
// Run the method
247+
await callCommand.run(mockCommander);
248+
// Assertions
249+
expect(inquirer.prompt).toHaveBeenCalledWith(
250+
expect.objectContaining({
251+
type: 'password',
252+
name: 'password',
253+
message: 'Please enter your password:',
254+
mask: '*'
255+
})
256+
);
257+
});
258+
});

0 commit comments

Comments
 (0)