|
| 1 | +'use strict'; |
| 2 | +const expect = require('chai').expect; |
| 3 | +const sinon = require('sinon'); |
| 4 | +const errors = require('../../../../../lib/errors'); |
| 5 | +const ghostUser = require('../../../../../lib/utils/use-ghost-user'); |
| 6 | + |
| 7 | +const loggedInUser = require('../../../../../lib/commands/doctor/checks/logged-in-user'); |
| 8 | + |
| 9 | +describe('Unit: Doctor Checks > loggedInUser', function () { |
| 10 | + const sandbox = sinon.sandbox.create(); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + sandbox.restore(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('enabled works', function () { |
| 17 | + expect(loggedInUser.enabled({ |
| 18 | + local: true, |
| 19 | + system: {platform: {linux: true}}, |
| 20 | + argv: {} |
| 21 | + }), 'false if local is true').to.be.false; |
| 22 | + expect(loggedInUser.enabled({ |
| 23 | + local: false, |
| 24 | + instance: {process: {name: 'local'}}, |
| 25 | + system: {platform: {linux: false}} |
| 26 | + }), 'false if local is false and process name is local').to.be.false; |
| 27 | + expect(loggedInUser.enabled({ |
| 28 | + local: false, |
| 29 | + instance: {process: {name: 'systemd'}}, |
| 30 | + system: {platform: {linux: false}} |
| 31 | + }), 'false if local is false and process name is not local and platform is not linux').to.be.false; |
| 32 | + expect(loggedInUser.enabled({ |
| 33 | + local: false, |
| 34 | + instance: {process: {name: 'systemd'}}, |
| 35 | + system: {platform: {linux: true}} |
| 36 | + }), 'true if local is false and process name is not local and platform is linux').to.be.true; |
| 37 | + expect(loggedInUser.enabled({ |
| 38 | + local: false, |
| 39 | + instance: {process: {name: 'systemd'}}, |
| 40 | + system: {platform: {linux: true}}, |
| 41 | + argv: {process: 'local'} |
| 42 | + }), 'false if local is false and process name is not local and platform is linux, but argv local is given').to.be.false; |
| 43 | + }); |
| 44 | + |
| 45 | + it('rejects if user name is ghost', function () { |
| 46 | + const processStub = sandbox.stub(process, 'getuid').returns(501); |
| 47 | + const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns({uid: 501, guid: 501}); |
| 48 | + |
| 49 | + try { |
| 50 | + loggedInUser.task(); |
| 51 | + } catch (error) { |
| 52 | + expect(error).to.exist; |
| 53 | + expect(error).to.be.an.instanceof(errors.SystemError); |
| 54 | + expect(processStub.calledOnce).to.be.true; |
| 55 | + expect(ghostUserStub.calledOnce).to.be.true; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it('passes if user name is not ghost', function () { |
| 60 | + const processStub = sandbox.stub(process, 'getuid').returns(1000); |
| 61 | + const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns(false); |
| 62 | + |
| 63 | + try { |
| 64 | + loggedInUser.task(); |
| 65 | + expect(processStub.calledOnce).to.be.true; |
| 66 | + expect(ghostUserStub.calledOnce).to.be.true; |
| 67 | + } catch (error) { |
| 68 | + expect(error).to.not.exist; |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('passes if ghost user exists but not currently used', function () { |
| 73 | + const processStub = sandbox.stub(process, 'getuid').returns(1000); |
| 74 | + const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns({uid: 501, guid: 501}); |
| 75 | + |
| 76 | + try { |
| 77 | + loggedInUser.task(); |
| 78 | + expect(processStub.calledOnce).to.be.true; |
| 79 | + expect(ghostUserStub.calledOnce).to.be.true; |
| 80 | + } catch (error) { |
| 81 | + expect(error).to.not.exist; |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments