Files
cloudron-box/src/test/apppasswords-test.js

94 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-06-25 22:11:17 -07:00
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
const appPasswords = require('../apppasswords.js'),
BoxError = require('../boxerror.js'),
common = require('./common.js'),
expect = require('expect.js'),
2021-06-26 09:57:07 -07:00
safe = require('safetydance'),
users = require('../users.js');
2021-06-25 22:11:17 -07:00
describe('App passwords', function () {
2021-08-13 10:41:10 -07:00
const { setup, cleanup, admin } = common;
2021-06-25 22:11:17 -07:00
before(setup);
after(cleanup);
2021-06-26 09:57:07 -07:00
let id, password;
2021-06-25 22:11:17 -07:00
it('cannot add bad app password', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(appPasswords.add(admin.id, 'appid', 'x'.repeat(201)));
2021-06-25 22:11:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('can add app password', async function () {
2021-08-13 10:41:10 -07:00
const result = await appPasswords.add(admin.id, 'appid', 'spark');
2021-06-25 22:11:17 -07:00
expect(result.id).to.be.a('string');
expect(result.password).to.be.a('string');
id = result.id;
2021-06-26 09:57:07 -07:00
password = result.password;
2021-06-25 22:11:17 -07:00
});
it('can get app password', async function () {
const result = await appPasswords.get(id);
expect(result.hashedPassword).to.be.a('string');
expect(result.name).to.be('spark');
expect(result.identifier).to.be('appid');
});
it('cannot get random app password', async function () {
const result = await appPasswords.get('random');
expect(result).to.be(null);
});
it('can get app passwords', async function () {
2021-08-13 10:41:10 -07:00
const results = await appPasswords.list(admin.id);
2021-06-25 22:11:17 -07:00
expect(results.length).to.be(1);
expect(results[0].hashedPassword).to.be.a('string');
expect(results[0].name).to.be('spark');
expect(results[0].identifier).to.be('appid');
});
2021-07-15 09:50:11 -07:00
it('can verify app password', async function () {
const result = await users.verifyWithId(admin.id, password, 'appid', {});
2021-07-15 09:50:11 -07:00
expect(result).to.be.ok();
expect(result.appPassword).to.be(true);
2021-06-26 09:57:07 -07:00
});
2021-07-15 09:50:11 -07:00
it('can verify non-app password', async function () {
const result = await users.verifyWithId(admin.id, admin.password, 'appid', {});
2021-07-15 09:50:11 -07:00
expect(result).to.be.ok();
expect(result.appPassword).to.be(undefined);
2021-06-26 09:57:07 -07:00
});
2021-07-15 09:50:11 -07:00
it('cannot verify bad password', async function () {
const [error, result] = await safe(users.verifyWithId(admin.id, 'bad', 'appid', {}));
2021-07-15 09:50:11 -07:00
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
2021-06-26 09:57:07 -07:00
});
2021-07-15 09:50:11 -07:00
it('cannot verify password for another app', async function () {
const [error, result] = await safe(users.verifyWithId(admin.id, password, 'appid2', {}));
2021-07-15 09:50:11 -07:00
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
2021-06-26 09:57:07 -07:00
});
2021-06-25 22:11:17 -07:00
it('can del app password', async function () {
await appPasswords.del(id);
});
2021-07-15 09:50:11 -07:00
it('cannot verify deleted app password', async function () {
const [error] = await safe(users.verifyWithId(admin.id, password, 'appid', {}));
2021-07-15 09:50:11 -07:00
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
2021-06-26 09:57:07 -07:00
});
2021-06-25 22:11:17 -07:00
it('cannot del random app password', async function () {
const [error] = await safe(appPasswords.del('random'));
expect(error.reason).to.be(BoxError.NOT_FOUND);
});
});