Merge pull request #624 from omnivore-app/morning-brew-newsletters

Support morning brew newsletter and add tests
This commit is contained in:
Hongbo Wu
2022-05-16 11:06:50 +08:00
committed by GitHub
3 changed files with 33 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import { SubstackHandler } from './substack-handler'
import { AxiosHandler } from './axios-handler'
import { BloombergHandler } from './bloomberg-handler'
import { GolangHandler } from './golang-handler'
import { MorningBrewHandler } from './morning-brew-handler'
const NON_NEWSLETTER_EMAIL_TOPIC = 'nonNewsletterEmailReceived'
const pubsub = new PubSub()
@ -26,6 +27,7 @@ const NEWSLETTER_HANDLERS = [
new AxiosHandler(),
new BloombergHandler(),
new GolangHandler(),
new MorningBrewHandler(),
]
export const getNewsletterHandler = (

View File

@ -0,0 +1,10 @@
import { NewsletterHandler } from './newsletter'
export class MorningBrewHandler extends NewsletterHandler {
constructor() {
super()
this.senderRegex = /Morning Brew <crew@morningbrew.com>/
this.urlRegex = /<a.* href=["']([^"']*)["'].*>View Online<\/a>/
this.defaultUrl = 'https://www.morningbrew.com'
}
}

View File

@ -9,6 +9,7 @@ import { AxiosHandler } from '../src/axios-handler'
import { BloombergHandler } from '../src/bloomberg-handler'
import { GolangHandler } from '../src/golang-handler'
import { getNewsletterHandler } from '../src'
import { MorningBrewHandler } from '../src/morning-brew-handler'
describe('Confirmation email test', () => {
describe('#isConfirmationEmail()', () => {
@ -77,6 +78,15 @@ describe('Newsletter email test', () => {
GolangHandler
)
})
it('should return MorningBrewHandler when email is from Morning Brew', () => {
const from = 'Morning Brew <crew@morningbrew.com>'
const unSubRawUrl = '<https://morningbrew.com/unsubscribe>'
expect(getNewsletterHandler('', from, unSubRawUrl)).to.be.instanceof(
MorningBrewHandler
)
})
})
describe('#getNewsletterUrl()', () => {
@ -114,6 +124,17 @@ describe('Newsletter email test', () => {
expect(new GolangHandler().parseNewsletterUrl('', html)).to.equal(url)
})
it('returns url when email is from Morning Brew', () => {
const url = 'https://www.morningbrew.com/daily/issues/first'
const html = `
<a style="color: #000000; text-decoration: none;" target="_blank" rel="noopener" href="${url}">View Online</a>
`
expect(new MorningBrewHandler().parseNewsletterUrl('', html)).to.equal(
url
)
})
})
describe('get author from email address', () => {