mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Implement + test unique slugs for user
I put the relevant line on categories and organizations too, but I didn't test it separately. I could have tested it but found it unnecessary repeating
This commit is contained in:
parent
bfc5603fa8
commit
7abc1583dc
@ -17,35 +17,19 @@ export default {
|
||||
Mutation: {
|
||||
CreatePost: async (resolve, root, args, context, info) => {
|
||||
args.slug = args.slug || await uniqueSlug(args.title, isUniqueFor(context, 'Post'))
|
||||
const result = await resolve(root, args, context, info)
|
||||
return result
|
||||
return resolve(root, args, context, info)
|
||||
},
|
||||
CreateUser: async (resolve, root, args, context, info) => {
|
||||
if (!args.slug) {
|
||||
args.slug = slug(args.name, {
|
||||
lower: true
|
||||
})
|
||||
}
|
||||
const result = await resolve(root, args, context, info)
|
||||
return result
|
||||
args.slug = args.slug || await uniqueSlug(args.name, isUniqueFor(context, 'User'))
|
||||
return resolve(root, args, context, info)
|
||||
},
|
||||
CreateOrganization: async (resolve, root, args, context, info) => {
|
||||
if (!args.slug) {
|
||||
args.slug = slug(args.name, {
|
||||
lower: true
|
||||
})
|
||||
}
|
||||
const result = await resolve(root, args, context, info)
|
||||
return result
|
||||
args.slug = args.slug || await uniqueSlug(args.name, isUniqueFor(context, 'Organization'))
|
||||
return resolve(root, args, context, info)
|
||||
},
|
||||
CreateCategory: async (resolve, root, args, context, info) => {
|
||||
if (!args.slug) {
|
||||
args.slug = slug(args.name, {
|
||||
lower: true
|
||||
})
|
||||
}
|
||||
const result = await resolve(root, args, context, info)
|
||||
return result
|
||||
args.slug = args.slug || await uniqueSlug(args.name, isUniqueFor(context, 'Category'))
|
||||
return resolve(root, args, context, info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ afterEach(async () => {
|
||||
|
||||
describe('slugify', () => {
|
||||
describe('CreatePost', () => {
|
||||
it('generates a slug based on the title', async () => {
|
||||
it('generates a slug based on title', async () => {
|
||||
const response = await client.request(`mutation {
|
||||
CreatePost(
|
||||
title: "I am a brand new post",
|
||||
@ -44,7 +44,7 @@ describe('slugify', () => {
|
||||
expect(response).toEqual({ CreatePost: { slug: 'pre-existing-post-1' } })
|
||||
})
|
||||
|
||||
describe('but if the client requested a slug', () => {
|
||||
describe('but if the client specifies a slug', () => {
|
||||
it('rejects CreatePost', async () => {
|
||||
await expect(client.request(`mutation {
|
||||
CreatePost(
|
||||
@ -58,4 +58,38 @@ describe('slugify', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('CreateUser', () => {
|
||||
const action = async (mutation, params) => {
|
||||
return client.request(`mutation {
|
||||
${mutation}(password: "yo", ${params}) { slug }
|
||||
}`, { headers })
|
||||
}
|
||||
it('generates a slug based on name', async () => {
|
||||
await expect(action('CreateUser', 'name: "I am a user"'))
|
||||
.resolves.toEqual({ CreateUser: { slug: 'i-am-a-user' } })
|
||||
})
|
||||
|
||||
describe('if slug exists', () => {
|
||||
beforeEach(async () => {
|
||||
await action('CreateUser', 'name: "Pre-existing user", slug: "pre-existing-user"')
|
||||
})
|
||||
|
||||
it('chooses another slug', async () => {
|
||||
await expect(action(
|
||||
'CreateUser',
|
||||
'name: "pre-existing-user"'
|
||||
)).resolves.toEqual({ CreateUser: { slug: 'pre-existing-user-1' } })
|
||||
})
|
||||
|
||||
describe('but if the client specifies a slug', () => {
|
||||
it('rejects CreateUser', async () => {
|
||||
await expect(action(
|
||||
'CreateUser',
|
||||
'name: "Pre-existing user", slug: "pre-existing-user"'
|
||||
)).rejects.toThrow('already exists')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user