2022-03-08 23:17:11 +00:00
|
|
|
import { expect } from 'chai'
|
2023-08-24 02:07:44 +00:00
|
|
|
import 'mocha'
|
2022-11-21 13:04:09 +00:00
|
|
|
import nock from 'nock'
|
2023-09-06 14:53:58 +00:00
|
|
|
import { ReceivedEmail } from '../../src/entity/received_email'
|
2022-08-10 08:36:31 +00:00
|
|
|
import { User } from '../../src/entity/user'
|
2023-09-06 11:31:34 +00:00
|
|
|
import { findLibraryItemByUrl } from '../../src/services/library_item'
|
2023-09-07 04:35:05 +00:00
|
|
|
import { saveReceivedEmail } from '../../src/services/received_emails'
|
2023-08-24 02:07:44 +00:00
|
|
|
import { saveEmail } from '../../src/services/save_email'
|
2023-09-07 04:35:05 +00:00
|
|
|
import { deleteUser } from '../../src/services/user'
|
|
|
|
|
import { createTestUser } from '../db'
|
2022-03-08 23:17:11 +00:00
|
|
|
|
|
|
|
|
describe('saveEmail', () => {
|
2022-05-12 09:41:11 +00:00
|
|
|
const fakeContent = 'fake content'
|
2022-08-10 08:36:31 +00:00
|
|
|
let user: User
|
2023-03-06 08:24:25 +00:00
|
|
|
let scope: nock.Scope
|
2023-09-06 14:53:58 +00:00
|
|
|
let receivedEmail: ReceivedEmail
|
2022-08-10 08:36:31 +00:00
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
|
// create test user
|
|
|
|
|
user = await createTestUser('fakeUser')
|
2025-09-24 10:37:39 +00:00
|
|
|
scope = nock('https://blog.omnivore.work')
|
2023-03-06 08:24:25 +00:00
|
|
|
.get('/fake-url')
|
|
|
|
|
.reply(200)
|
|
|
|
|
.persist()
|
2023-09-06 14:53:58 +00:00
|
|
|
|
2023-09-07 04:35:05 +00:00
|
|
|
receivedEmail = await saveReceivedEmail(
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
user.id,
|
|
|
|
|
'non-article'
|
2023-09-06 14:53:58 +00:00
|
|
|
)
|
2022-08-10 08:36:31 +00:00
|
|
|
})
|
2022-05-12 09:41:11 +00:00
|
|
|
|
2022-03-08 23:17:11 +00:00
|
|
|
after(async () => {
|
2023-09-07 04:35:05 +00:00
|
|
|
await deleteUser(user.id)
|
2023-03-06 08:24:25 +00:00
|
|
|
scope.persist(false)
|
2022-03-08 23:17:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('doesnt fail if saved twice', async () => {
|
2025-09-24 10:37:39 +00:00
|
|
|
const url = 'https://blog.omnivore.work/fake-url'
|
2022-10-12 10:05:17 +00:00
|
|
|
const title = 'fake title'
|
|
|
|
|
const author = 'fake author'
|
2022-03-08 23:17:11 +00:00
|
|
|
|
2023-08-24 02:07:44 +00:00
|
|
|
await saveEmail({
|
2022-05-12 09:41:11 +00:00
|
|
|
originalContent: `<html><body>${fakeContent}</body></html>`,
|
2022-10-12 10:05:17 +00:00
|
|
|
url,
|
|
|
|
|
title,
|
|
|
|
|
author,
|
2023-08-24 02:07:44 +00:00
|
|
|
userId: user.id,
|
2023-09-06 14:53:58 +00:00
|
|
|
receivedEmailId: receivedEmail.id,
|
2022-03-08 23:17:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// This ensures row level security doesnt prevent
|
2022-10-12 10:05:17 +00:00
|
|
|
// saving the same URL
|
2023-08-24 02:07:44 +00:00
|
|
|
const secondResult = await saveEmail({
|
2022-05-12 09:41:11 +00:00
|
|
|
originalContent: `<html><body>${fakeContent}</body></html>`,
|
2022-10-12 10:05:17 +00:00
|
|
|
url,
|
|
|
|
|
title,
|
|
|
|
|
author,
|
2023-08-24 02:07:44 +00:00
|
|
|
userId: user.id,
|
2023-09-06 14:53:58 +00:00
|
|
|
receivedEmailId: receivedEmail.id,
|
2022-03-08 23:17:11 +00:00
|
|
|
})
|
|
|
|
|
expect(secondResult).to.not.be.undefined
|
|
|
|
|
|
2023-09-06 11:31:34 +00:00
|
|
|
const item = await findLibraryItemByUrl(url, user.id)
|
|
|
|
|
expect(item).to.exist
|
|
|
|
|
expect(item?.originalUrl).to.equal(url)
|
|
|
|
|
expect(item?.title).to.equal(title)
|
|
|
|
|
expect(item?.author).to.equal(author)
|
|
|
|
|
expect(item?.readableContent).to.contain(fakeContent)
|
2022-03-16 10:39:07 +00:00
|
|
|
})
|
2022-03-08 23:17:11 +00:00
|
|
|
})
|