From 69dc1ba47a5f4450f13b66d86862016cf1acad2f Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 9 Nov 2023 22:39:51 +0800 Subject: [PATCH] fix tests --- packages/api/src/entity/subscription.ts | 2 +- packages/api/src/generated/graphql.ts | 8 +++-- packages/api/src/generated/schema.graphql | 6 ++-- .../api/src/resolvers/subscriptions/index.ts | 34 ++++++++++--------- packages/api/src/schema.ts | 6 ++-- .../api/test/resolvers/subscriptions.test.ts | 27 +++++++++------ packages/db/migrations/0146.do.following.sql | 4 +-- .../db/migrations/0146.undo.following.sql | 2 +- 8 files changed, 53 insertions(+), 36 deletions(-) diff --git a/packages/api/src/entity/subscription.ts b/packages/api/src/entity/subscription.ts index b144234c4..10416a551 100644 --- a/packages/api/src/entity/subscription.ts +++ b/packages/api/src/entity/subscription.ts @@ -72,7 +72,7 @@ export class Subscription { scheduledAt?: Date | null @Column('boolean') - isPublic?: boolean | null + isPrivate?: boolean | null @Column('boolean') autoAddToLibrary?: boolean | null diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 11e30a4db..c9ba27c77 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -2703,7 +2703,7 @@ export enum SubscribeErrorCode { export type SubscribeInput = { autoAddToLibrary?: InputMaybe; - isPublic?: InputMaybe; + isPrivate?: InputMaybe; subscriptionType?: InputMaybe; url: Scalars['String']; }; @@ -2717,11 +2717,13 @@ export type SubscribeSuccess = { export type Subscription = { __typename?: 'Subscription'; + autoAddToLibrary?: Maybe; count: Scalars['Int']; createdAt: Scalars['Date']; description?: Maybe; icon?: Maybe; id: Scalars['ID']; + isPrivate?: Maybe; lastFetchedAt?: Maybe; name: Scalars['String']; newsletterEmail?: Maybe; @@ -3065,7 +3067,7 @@ export type UpdateSubscriptionInput = { autoAddToLibrary?: InputMaybe; description?: InputMaybe; id: Scalars['ID']; - isPublic?: InputMaybe; + isPrivate?: InputMaybe; lastFetchedAt?: InputMaybe; lastFetchedChecksum?: InputMaybe; name?: InputMaybe; @@ -5948,11 +5950,13 @@ export type SubscribeSuccessResolvers = { + autoAddToLibrary?: SubscriptionResolver, "autoAddToLibrary", ParentType, ContextType>; count?: SubscriptionResolver; createdAt?: SubscriptionResolver; description?: SubscriptionResolver, "description", ParentType, ContextType>; icon?: SubscriptionResolver, "icon", ParentType, ContextType>; id?: SubscriptionResolver; + isPrivate?: SubscriptionResolver, "isPrivate", ParentType, ContextType>; lastFetchedAt?: SubscriptionResolver, "lastFetchedAt", ParentType, ContextType>; name?: SubscriptionResolver; newsletterEmail?: SubscriptionResolver, "newsletterEmail", ParentType, ContextType>; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index d1c693d02..4a8dcedc4 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -2130,7 +2130,7 @@ enum SubscribeErrorCode { input SubscribeInput { autoAddToLibrary: Boolean - isPublic: Boolean + isPrivate: Boolean subscriptionType: SubscriptionType url: String! } @@ -2142,11 +2142,13 @@ type SubscribeSuccess { } type Subscription { + autoAddToLibrary: Boolean count: Int! createdAt: Date! description: String icon: String id: ID! + isPrivate: Boolean lastFetchedAt: Date name: String! newsletterEmail: String @@ -2463,7 +2465,7 @@ input UpdateSubscriptionInput { autoAddToLibrary: Boolean description: String id: ID! - isPublic: Boolean + isPrivate: Boolean lastFetchedAt: Date lastFetchedChecksum: String name: String diff --git a/packages/api/src/resolvers/subscriptions/index.ts b/packages/api/src/resolvers/subscriptions/index.ts index 1a25f482a..4b4ede2ec 100644 --- a/packages/api/src/resolvers/subscriptions/index.ts +++ b/packages/api/src/resolvers/subscriptions/index.ts @@ -228,35 +228,37 @@ export const subscribeResolver = authorized< const feed = await parser.parseURL(input.url) const results = await authTrx(async (t) => { - await t.getRepository(Feed).upsert( - { - url: feed.feedUrl, - title: feed.title, - description: feed.description, - image: feed.image?.url, - }, - { - conflictPaths: ['url'], - skipUpdateIfNoValuesChanged: true, - } - ) + if (!input.isPrivate) { + await t.getRepository(Feed).upsert( + { + url: feed.feedUrl, + title: feed.title, + description: feed.description, + image: feed.image?.url, + }, + { + conflictPaths: ['url'], + skipUpdateIfNoValuesChanged: true, + } + ) + } // limit number of rss subscriptions to 150 const results = (await t.getRepository(Subscription).query( - `insert into omnivore.subscriptions (name, url, description, type, user_id, icon, auto_add_to_library, is_public) + `insert into omnivore.subscriptions (name, url, description, type, user_id, icon, auto_add_to_library, is_private) select $1, $2, $3, $4, $5, $6, $7, $8 from omnivore.subscriptions where user_id = $5 and type = 'RSS' and status = 'ACTIVE' having count(*) < $9 returning *;`, [ feed.title, - input.url, + feed.feedUrl, feed.description || null, SubscriptionType.Rss, uid, feed.image?.url || null, input.autoAddToLibrary ?? null, - input.isPublic ?? null, + input.isPrivate ?? null, MAX_RSS_SUBSCRIPTIONS, ] )) as Subscription[] @@ -335,7 +337,7 @@ export const updateSubscriptionResolver = authorized< ? new Date(input.scheduledAt) : undefined, autoAddToLibrary: input.autoAddToLibrary ?? undefined, - isPublic: input.isPublic ?? undefined, + isPrivate: input.isPrivate ?? undefined, }) return repo.findOneByOrFail({ diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 3e6ad457d..69a95fd79 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -1658,6 +1658,8 @@ const schema = gql` lastFetchedAt: Date createdAt: Date! updatedAt: Date + isPrivate: Boolean + autoAddToLibrary: Boolean } enum SubscriptionStatus { @@ -2554,7 +2556,7 @@ const schema = gql` input SubscribeInput { url: String! subscriptionType: SubscriptionType - isPublic: Boolean + isPrivate: Boolean autoAddToLibrary: Boolean } @@ -2566,7 +2568,7 @@ const schema = gql` lastFetchedChecksum: String status: SubscriptionStatus scheduledAt: Date - isPublic: Boolean + isPrivate: Boolean autoAddToLibrary: Boolean } diff --git a/packages/api/test/resolvers/subscriptions.test.ts b/packages/api/test/resolvers/subscriptions.test.ts index 02f7cd1b8..28d96b564 100644 --- a/packages/api/test/resolvers/subscriptions.test.ts +++ b/packages/api/test/resolvers/subscriptions.test.ts @@ -3,6 +3,7 @@ import 'mocha' import Parser from 'rss-parser' import sinon from 'sinon' import sinonChai from 'sinon-chai' +import { Feed } from '../../src/entity/feed' import { NewsletterEmail } from '../../src/entity/newsletter_email' import { Subscription } from '../../src/entity/subscription' import { User } from '../../src/entity/user' @@ -332,7 +333,9 @@ describe('Subscriptions API', () => { const updatedSubscription = await getRepository(Subscription).findOneBy({ id: subscription.id, }) - expect(updatedSubscription?.status).to.eql(SubscriptionStatus.Unsubscribed) + expect(updatedSubscription?.status).to.eql( + SubscriptionStatus.Unsubscribed + ) // check if the email was sent expect(fake).to.have.been.calledOnceWith({ @@ -371,10 +374,15 @@ describe('Subscriptions API', () => { before(async () => { // fake rss parser - sinon.replace(Parser.prototype, 'parseURL', sinon.fake.resolves({ - title: 'RSS Feed', - description: 'RSS Feed Description', - })) + sinon.replace( + Parser.prototype, + 'parseURL', + sinon.fake.resolves({ + title: 'RSS Feed', + description: 'RSS Feed Description', + feedUrl: url, + }) + ) }) after(() => { @@ -398,6 +406,7 @@ describe('Subscriptions API', () => { after(async () => { await deleteSubscription(existingSubscription.id) + await getRepository(Feed).delete({ url: existingSubscription.url }) }) it('returns an error', async () => { @@ -439,11 +448,9 @@ describe('Subscriptions API', () => { }) it('creates a rss subscription', async () => { - const res = await graphqlRequest( - query, - authToken, - { input: { url, subscriptionType } }, - ).expect(200) + const res = await graphqlRequest(query, authToken, { + input: { url, subscriptionType }, + }).expect(200) expect(res.body.data.subscribe.subscriptions).to.have.lengthOf(1) expect(res.body.data.subscribe.subscriptions[0].id).to.be.a('string') diff --git a/packages/db/migrations/0146.do.following.sql b/packages/db/migrations/0146.do.following.sql index b99889b09..e5ea56937 100755 --- a/packages/db/migrations/0146.do.following.sql +++ b/packages/db/migrations/0146.do.following.sql @@ -5,7 +5,7 @@ BEGIN; ALTER TABLE omnivore.subscriptions - ADD COLUMN is_public boolean, + ADD COLUMN is_private boolean, ADD COLUMN auto_add_to_library boolean; ALTER TABLE omnivore.library_item @@ -40,6 +40,6 @@ CREATE INDEX feed_title_idx ON omnivore.feed(title); CREATE TRIGGER update_feed_modtime BEFORE UPDATE ON omnivore.feed FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); -GRANT SELECT, INSERT, UPDATE ON omnivore.feed TO omnivore_user; +GRANT SELECT, INSERT, UPDATE, DELETE ON omnivore.feed TO omnivore_user; COMMIT; diff --git a/packages/db/migrations/0146.undo.following.sql b/packages/db/migrations/0146.undo.following.sql index 169d4dbf5..46d8abbae 100755 --- a/packages/db/migrations/0146.undo.following.sql +++ b/packages/db/migrations/0146.undo.following.sql @@ -19,7 +19,7 @@ ALTER TABLE omnivore.library_item DROP COLUMN added_to_library_at; ALTER TABLE omnivore.subscriptions - DROP COLUMN is_public, + DROP COLUMN is_private, DROP COLUMN auto_add_to_library; COMMIT;