diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee new file mode 100644 index 0000000000..22cfc018d6 --- /dev/null +++ b/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee @@ -0,0 +1,23 @@ +async = require("async") +_ = require("lodash") +settings = require("settings-sharelatex") +SubscriptionGroupHandler = require("./SubscriptionGroupHandler") + +module.exports = SubscriptionDomainAllocator = + + autoAllocate: (user, callback = ->)-> + licence = SubscriptionDomainAllocator._findDomainLicence(user.email) + if licence? + SubscriptionGroupHandler.addUserToGroup licence.adminUser_id, user.email, callback + else + callback() + + + _findDomainLicence: (email)-> + licence = _.find settings.domainLicences, (licence)-> + _.find licence.domains, (domain)-> + _.includes email, domain + + return licence + + diff --git a/services/web/package.json b/services/web/package.json index 08ef2aadd1..98551e5dd6 100644 --- a/services/web/package.json +++ b/services/web/package.json @@ -19,6 +19,7 @@ "express": "3.3.4", "fairy": "0.0.2", "jade": "~1.3.1", + "lodash": "3.0.0", "logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#v1.0.0", "lynx": "0.1.1", "metrics-sharelatex": "git+https://github.com/sharelatex/metrics-sharelatex.git#v1.0.0", diff --git a/services/web/test/UnitTests/coffee/Subscription/SubscriptionDomainAllocatorTests.coffee b/services/web/test/UnitTests/coffee/Subscription/SubscriptionDomainAllocatorTests.coffee new file mode 100644 index 0000000000..7c48b889fb --- /dev/null +++ b/services/web/test/UnitTests/coffee/Subscription/SubscriptionDomainAllocatorTests.coffee @@ -0,0 +1,60 @@ +should = require('chai').should() +SandboxedModule = require('sandboxed-module') +assert = require('assert') +path = require('path') +sinon = require('sinon') +modulePath = path.join __dirname, "../../../../app/js/Features/Subscription/SubscriptionDomainAllocator" +expect = require("chai").expect + +describe "SubscriptionDomainAllocator", -> + + beforeEach -> + + @adminUser_id = 12345 + @settings = + domainLicences: [ + {domains:["highcools.site"], adminUser_id:"not this one"} + {domains:["uni.edu", "student.uni.edu"], adminUser_id:@adminUser_id} + ] + @SubscriptionGroupHandler = + addUserToGroup: sinon.stub().callsArg(2) + @SubscriptionDomainAllocator = SandboxedModule.require modulePath, requires: + "settings-sharelatex":@settings + "logger-sharelatex": log:-> + "./SubscriptionGroupHandler": @SubscriptionGroupHandler + + + + describe "_findDomainLicence", -> + + it "should find the domain", (done)-> + licence = @SubscriptionDomainAllocator._findDomainLicence "bob@uni.edu" + licence.adminUser_id.should.equal @adminUser_id + done() + + it "should find one of the other emails in the domain list", (done)-> + licence = @SubscriptionDomainAllocator._findDomainLicence "sally@student.uni.edu" + licence.adminUser_id.should.equal @adminUser_id + done() + + it "should return undefined if no licence matches", (done)-> + licence = @SubscriptionDomainAllocator._findDomainLicence "bob@other.edu" + expect(licence).to.not.exist + done(licence) + + describe "autoAllocate", -> + beforeEach -> + @email = "bob@somewhere.com" + @SubscriptionDomainAllocator._findDomainLicence = sinon.stub() + + it "should call the SubscriptionGroupHandler if there is licence", (done)-> + @SubscriptionDomainAllocator._findDomainLicence.returns(@settings.domainLicences[1]) + @SubscriptionDomainAllocator.autoAllocate {email:@email}, (err)=> + @SubscriptionGroupHandler.addUserToGroup.calledWith(@adminUser_id, @email).should.equal true + done() + + it "should not call the SubscriptionGroupHandler if there is no licence", (done)-> + @SubscriptionDomainAllocator._findDomainLicence.returns() + @SubscriptionDomainAllocator.autoAllocate {email:@email}, (err)=> + @SubscriptionGroupHandler.addUserToGroup.called.should.equal false + done() \ No newline at end of file