test search posts with groups

This commit is contained in:
Moriz Wahl 2022-10-05 21:54:52 +02:00
parent 74505a11c5
commit a4cd7a8698
4 changed files with 210 additions and 6 deletions

View File

@ -71,3 +71,18 @@ export const profilePagePosts = () => {
}
`
}
export const searchPosts = () => {
return gql`
query ($query: String!, $firstPosts: Int, $postsOffset: Int) {
searchPosts(query: $query, firstPosts: $firstPosts, postsOffset: $postsOffset) {
postCount
posts {
id
title
content
}
}
}
`
}

View File

@ -299,10 +299,10 @@ export default shield(
{
Query: {
'*': deny,
searchResults: isAuthenticated,
searchPosts: isAuthenticated,
searchUsers: isAuthenticated,
searchHashtags: isAuthenticated,
searchResults: allow,
searchPosts: allow,
searchUsers: allow,
searchHashtags: allow,
embed: allow,
Category: allow,
Tag: allow,

View File

@ -12,6 +12,7 @@ import {
postQuery,
filterPosts,
profilePagePosts,
searchPosts,
} from '../../db/graphql/posts'
// eslint-disable-next-line no-unused-vars
import { DESCRIPTION_WITHOUT_HTML_LENGTH_MIN } from '../../constants/groups'
@ -943,6 +944,192 @@ describe('Posts in Groups', () => {
})
})
})
describe('searchPosts', () => {
describe('without authentication', () => {
beforeEach(async () => {
authenticatedUser = null
})
it('finds nothing', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
expect(result.data.searchPosts.posts).toHaveLength(0)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 0,
posts: [],
},
},
})
})
})
describe('as new user', () => {
beforeEach(async () => {
authenticatedUser = newUser
})
it('finds the post of the public group and the post without group', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
expect(result.data.searchPosts.posts).toHaveLength(2)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 2,
posts: expect.arrayContaining([
{
id: 'post-to-public-group',
title: 'A post to a public group',
content: 'I am posting into a public group as a member of the group',
},
{
id: 'post-without-group',
title: 'A post without a group',
content: 'As a new user, I do not belong to a group yet.',
},
]),
},
},
})
})
})
describe('without membership of group', () => {
beforeEach(async () => {
authenticatedUser = await anyUser.toJson()
})
it('finds the post of the public group and the post without group', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
expect(result.data.searchPosts.posts).toHaveLength(2)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 2,
posts: expect.arrayContaining([
{
id: 'post-to-public-group',
title: 'A post to a public group',
content: 'I am posting into a public group as a member of the group',
},
{
id: 'post-without-group',
title: 'A post without a group',
content: 'As a new user, I do not belong to a group yet.',
},
]),
},
},
})
})
})
describe('with pending membership of group', () => {
beforeEach(async () => {
authenticatedUser = await pendingUser.toJson()
})
it('finds the post of the public group and the post without group', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
expect(result.data.searchPosts.posts).toHaveLength(2)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 2,
posts: expect.arrayContaining([
{
id: 'post-to-public-group',
title: 'A post to a public group',
content: 'I am posting into a public group as a member of the group',
},
{
id: 'post-without-group',
title: 'A post without a group',
content: 'As a new user, I do not belong to a group yet.',
},
]),
},
},
})
})
})
describe('as member of group', () => {
beforeEach(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
it('finds all posts', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
expect(result.data.searchPosts.posts).toHaveLength(4)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 4,
posts: expect.arrayContaining([
{
id: 'post-to-public-group',
title: 'A post to a public group',
content: 'I am posting into a public group as a member of the group',
},
{
id: 'post-without-group',
title: 'A post without a group',
content: 'As a new user, I do not belong to a group yet.',
},
{
id: 'post-to-closed-group',
title: 'A post to a closed group',
content: 'I am posting into a closed group as a member of the group',
},
{
id: 'post-to-hidden-group',
title: 'A post to a hidden group',
content: 'I am posting into a hidden group as a member of the group',
},
]),
},
},
})
})
})
})
})
})

View File

@ -119,7 +119,8 @@ export default {
Query: {
searchPosts: async (_parent, args, context, _resolveInfo) => {
const { query, postsOffset, firstPosts } = args
const { id: userId } = context.user
let userId = null
if (context.user) userId = context.user.id
return {
postCount: getSearchResults(
context,
@ -179,7 +180,8 @@ export default {
},
searchResults: async (_parent, args, context, _resolveInfo) => {
const { query, limit } = args
const { id: userId } = context.user
let userId = null
if (context.user) userId = context.user.id
const searchType = query.replace(/^([!@#]?).*$/, '$1')
const searchString = query.replace(/^([!@#])/, '')