2018-05-30 08:23:57 -07:00
|
|
|
// Copyright (c) 2015-2016 Yuya Ochiai
|
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2018-04-10 04:51:07 -07:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2018-02-20 04:44:23 -08:00
|
|
|
|
2018-04-10 04:51:07 -07:00
|
|
|
import env from '../modules/environment';
|
|
|
|
import PermissionManager from '../../src/main/PermissionManager';
|
2017-10-04 08:02:54 -07:00
|
|
|
|
|
|
|
const permissionFile = path.join(env.userDataDir, 'permission.json');
|
|
|
|
|
|
|
|
describe('PermissionManager', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
fs.unlink(permissionFile, () => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-02 05:07:31 -08:00
|
|
|
it('should grant a permission for an origin', function() {
|
2017-10-04 08:02:54 -07:00
|
|
|
const ORIGIN = 'origin';
|
|
|
|
const PERMISSION = 'permission';
|
|
|
|
const manager = new PermissionManager(permissionFile);
|
|
|
|
|
|
|
|
manager.isGranted(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
manager.isDenied(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
|
|
|
|
manager.grant(ORIGIN, PERMISSION);
|
|
|
|
|
|
|
|
manager.isGranted(ORIGIN, PERMISSION).should.be.true;
|
|
|
|
manager.isDenied(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
|
|
|
|
manager.isGranted(ORIGIN + '_another', PERMISSION).should.be.false;
|
|
|
|
manager.isGranted(ORIGIN, PERMISSION + '_another').should.be.false;
|
|
|
|
});
|
|
|
|
|
2018-12-02 05:07:31 -08:00
|
|
|
it('should deny a permission for an origin', function() {
|
2017-10-04 08:02:54 -07:00
|
|
|
const ORIGIN = 'origin';
|
|
|
|
const PERMISSION = 'permission';
|
|
|
|
const manager = new PermissionManager(permissionFile);
|
|
|
|
|
|
|
|
manager.isGranted(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
manager.isDenied(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
|
|
|
|
manager.deny(ORIGIN, PERMISSION);
|
|
|
|
|
|
|
|
manager.isGranted(ORIGIN, PERMISSION).should.be.false;
|
|
|
|
manager.isDenied(ORIGIN, PERMISSION).should.be.true;
|
|
|
|
|
|
|
|
manager.isDenied(ORIGIN + '_another', PERMISSION).should.be.false;
|
|
|
|
manager.isDenied(ORIGIN, PERMISSION + '_another').should.be.false;
|
|
|
|
});
|
|
|
|
|
2018-12-02 05:07:31 -08:00
|
|
|
it('should save permissions to the file', function() {
|
2017-10-04 08:02:54 -07:00
|
|
|
const ORIGIN = 'origin';
|
|
|
|
const PERMISSION = 'permission';
|
|
|
|
const manager = new PermissionManager(permissionFile);
|
|
|
|
manager.deny(ORIGIN, PERMISSION);
|
|
|
|
manager.grant(ORIGIN + '_another', PERMISSION + '_another');
|
|
|
|
JSON.parse(fs.readFileSync(permissionFile)).should.deep.equal({
|
|
|
|
origin: {
|
2018-02-21 11:20:33 -08:00
|
|
|
permission: 'denied',
|
2017-10-04 08:02:54 -07:00
|
|
|
},
|
|
|
|
origin_another: {
|
2018-02-21 11:20:33 -08:00
|
|
|
permission_another: 'granted',
|
|
|
|
},
|
2017-10-04 08:02:54 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should restore permissions from the file', function() {
|
|
|
|
fs.writeFileSync(permissionFile, JSON.stringify({
|
|
|
|
origin: {
|
2018-02-21 11:20:33 -08:00
|
|
|
permission: 'denied',
|
2017-10-04 08:02:54 -07:00
|
|
|
},
|
|
|
|
origin_another: {
|
2018-02-21 11:20:33 -08:00
|
|
|
permission_another: 'granted',
|
|
|
|
},
|
2017-10-04 08:02:54 -07:00
|
|
|
}));
|
|
|
|
const manager = new PermissionManager(permissionFile);
|
|
|
|
manager.isDenied('origin', 'permission').should.be.true;
|
|
|
|
manager.isGranted('origin_another', 'permission_another').should.be.true;
|
|
|
|
});
|
2017-12-04 06:52:49 -08:00
|
|
|
|
|
|
|
it('should allow permissions for trusted URLs', function() {
|
|
|
|
fs.writeFileSync(permissionFile, JSON.stringify({}));
|
|
|
|
const manager = new PermissionManager(permissionFile, ['https://example.com', 'https://example2.com/2']);
|
|
|
|
manager.isGranted('https://example.com', 'notifications').should.be.true;
|
|
|
|
manager.isGranted('https://example2.com', 'test').should.be.true;
|
|
|
|
});
|
2017-10-04 08:02:54 -07:00
|
|
|
});
|