Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rotorsoft committed Oct 11, 2024
1 parent c7914ff commit 025c18a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
21 changes: 17 additions & 4 deletions libs/model/src/feed/GetUserActivity.query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import {
GlobalActivityCache,
getUserActivityFeed,
Expand All @@ -10,13 +11,25 @@ export function GetUserActivity(): Query<typeof schemas.ActivityFeed> {
...schemas.ActivityFeed,
auth: [],
secure: false,
body: async ({ actor, payload }) =>
payload.is_global
body: async ({ actor, payload }) => {
// ensure we have a user id when not global
const user_id = payload.is_global
? 0
: (actor.user?.id ??
(
await models.Address.findOne({
where: { address: actor.address },
attributes: ['user_id'],
})
)?.user_id ??
0);
return payload.is_global
? await GlobalActivityCache.getInstance().getGlobalActivity()
: await getUserActivityFeed({
user_id: actor.user?.id ?? 0,
user_id,
thread_limit: Math.min(payload.thread_limit ?? 50, 50),
comment_limit: Math.min(payload.comment_limit ?? 3, 5),
}),
});
},
};
}
8 changes: 4 additions & 4 deletions libs/schemas/src/queries/feed.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';
import { DiscordMetaSchema, PG_INT } from '../utils';

export const ActivityComment = z.object({
id: PG_INT,
id: z.number(),
address: z.string(),
user_id: z.number().nullish(),
profile_name: z.string().nullish(),
Expand All @@ -18,8 +18,8 @@ export const ActivityComment = z.object({
export const ActivityThread = z.object({
community_id: z.string(),
community_icon: z.string().nullish(),
id: PG_INT,
user_id: PG_INT,
id: z.number(),
user_id: z.number(),
user_address: z.string(),
profile_name: z.string().nullish(),
profile_avatar: z.string().nullish(),
Expand All @@ -38,7 +38,7 @@ export const ActivityThread = z.object({
has_poll: z.boolean().nullish(),
discord_meta: DiscordMetaSchema.nullish(),
topic: z.object({
id: PG_INT,
id: z.number(),
name: z.string(),
description: z.string(),
}),
Expand Down
30 changes: 17 additions & 13 deletions packages/commonwealth/test/integration/api/userDashboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ describe('User Dashboard API', () => {
});

describe('/GetUserActivity', () => {
const apiUrl = '/api/internal/trpc/feed.getUserActivity';
const queryArgs = { is_global: false };
const apiUrl =
'/api/internal/trpc/feed.getUserActivity?input=' +
encodeURIComponent(JSON.stringify(queryArgs));

test('should fail without JWT', async () => {
const res = await chai.request
.agent(server.app)
.get(apiUrl)
.set('Accept', 'application/json')
.set('address', userAddress)
.send({ chain });
expect(res).to.not.be.null;
expect(res.error).to.not.be.null;
Expand All @@ -179,14 +183,14 @@ describe('User Dashboard API', () => {
.agent(server.app)
.get(apiUrl)
.set('Accept', 'application/json')
.set('address', userAddress)
.send({ chain, jwt: userJWT });

expect(res.status).to.be.equal(200);
expect(res.body.status).to.be.equal('Success');
expect(res.body).to.not.be.null;
expect(res.body.result).to.not.be.null;
expect(res.text).to.not.be.null;

const threadIds = res.body.result.map((a) => a.thread.id);
const resBody = JSON.parse(res.text);
const threadIds = resBody.result.data.map((a) => a.id);
const chains = await server.models.Thread.findAll({
attributes: attributesOf<ThreadAttributes>('community_id'),
where: {
Expand All @@ -213,14 +217,14 @@ describe('User Dashboard API', () => {
.agent(server.app)
.get(apiUrl)
.set('Accept', 'application/json')
.set('address', userAddress)
.send({ chain, jwt: userJWT });

expect(res.status).to.be.equal(200);
expect(res.body.status).to.be.equal('Success');
expect(res.body).to.not.be.null;
expect(res.body.result).to.not.be.null;
expect(res.text).to.not.be.null;

const threadIds = res.body.result.map((a) => a.thread.id);
const resBody = JSON.parse(res.text);
const threadIds = resBody.result.data.map((a) => a.id);
const chains = await server.models.Thread.findAll({
attributes: attributesOf<ThreadAttributes>('community_id'),
where: {
Expand Down Expand Up @@ -261,14 +265,14 @@ describe('User Dashboard API', () => {
.agent(server.app)
.get(apiUrl)
.set('Accept', 'application/json')
.set('address', userAddress)
.send({ chain, jwt: userJWT });

expect(res.status).to.be.equal(200);
expect(res.body.status).to.be.equal('Success');
expect(res.body).to.not.be.null;
expect(res.body.result).to.not.be.null;
expect(res.text).to.not.be.null;

const threadIds = res.body.result.map((a) => a.thread_id);
const resBody = JSON.parse(res.text);
const threadIds = resBody.result.data.map((a) => a.id);
const chains = (
await server.models.Thread.findAll({
attributes: attributesOf<ThreadAttributes>('community_id'),
Expand Down

0 comments on commit 025c18a

Please sign in to comment.