mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Refactor tests, extract validation to middleware
This commit is contained in:
parent
8a93e402b9
commit
3e15ecdfa2
0
backend/src/middleware/userMiddleware.spec.js
Normal file
0
backend/src/middleware/userMiddleware.spec.js
Normal file
@ -4,7 +4,7 @@ const COMMENT_MIN_LENGTH = 1
|
|||||||
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
|
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
|
||||||
const NO_CATEGORIES_ERR_MESSAGE =
|
const NO_CATEGORIES_ERR_MESSAGE =
|
||||||
'You cannot save a post without at least one category or more than three'
|
'You cannot save a post without at least one category or more than three'
|
||||||
|
const USERNAME_MIN_LENGTH = 3
|
||||||
const validateCreateComment = async (resolve, root, args, context, info) => {
|
const validateCreateComment = async (resolve, root, args, context, info) => {
|
||||||
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
|
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
|
||||||
const { postId } = args
|
const { postId } = args
|
||||||
@ -127,12 +127,19 @@ export const validateNotifyUsers = async (label, reason) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const validateUpdateUser = async (resolve, root, params, context, info) => {
|
||||||
|
if (!params.name || params.name.length < USERNAME_MIN_LENGTH)
|
||||||
|
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} character long!`)
|
||||||
|
return resolve(root, params, context, info)
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Mutation: {
|
Mutation: {
|
||||||
CreateComment: validateCreateComment,
|
CreateComment: validateCreateComment,
|
||||||
UpdateComment: validateUpdateComment,
|
UpdateComment: validateUpdateComment,
|
||||||
CreatePost: validatePost,
|
CreatePost: validatePost,
|
||||||
UpdatePost: validateUpdatePost,
|
UpdatePost: validateUpdatePost,
|
||||||
|
UpdateUser: validateUpdateUser,
|
||||||
fileReport: validateReport,
|
fileReport: validateReport,
|
||||||
review: validateReview,
|
review: validateReview,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -71,6 +71,14 @@ const reviewMutation = gql`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const updateUserMutation = gql`
|
||||||
|
mutation($id: ID!, $name: String) {
|
||||||
|
UpdateUser(id: $id, name: $name) {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
const { server } = createServer({
|
const { server } = createServer({
|
||||||
context: () => {
|
context: () => {
|
||||||
@ -397,4 +405,44 @@ describe('validateReview', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('validateUpdateUser', () => {
|
||||||
|
let userParams, variables, updatingUser
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
userParams = {
|
||||||
|
id: 'updating-user',
|
||||||
|
name: 'John Doe',
|
||||||
|
}
|
||||||
|
|
||||||
|
variables = {
|
||||||
|
id: 'updating-user',
|
||||||
|
name: 'John Doughnut',
|
||||||
|
}
|
||||||
|
updatingUser = await factory.create('User', userParams)
|
||||||
|
authenticatedUser = await updatingUser.toJson()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('with `null` as name', async () => {
|
||||||
|
variables = {
|
||||||
|
...variables,
|
||||||
|
name: null,
|
||||||
|
}
|
||||||
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { UpdateUser: null },
|
||||||
|
errors: [{ message: 'Username must be at least 3 character long!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('with name too short', async () => {
|
||||||
|
variables = {
|
||||||
|
...variables,
|
||||||
|
name: ' ',
|
||||||
|
}
|
||||||
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { UpdateUser: null },
|
||||||
|
errors: [{ message: 'Username must be at least 3 character long!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -68,6 +68,7 @@ describe('User', () => {
|
|||||||
it('is permitted', async () => {
|
it('is permitted', async () => {
|
||||||
await expect(query({ query: userQuery, variables })).resolves.toMatchObject({
|
await expect(query({ query: userQuery, variables })).resolves.toMatchObject({
|
||||||
data: { User: [{ name: 'Johnny' }] },
|
data: { User: [{ name: 'Johnny' }] },
|
||||||
|
errors: undefined,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -90,8 +91,7 @@ describe('User', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('UpdateUser', () => {
|
describe('UpdateUser', () => {
|
||||||
let userParams
|
let userParams, variables
|
||||||
let variables
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
userParams = {
|
userParams = {
|
||||||
@ -111,16 +111,23 @@ describe('UpdateUser', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const updateUserMutation = gql`
|
const updateUserMutation = gql`
|
||||||
mutation($id: ID!, $name: String, $termsAndConditionsAgreedVersion: String) {
|
mutation(
|
||||||
|
$id: ID!
|
||||||
|
$name: String
|
||||||
|
$termsAndConditionsAgreedVersion: String
|
||||||
|
$locationName: String
|
||||||
|
) {
|
||||||
UpdateUser(
|
UpdateUser(
|
||||||
id: $id
|
id: $id
|
||||||
name: $name
|
name: $name
|
||||||
termsAndConditionsAgreedVersion: $termsAndConditionsAgreedVersion
|
termsAndConditionsAgreedVersion: $termsAndConditionsAgreedVersion
|
||||||
|
locationName: $locationName
|
||||||
) {
|
) {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
termsAndConditionsAgreedVersion
|
termsAndConditionsAgreedVersion
|
||||||
termsAndConditionsAgreedAt
|
termsAndConditionsAgreedAt
|
||||||
|
locationName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@ -152,7 +159,7 @@ describe('UpdateUser', () => {
|
|||||||
authenticatedUser = await user.toJson()
|
authenticatedUser = await user.toJson()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('name within specifications', async () => {
|
it('updates the name', async () => {
|
||||||
const expected = {
|
const expected = {
|
||||||
data: {
|
data: {
|
||||||
UpdateUser: {
|
UpdateUser: {
|
||||||
@ -160,36 +167,13 @@ describe('UpdateUser', () => {
|
|||||||
name: 'John Doughnut',
|
name: 'John Doughnut',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
errors: undefined,
|
||||||
}
|
}
|
||||||
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
||||||
expected,
|
expected,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('with `null` as name', async () => {
|
|
||||||
const variables = {
|
|
||||||
id: 'u47',
|
|
||||||
name: null,
|
|
||||||
}
|
|
||||||
const { errors } = await mutate({ mutation: updateUserMutation, variables })
|
|
||||||
expect(errors[0]).toHaveProperty(
|
|
||||||
'message',
|
|
||||||
'child "name" fails because ["name" contains an invalid value, "name" must be a string]',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('with too short name', async () => {
|
|
||||||
const variables = {
|
|
||||||
id: 'u47',
|
|
||||||
name: ' ',
|
|
||||||
}
|
|
||||||
const { errors } = await mutate({ mutation: updateUserMutation, variables })
|
|
||||||
expect(errors[0]).toHaveProperty(
|
|
||||||
'message',
|
|
||||||
'child "name" fails because ["name" length must be at least 3 characters long]',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('given a new agreed version of terms and conditions', () => {
|
describe('given a new agreed version of terms and conditions', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
variables = { ...variables, termsAndConditionsAgreedVersion: '0.0.2' }
|
variables = { ...variables, termsAndConditionsAgreedVersion: '0.0.2' }
|
||||||
@ -202,6 +186,7 @@ describe('UpdateUser', () => {
|
|||||||
termsAndConditionsAgreedAt: expect.any(String),
|
termsAndConditionsAgreedAt: expect.any(String),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
errors: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
||||||
@ -222,6 +207,7 @@ describe('UpdateUser', () => {
|
|||||||
termsAndConditionsAgreedAt: null,
|
termsAndConditionsAgreedAt: null,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
errors: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject(
|
||||||
@ -238,6 +224,14 @@ describe('UpdateUser', () => {
|
|||||||
const { errors } = await mutate({ mutation: updateUserMutation, variables })
|
const { errors } = await mutate({ mutation: updateUserMutation, variables })
|
||||||
expect(errors[0]).toHaveProperty('message', 'Invalid version format!')
|
expect(errors[0]).toHaveProperty('message', 'Invalid version format!')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('supports updating location', async () => {
|
||||||
|
variables = { ...variables, locationName: 'Hamburg, New Jersey, United States of America' }
|
||||||
|
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { UpdateUser: { locationName: 'Hamburg, New Jersey, United States of America' } },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user