| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- /* eslint-disable
- handle-callback-err,
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const assert = require('assert')
- require('chai').should()
- const { expect } = require('chai')
- const sinon = require('sinon')
- const modulePath = require('path').join(
- __dirname,
- '../../../../app/src/Features/SudoMode/SudoModeHandler'
- )
- describe('SudoModeHandler', function() {
- beforeEach(function() {
- this.userId = 'some_user_id'
- this.email = 'someuser@example.com'
- this.user = {
- _id: this.userId,
- email: this.email
- }
- this.rclient = { get: sinon.stub(), set: sinon.stub(), del: sinon.stub() }
- this.RedisWrapper = { client: () => this.rclient }
- return (this.SudoModeHandler = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- '../../infrastructure/RedisWrapper': this.RedisWrapper,
- 'logger-sharelatex': (this.logger = {
- log: sinon.stub(),
- err: sinon.stub()
- }),
- '../Authentication/AuthenticationManager': (this.AuthenticationManager = {}),
- 'settings-sharelatex': (this.Settings = {}),
- '../V1/V1Handler': (this.V1Handler = { authWithV1: sinon.stub() }),
- '../User/UserGetter': (this.UserGetter = { getUser: sinon.stub() })
- }
- }))
- })
- describe('_buildKey', function() {
- it('should build a properly formed key', function() {
- return expect(this.SudoModeHandler._buildKey('123')).to.equal(
- 'SudoMode:{123}'
- )
- })
- })
- describe('activateSudoMode', function() {
- beforeEach(function() {
- return (this.call = cb => {
- return this.SudoModeHandler.activateSudoMode(this.userId, cb)
- })
- })
- describe('when all goes well', function() {
- beforeEach(function() {
- return (this.rclient.set = sinon.stub().callsArgWith(4, null))
- })
- it('should not produce an error', function(done) {
- return this.call(err => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should set a value in redis', function(done) {
- return this.call(err => {
- expect(this.rclient.set.callCount).to.equal(1)
- expect(
- this.rclient.set.calledWith(
- 'SudoMode:{some_user_id}',
- '1',
- 'EX',
- 60 * 60
- )
- ).to.equal(true)
- return done()
- })
- })
- })
- describe('when user id is not supplied', function() {
- beforeEach(function() {
- return (this.call = cb => {
- return this.SudoModeHandler.activateSudoMode(null, cb)
- })
- })
- it('should produce an error', function(done) {
- return this.call(err => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- return done()
- })
- })
- it('should not set value in redis', function(done) {
- return this.call(err => {
- expect(this.rclient.set.callCount).to.equal(0)
- return done()
- })
- })
- })
- describe('when rclient.set produces an error', function() {
- beforeEach(function() {
- return (this.rclient.set = sinon
- .stub()
- .callsArgWith(4, new Error('woops')))
- })
- it('should produce an error', function(done) {
- return this.call(err => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- return done()
- })
- })
- })
- })
- describe('clearSudoMode', function() {
- beforeEach(function() {
- this.rclient.del = sinon.stub().callsArgWith(1, null)
- return (this.call = cb => {
- return this.SudoModeHandler.clearSudoMode(this.userId, cb)
- })
- })
- it('should not produce an error', function(done) {
- return this.call(err => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should delete key from redis', function(done) {
- return this.call(err => {
- expect(this.rclient.del.callCount).to.equal(1)
- expect(this.rclient.del.calledWith('SudoMode:{some_user_id}')).to.equal(
- true
- )
- return done()
- })
- })
- describe('when rclient.del produces an error', function() {
- beforeEach(function() {
- return (this.rclient.del = sinon
- .stub()
- .callsArgWith(1, new Error('woops')))
- })
- it('should produce an error', function(done) {
- return this.call(err => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- return done()
- })
- })
- })
- describe('when user id is not supplied', function() {
- beforeEach(function() {
- return (this.call = cb => {
- return this.SudoModeHandler.clearSudoMode(null, cb)
- })
- })
- it('should produce an error', function(done) {
- return this.call(err => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- return done()
- })
- })
- it('should not delete value in redis', function(done) {
- return this.call(err => {
- expect(this.rclient.del.callCount).to.equal(0)
- return done()
- })
- })
- })
- })
- describe('authenticate', function() {
- beforeEach(function() {
- return (this.AuthenticationManager.authenticate = sinon
- .stub()
- .callsArgWith(2, null, this.user))
- })
- it('should call AuthenticationManager.authenticate', function(done) {
- return this.SudoModeHandler.authenticate(
- this.email,
- 'password',
- (err, user) => {
- expect(err).to.not.exist
- expect(user).to.exist
- expect(user).to.deep.equal(this.user)
- expect(this.AuthenticationManager.authenticate.callCount).to.equal(1)
- return done()
- }
- )
- })
- })
- describe('isSudoModeActive', function() {
- beforeEach(function() {
- return (this.call = cb => {
- return this.SudoModeHandler.isSudoModeActive(this.userId, cb)
- })
- })
- describe('when sudo-mode is active for that user', function() {
- beforeEach(function() {
- return (this.rclient.get = sinon.stub().callsArgWith(1, null, '1'))
- })
- it('should not produce an error', function(done) {
- return this.call((err, isActive) => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should get the value from redis', function(done) {
- return this.call((err, isActive) => {
- expect(this.rclient.get.callCount).to.equal(1)
- expect(
- this.rclient.get.calledWith('SudoMode:{some_user_id}')
- ).to.equal(true)
- return done()
- })
- })
- it('should produce a true result', function(done) {
- return this.call((err, isActive) => {
- expect(isActive).to.equal(true)
- return done()
- })
- })
- })
- describe('when sudo-mode is not active for that user', function() {
- beforeEach(function() {
- return (this.rclient.get = sinon.stub().callsArgWith(1, null, null))
- })
- it('should not produce an error', function(done) {
- return this.call((err, isActive) => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should get the value from redis', function(done) {
- return this.call((err, isActive) => {
- expect(this.rclient.get.callCount).to.equal(1)
- expect(
- this.rclient.get.calledWith('SudoMode:{some_user_id}')
- ).to.equal(true)
- return done()
- })
- })
- it('should produce a false result', function(done) {
- return this.call((err, isActive) => {
- expect(isActive).to.equal(false)
- return done()
- })
- })
- })
- describe('when rclient.get produces an error', function() {
- beforeEach(function() {
- return (this.rclient.get = sinon
- .stub()
- .callsArgWith(1, new Error('woops')))
- })
- it('should produce an error', function(done) {
- return this.call((err, isActive) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(isActive).to.be.oneOf([null, undefined])
- return done()
- })
- })
- })
- describe('when user id is not supplied', function() {
- beforeEach(function() {
- return (this.call = cb => {
- return this.SudoModeHandler.isSudoModeActive(null, cb)
- })
- })
- it('should produce an error', function(done) {
- return this.call(err => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- return done()
- })
- })
- it('should not get value in redis', function(done) {
- return this.call(err => {
- expect(this.rclient.get.callCount).to.equal(0)
- return done()
- })
- })
- })
- })
- })
|