Skip to content

Commit

Permalink
[cli] Add CLI login unit test (vercel#6866)
Browse files Browse the repository at this point in the history
This has just been sitting in my working tree for a while, might as well push it up.
  • Loading branch information
TooTallNate authored Oct 19, 2021
1 parent b88e65c commit c35c054
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/util/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Client extends EventEmitter {
let body;
if (isJSONObject(opts.body)) {
body = JSON.stringify(opts.body);
headers.set('content-type', 'application/json; charset=utf8');
headers.set('content-type', 'application/json; charset=utf-8');
} else {
body = opts.body;
}
Expand Down
15 changes: 14 additions & 1 deletion packages/cli/test/commands/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { client } from '../mocks/client';
import login from '../../src/commands/login';
import { client } from '../mocks/client';
import { useUser } from '../mocks/user';

describe('login', () => {
it('should not allow the `--token` flag', async () => {
Expand All @@ -10,4 +11,16 @@ describe('login', () => {
'Error! `--token` may not be used with the "login" command\n'
);
});

it('should allow login via email as argument', async () => {
const user = useUser();
client.setArgv('login', user.email);
const exitCode = await login(client);
expect(exitCode).toEqual(0);
expect(
client.outputBuffer.includes(
`Success! Email authentication complete for ${user.email}`
)
).toEqual(true);
});
});
14 changes: 14 additions & 0 deletions packages/cli/test/mocks/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,19 @@ export function useUser() {
});
});

client.scenario.post('/registration', (_req, res) => {
res.json({
token: 'T1dmvPu36nmyYisXAs7IRzcR',
securityCode: 'Practical Saola',
});
});

client.scenario.get('/registration/verify', (_req, res) => {
res.json({
token: 'hjkjn',
email: user.email,
});
});

return user;
}

0 comments on commit c35c054

Please sign in to comment.