| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- import { vi, assert } from 'vitest'
- import sinon from 'sinon'
- const modulePath = '../../../../app/src/Features/User/UserCreator.mjs'
- describe('UserCreator', function () {
- beforeEach(async function (ctx) {
- const self = ctx
- ctx.user = { _id: '12390i', ace: {} }
- ctx.user.save = sinon.stub().resolves(self.user)
- ctx.UserModel = class Project {
- constructor() {
- return self.user
- }
- }
- ctx.logger = {
- error: sinon.stub(),
- }
- vi.doMock('@overleaf/logger', () => ({
- default: ctx.logger,
- }))
- vi.doMock('../../../../app/src/models/User', () => ({
- User: ctx.UserModel,
- }))
- vi.doMock('../../../../app/src/infrastructure/Features', () => ({
- default: (ctx.Features = {
- hasFeature: sinon.stub().returns(false),
- }),
- }))
- vi.doMock('../../../../app/src/Features/User/UserDeleter', () => ({
- default: (ctx.UserDeleter = {
- promises: {
- deleteNewUser: sinon.stub().resolves(),
- },
- }),
- }))
- vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
- default: (ctx.UserGetter = {
- promises: {
- getUser: sinon.stub().resolves(ctx.user),
- },
- }),
- }))
- vi.doMock('../../../../app/src/Features/User/UserUpdater', () => ({
- default: (ctx.UserUpdater = {
- promises: {
- addAffiliationForNewUser: sinon.stub().resolves({
- matchedCount: 1,
- modifiedCount: 1,
- acknowledged: true,
- }),
- updateUser: sinon.stub().resolves(),
- },
- }),
- }))
- vi.doMock(
- '../../../../app/src/Features/Analytics/AnalyticsManager',
- () => ({
- default: (ctx.Analytics = {
- recordEventForUserInBackground: sinon.stub(),
- setUserPropertyForUser: sinon.stub(),
- }),
- })
- )
- vi.doMock(
- '../../../../app/src/Features/SplitTests/SplitTestHandler',
- () => ({
- default: (ctx.SplitTestHandler = {
- promises: {
- getAssignmentForUser: sinon.stub().resolves({ variant: 'active' }),
- },
- }),
- })
- )
- vi.doMock(
- '../../../../app/src/Features/User/UserOnboardingEmailManager',
- () => ({
- default: (ctx.UserOnboardingEmailManager = {
- scheduleOnboardingEmail: sinon.stub(),
- }),
- })
- )
- vi.doMock(
- '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager',
- () => ({
- default: (ctx.UserPostRegistrationAnalyticsManager = {
- schedulePostRegistrationAnalytics: sinon.stub(),
- }),
- })
- )
- ctx.UserCreator = (await import(modulePath)).default
- ctx.email = 'bob.oswald@gmail.com'
- ctx.attributes = {
- email: ctx.email,
- analyticsId: '8055c676-bcc7-4e64-a66f-8069f9a0bd92',
- }
- })
- describe('createNewUser', function () {
- describe('with callbacks', function () {
- it('should take the opts and put them in the model', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser({
- ...ctx.attributes,
- holdingAccount: true,
- })
- assert.equal(user.email, ctx.email)
- assert.equal(user.holdingAccount, true)
- assert.equal(user.first_name, 'bob.oswald')
- })
- it('should use the start of the email if the first name is empty string', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser({
- ...ctx.attributes,
- holdingAccount: true,
- first_name: '',
- })
- assert.equal(user.email, ctx.email)
- assert.equal(user.holdingAccount, true)
- assert.equal(user.first_name, 'bob.oswald')
- })
- it('should use the first name if passed', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser({
- ...ctx.attributes,
- holdingAccount: true,
- first_name: 'fiiirstname',
- })
- assert.equal(user.email, ctx.email)
- assert.equal(user.holdingAccount, true)
- assert.equal(user.first_name, 'fiiirstname')
- })
- it('should use the last name if passed', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser({
- ...ctx.attributes,
- holdingAccount: true,
- last_name: 'lastNammmmeee',
- })
- assert.equal(user.email, ctx.email)
- assert.equal(user.holdingAccount, true)
- assert.equal(user.last_name, 'lastNammmmeee')
- })
- it('should set emails attribute', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser(
- ctx.attributes
- )
- user.email.should.equal(ctx.email)
- user.emails.length.should.equal(1)
- user.emails[0].email.should.equal(ctx.email)
- user.emails[0].createdAt.should.be.a('date')
- user.emails[0].reversedHostname.should.equal('moc.liamg')
- })
- describe('with affiliations feature', function () {
- let user
- beforeEach(function (ctx) {
- ctx.Features.hasFeature = sinon
- .stub()
- .withArgs('affiliations')
- .returns(true)
- })
- describe('when v1 affiliations API does not return an error', function () {
- beforeEach(async function (ctx) {
- user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- })
- it('should flag that affiliation is unchecked', function () {
- user.emails[0].affiliationUnchecked.should.equal(true)
- })
- it('should try to add affiliation to v1', function (ctx) {
- sinon.assert.calledOnce(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- sinon.assert.calledWithMatch(
- ctx.UserUpdater.promises.addAffiliationForNewUser,
- user._id,
- ctx.email
- )
- })
- it('should query for updated user data', function (ctx) {
- sinon.assert.calledOnce(ctx.UserGetter.promises.getUser)
- })
- })
- describe('when v1 affiliations API does return an error', function () {
- beforeEach(async function (ctx) {
- ctx.UserUpdater.promises.addAffiliationForNewUser.rejects()
- user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- })
- it('should flag that affiliation is unchecked', function () {
- user.emails[0].affiliationUnchecked.should.equal(true)
- })
- it('should try to add affiliation to v1', function (ctx) {
- sinon.assert.calledOnce(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- sinon.assert.calledWithMatch(
- ctx.UserUpdater.promises.addAffiliationForNewUser,
- user._id,
- ctx.email
- )
- })
- it('should not query for updated user data', function (ctx) {
- sinon.assert.notCalled(ctx.UserGetter.promises.getUser)
- })
- it('should log error', function (ctx) {
- sinon.assert.calledOnce(ctx.logger.error)
- })
- })
- describe('when v1 affiliations API returns an error and requireAffiliation=true', function () {
- beforeEach(async function (ctx) {
- ctx.UserUpdater.promises.addAffiliationForNewUser.rejects()
- user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- })
- it('should flag that affiliation is unchecked', function () {
- user.emails[0].affiliationUnchecked.should.equal(true)
- })
- it('should try to add affiliation to v1', function (ctx) {
- sinon.assert.calledOnce(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- sinon.assert.calledWithMatch(
- ctx.UserUpdater.promises.addAffiliationForNewUser,
- user._id,
- ctx.email
- )
- })
- it('should not query for updated user data', function (ctx) {
- sinon.assert.notCalled(ctx.UserGetter.promises.getUser)
- })
- it('should log error', function (ctx) {
- sinon.assert.calledOnce(ctx.logger.error)
- })
- })
- })
- it('should not add affiliation when without affiliation feature', async function (ctx) {
- await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- sinon.assert.notCalled(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- })
- })
- describe('with promises', function () {
- it('should take the opts and put them in the model', async function (ctx) {
- const opts = {
- ...ctx.attributes,
- holdingAccount: true,
- }
- const user = await ctx.UserCreator.promises.createNewUser(opts)
- assert.equal(user.email, ctx.email)
- assert.equal(user.holdingAccount, true)
- assert.equal(user.first_name, 'bob.oswald')
- })
- it('should add affiliation when with affiliation feature', async function (ctx) {
- ctx.Features.hasFeature = sinon
- .stub()
- .withArgs('affiliations')
- .returns(true)
- const user = await ctx.UserCreator.promises.createNewUser(
- ctx.attributes
- )
- sinon.assert.calledOnce(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- sinon.assert.calledWithMatch(
- ctx.UserUpdater.promises.addAffiliationForNewUser,
- user._id,
- ctx.email
- )
- })
- it('should not add affiliation when without affiliation feature', async function (ctx) {
- ctx.Features.hasFeature = sinon.stub().returns(false)
- await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- sinon.assert.notCalled(
- ctx.UserUpdater.promises.addAffiliationForNewUser
- )
- })
- it('should include SAML provider ID with email', async function (ctx) {
- const attributes = {
- ...ctx.attributes,
- samlIdentifiers: [{ email: ctx.email, providerId: '1' }],
- }
- const user = await ctx.UserCreator.promises.createNewUser(attributes)
- assert.equal(user.emails[0].samlProviderId, '1')
- })
- it('should fire an analytics event and user property on registration', async function (ctx) {
- const user = await ctx.UserCreator.promises.createNewUser(
- ctx.attributes
- )
- assert.equal(user.email, ctx.email)
- sinon.assert.calledWith(
- ctx.Analytics.recordEventForUserInBackground,
- user._id,
- 'user-registered'
- )
- sinon.assert.calledWith(
- ctx.Analytics.setUserPropertyForUser,
- user._id,
- 'created-at'
- )
- })
- it('should schedule post registration jobs on registration with saas feature', async function (ctx) {
- ctx.Features.hasFeature = sinon.stub().withArgs('saas').returns(true)
- const user = await ctx.UserCreator.promises.createNewUser(
- ctx.attributes
- )
- assert.equal(user.email, ctx.email)
- sinon.assert.calledWith(
- ctx.UserOnboardingEmailManager.scheduleOnboardingEmail,
- user
- )
- sinon.assert.calledWith(
- ctx.UserPostRegistrationAnalyticsManager
- .schedulePostRegistrationAnalytics,
- user
- )
- })
- it('should not schedule post registration checks when without saas feature', async function (ctx) {
- await ctx.UserCreator.promises.createNewUser(ctx.attributes)
- sinon.assert.notCalled(
- ctx.UserOnboardingEmailManager.scheduleOnboardingEmail
- )
- sinon.assert.notCalled(
- ctx.UserPostRegistrationAnalyticsManager
- .schedulePostRegistrationAnalytics
- )
- })
- it('should throw without analyticsId', async function (ctx) {
- await expect(
- ctx.UserCreator.promises.createNewUser({ email: ctx.email })
- ).to.eventually.be.rejectedWith(
- /bug: attributes.analyticsId is missing/
- )
- })
- })
- })
- })
|