improved code and tests as suggested by @tirokk, thanks for the great review!

This commit is contained in:
Moriz Wahl 2022-10-10 21:21:45 +02:00
parent 4556276351
commit 631f34a2e5
3 changed files with 220 additions and 239 deletions

View File

@ -287,17 +287,16 @@ export default {
const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
let postRestrictionCypher = ''
if (['owner', 'admin', 'usual'].includes(roleInGroup)) {
postRestrictionCypher = `
WITH group, member, membership
FOREACH (restriction IN [(member)-[r:CANNOT_SEE]->(:Post)-[:IN]->(group) | r] |
DELETE restriction)`
} else {
// user becomes pending member
if (roleInGroup === 'pending') {
postRestrictionCypher = `
WITH group, member, membership
FOREACH (post IN [(p:Post)-[:IN]->(group) | p] |
MERGE (member)-[:CANNOT_SEE]->(post))`
} else {
postRestrictionCypher = `
WITH group, member, membership
FOREACH (restriction IN [(member)-[r:CANNOT_SEE]->(:Post)-[:IN]->(group) | r] |
DELETE restriction)`
}
const joinGroupCypher = `

View File

@ -102,7 +102,7 @@ export default {
WHERE group.groupType IN ['closed', 'hidden']
AND membership.role IN ['usual', 'admin', 'owner']
WITH post, collect(user.id) AS userIds
OPTIONAL MATCH path =(blocked:User) WHERE NOT blocked.id IN userIds
OPTIONAL MATCH path =(restricted:User) WHERE NOT restricted.id IN userIds
FOREACH (user IN nodes(path) |
MERGE (user)-[:CANNOT_SEE]->(post)
)`
@ -129,7 +129,7 @@ export default {
${groupCypher}
RETURN post {.*}
`,
{ userId: context.user.id, params, categoryIds, groupId },
{ userId: context.user.id, categoryIds, groupId, params },
)
const [post] = createPostTransactionResponse.records.map((record) => record.get('post'))
if (imageInput) {

View File

@ -59,7 +59,7 @@ beforeAll(async () => {
})
afterAll(async () => {
// await cleanDatabase()
await cleanDatabase()
})
describe('Posts in Groups', () => {
@ -182,24 +182,6 @@ describe('Posts in Groups', () => {
roleInGroup: 'usual',
},
})
await Factory.build('emailAddress', {
email: 'new-user@example.org',
nonce: '12345',
verifiedAt: null,
})
const result = await mutate({
mutation: signupVerificationMutation,
variables: {
name: 'New User',
slug: 'new-user',
nonce: '12345',
password: '1234',
about: 'I am a new user!',
email: 'new-user@example.org',
termsAndConditionsAgreedVersion: '0.0.1',
},
})
newUser = result.data.SignupVerification
authenticatedUser = await anyUser.toJson()
await mutate({
mutation: createPostMutation(),
@ -213,7 +195,7 @@ describe('Posts in Groups', () => {
describe('creating posts in groups', () => {
describe('without membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await anyUser.toJson()
})
@ -267,7 +249,7 @@ describe('Posts in Groups', () => {
})
describe('as a pending member of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await pendingUser.toJson()
})
@ -321,7 +303,7 @@ describe('Posts in Groups', () => {
})
describe('as a member of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
@ -399,7 +381,7 @@ describe('Posts in Groups', () => {
describe('visibility of posts', () => {
describe('query post by ID', () => {
describe('without authentication', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = null
})
@ -444,7 +426,25 @@ describe('Posts in Groups', () => {
})
describe('as new user', () => {
beforeEach(async () => {
beforeAll(async () => {
await Factory.build('emailAddress', {
email: 'new-user@example.org',
nonce: '12345',
verifiedAt: null,
})
const result = await mutate({
mutation: signupVerificationMutation,
variables: {
name: 'New User',
slug: 'new-user',
nonce: '12345',
password: '1234',
about: 'I am a new user!',
email: 'new-user@example.org',
termsAndConditionsAgreedVersion: '0.0.1',
},
})
newUser = result.data.SignupVerification
authenticatedUser = newUser
})
@ -489,7 +489,7 @@ describe('Posts in Groups', () => {
})
describe('without membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await anyUser.toJson()
})
@ -534,7 +534,7 @@ describe('Posts in Groups', () => {
})
describe('with pending membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await pendingUser.toJson()
})
@ -579,7 +579,7 @@ describe('Posts in Groups', () => {
})
describe('as member of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
@ -638,11 +638,11 @@ describe('Posts in Groups', () => {
describe('filter posts', () => {
describe('without authentication', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = null
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(2)
expect(result).toMatchObject({
@ -666,11 +666,11 @@ describe('Posts in Groups', () => {
})
describe('as new user', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = newUser
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(2)
expect(result).toMatchObject({
@ -694,11 +694,11 @@ describe('Posts in Groups', () => {
})
describe('without membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await anyUser.toJson()
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(2)
expect(result).toMatchObject({
@ -722,11 +722,11 @@ describe('Posts in Groups', () => {
})
describe('with pending membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await pendingUser.toJson()
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(2)
expect(result).toMatchObject({
@ -750,7 +750,7 @@ describe('Posts in Groups', () => {
})
describe('as member of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
@ -790,11 +790,11 @@ describe('Posts in Groups', () => {
describe('profile page posts', () => {
describe('without authentication', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = null
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: profilePagePosts(), variables: {} })
expect(result.data.profilePagePosts).toHaveLength(2)
expect(result).toMatchObject({
@ -818,11 +818,11 @@ describe('Posts in Groups', () => {
})
describe('as new user', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = newUser
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: profilePagePosts(), variables: {} })
expect(result.data.profilePagePosts).toHaveLength(2)
expect(result).toMatchObject({
@ -846,11 +846,11 @@ describe('Posts in Groups', () => {
})
describe('without membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await anyUser.toJson()
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: profilePagePosts(), variables: {} })
expect(result.data.profilePagePosts).toHaveLength(2)
expect(result).toMatchObject({
@ -874,11 +874,11 @@ describe('Posts in Groups', () => {
})
describe('with pending membership of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await pendingUser.toJson()
})
it('shows a the post of the public group and the post without group', async () => {
it('shows the post of the public group and the post without group', async () => {
const result = await query({ query: profilePagePosts(), variables: {} })
expect(result.data.profilePagePosts).toHaveLength(2)
expect(result).toMatchObject({
@ -902,7 +902,7 @@ describe('Posts in Groups', () => {
})
describe('as member of group', () => {
beforeEach(async () => {
beforeAll(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
@ -938,189 +938,189 @@ 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('searchPosts', () => {
describe('without authentication', () => {
beforeAll(async () => {
authenticatedUser = null
})
describe('as new user', () => {
beforeEach(async () => {
authenticatedUser = newUser
it('finds nothing', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
},
})
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(0)
expect(result).toMatchObject({
data: {
searchPosts: {
postCount: 0,
posts: [],
},
})
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: 'I am a user who does 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: 'I am a user who does not belong to a group yet.',
},
]),
},
},
})
})
describe('as new user', () => {
beforeAll(async () => {
authenticatedUser = newUser
})
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,
},
})
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: 'I am a user who does not belong to a group yet.',
},
]),
},
})
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: 'I am a user who does not belong to a group yet.',
},
]),
},
},
})
},
})
})
})
describe('as member of group', () => {
beforeEach(async () => {
authenticatedUser = await allGroupsUser.toJson()
describe('without membership of group', () => {
beforeAll(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: 'I am a user who does not belong to a group yet.',
},
]),
},
},
})
})
})
it('finds all posts', async () => {
const result = await query({
query: searchPosts(),
variables: {
query: 'post',
postsOffset: 0,
firstPosts: 25,
describe('with pending membership of group', () => {
beforeAll(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: 'I am a user who does not belong to a group yet.',
},
]),
},
})
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: 'I am a user who does 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',
},
]),
},
},
})
})
})
describe('as member of group', () => {
beforeAll(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: 'I am a user who does 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',
},
]),
},
})
},
})
})
})
@ -1140,9 +1140,6 @@ describe('Posts in Groups', () => {
roleInGroup: 'usual',
},
})
})
beforeEach(async () => {
authenticatedUser = await pendingUser.toJson()
})
@ -1185,9 +1182,6 @@ describe('Posts in Groups', () => {
roleInGroup: 'usual',
},
})
})
beforeEach(async () => {
authenticatedUser = await pendingUser.toJson()
})
@ -1237,13 +1231,10 @@ describe('Posts in Groups', () => {
roleInGroup: 'pending',
},
})
})
beforeEach(async () => {
authenticatedUser = await pendingUser.toJson()
})
it('does not shows the posts of the closed group anymore', async () => {
it('does not show the posts of the closed group anymore', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(3)
expect(result).toMatchObject({
@ -1282,9 +1273,6 @@ describe('Posts in Groups', () => {
roleInGroup: 'pending',
},
})
})
beforeEach(async () => {
authenticatedUser = await pendingUser.toJson()
})
@ -1370,7 +1358,7 @@ describe('Posts in Groups', () => {
})
})
it('does not shows the posts of the closed group anymore', async () => {
it('does not show the posts of the closed group anymore', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(3)
expect(result).toMatchObject({
@ -1446,13 +1434,10 @@ describe('Posts in Groups', () => {
roleInGroup: 'usual',
},
})
})
beforeEach(async () => {
authenticatedUser = await allGroupsUser.toJson()
})
it('does not shows the posts of the closed group', async () => {
it('does not show the posts of the closed group', async () => {
const result = await query({ query: filterPosts(), variables: {} })
expect(result.data.Post).toHaveLength(3)
expect(result).toMatchObject({
@ -1491,9 +1476,6 @@ describe('Posts in Groups', () => {
roleInGroup: 'usual',
},
})
})
beforeEach(async () => {
authenticatedUser = await allGroupsUser.toJson()
})