2022-03-08 23:17:11 +00:00
|
|
|
import 'mocha'
|
|
|
|
|
import { expect } from 'chai'
|
|
|
|
|
import 'chai/register-should'
|
2022-03-16 10:39:07 +00:00
|
|
|
import { createTestUser, deleteTestUser } from '../db'
|
2022-03-08 23:17:11 +00:00
|
|
|
import { SaveContext, saveEmail } from '../../src/services/save_email'
|
|
|
|
|
import { createPubSubClient } from '../../src/datalayer/pubsub'
|
2022-04-12 04:31:08 +00:00
|
|
|
import { getPageByParam } from '../../src/elastic/pages'
|
2022-03-08 23:17:11 +00:00
|
|
|
|
|
|
|
|
describe('saveEmail', () => {
|
|
|
|
|
const username = 'fakeUser'
|
|
|
|
|
after(async () => {
|
|
|
|
|
await deleteTestUser(username)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('doesnt fail if saved twice', async () => {
|
|
|
|
|
const user = await createTestUser(username)
|
|
|
|
|
const ctx: SaveContext = {
|
|
|
|
|
pubsub: createPubSubClient(),
|
2022-03-16 10:39:07 +00:00
|
|
|
uid: user.id,
|
2022-03-31 18:21:41 +00:00
|
|
|
refresh: true,
|
2022-03-08 23:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-16 10:39:07 +00:00
|
|
|
await saveEmail(ctx, {
|
2022-03-08 23:17:11 +00:00
|
|
|
originalContent: 'fake content',
|
|
|
|
|
url: 'https://example.com',
|
|
|
|
|
title: 'fake title',
|
|
|
|
|
author: 'fake author',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// This ensures row level security doesnt prevent
|
|
|
|
|
// resaving the same URL
|
2022-03-16 10:39:07 +00:00
|
|
|
const secondResult = await saveEmail(ctx, {
|
2022-03-08 23:17:11 +00:00
|
|
|
originalContent: 'fake content',
|
|
|
|
|
url: 'https://example.com',
|
|
|
|
|
title: 'fake title',
|
|
|
|
|
author: 'fake author',
|
|
|
|
|
})
|
|
|
|
|
expect(secondResult).to.not.be.undefined
|
|
|
|
|
|
2022-03-31 18:21:41 +00:00
|
|
|
const page = await getPageByParam({ userId: user.id })
|
|
|
|
|
expect(page).to.exist
|
|
|
|
|
expect(page?.url).to.equal('https://example.com')
|
|
|
|
|
expect(page?.title).to.equal('fake title')
|
|
|
|
|
expect(page?.author).to.equal('fake author')
|
|
|
|
|
expect(page?.content).to.contain('fake content')
|
2022-03-16 10:39:07 +00:00
|
|
|
})
|
2022-03-08 23:17:11 +00:00
|
|
|
})
|