diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index f70df708f..fd970c1b1 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -23,7 +23,6 @@ const createPostMutation = gql` $content: String! $language: String $categoryIds: [ID] - $pinned: Boolean ) { CreatePost( id: $id @@ -31,7 +30,6 @@ const createPostMutation = gql` content: $content language: $language categoryIds: $categoryIds - pinned: $pinned ) { id title @@ -52,7 +50,8 @@ const createPostMutation = gql` } ` -beforeAll(() => { +beforeAll(async () => { + await factory.cleanDatabase() const { server } = createServer({ context: () => { return { @@ -374,86 +373,14 @@ describe('CreatePost', () => { }) }) }) - - describe('pinned posts', () => { - beforeEach(async () => { - variables = { ...variables, categoryIds: ['cat9', 'cat27', 'cat15'], pinned: true } - }) - describe('users cannot create pinned posts', () => { - it('throws authorization error', async () => { - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ - errors: [{ message: 'Not Authorised!' }], - data: { CreatePost: null }, - }) - }) - }) - - describe('moderator cannot create pinned posts', () => { - let moderator - beforeEach(async () => { - moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() }) - authenticatedUser = await moderator.toJson() - }) - - it('throws authorization error', async () => { - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ - errors: [{ message: 'Not Authorised!' }], - data: { CreatePost: null }, - }) - }) - }) - - describe('admin can create pinned posts', () => { - let admin - beforeEach(async () => { - admin = await user.update({ - role: 'admin', - name: 'Admin', - updatedAt: new Date().toISOString(), - }) - authenticatedUser = await admin.toJson() - variables = { - ...variables, - title: 'pinned post', - content: - 'this is super important for the community and we promise not to have too many', - } - }) - - it('throws authorization error', async () => { - const expected = { - data: { - CreatePost: { - title: 'pinned post', - content: - 'this is super important for the community and we promise not to have too many', - author: { - name: 'Admin', - }, - pinnedBy: { - id: 'current-user', - name: 'Admin', - role: 'admin', - }, - }, - }, - errors: undefined, - } - - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject( - expected, - ) - }) - }) - }) }) }) describe('UpdatePost', () => { let author, newlyCreatedPost const updatePostMutation = gql` - mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) { - UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) { + mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID], $pinned: Boolean) { + UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds, pinned: $pinned) { id content categories { @@ -641,6 +568,77 @@ describe('UpdatePost', () => { }) }) }) + + describe('pinned posts', () => { + beforeEach(async () => { + variables = { ...variables, categoryIds: ['cat9', 'cat27', 'cat15'], pinned: true } + }) + describe('users cannot pin posts on update', () => { + it.only('throws authorization error', async () => { + await expect(mutate({ mutation: updatePostMutation, variables })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorised!' }], + data: { UpdatePost: null }, + }) + }) + }) + + describe('moderator cannot create pinned posts', () => { + let moderator + beforeEach(async () => { + moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() }) + authenticatedUser = await moderator.toJson() + }) + + it('throws authorization error', async () => { + await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorised!' }], + data: { CreatePost: null }, + }) + }) + }) + + describe('admin can create pinned posts', () => { + let admin + beforeEach(async () => { + admin = await user.update({ + role: 'admin', + name: 'Admin', + updatedAt: new Date().toISOString(), + }) + authenticatedUser = await admin.toJson() + variables = { + ...variables, + title: 'pinned post', + content: 'this is super important for the community and we promise not to have too many', + } + }) + + it('throws authorization error', async () => { + const expected = { + data: { + CreatePost: { + title: 'pinned post', + content: + 'this is super important for the community and we promise not to have too many', + author: { + name: 'Admin', + }, + pinnedBy: { + id: 'current-user', + name: 'Admin', + role: 'admin', + }, + }, + }, + errors: undefined, + } + + await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject( + expected, + ) + }) + }) + }) }) describe('DeletePost', () => { diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index 85e73975f..278513e2c 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -16,6 +16,9 @@ type Post { createdAt: String updatedAt: String language: String + pinnedAt: String @cypher( + statement: "MATCH (this)<-[pinned:PINNED]-(:User) WHERE NOT this.deleted = true AND NOT this.disabled = true RETURN this.createdAt" + ) pinnedBy: User @relation(name:"PINNED", direction: "IN") relatedContributions: [Post]! @cypher( @@ -69,7 +72,6 @@ type Mutation { language: String categoryIds: [ID] contentExcerpt: String - pinned: Boolean ): Post UpdatePost( id: ID! @@ -82,6 +84,7 @@ type Mutation { visibility: Visibility language: String categoryIds: [ID] + pinned: Boolean ): Post DeletePost(id: ID!): Post AddPostEmotions(to: _PostInput!, data: _EMOTEDInput!): EMOTED