Merge pull request #3543 from overleaf/jel-em-ip-matcher

IP matcher notifications

GitOrigin-RevId: 50811fb683961f4d021b37b1d97c24da48c59720
This commit is contained in:
Jessica Lawshe
2021-01-20 03:04:42 +00:00
committed by Copybot
parent b44aed56dd
commit d612c03f8e
4 changed files with 137 additions and 69 deletions
@@ -1,18 +1,4 @@
/* eslint-disable
camelcase,
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('chai')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
@@ -21,53 +7,100 @@ const modulePath = require('path').join(
)
describe('NotificationsBuilder', function() {
const user_id = '123nd3ijdks'
const userId = '123nd3ijdks'
beforeEach(function() {
this.handler = { createNotification: sinon.stub().callsArgWith(6) }
this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
this.body = { id: 1, name: 'stanford', enrolment_ad_html: 'v1 ad content' }
const response = { statusCode: 200 }
this.request = sinon
.stub()
.returns(this.stubResponse)
.callsArgWith(1, null, response, this.body)
return (this.controller = SandboxedModule.require(modulePath, {
globals: {
console: console
},
requires: {
'./NotificationsHandler': this.handler,
'settings-sharelatex': this.settings,
request: this.request,
'logger-sharelatex': {
log() {},
err() {}
describe('ipMatcherAffiliation', function() {
beforeEach(function() {
this.handler = { createNotification: sinon.stub().callsArgWith(6) }
this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
this.request = sinon.stub()
this.controller = SandboxedModule.require(modulePath, {
globals: {
console: console
},
requires: {
'./NotificationsHandler': this.handler,
'settings-sharelatex': this.settings,
request: this.request,
'logger-sharelatex': {
log() {},
err() {}
}
}
}
}))
})
it('should call v1 and create affiliation notifications', function(done) {
const ip = '192.168.0.1'
return this.controller
.ipMatcherAffiliation(user_id)
.create(ip, callback => {
this.request.calledOnce.should.equal(true)
const expectedOpts = {
university_name: this.body.name,
content: this.body.enrolment_ad_html
}
this.handler.createNotification
.calledWith(
user_id,
`ip-matched-affiliation-${this.body.id}`,
'notification_ip_matched_affiliation',
expectedOpts
)
.should.equal(true)
return done()
})
})
describe('with portal and with SSO', function() {
beforeEach(function() {
this.body = {
id: 1,
name: 'stanford',
enrolment_ad_html: 'v1 ad content',
is_university: true,
portal_slug: null,
sso_enabled: false
}
this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
})
it('should call v1 and create affiliation notifications', function(done) {
const ip = '192.168.0.1'
this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
this.request.calledOnce.should.equal(true)
const expectedOpts = {
institutionId: this.body.id,
university_name: this.body.name,
content: this.body.enrolment_ad_html,
ssoEnabled: false,
portalPath: undefined
}
this.handler.createNotification
.calledWith(
userId,
`ip-matched-affiliation-${this.body.id}`,
'notification_ip_matched_affiliation',
expectedOpts
)
.should.equal(true)
done()
})
})
})
describe('without portal and without SSO', function() {
beforeEach(function() {
this.body = {
id: 1,
name: 'stanford',
enrolment_ad_html: 'v1 ad content',
is_university: true,
portal_slug: 'stanford',
sso_enabled: true
}
this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
})
it('should call v1 and create affiliation notifications', function(done) {
const ip = '192.168.0.1'
this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
this.request.calledOnce.should.equal(true)
const expectedOpts = {
institutionId: this.body.id,
university_name: this.body.name,
content: this.body.enrolment_ad_html,
ssoEnabled: true,
portalPath: '/edu/stanford'
}
this.handler.createNotification
.calledWith(
userId,
`ip-matched-affiliation-${this.body.id}`,
'notification_ip_matched_affiliation',
expectedOpts
)
.should.equal(true)
done()
})
})
})
})
})