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

113 lines
3.5 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 () {
const { setup, cleanup, ADMIN } = common;
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 () {
const [error] = await safe(appPasswords.add(ADMIN.id, 'appid', 'x'.repeat(201)));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('can add app password', async function () {
const result = await appPasswords.add(ADMIN.id, 'appid', 'spark');
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 () {
const results = await appPasswords.list(ADMIN.id);
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-06-26 09:57:07 -07:00
it('can verify app password', function (done) {
users.verify(ADMIN.id, password, 'appid', function (error, result) {
expect(error).to.not.be.ok();
expect(result).to.be.ok();
expect(result.appPassword).to.be(true);
done();
});
});
it('can verify non-app password', function (done) {
users.verify(ADMIN.id, ADMIN.password, 'appid', function (error, result) {
expect(error).to.not.be.ok();
expect(result).to.be.ok();
expect(result.appPassword).to.be(undefined);
done();
});
});
it('cannot verify bad password', function (done) {
users.verify(ADMIN.id, 'bad', 'appid', function (error, result) {
expect(error).to.be.ok();
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
done();
});
});
it('cannot verify password for another app', function (done) {
users.verify(ADMIN.id, password, 'appid2', function (error, result) {
expect(error).to.be.ok();
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
done();
});
});
2021-06-25 22:11:17 -07:00
it('can del app password', async function () {
await appPasswords.del(id);
});
2021-06-26 09:57:07 -07:00
it('cannot verify deleted app password', function (done) {
users.verify(ADMIN.id, password, 'appid', function (error) {
expect(error).to.be.ok();
done();
});
});
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);
});
});