mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-04-06 01:25:38 +00:00
add more thorough backend tests (wip)
This commit is contained in:
parent
d1cb72b9a0
commit
59321ec8f4
@ -26,8 +26,7 @@ export default {
|
|||||||
UpdateSocialMedia: async (object, params, context, resolveInfo) => {
|
UpdateSocialMedia: async (object, params, context, resolveInfo) => {
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
await session.run(
|
await session.run(
|
||||||
`MATCH (owner:User {id: $userId}), (socialMedia:SocialMedia {id: $socialMediaId})
|
`MATCH (owner: User { id: $userId })-[:OWNED]->(socialMedia: SocialMedia { id: $socialMediaId })
|
||||||
WHERE (socialMedia)<-[:OWNED]-(owner)
|
|
||||||
SET socialMedia.url = $socialMediaUrl
|
SET socialMedia.url = $socialMediaUrl
|
||||||
RETURN owner`,
|
RETURN owner`,
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,8 +5,25 @@ import { host, login, gql } from '../../jest/helpers'
|
|||||||
const factory = Factory()
|
const factory = Factory()
|
||||||
|
|
||||||
describe('SocialMedia', () => {
|
describe('SocialMedia', () => {
|
||||||
let client
|
let client, headers, variables, mutation
|
||||||
let headers
|
|
||||||
|
const ownerParams = {
|
||||||
|
email: 'owner@example.com',
|
||||||
|
password: '1234',
|
||||||
|
id: '1234',
|
||||||
|
name: 'Pippi Langstrumpf',
|
||||||
|
}
|
||||||
|
|
||||||
|
const userParams = {
|
||||||
|
email: 'someuser@example.com',
|
||||||
|
password: 'abcd',
|
||||||
|
id: 'abcd',
|
||||||
|
name: 'Kalle Blomqvist',
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = 'https://twitter.com/pippi-langstrumpf'
|
||||||
|
const newUrl = 'https://twitter.com/bullerby'
|
||||||
|
|
||||||
const createSocialMediaMutation = gql`
|
const createSocialMediaMutation = gql`
|
||||||
mutation($url: String!) {
|
mutation($url: String!) {
|
||||||
CreateSocialMedia(url: $url) {
|
CreateSocialMedia(url: $url) {
|
||||||
@ -32,113 +49,152 @@ describe('SocialMedia', () => {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await factory.create('User', {
|
await factory.create('User', userParams)
|
||||||
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg',
|
await factory.create('User', ownerParams)
|
||||||
id: 'acb2d923-f3af-479e-9f00-61b12e864666',
|
|
||||||
name: 'Matilde Hermiston',
|
|
||||||
slug: 'matilde-hermiston',
|
|
||||||
role: 'user',
|
|
||||||
email: 'test@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await factory.cleanDatabase()
|
await factory.cleanDatabase()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('unauthenticated', () => {
|
describe('create social media', () => {
|
||||||
it('throws authorization error', async () => {
|
beforeEach(() => {
|
||||||
client = new GraphQLClient(host)
|
variables = { url }
|
||||||
const variables = {
|
mutation = createSocialMediaMutation
|
||||||
url: 'http://nsosp.org',
|
})
|
||||||
}
|
|
||||||
await expect(client.request(createSocialMediaMutation, variables)).rejects.toThrow('Not Authorised')
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
headers = await login(userParams)
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('creates social media with correct URL', async () => {
|
||||||
|
await expect(client.request(mutation, variables)).resolves.toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
CreateSocialMedia: {
|
||||||
|
id: expect.any(String),
|
||||||
|
url: url,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects empty string', async () => {
|
||||||
|
variables = { url: '' }
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow(
|
||||||
|
'"url" is not allowed to be empty',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects invalid URLs', async () => {
|
||||||
|
variables = { url: 'not-a-url' }
|
||||||
|
|
||||||
|
await expect(client.request(createSocialMediaMutation, variables)).rejects.toThrow(
|
||||||
|
'"url" must be a valid uri',
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('authenticated', () => {
|
describe('update social media', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
headers = await login({
|
headers = await login(ownerParams)
|
||||||
email: 'test@example.org',
|
client = new GraphQLClient(host, { headers })
|
||||||
password: '1234',
|
|
||||||
})
|
const { CreateSocialMedia } = await client.request(createSocialMediaMutation, { url })
|
||||||
client = new GraphQLClient(host, {
|
const { id } = CreateSocialMedia
|
||||||
headers,
|
|
||||||
|
variables = { url: newUrl, id }
|
||||||
|
mutation = updateSocialMediaMutation
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('creates social media with correct URL', async () => {
|
describe('authenticated as other user', () => {
|
||||||
const variables = {
|
it('throws authorization error', async () => {
|
||||||
url: 'http://nsosp.org',
|
headers = await login(userParams)
|
||||||
}
|
client = new GraphQLClient(host, { headers })
|
||||||
await expect(client.request(createSocialMediaMutation, variables)).resolves.toEqual(
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
expect.objectContaining({
|
})
|
||||||
CreateSocialMedia: {
|
})
|
||||||
id: expect.any(String),
|
|
||||||
url: 'http://nsosp.org',
|
describe('authenticated as owner', () => {
|
||||||
|
it('updates social media', async () => {
|
||||||
|
const expected = { UpdateSocialMedia: { ...variables } }
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).resolves.toEqual(
|
||||||
|
expect.objectContaining(expected),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('given a non-existent id', () => {
|
||||||
|
it('does not update', async () => {
|
||||||
|
variables.id = 'some-id'
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('delete social media', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
headers = await login(ownerParams)
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
|
||||||
|
const { CreateSocialMedia } = await client.request(createSocialMediaMutation, { url })
|
||||||
|
const { id } = CreateSocialMedia
|
||||||
|
|
||||||
|
variables = { id }
|
||||||
|
mutation = deleteSocialMediaMutation
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated as other user', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
headers = await login(userParams)
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated as owner', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
headers = await login(ownerParams)
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('deletes social media', async () => {
|
||||||
|
const expected = {
|
||||||
|
DeleteSocialMedia: {
|
||||||
|
id: variables.id,
|
||||||
|
url: url,
|
||||||
},
|
},
|
||||||
}),
|
}
|
||||||
)
|
await expect(client.request(mutation, variables)).resolves.toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('updates social media', async () => {
|
|
||||||
const creationVariables = {
|
|
||||||
url: 'http://nsosp.org',
|
|
||||||
}
|
|
||||||
const { CreateSocialMedia } = await client.request(createSocialMediaMutation, creationVariables)
|
|
||||||
const { id } = CreateSocialMedia
|
|
||||||
const variables = {
|
|
||||||
id,
|
|
||||||
url: 'https://newurl.org',
|
|
||||||
}
|
|
||||||
const expected = {
|
|
||||||
UpdateSocialMedia: {
|
|
||||||
id: id,
|
|
||||||
url: 'https://newurl.org',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
await expect(client.request(updateSocialMediaMutation, variables)).resolves.toEqual(
|
|
||||||
expect.objectContaining(expected),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('deletes social media', async () => {
|
|
||||||
const creationVariables = {
|
|
||||||
url: 'http://nsosp.org',
|
|
||||||
}
|
|
||||||
const { CreateSocialMedia } = await client.request(createSocialMediaMutation, creationVariables)
|
|
||||||
const { id } = CreateSocialMedia
|
|
||||||
|
|
||||||
const deletionVariables = {
|
|
||||||
id,
|
|
||||||
}
|
|
||||||
const expected = {
|
|
||||||
DeleteSocialMedia: {
|
|
||||||
id: id,
|
|
||||||
url: 'http://nsosp.org',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
await expect(client.request(deleteSocialMediaMutation, deletionVariables)).resolves.toEqual(expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('rejects empty string', async () => {
|
|
||||||
const variables = {
|
|
||||||
url: '',
|
|
||||||
}
|
|
||||||
await expect(client.request(createSocialMediaMutation, variables)).rejects.toThrow(
|
|
||||||
'"url" is not allowed to be empty',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('validates URLs', async () => {
|
|
||||||
const variables = {
|
|
||||||
url: 'not-a-url',
|
|
||||||
}
|
|
||||||
|
|
||||||
await expect(client.request(createSocialMediaMutation, variables)).rejects.toThrow(
|
|
||||||
'"url" must be a valid uri',
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user