Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tktcorporation committed Oct 10, 2022
1 parent 20525b9 commit 6807633
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/src/graphql/comments.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const schema = gql`
type Mutation {
createComment(input: CreateCommentInput!): Comment! @skipAuth
deleteComment(id: Int!): Comment! @requireAuth
deleteComment(id: Int!): Comment! @requireAuth(roles: "moderator")
}
`
60 changes: 50 additions & 10 deletions api/src/services/comments/comments.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { db } from 'src/lib/db'
import { AuthenticationError, ForbiddenError } from '@redwoodjs/graphql-server'

import { comments, createComment } from './comments'
import type { StandardScenario, PostOnlyScenario } from './comments.scenarios'
import { db } from 'src/lib/db'

// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
import { comments, createComment, deleteComment } from './comments'
import type { PostOnlyScenario, StandardScenario } from './comments.scenarios'

describe('comments', () => {
scenario(
'returns all comments for a single post from the database',
async (scenario: StandardScenario) => {
async (scenario) => {
const result = await comments({ postId: scenario.comment.jane.postId })

const post = await db.post.findUnique({
where: { id: scenario.comment.jane.postId },
include: { comments: true },
Expand Down Expand Up @@ -43,4 +38,49 @@ describe('comments', () => {
expect(comment.createdAt).not.toEqual(null)
}
)

scenario(
'allows a moderator to delete a comment',
async (scenario: StandardScenario) => {
mockCurrentUser({
roles: 'moderator',
id: 1,
email: '[email protected]',
})

const comment = await deleteComment({
id: scenario.comment.jane.id,
})
expect(comment.id).toEqual(scenario.comment.jane.id)

const result = await comments({ postId: scenario.comment.jane.id })
expect(result.length).toEqual(0)
}
)

scenario(
'does not allow a non-moderator to delete a comment',
async (scenario: StandardScenario) => {
mockCurrentUser({ roles: 'user', id: 1, email: '[email protected]' })

expect(() =>
deleteComment({
id: scenario.comment.jane.id,
})
).toThrow(ForbiddenError)
}
)

scenario(
'does not allow a logged out user to delete a comment',
async (scenario: StandardScenario) => {
mockCurrentUser(null)

expect(() =>
deleteComment({
id: scenario.comment.jane.id,
})
).toThrow(AuthenticationError)
}
)
})

0 comments on commit 6807633

Please sign in to comment.