diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 6da610fee..0086a086e 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -63,6 +63,18 @@ jobs: run: | source ~/.nvm/nvm.sh yarn install --frozen-lockfile + - name: Database Migration + run: | + yarn workspace @omnivore/db migrate + psql -h localhost -p ${{ job.services.postgres.ports[5432] }} -U postgres -c "CREATE USER app_user WITH ENCRYPTED PASSWORD 'app_pass';GRANT omnivore_user to app_user;" + env: + PG_HOST: localhost + PG_PORT: ${{ job.services.postgres.ports[5432] }} + PG_USER: postgres + PG_PASSWORD: postgres + PG_DB: omnivore_test + ELASTIC_URL: http://localhost:${{ job.services.elastic.ports[9200] }}/ + PGPASSWORD: postgres # This is required for the psql command to work without a password prompt - name: TypeScript, Lint, Tests run: | source ~/.nvm/nvm.sh @@ -72,8 +84,8 @@ jobs: env: PG_HOST: localhost PG_PORT: ${{ job.services.postgres.ports[5432] }} - PG_USER: postgres - PG_PASSWORD: postgres + PG_USER: app_user + PG_PASSWORD: app_pass PG_DB: omnivore_test PG_POOL_MAX: 10 ELASTIC_URL: http://localhost:${{ job.services.elastic.ports[9200] }}/ diff --git a/packages/api/src/entity/reports/content_display_report.ts b/packages/api/src/entity/reports/content_display_report.ts index b15b45d41..627a83870 100644 --- a/packages/api/src/entity/reports/content_display_report.ts +++ b/packages/api/src/entity/reports/content_display_report.ts @@ -2,17 +2,21 @@ import { Column, CreateDateColumn, Entity, + JoinColumn, + ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn, } from 'typeorm' +import { User } from '../user' @Entity() export class ContentDisplayReport { @PrimaryGeneratedColumn('uuid') id?: string - @Column('text') - userId!: string + @ManyToOne(() => User, { onDelete: 'CASCADE' }) + @JoinColumn({ name: 'user_id' }) + user!: User @Column('text') pageId?: string diff --git a/packages/api/src/events/reports/content_display_report_created.ts b/packages/api/src/events/reports/content_display_report_created.ts index d8d1ab44e..add7b1d80 100644 --- a/packages/api/src/events/reports/content_display_report_created.ts +++ b/packages/api/src/events/reports/content_display_report_created.ts @@ -20,7 +20,7 @@ export class ContentDisplayReportSubscriber async afterInsert(event: InsertEvent): Promise { const report = event.entity const message = `A new content display report was created by: - ${report.userId} for URL: ${report.originalUrl} + ${report.user.id} for URL: ${report.originalUrl} ${report.reportComment}` console.log(message) diff --git a/packages/api/test/db.ts b/packages/api/test/db.ts index d1e752c17..51902962e 100644 --- a/packages/api/test/db.ts +++ b/packages/api/test/db.ts @@ -9,10 +9,12 @@ import { UserDeviceToken } from '../src/entity/user_device_tokens' import { Label } from '../src/entity/label' import { Subscription } from '../src/entity/subscription' import { AppDataSource } from '../src/server' -import { getRepository } from '../src/entity/utils' +import { getRepository, setClaims } from '../src/entity/utils' import { createUser } from '../src/services/create_user' import { SnakeNamingStrategy } from 'typeorm-naming-strategies' import { SubscriptionStatus } from '../src/generated/graphql' +import { Integration } from '../src/entity/integration' +import { FindOptionsWhere } from 'typeorm' const runMigrations = async () => { const migrationDirectory = __dirname + '/../../db/migrations' @@ -42,7 +44,8 @@ const runMigrations = async () => { } export const createTestConnection = async (): Promise => { - await runMigrations() + // need to manually run migrations before creating the connection + // await runMigrations() AppDataSource.setOptions({ type: 'postgres', @@ -60,12 +63,11 @@ export const createTestConnection = async (): Promise => { await AppDataSource.initialize() } -export const deleteTestUser = async (name: string) => { - await AppDataSource.createQueryBuilder() - .delete() - .from(User) - .where({ email: `${name}@omnivore.app` }) - .execute() +export const deleteTestUser = async (userId: string) => { + await AppDataSource.transaction(async (t) => { + await setClaims(t, userId) + await t.getRepository(User).delete(userId) + }) } export const createTestUser = async ( @@ -202,3 +204,40 @@ export const createTestSubscription = async ( status: SubscriptionStatus.Active, }) } + +export const deleteTestLabels = async ( + userId: string, + criteria: string[] | FindOptionsWhere