-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new subStatus command (#248)
- Loading branch information
Showing
8 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'pwned': minor | ||
--- | ||
|
||
Add new `subStatus` command to get the current subscription status of your HIBP API key. See https://haveibeenpwned.com/API/v3#SubscriptionStatus for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { vi, type SpyInstance } from 'vitest'; | ||
import { http } from 'msw'; | ||
import { server } from '../../../test/server.js'; | ||
import { spinnerFns, loggerFns, ERROR_MSG } from '../../../test/fixtures.js'; | ||
import { logger as mockLogger, type Logger } from '../../utils/logger.js'; | ||
import { spinner as mockSpinner } from '../../utils/spinner.js'; | ||
import { handler as subStatus } from '../sub-status.js'; | ||
|
||
vi.mock('../../utils/logger'); | ||
vi.mock('../../utils/spinner'); | ||
|
||
const logger = mockLogger as Logger & Record<string, SpyInstance>; | ||
const spinner = mockSpinner as typeof mockSpinner & Record<string, SpyInstance>; | ||
|
||
describe('command: subStatus', () => { | ||
describe('normal output (default)', () => { | ||
it('calls spinner.start', async () => { | ||
expect(spinner.start).toHaveBeenCalledTimes(0); | ||
await subStatus({ raw: false }); | ||
expect(spinner.start).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('with data: calls spinner.stop and logger.log', async () => { | ||
expect(spinner.stop).toHaveBeenCalledTimes(0); | ||
expect(logger.log).toHaveBeenCalledTimes(0); | ||
await subStatus({ raw: false }); | ||
expect(spinner.stop).toHaveBeenCalledTimes(1); | ||
expect(logger.log).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('on error: only calls spinner.fail', async () => { | ||
server.use( | ||
http.get('*', () => { | ||
throw new Error(ERROR_MSG); | ||
}), | ||
); | ||
|
||
expect(spinner.fail).toHaveBeenCalledTimes(0); | ||
loggerFns.forEach((fn) => expect(logger[fn]).toHaveBeenCalledTimes(0)); | ||
await subStatus({ raw: false }); | ||
expect(spinner.fail).toHaveBeenCalledTimes(1); | ||
loggerFns.forEach((fn) => expect(logger[fn]).toHaveBeenCalledTimes(0)); | ||
}); | ||
}); | ||
|
||
describe('raw mode', () => { | ||
it('does not call spinner.start', async () => { | ||
expect(spinner.start).toHaveBeenCalledTimes(0); | ||
await subStatus({ raw: true }); | ||
expect(spinner.start).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('with data: only calls logger.log', async () => { | ||
spinnerFns.forEach((fn) => expect(spinner[fn]).toHaveBeenCalledTimes(0)); | ||
expect(logger.log).toHaveBeenCalledTimes(0); | ||
await subStatus({ raw: true }); | ||
spinnerFns.forEach((fn) => expect(spinner[fn]).toHaveBeenCalledTimes(0)); | ||
expect(logger.log).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('on error: only calls logger.error', async () => { | ||
server.use( | ||
http.get('*', () => { | ||
throw new Error(ERROR_MSG); | ||
}), | ||
); | ||
|
||
spinnerFns.forEach((fn) => expect(spinner[fn]).toHaveBeenCalledTimes(0)); | ||
expect(logger.error).toHaveBeenCalledTimes(0); | ||
await subStatus({ raw: true }); | ||
spinnerFns.forEach((fn) => expect(spinner[fn]).toHaveBeenCalledTimes(0)); | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Argv } from 'yargs'; | ||
import { subscriptionStatus } from 'hibp'; | ||
import prettyjson from 'prettyjson'; | ||
import { config } from '../config.js'; | ||
import { logger } from '../utils/logger.js'; | ||
import { spinner } from '../utils/spinner.js'; | ||
import { userAgent } from '../utils/user-agent.js'; | ||
|
||
export const command = 'subStatus'; | ||
export const describe = 'get your subscription status'; | ||
|
||
interface SubStatusArgvOptions { | ||
r?: boolean; | ||
} | ||
|
||
interface SubStatusHandlerOptions { | ||
raw?: boolean; | ||
} | ||
|
||
/* c8 ignore start */ | ||
export function builder( | ||
yargs: Argv<SubStatusArgvOptions>, | ||
): Argv<SubStatusHandlerOptions> { | ||
return yargs | ||
.option('r', { | ||
describe: 'output the raw JSON data', | ||
type: 'boolean', | ||
default: false, | ||
}) | ||
.alias('r', 'raw') | ||
.group(['r'], 'Command Options:') | ||
.group(['h', 'v'], 'Global Options:'); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
/** | ||
* Fetches and outputs your subscription status (of your API key). | ||
* | ||
* @param {object} argv the parsed argv object | ||
* @param {boolean} [argv.raw] output the raw JSON data (default: false) | ||
* @returns {Promise<void>} the resulting Promise where output is rendered | ||
*/ | ||
export async function handler({ raw }: SubStatusHandlerOptions): Promise<void> { | ||
if (!raw) { | ||
spinner.start(); | ||
} | ||
|
||
try { | ||
const subStatusData = await subscriptionStatus({ | ||
apiKey: config.get('apiKey'), | ||
userAgent, | ||
}); | ||
if (raw) { | ||
logger.log(JSON.stringify(subStatusData)); | ||
} else { | ||
spinner.stop(); | ||
logger.log(prettyjson.render(subStatusData)); | ||
} | ||
} catch (err: unknown) { | ||
/* c8 ignore else */ | ||
if (err instanceof Error) { | ||
if (!raw) { | ||
spinner.fail(err.message); | ||
} else { | ||
logger.error(err.message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { stripIndents } from 'common-tags'; | ||
import type { Breach, Paste } from 'hibp'; | ||
import type { Breach, Paste, SubscriptionStatus } from 'hibp'; | ||
|
||
export const spinnerFns = ['start', 'stop', 'succeed', 'warn', 'fail']; | ||
export const loggerFns = ['info', 'log', 'warn', 'error']; | ||
|
@@ -40,6 +40,14 @@ export const PASSWORD_HASHES = stripIndents` | |
01330C689E5D64F660D6947A93AD634EF8F:1 | ||
`; | ||
|
||
export const SUBSCRIPTION_STATUS: SubscriptionStatus = { | ||
SubscriptionName: 'Pwned 42', | ||
Description: 'A mock subscrpition', | ||
SubscribedUntil: '2023-12-31T01:23:45', | ||
Rpm: 69, | ||
DomainSearchMaxBreachedAccounts: 0, | ||
}; | ||
|
||
export const EMAIL = '[email protected]'; | ||
export const EMPTY_ARRAY = []; | ||
export const ERROR = 'foo'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters