Skip to content

Commit

Permalink
Add cli options for password & mode to user cli
Browse files Browse the repository at this point in the history
This makes testing alot easier. Also a nice to have.
  • Loading branch information
LudvigHz committed Mar 14, 2023
1 parent e33c816 commit 66649f7
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 111 deletions.
33 changes: 18 additions & 15 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,34 @@ steps:
commands:
- yarn lint

- name: test
- name: build
image: node:16-alpine
when:
event:
- push
branch:
exclude: [prod]
depends_on:
- setup
commands:
- yarn build
- yarn build:cli

- name: test
image: node:16-alpine
when:
event:
- push
depends_on:
- build
commands:
- yarn build:cli
- yarn coverage
environment:
MONGO_URL: mongodb://mongodb:27017/vote-test
REDIS_URL: redis
VITEST_MAX_THREADS: 4
VITEST_MIN_THREADS: 1

# - name: coverage
# image: node:16-alpine
Expand All @@ -51,19 +67,6 @@ steps:
# COVERALLS_GIT_BRANCH: ${DRONE_BRANCH}
# COVERALLS_SERVICE_NUMBER: ${DRONE_BUILD_NUMBER}

- name: build
image: node:16-alpine
when:
event:
- push
branch:
exclude: [prod]
depends_on:
- setup
commands:
- yarn build
- yarn build:cli

- name: server
image: node:16-alpine
detach: true
Expand Down Expand Up @@ -149,4 +152,4 @@ services:

---
kind: signature
hmac: de4f6c10d05a6d14bbd096067143a17bd6a545411b9f2582123e3192260861e1
hmac: a9799b722d1a4b65132777388561edff214578383b3cc305caa23bd7b695dff0
63 changes: 38 additions & 25 deletions bin/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import mongoose, { HydratedDocumentFromSchema } from 'mongoose';
import chalk from 'chalk';
import promptly from 'promptly';
import User, { userSchema } from '../app/models/user';
import { parseInt } from 'lodash';
const program = new Command();

function done(err: Error, user: HydratedDocumentFromSchema<typeof userSchema>) {
Expand All @@ -31,35 +32,47 @@ function validator(value: string) {
program
.version('1.0.0')
.command('create-user <username> <cardKey>')
.option('-m, --mode <mode>', 'The mode for the user. [1|2|3]')
.option('-p, --password <password>', 'password for user')
.description('create a new user')
.action((username, cardKey) => {
.action(async (username, cardKey, options) => {
const mongoURL = process.env.MONGO_URL || 'mongodb://localhost:27017/vote';

promptly.prompt(
'Usermode: \n [1] for User \n [2] for Moderator \n [3] for Admin \n Enter mode: ',
{ validator, retry: true },
(modeErr, mode) => {
const modeId = parseInt(mode);
mongoose.set('strictQuery', true);
mongoose.connect(mongoURL, {}, (connectErr) => {
if (connectErr) return done(connectErr, null);
const mongoDbName =
process.env.NODE_ENV == 'test' && process.env.VITEST_WORKER_ID
? `vote-test-${process.env.VITEST_WORKER_ID}`
: null;

promptly.password('Enter password: ', async (pwErr, password) => {
if (pwErr) return done(pwErr, null);
const user = new User({
username: username,
admin: modeId == 3,
moderator: modeId == 2 || modeId == 3,
cardKey: cardKey,
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const registeredUser = await User.register(user, password);
done(null, registeredUser);
});
});
}
);
let { mode, password } = options;

if (!['1', '2', '3'].includes(mode)) {
mode = await promptly.prompt(
'Usermode: \n [1] for User \n [2] for Moderator \n [3] for Admin \n Enter mode: ',
{ validator, retry: true }
);
}

const modeId = parseInt(mode);

if (!(password?.length > 0)) {
password = await promptly.password('Enter password: ');
}

mongoose.set('strictQuery', true);
mongoose.connect(mongoURL, { dbName: mongoDbName }, async (connectErr) => {
if (connectErr) return done(connectErr, null);

const user = new User({
username: username,
admin: modeId == 3,
moderator: modeId == 2 || modeId == 3,
cardKey: cardKey,
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const registeredUser = await User.register(user, password);
done(null, registeredUser);
});
});

program.parse(process.argv);
153 changes: 82 additions & 71 deletions test/cli/users.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import { describe, test, beforeEach } from 'vitest';
import { describe, test, beforeEach, afterAll } from 'vitest';
import chai from 'chai';
import { spawn } from 'child_process';
import User from '../../app/models/user';
const should = chai.should();

describe('User CLI', () => {
beforeEach(() => {
User.deleteMany({});
beforeEach(async () => {
await User.deleteMany({});
});

test('should create normal user', async () =>
afterAll(async () => {
await User.deleteMany({});
});

test('should create normal user', () =>
new Promise((done) => {
const stream = spawn(`${process.cwd()}/bin/users`, [
'create-user',
'normaluser',
'testcardkey1',
'-m',
'1',
'-p',
'testpassword',
]);

stream.stdin.setEncoding('utf8');
stream.stdout.setEncoding('utf8');
stream.stderr.setEncoding('utf8');

stream.stdout.on('data', (data) => {
stream.stdin.write('1\n');
});

stream.stdout.on('data', (data) => {
stream.stdin.write('testpassword\n');
stream.stderr.on('data', (data) => {
console.log(data);
});

let output = '';
stream.stdout.on('data', (data) => {
output += data;
console.log(data);
});

stream.on('close', async () => {
Expand All @@ -44,67 +49,73 @@ describe('User CLI', () => {
});
}));

test('should create moderator user', (done) => {
const stream = spawn(`${process.cwd()}/bin/users`, [
'create-user',
'moderator',
'testcardkey2',
]);

stream.stdin.setEncoding('utf8');
stream.stdout.setEncoding('utf8');

stream.stdin.write('2\n');

stream.stdout.on('data', (data) => {
stream.stdin.write('testpassword\n');
});

let output = '';
stream.stdout.on('data', (data) => {
output += data;
});

stream.on('close', async () => {
output.should.include('Created user moderator');
await User.findOne({ username: 'moderator' }).then((user) => {
user.admin.should.equal(false);
user.moderator.should.equal(true);
should.not.exist(user.password);
test('should create moderator user', () =>
new Promise((done) => {
const stream = spawn(`${process.cwd()}/bin/users`, [
'create-user',
'moderator',
'testcardkey2',
'-m',
'2',
'-p',
'testpassword',
]);

stream.stderr.setEncoding('utf8');
stream.stderr.on('data', (data) => {
console.log(data);
});
done();
});
});

test('should create admin user', (done) => {
const stream = spawn(`${process.cwd()}/bin/users`, [
'create-user',
'admin',
'testcardkey3',
]);

stream.stdin.setEncoding('utf8');
stream.stdout.setEncoding('utf8');

stream.stdin.write('3\n');

stream.stdout.on('data', (data) => {
stream.stdin.write('testpassword\n');
});

let output = '';
stream.stdout.on('data', (data) => {
output += data;
});

stream.on('close', async () => {
output.should.include('Created user admin');
await User.findOne({ username: 'admin' }).then((user) => {
user.admin.should.equal(true);
user.moderator.should.equal(true);
should.not.exist(user.password);
stream.stdout.setEncoding('utf8');

let output = '';
stream.stdout.on('data', (data) => {
output += data;
});
done();
});
});

stream.on('close', async () => {
output.should.include('Created user moderator');
await User.findOne({ username: 'moderator' }).then((user) => {
user.admin.should.equal(false);
user.moderator.should.equal(true);
should.not.exist(user.password);
});
done();
});
}));

test('should create admin user', () =>
new Promise((done) => {
const stream = spawn(`${process.cwd()}/bin/users`, [
'create-user',
'admin',
'testcardkey3',
'-m',
'3',
'-p',
'testpassword',
]);

stream.stderr.setEncoding('utf8');
stream.stderr.on('data', (data) => {
console.log(data);
});

stream.stdout.setEncoding('utf8');

let output = '';
stream.stdout.on('data', (data) => {
output += data;
});

stream.on('close', async () => {
output.should.include('Created user admin');
await User.findOne({ username: 'admin' }).then((user) => {
user.admin.should.equal(true);
user.moderator.should.equal(true);
should.not.exist(user.password);
});
done();
});
}));
});

0 comments on commit 66649f7

Please sign in to comment.