mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Start to refactor backend tests
This commit is contained in:
parent
4b96454b90
commit
cf8ead10f3
@ -197,7 +197,7 @@ const permissions = shield(
|
|||||||
RemovePostEmotions: isAuthenticated,
|
RemovePostEmotions: isAuthenticated,
|
||||||
block: isAuthenticated,
|
block: isAuthenticated,
|
||||||
unblock: isAuthenticated,
|
unblock: isAuthenticated,
|
||||||
markAsRead: belongsToMe
|
markAsRead: belongsToMe,
|
||||||
},
|
},
|
||||||
User: {
|
User: {
|
||||||
email: isMyOwn,
|
email: isMyOwn,
|
||||||
|
|||||||
@ -1,36 +1,33 @@
|
|||||||
import { neo4jgraphql } from 'neo4j-graphql-js'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Query: {
|
Query: {
|
||||||
notifications: async (parent, args, context, resolveInfo) => {
|
notifications: async (parent, args, context, resolveInfo) => {
|
||||||
const { user, driver } = context
|
const { user } = context
|
||||||
let session
|
const session = context.driver.session()
|
||||||
let notifications
|
let notifications
|
||||||
let whereClause
|
let whereClause
|
||||||
let orderByClause
|
let orderByClause
|
||||||
switch (args.read) {
|
switch (args.read) {
|
||||||
case true:
|
case true:
|
||||||
whereClause = 'WHERE notification.read = TRUE'
|
whereClause = 'WHERE notification.read = TRUE'
|
||||||
break;
|
break
|
||||||
case false:
|
case false:
|
||||||
whereClause = 'WHERE notification.read = FALSE'
|
whereClause = 'WHERE notification.read = FALSE'
|
||||||
break;
|
break
|
||||||
default:
|
default:
|
||||||
whereClause = ''
|
whereClause = ''
|
||||||
}
|
}
|
||||||
switch (args.orderBy) {
|
switch (args.orderBy) {
|
||||||
case 'createdAt_asc':
|
case 'createdAt_asc':
|
||||||
orderByClause = 'ORDER BY notification.createdAt ASC'
|
orderByClause = 'ORDER BY notification.createdAt ASC'
|
||||||
break;
|
break
|
||||||
case 'createdAt_desc':
|
case 'createdAt_desc':
|
||||||
orderByClause = 'ORDER BY notification.createdAt DESC'
|
orderByClause = 'ORDER BY notification.createdAt DESC'
|
||||||
break;
|
break
|
||||||
default:
|
default:
|
||||||
orderByClause = ''
|
orderByClause = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
session = context.driver.session()
|
|
||||||
const cypher = `
|
const cypher = `
|
||||||
MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id})
|
MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id})
|
||||||
${whereClause}
|
${whereClause}
|
||||||
@ -44,12 +41,12 @@ export default {
|
|||||||
...record.get('notification').properties,
|
...record.get('notification').properties,
|
||||||
from: {
|
from: {
|
||||||
__typename: record.get('resource').labels.find(l => resourceTypes.includes(l)),
|
__typename: record.get('resource').labels.find(l => resourceTypes.includes(l)),
|
||||||
...record.get('resource').properties
|
...record.get('resource').properties,
|
||||||
},
|
},
|
||||||
to: {
|
to: {
|
||||||
__typename: 'User',
|
__typename: 'User',
|
||||||
...record.get('user').properties,
|
...record.get('user').properties,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} finally {
|
} finally {
|
||||||
@ -61,6 +58,6 @@ export default {
|
|||||||
Mutation: {
|
Mutation: {
|
||||||
markAsRead: async (parent, params, context, resolveInfo) => {
|
markAsRead: async (parent, params, context, resolveInfo) => {
|
||||||
return null
|
return null
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +1,41 @@
|
|||||||
import { GraphQLClient } from 'graphql-request'
|
import { GraphQLClient } from 'graphql-request'
|
||||||
import Factory from '../../seed/factories'
|
import Factory from '../../seed/factories'
|
||||||
import { host, login, gql } from '../../jest/helpers'
|
import { host, login, gql } from '../../jest/helpers'
|
||||||
import { neode } from '../../bootstrap/neo4j'
|
import { neode as getNeode, getDriver } from '../../bootstrap/neo4j'
|
||||||
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
|
import createServer from '../.././server'
|
||||||
|
|
||||||
let client
|
let client
|
||||||
const factory = Factory()
|
const factory = Factory()
|
||||||
const instance = neode()
|
const neode = getNeode()
|
||||||
|
const driver = getDriver()
|
||||||
const userParams = {
|
const userParams = {
|
||||||
id: 'you',
|
id: 'you',
|
||||||
email: 'test@example.org',
|
email: 'test@example.org',
|
||||||
password: '1234',
|
password: '1234',
|
||||||
}
|
}
|
||||||
const categoryIds = ['cat9']
|
|
||||||
|
let authenticatedUser
|
||||||
|
let variables
|
||||||
|
let query
|
||||||
|
let mutate
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
const { server } = createServer({
|
||||||
|
context: () => {
|
||||||
|
return {
|
||||||
|
driver,
|
||||||
|
user: authenticatedUser,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
query = createTestClient(server).query
|
||||||
|
mutate = createTestClient(server).mutate
|
||||||
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await factory.create('User', userParams)
|
authenticatedUser = null
|
||||||
await instance.create('Category', {
|
variables = { orderBy: 'createdAt_asc' }
|
||||||
id: 'cat9',
|
|
||||||
name: 'Democracy & Politics',
|
|
||||||
icon: 'university',
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@ -28,129 +44,132 @@ afterEach(async () => {
|
|||||||
|
|
||||||
describe('notifications', () => {
|
describe('notifications', () => {
|
||||||
const notificationQuery = gql`
|
const notificationQuery = gql`
|
||||||
query {
|
query($read: Boolean, $orderBy: NOTIFIEDOrdering) {
|
||||||
notifications {
|
notifications(read: $read, orderBy: $orderBy) {
|
||||||
id
|
from {
|
||||||
|
__typename
|
||||||
|
... on Post {
|
||||||
|
content
|
||||||
|
}
|
||||||
|
... on Comment {
|
||||||
|
content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
read
|
||||||
|
createdAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const setupNotifications = [
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'neighbor'})
|
||||||
|
MERGE (:Post {id: 'p1', content: 'Not for you'})
|
||||||
|
-[:NOTIFIED {createdAt: "2019-08-29T17:33:48.651Z", read: false, reason: "mentioned_in_post"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'you'})
|
||||||
|
MERGE (:Post {id: 'p2', content: 'Already seen post mentioning'})
|
||||||
|
-[:NOTIFIED {createdAt: "2019-08-30T17:33:48.651Z", read: true, reason: "mentioned_in_post"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'you'})
|
||||||
|
MERGE (:Post {id: 'p3', content: 'You have been mentioned in a post'})
|
||||||
|
-[:NOTIFIED {createdAt: "2019-08-31T17:33:48.651Z", read: false, reason: "mentioned_in_post"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'you'})
|
||||||
|
MATCH(post:Post {id: 'p3'})
|
||||||
|
CREATE (comment:Comment {id: 'c1', content: 'You have seen this comment mentioning already'})
|
||||||
|
MERGE (comment)-[:COMMENTS]->(post)
|
||||||
|
MERGE (comment)
|
||||||
|
-[:NOTIFIED {createdAt: "2019-08-30T15:33:48.651Z", read: true, reason: "mentioned_in_comment"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'you'})
|
||||||
|
MATCH(post:Post {id: 'p3'})
|
||||||
|
CREATE (comment:Comment {id: 'c2', content: 'You have been mentioned in a comment'})
|
||||||
|
MERGE (comment)-[:COMMENTS]->(post)
|
||||||
|
MERGE (comment)
|
||||||
|
-[:NOTIFIED {createdAt: "2019-08-31T17:33:48.651Z", read: false, reason: "mentioned_in_comment"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
MATCH(user:User {id: 'neighbor'})
|
||||||
|
MATCH(post:Post {id: 'p3'})
|
||||||
|
CREATE (comment:Comment {id: 'c3', content: 'Somebody else was mentioned in a comment'})
|
||||||
|
MERGE (comment)-[:COMMENTS]->(post)
|
||||||
|
MERGE (comment)
|
||||||
|
-[:NOTIFIED {createdAt: "2019-09-01T17:33:48.651Z", read: false, reason: "mentioned_in_comment"}]
|
||||||
|
->(user);
|
||||||
|
`,
|
||||||
|
]
|
||||||
|
|
||||||
describe('unauthenticated', () => {
|
describe('unauthenticated', () => {
|
||||||
it('throws authorization error', async () => {
|
it('throws authorization error', async () => {
|
||||||
client = new GraphQLClient(host)
|
const result = await query({ query: notificationQuery })
|
||||||
await expect(client.request(notificationQuery)).rejects.toThrow('Not Authorised')
|
expect(result.errors[0]).toHaveProperty('message', 'Not Authorised!')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('authenticated', () => {
|
describe('authenticated', () => {
|
||||||
let headers
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
headers = await login({
|
const user = await factory.create('User', userParams)
|
||||||
email: 'test@example.org',
|
authenticatedUser = await user.toJson()
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
client = new GraphQLClient(host, {
|
|
||||||
headers,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('given some notifications', () => {
|
describe('given some notifications', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const neighborParams = {
|
await factory.create('User', { id: 'neighbor' })
|
||||||
email: 'neighbor@example.org',
|
await Promise.all(setupNotifications.map(s => neode.cypher(s)))
|
||||||
password: '1234',
|
})
|
||||||
id: 'neighbor',
|
|
||||||
}
|
describe('no filters', () => {
|
||||||
await Promise.all([
|
it('returns all notifications of current user', async () => {
|
||||||
factory.create('User', neighborParams),
|
const expected = expect.objectContaining({
|
||||||
factory.create('Notification', {
|
data: {
|
||||||
id: 'post-mention-not-for-you',
|
notifications: [
|
||||||
reason: 'mentioned_in_post',
|
{
|
||||||
}),
|
from: {
|
||||||
factory.create('Notification', {
|
__typename: 'Comment',
|
||||||
id: 'post-mention-already-seen',
|
content: 'You have seen this comment mentioning already',
|
||||||
|
},
|
||||||
read: true,
|
read: true,
|
||||||
reason: 'mentioned_in_post',
|
createdAt: '2019-08-30T15:33:48.651Z',
|
||||||
}),
|
},
|
||||||
factory.create('Notification', {
|
{
|
||||||
id: 'post-mention-unseen',
|
from: {
|
||||||
reason: 'mentioned_in_post',
|
__typename: 'Post',
|
||||||
}),
|
content: 'Already seen post mentioning',
|
||||||
factory.create('Notification', {
|
},
|
||||||
id: 'comment-mention-not-for-you',
|
|
||||||
reason: 'mentioned_in_comment',
|
|
||||||
}),
|
|
||||||
factory.create('Notification', {
|
|
||||||
id: 'comment-mention-already-seen',
|
|
||||||
read: true,
|
read: true,
|
||||||
reason: 'mentioned_in_comment',
|
createdAt: '2019-08-30T17:33:48.651Z',
|
||||||
}),
|
},
|
||||||
factory.create('Notification', {
|
{
|
||||||
id: 'comment-mention-unseen',
|
from: {
|
||||||
reason: 'mentioned_in_comment',
|
__typename: 'Comment',
|
||||||
}),
|
content: 'You have been mentioned in a comment',
|
||||||
])
|
},
|
||||||
await factory.authenticateAs(neighborParams)
|
read: false,
|
||||||
await factory.create('Post', { id: 'p1', categoryIds })
|
createdAt: '2019-08-31T17:33:48.651Z',
|
||||||
await Promise.all([
|
},
|
||||||
factory.relate('Notification', 'User', {
|
{
|
||||||
from: 'post-mention-not-for-you',
|
from: {
|
||||||
to: 'neighbor',
|
__typename: 'Post',
|
||||||
}),
|
content: 'You have been mentioned in a post',
|
||||||
factory.relate('Notification', 'Post', {
|
},
|
||||||
from: 'p1',
|
read: false,
|
||||||
to: 'post-mention-not-for-you',
|
createdAt: '2019-08-31T17:33:48.651Z',
|
||||||
}),
|
},
|
||||||
factory.relate('Notification', 'User', {
|
],
|
||||||
from: 'post-mention-unseen',
|
},
|
||||||
to: 'you',
|
})
|
||||||
}),
|
await expect(query({ query: notificationQuery, variables })).resolves.toEqual(expected)
|
||||||
factory.relate('Notification', 'Post', {
|
})
|
||||||
from: 'p1',
|
|
||||||
to: 'post-mention-unseen',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'User', {
|
|
||||||
from: 'post-mention-already-seen',
|
|
||||||
to: 'you',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'Post', {
|
|
||||||
from: 'p1',
|
|
||||||
to: 'post-mention-already-seen',
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
// Comment and its notifications
|
|
||||||
await Promise.all([
|
|
||||||
factory.create('Comment', {
|
|
||||||
id: 'c1',
|
|
||||||
postId: 'p1',
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
await Promise.all([
|
|
||||||
factory.relate('Notification', 'User', {
|
|
||||||
from: 'comment-mention-not-for-you',
|
|
||||||
to: 'neighbor',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'Comment', {
|
|
||||||
from: 'c1',
|
|
||||||
to: 'comment-mention-not-for-you',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'User', {
|
|
||||||
from: 'comment-mention-unseen',
|
|
||||||
to: 'you',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'Comment', {
|
|
||||||
from: 'c1',
|
|
||||||
to: 'comment-mention-unseen',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'User', {
|
|
||||||
from: 'comment-mention-already-seen',
|
|
||||||
to: 'you',
|
|
||||||
}),
|
|
||||||
factory.relate('Notification', 'Comment', {
|
|
||||||
from: 'c1',
|
|
||||||
to: 'comment-mention-already-seen',
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('filter for read: false', () => {
|
describe('filter for read: false', () => {
|
||||||
@ -167,7 +186,7 @@ describe('notifications', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
const variables = { read: false }
|
|
||||||
it('returns only unread notifications of current user', async () => {
|
it('returns only unread notifications of current user', async () => {
|
||||||
const expected = {
|
const expected = {
|
||||||
currentUser: {
|
currentUser: {
|
||||||
@ -194,61 +213,6 @@ describe('notifications', () => {
|
|||||||
).resolves.toEqual(expected)
|
).resolves.toEqual(expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('no filters', () => {
|
|
||||||
const queryCurrentUserNotifications = gql`
|
|
||||||
query {
|
|
||||||
notifications(orderBy: createdAt_desc) {
|
|
||||||
id
|
|
||||||
post {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
comment {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
it('returns all notifications of current user', async () => {
|
|
||||||
const expected = {
|
|
||||||
currentUser: {
|
|
||||||
notifications: expect.arrayContaining([
|
|
||||||
{
|
|
||||||
id: 'post-mention-unseen',
|
|
||||||
post: {
|
|
||||||
id: 'p1',
|
|
||||||
},
|
|
||||||
comment: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'post-mention-already-seen',
|
|
||||||
post: {
|
|
||||||
id: 'p1',
|
|
||||||
},
|
|
||||||
comment: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'comment-mention-unseen',
|
|
||||||
comment: {
|
|
||||||
id: 'c1',
|
|
||||||
},
|
|
||||||
post: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'comment-mention-already-seen',
|
|
||||||
comment: {
|
|
||||||
id: 'c1',
|
|
||||||
},
|
|
||||||
post: null,
|
|
||||||
},
|
|
||||||
]),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
await expect(client.request(queryCurrentUserNotifications, variables)).resolves.toEqual(
|
|
||||||
expected,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user