Skip to content

Commit

Permalink
feat: account provided but no password, prompt user for password
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng committed Dec 3, 2024
1 parent 914eaa2 commit 9247f33
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-command",
"version": "0.1.51-beta.0",
"version": "0.1.52",
"description": "A CLI tools for AElf",
"main": "src/index.js",
"type": "module",
Expand Down
13 changes: 11 additions & 2 deletions src/command/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ class CallCommand extends BaseSubCommand {
try {
let { contractAddress, method, params } = subOptions;
let wallet;
if (!account || !password) {
// no need to provide account and password
if (!account) {
// No account and password provided, create a new wallet
wallet = AElf.wallet.createNewWallet();
} else if (account && !password) {
// Account provided but no password, prompt user for password
const passwordPrompt = await inquirer.prompt({
type: 'password',
name: 'password',
message: 'Please enter your password:',
mask: '*'
});
wallet = getWallet(datadir, account, passwordPrompt.password);
} else {
wallet = getWallet(datadir, account, password);
}
Expand Down
62 changes: 61 additions & 1 deletion test/command/call.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Command } from 'commander';
import path from 'path';
import AElf from 'aelf-sdk';
import inquirer from 'inquirer';
import { CallCommand } from '../../src/command';
Expand Down Expand Up @@ -196,3 +195,64 @@ describe('CallCommand', () => {
inquirer.prompt = backup;
});
});

describe('run call method when only account is provided', () => {
let callCommand;
let mockCommander;
let mockOraInstance;
let mockInquirer;
let getWallet;
let AElf;
beforeEach(() => {
jest.resetModules();
jest.mock('inquirer');
jest.mock('../../src/utils/wallet.js');
jest.mock('aelf-sdk');
mockInquirer = require('inquirer');
mockOraInstance = {
start: jest.fn(),
succeed: jest.fn(),
fail: jest.fn()
};
getWallet = require('../../src/utils/wallet.js').getWallet;
AElf = require('aelf-sdk');
mockCommander = {
name: 'call',
opts: jest.fn(() => ({
account,
endpoint: endPoint,
datadir: dataDir,
password: null
}))
};

callCommand = new CallCommand(sampleRc, 'call', 'Test description', [], [], []);
callCommand.oraInstance = mockOraInstance;
});
afterEach(() => {
jest.resetAllMocks();
});
test('should prompt for password when only account is provided', async () => {
// Mock getWallet to ensure it's called correctly
getWallet.mockReturnValueOnce({
address: 'testAddress'
});

// Mock AElf instance creation
AElf.providers.HttpProvider.mockImplementation(() => ({
send: jest.fn()
}));
inquirer.prompt = jest.fn();
// Run the method
await callCommand.run(mockCommander);
// Assertions
expect(inquirer.prompt).toHaveBeenCalledWith(
expect.objectContaining({
type: 'password',
name: 'password',
message: 'Please enter your password:',
mask: '*'
})
);
});
});

0 comments on commit 9247f33

Please sign in to comment.