Fix tests

This commit is contained in:
Hongbo Wu 2022-10-12 18:05:17 +08:00
parent 7a48cccac9
commit 19d0d314db
9 changed files with 65 additions and 84 deletions

View file

@ -5,51 +5,16 @@ import {
} from 'typeorm'
import { Profile } from '../../entity/profile'
import { Follower } from '../../entity/follower'
import { IntercomClient } from '../../utils/intercom'
import { createPubSubClient } from '../../datalayer/pubsub'
import { env } from '../../env'
import { analytics } from '../../utils/analytics'
import { addPopularReadsForNewUser } from '../../services/popular_reads'
@EventSubscriber()
export class FollowOmnivoreUser implements EntitySubscriberInterface<Profile> {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}
async afterInsert(event: InsertEvent<Profile>): Promise<void> {
const OMNIVORE_USER = 'hello'
const omnivoreProfile = await event.manager.getRepository(Profile).findOne({
where: { username: OMNIVORE_USER },
relations: ['user'],
})
if (!omnivoreProfile) {
console.log('unable to find omnivore hello user')
return
}
await event.manager
.getRepository(Follower)
.save({ user: event.entity.user, followee: omnivoreProfile.user })
await event.manager.query(
`insert into omnivore.links (user_id, article_id, article_url, article_hash, slug)
select $1, article_id, article_url, article_hash, slug
from omnivore.links l where l.user_id = $2
`,
[event.entity.user.id, omnivoreProfile.user.id]
)
}
}
@EventSubscriber()
export class CreateIntercomAccount
implements EntitySubscriberInterface<Profile>
{
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}
@ -73,7 +38,6 @@ export class CreateIntercomAccount
@EventSubscriber()
export class IdentifySegmentUser implements EntitySubscriberInterface<Profile> {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}
@ -100,7 +64,6 @@ export class IdentifySegmentUser implements EntitySubscriberInterface<Profile> {
@EventSubscriber()
export class PublishNewUserEvent implements EntitySubscriberInterface<Profile> {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}
@ -120,7 +83,6 @@ export class PublishNewUserEvent implements EntitySubscriberInterface<Profile> {
export class AddPopularReadsToNewUser
implements EntitySubscriberInterface<Profile>
{
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}

View file

@ -402,7 +402,7 @@ describe('Article API', () => {
context('when saving an archived article', () => {
before(async () => {
url = 'https://example.com/saving-archived-article.com'
url = 'https://blog.omnivore.app/saving-archived-article.com'
source = 'puppeteer-parse'
document = '<p>test</p>'
title = 'new title'
@ -432,7 +432,7 @@ describe('Article API', () => {
await deletePage(pageId, ctx)
})
it('should unarchive the article', async () => {
it('unarchives the article', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.createArticle.createdArticle.isArchived).to.false
@ -541,7 +541,7 @@ describe('Article API', () => {
describe('SavePage', () => {
let query = ''
let title = 'Example Title'
let url = 'https://example.com'
let url = 'https://blog.omnivore.app'
let originalContent = '<div>Example Content</div>'
beforeEach(() => {
@ -563,7 +563,7 @@ describe('Article API', () => {
context('when we save a page that is already archived', () => {
before(() => {
url = 'https://example.com/new-url'
url = 'https://blog.omnivore.app/new-url'
})
after(async () => {
@ -610,7 +610,7 @@ describe('Article API', () => {
describe('SaveUrl', () => {
let query = ''
let url = 'https://example.com/new-url-1'
let url = 'https://blog.omnivore.app/new-url-1'
beforeEach(() => {
query = saveUrlQuery(url)
@ -771,7 +771,7 @@ describe('Article API', () => {
context('when the file is uploaded', () => {
before(async () => {
url = 'https://example.com/'
url = 'https://blog.omnivore.app/'
const uploadFile = await getRepository(UploadFile).save({
fileName: 'test.pdf',
contentType: 'application/pdf',

View file

@ -78,7 +78,7 @@ describe('ArticleSavingRequest API', () => {
describe('createArticleSavingRequest', () => {
it('returns the article saving request', async () => {
const res = await graphqlRequest(
createArticleSavingRequestMutation('https://example.com'),
createArticleSavingRequestMutation('https://blog.omnivore.app'),
authToken
).expect(200)
@ -89,7 +89,7 @@ describe('ArticleSavingRequest API', () => {
it('creates a page in elastic', async () => {
const res = await graphqlRequest(
createArticleSavingRequestMutation('https://example.com/1'),
createArticleSavingRequestMutation('https://blog.omnivore.app/1'),
authToken
).expect(200)
@ -117,7 +117,7 @@ describe('ArticleSavingRequest API', () => {
before(async () => {
// create article saving request
const res = await graphqlRequest(
createArticleSavingRequestMutation('https://example.com/2'),
createArticleSavingRequestMutation('https://blog.omnivore.app/2'),
authToken
).expect(200)
articleSavingRequestId =

View file

@ -1,10 +1,9 @@
import { createTestSubscription, createTestUser, deleteTestUser } from '../db'
import { createTestUser, deleteTestUser } from '../db'
import { graphqlRequest, request } from '../util'
import { Subscription } from '../../src/entity/subscription'
import { expect } from 'chai'
import 'mocha'
import { User } from '../../src/entity/user'
import { getPageById, getPageByParam } from '../../src/elastic/pages'
import { getPageByParam } from '../../src/elastic/pages'
describe('PopularReads API', () => {
const username = 'fakeUser'
@ -45,17 +44,28 @@ describe('PopularReads API', () => {
describe('addPopularRead', () => {
it('should add a new article if the readName is valid', async () => {
const readName = 'omnivore_get_started'
const res = await graphqlRequest(addPopularReadQuery(readName), authToken).expect(200)
const res = await graphqlRequest(
addPopularReadQuery(readName),
authToken
).expect(200)
expect(res.body.data.addPopularRead.pageId).to.be
const page = await getPageByParam({ userId: user.id, _id: res.body.data.addPopularRead.pageId })
expect(page?.url).to.eq('https://blog.omnivore.app/p/getting-started-with-omnivore')
const page = await getPageByParam({
userId: user.id,
_id: res.body.data.addPopularRead.pageId,
})
expect(page?.url).to.eq(
'https://blog.omnivore.app/p/getting-started-with-omnivore'
)
})
it('responds status code 500 when invalid user', async () => {
const invalidAuthToken = 'Fake token'
const readName = 'omnivore_get_started'
return graphqlRequest(addPopularReadQuery(readName), invalidAuthToken).expect(500)
return graphqlRequest(
addPopularReadQuery(readName),
invalidAuthToken
).expect(500)
})
})
})

View file

@ -31,7 +31,7 @@ describe('/article/save API', () => {
})
describe('POST /article/save', () => {
const url = 'https://www.example.com'
const url = 'https://blog.omnivore.app'
context('when token and url are valid', () => {
it('should create an article saving request', async () => {

View file

@ -15,6 +15,9 @@ describe('saveEmail', () => {
})
it('doesnt fail if saved twice', async () => {
const url = 'https://blog.omnivore.app/fake-url'
const title = 'fake title'
const author = 'fake author'
const user = await createTestUser(username)
const ctx: SaveContext = {
pubsub: createPubSubClient(),
@ -24,26 +27,29 @@ describe('saveEmail', () => {
await saveEmail(ctx, {
originalContent: `<html><body>${fakeContent}</body></html>`,
url: 'https://example.com',
title: 'fake title',
author: 'fake author',
url,
title,
author,
})
// This ensures row level security doesnt prevent
// resaving the same URL
// saving the same URL
const secondResult = await saveEmail(ctx, {
originalContent: `<html><body>${fakeContent}</body></html>`,
url: 'https://example.com',
title: 'fake title',
author: 'fake author',
url,
title,
author,
})
expect(secondResult).to.not.be.undefined
const page = await getPageByParam({ userId: user.id })
const page = await getPageByParam({
userId: user.id,
url,
})
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?.url).to.equal(url)
expect(page?.title).to.equal(title)
expect(page?.author).to.equal(author)
expect(page?.content).to.contain(fakeContent)
})
})

View file

@ -13,6 +13,8 @@ import { getPageByParam } from '../../src/elastic/pages'
describe('saveNewsletterEmail', () => {
const username = 'fakeUser'
const fakeContent = 'fake content'
const title = 'fake title'
const author = 'fake author'
let user: User
let email: NewsletterEmail
@ -33,28 +35,29 @@ describe('saveNewsletterEmail', () => {
})
it('adds the newsletter to the library', async () => {
const url = 'https://blog.omnivore.app/fake-url'
await saveNewsletterEmail(
{
email: email.address,
content: `<html><body>${fakeContent}</body></html>`,
url: 'https://example.com',
title: 'fake title',
author: 'fake author',
url,
title,
author,
},
ctx
)
const page = await getPageByParam({ userId: user.id })
if (!page) {
expect.fail('page not found')
}
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(fakeContent)
const page = await getPageByParam({ userId: user.id, url })
expect(page).to.exist
expect(page?.url).to.equal(url)
expect(page?.title).to.equal(title)
expect(page?.author).to.equal(author)
expect(page?.content).to.contain(fakeContent)
})
it('should adds a Newsletter label to that page', async () => {
const url = 'https://blog.omnivore.app/new-fake-url'
const newLabel = {
name: 'Newsletter',
color: '#07D2D1',
@ -64,14 +67,14 @@ describe('saveNewsletterEmail', () => {
{
email: email.address,
content: `<html><body>fake content 2</body></html>`,
url: 'https://example.com/2',
title: 'fake title',
author: 'fake author',
url,
title,
author,
},
ctx
)
const page = await getPageByParam({ userId: user.id })
const page = await getPageByParam({ userId: user.id, url })
expect(page?.labels?.[0]).to.deep.include(newLabel)
})
})

View file

@ -50,7 +50,7 @@ export const createTestElasticPage = async (
content: '<p>test content</p>',
createdAt: new Date(),
savedAt: new Date(),
url: 'https://example.com/test-url',
url: 'https://blog.omnivore.app/test-url',
slug: 'test-with-omnivore',
labels: labels,
readingProgressPercent: 0,

View file

@ -39,7 +39,7 @@ describe('parseMetadata', async () => {
describe('parsePreparedContent', async () => {
it('gets published date when JSONLD fails to load', async () => {
const html = load('./test/utils/data/stratechery-blog-post.html')
const result = await parsePreparedContent('https://example.com/', {
const result = await parsePreparedContent('https://blog.omnivore.app/', {
document: html,
pageInfo: {},
})
@ -66,7 +66,7 @@ describe('parsePreparedContent', async () => {
</head>
<body>body</body>
</html>`
const result = await parsePreparedContent('https://example.com/', {
const result = await parsePreparedContent('https://blog.omnivore.app/', {
document: html,
pageInfo: {},
})