Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ jest.mock('../server/models/draft')
const DraftDocument = oboRequire('server/models/draft')

describe('current document middleware', () => {
const standardPartialMockUser = { createdAt: Date.now() }

beforeAll(() => {})
afterAll(() => {})
beforeEach(() => {
Expand Down Expand Up @@ -105,7 +107,7 @@ describe('current document middleware', () => {
})
})

test('requireCurrentDocument loads the draft document from params when one is avalible', done => {
test('requireCurrentDocument loads a draft document from params when one is available', done => {
expect.assertions(2)

const { req } = mockArgs
Expand All @@ -120,7 +122,7 @@ describe('current document middleware', () => {
})
})

test('requireCurrentDocument loads the draft document from body when one is avalible', done => {
test('requireCurrentDocument loads a draft document from body when one is available', done => {
expect.assertions(2)

const { req } = mockArgs
Expand All @@ -135,7 +137,7 @@ describe('current document middleware', () => {
})
})

test('requireCurrentDocument loads the draft document from body.event when one is avalible', done => {
test('requireCurrentDocument loads a draft document from body.event when one is available', done => {
expect.assertions(2)

const { req } = mockArgs
Expand All @@ -152,6 +154,122 @@ describe('current document middleware', () => {
})
})

test('requireCurrentDocument loads a draft document from params when multiple are available', done => {
expect.assertions(2)
const mockDocument = new DraftDocument({
draftId: 'mockDraftId1',
contentId: 'mockContentId'
})

// The actual result of this will depend on the current user's creation timestamp,
// but for simplicity of this test we'll just mock the result
DraftDocument.fetchById = jest.fn().mockResolvedValue(mockDocument)

const { req } = mockArgs
req.params = {
draftA: 'mockDraftId1',
draftB: 'mockDraftId2'
}
req.currentUser = standardPartialMockUser

return req.requireCurrentDocument().then(draftDocument => {
expect(draftDocument.draftId).toBe('mockDraftId1')
expect(draftDocument).toBeInstanceOf(DraftDocument)
done()
})
})

test('requireCurrentDocument loads a draft document from body when multiple are available', done => {
expect.assertions(2)
const mockDocument = new DraftDocument({
draftId: 'mockDraftId1',
contentId: 'mockContentId'
})

// The actual result of this will depend on the current user's creation timestamp,
// but for simplicity of this test we'll just mock the result
DraftDocument.fetchById = jest.fn().mockResolvedValue(mockDocument)

const { req } = mockArgs
req.body = {
draftA: 'mockDraftId1',
draftB: 'mockDraftId2'
}
req.currentUser = standardPartialMockUser

return req.requireCurrentDocument().then(draftDocument => {
expect(draftDocument.draftId).toBe('mockDraftId1')
expect(draftDocument).toBeInstanceOf(DraftDocument)
done()
})
})

test('requireCurrentDocument loads a draft document from body.event when multiple are available', done => {
expect.assertions(2)
const mockDocument = new DraftDocument({
draftId: 'mockDraftId1',
contentId: 'mockContentId'
})

// The actual result of this will depend on the current user's creation timestamp,
// but for simplicity of this test we'll just mock the result
DraftDocument.fetchById = jest.fn().mockResolvedValue(mockDocument)

const { req } = mockArgs
req.body = {
event: {
draftA: 'mockDraftId1',
draftB: 'mockDraftId2'
}
}
req.currentUser = standardPartialMockUser

return req.requireCurrentDocument().then(draftDocument => {
expect(draftDocument.draftId).toBe('mockDraftId1')
expect(draftDocument).toBeInstanceOf(DraftDocument)
done()
})
})

// This may be subject to change in the future
// Currently, the module chosen in a split-run event is determined by the user's creation date
// Even numbers will go to draftA, odd numbers will go to draftB
test('DraftDocument.fetchById is provided the correct values in split-run requests based on user creation date', done => {
const { req } = mockArgs

const expectedEvenDraftId = 'mockDraftId1'
const expectedOddDraftId = 'mockDraftId2'

req.body = {
draftA: expectedEvenDraftId,
draftB: expectedOddDraftId
}

// Start by forcing a creation date that ends in an odd number
let mockCreationDate = Date.now()
if (mockCreationDate % 2 === 0) mockCreationDate++

req.currentUser = { createdAt: mockCreationDate }

return req
.requireCurrentDocument()
.then(() => {
expect(DraftDocument.fetchById).toBeCalledWith(expectedOddDraftId)
DraftDocument.fetchById.mockClear()
// Change the user's creation date to an even number - should be odd already
mockCreationDate++
req.currentUser = { createdAt: mockCreationDate }
// Remove the currentDocument so the full process runs again
req.currentDocument = null
// return req.requireCurrentDocument()
})
.then(req.requireCurrentDocument)
.then(() => {
expect(DraftDocument.fetchById).toBeCalledWith(expectedEvenDraftId)
done()
})
})

test('requireCurrentDocument rejects when no DraftDocument is set', done => {
expect.assertions(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Array [
first_name = $[firstName],
last_name = $[lastName],
roles = $[roles]
RETURNING id
RETURNING id, created_at
",
Object {
"accessLevel": undefined,
Expand Down Expand Up @@ -70,7 +70,7 @@ Array [
first_name = $[firstName],
last_name = $[lastName],
roles = $[roles]
RETURNING id
RETURNING id, created_at
",
Object {
"accessLevel": undefined,
Expand Down
8 changes: 6 additions & 2 deletions packages/app/obojobo-express/__tests__/models/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const User = require('../../server/models/user')
const db = require('../../server/db')
const oboEvents = require('../../server/obo_events')

const nowForTests = Date.now()

describe('user model', () => {
beforeAll(() => {
Date.now = () => 'mockNowDate'
Expand Down Expand Up @@ -137,7 +139,7 @@ describe('user model', () => {
test('creates a new user', () => {
expect.hasAssertions()

db.one.mockResolvedValueOnce({ id: 3 })
db.one.mockResolvedValueOnce({ id: 3, created_at: nowForTests })

const u = new User({
firstName: 'Roger',
Expand All @@ -149,6 +151,7 @@ describe('user model', () => {
return u.saveOrCreate().then(user => {
expect(user).toBeInstanceOf(User)
expect(user.id).toBe(3)
expect(user.createdAt).toBe(nowForTests)
expect(db.one).toHaveBeenCalledTimes(1)
expect(db.one.mock.calls[0]).toMatchSnapshot()
expect(oboEvents.emit).toHaveBeenCalledTimes(1)
Expand All @@ -159,7 +162,7 @@ describe('user model', () => {
test('saves an existing user', () => {
expect.hasAssertions()

db.one.mockResolvedValueOnce({ id: 10 })
db.one.mockResolvedValueOnce({ id: 10, created_at: nowForTests })

const u = new User({
id: 10,
Expand All @@ -172,6 +175,7 @@ describe('user model', () => {
return u.saveOrCreate().then(user => {
expect(user).toBeInstanceOf(User)
expect(user.id).toBe(10)
expect(user.createdAt).toBe(nowForTests)
expect(db.one).toHaveBeenCalledTimes(1)
expect(db.one.mock.calls[0]).toMatchSnapshot()
expect(oboEvents.emit).toHaveBeenCalledTimes(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`view-split route launch visit allows EVENT_BEFORE_NEW_VISIT to alter req 1`] = `
Array [
Object {
"action": "visit:create",
"actorTime": "2016-09-22T16:57:14.500Z",
"contentId": undefined,
"draftId": "00000000-0000-0000-0000-000000000000",
"eventVersion": "1.1.0",
"ip": "::ffff:127.0.0.1",
"isPreview": false,
"metadata": Object {},
"payload": Object {
"deactivatedVisitId": "mocked-deactivated-visit-id",
"visitId": "mocked-visit-id",
},
"resourceLinkId": 3,
"userId": 4,
"visitId": "mocked-visit-id",
},
]
`;

exports[`view-split route launch visit inserts event \`visit:create\` 1`] = `
Array [
Object {
"action": "visit:create",
"actorTime": "2016-09-22T16:57:14.500Z",
"contentId": undefined,
"draftId": "00000000-0000-0000-0000-000000000000",
"eventVersion": "1.1.0",
"ip": "::ffff:127.0.0.1",
"isPreview": false,
"metadata": Object {},
"payload": Object {
"deactivatedVisitId": "mocked-deactivated-visit-id",
"visitId": "mocked-visit-id",
},
"resourceLinkId": 3,
"userId": 4,
"visitId": "mocked-visit-id",
},
]
`;
Loading