Fix bug: Only add emotions for authenticated user

This commit is contained in:
Robert Schäfer 2019-08-08 22:58:20 +02:00
parent 39a94a8aac
commit f3e545a913
5 changed files with 55 additions and 51 deletions

View File

@ -73,12 +73,13 @@ export default {
}, },
AddPostEmotions: async (object, params, context, resolveInfo) => { AddPostEmotions: async (object, params, context, resolveInfo) => {
const session = context.driver.session() const session = context.driver.session()
const { from, to, data } = params const { to, data } = params
const { user } = context
const transactionRes = await session.run( const transactionRes = await session.run(
`MATCH (userFrom:User {id: $from.id}), (postTo:Post {id: $to.id}) `MATCH (userFrom:User {id: $user.id}), (postTo:Post {id: $to.id})
MERGE (userFrom)-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo) MERGE (userFrom)-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo)
RETURN userFrom, postTo, emotedRelation`, RETURN userFrom, postTo, emotedRelation`,
{ from, to, data }, { user, to, data },
) )
session.close() session.close()
const [emoted] = transactionRes.records.map(record => { const [emoted] = transactionRes.records.map(record => {

View File

@ -1,7 +1,7 @@
import { GraphQLClient } from 'graphql-request' import { GraphQLClient } from 'graphql-request'
import { createTestClient } from 'apollo-server-testing' import { createTestClient } from 'apollo-server-testing'
import Factory from '../../seed/factories' import Factory from '../../seed/factories'
import { host, login } from '../../jest/helpers' import { host, login, gql } from '../../jest/helpers'
import { neode, getDriver } from '../../bootstrap/neo4j' import { neode, getDriver } from '../../bootstrap/neo4j'
import createServer from '../../server' import createServer from '../../server'
@ -20,7 +20,7 @@ const oldContent = 'Old content'
const newTitle = 'New title' const newTitle = 'New title'
const newContent = 'New content' const newContent = 'New content'
const createPostVariables = { title: postTitle, content: postContent } const createPostVariables = { title: postTitle, content: postContent }
const createPostWithCategoriesMutation = ` const createPostWithCategoriesMutation = gql`
mutation($title: String!, $content: String!, $categoryIds: [ID]) { mutation($title: String!, $content: String!, $categoryIds: [ID]) {
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) { CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
id id
@ -33,7 +33,7 @@ const createPostWithCategoriesVariables = {
content: postContent, content: postContent,
categoryIds: ['cat9', 'cat4', 'cat15'], categoryIds: ['cat9', 'cat4', 'cat15'],
} }
const postQueryWithCategories = ` const postQueryWithCategories = gql`
query($id: ID) { query($id: ID) {
Post(id: $id) { Post(id: $id) {
categories { categories {
@ -47,8 +47,8 @@ const createPostWithoutCategoriesVariables = {
content: 'I should be able to filter it out', content: 'I should be able to filter it out',
categoryIds: null, categoryIds: null,
} }
const postQueryFilteredByCategory = ` const postQueryFilteredByCategory = gql`
query Post($filter: _PostFilter) { query Post($filter: _PostFilter) {
Post(filter: $filter) { Post(filter: $filter) {
title title
id id
@ -63,7 +63,7 @@ const postQueryFilteredByCategoryVariables = {
filter: postCategoriesFilterParam, filter: postCategoriesFilterParam,
} }
const createPostMutation = ` const createPostMutation = gql`
mutation($title: String!, $content: String!) { mutation($title: String!, $content: String!) {
CreatePost(title: $title, content: $content) { CreatePost(title: $title, content: $content) {
id id
@ -126,13 +126,15 @@ describe('CreatePost', () => {
it('assigns the authenticated user as author', async () => { it('assigns the authenticated user as author', async () => {
await client.request(createPostMutation, createPostVariables) await client.request(createPostMutation, createPostVariables)
const { User } = await client.request( const { User } = await client.request(
`{ gql`
{
User(name: "TestUser") { User(name: "TestUser") {
contributions { contributions {
title title
} }
} }
}`, }
`,
{ headers }, { headers },
) )
expect(User).toEqual([{ contributions: [{ title: postTitle }] }]) expect(User).toEqual([{ contributions: [{ title: postTitle }] }])
@ -149,7 +151,7 @@ describe('CreatePost', () => {
describe('language', () => { describe('language', () => {
it('allows a user to set the language of the post', async () => { it('allows a user to set the language of the post', async () => {
const createPostWithLanguageMutation = ` const createPostWithLanguageMutation = gql`
mutation($title: String!, $content: String!, $language: String) { mutation($title: String!, $content: String!, $language: String) {
CreatePost(title: $title, content: $content, language: $language) { CreatePost(title: $title, content: $content, language: $language) {
language language
@ -237,7 +239,7 @@ describe('UpdatePost', () => {
title: oldTitle, title: oldTitle,
content: oldContent, content: oldContent,
}) })
updatePostMutation = ` updatePostMutation = gql`
mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) { mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) {
UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) { UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) {
id id
@ -343,7 +345,7 @@ describe('UpdatePost', () => {
}) })
describe('DeletePost', () => { describe('DeletePost', () => {
const mutation = ` const mutation = gql`
mutation($id: ID!) { mutation($id: ID!) {
DeletePost(id: $id) { DeletePost(id: $id) {
id id
@ -416,7 +418,7 @@ describe('emotions', () => {
} }
} }
` `
const PostsEmotionsQuery = ` const PostsEmotionsQuery = gql`
query($id: ID!) { query($id: ID!) {
Post(id: $id) { Post(id: $id) {
emotions { emotions {
@ -427,16 +429,20 @@ describe('emotions', () => {
} }
} }
} }
` `
const addPostEmotionsMutation = ` const addPostEmotionsMutation = gql`
mutation($from: _UserInput!, $to: _PostInput!, $data: _EMOTEDInput!) { mutation($to: _PostInput!, $data: _EMOTEDInput!) {
AddPostEmotions(from: $from, to: $to, data: $data) { AddPostEmotions(to: $to, data: $data) {
from { id } from {
to { id } id
}
to {
id
}
emotion emotion
} }
} }
` `
beforeEach(async () => { beforeEach(async () => {
userParams.id = 'u1987' userParams.id = 'u1987'
authorParams.id = 'u257' authorParams.id = 'u257'
@ -478,7 +484,6 @@ describe('emotions', () => {
return query({ query: postQuery, variables }) return query({ query: postQuery, variables })
} }
addPostEmotionsVariables = { addPostEmotionsVariables = {
from: { id: authorParams.id },
to: { id: postToEmote.id }, to: { id: postToEmote.id },
data: { emotion: 'happy' }, data: { emotion: 'happy' },
} }
@ -509,11 +514,10 @@ describe('emotions', () => {
}) })
it('adds an emotion to the post', async () => { it('adds an emotion to the post', async () => {
addPostEmotionsVariables.from.id = userParams.id
const expected = { const expected = {
data: { data: {
AddPostEmotions: { AddPostEmotions: {
from: addPostEmotionsVariables.from, from: { id: user.id },
to: addPostEmotionsVariables.to, to: addPostEmotionsVariables.to,
emotion: 'happy', emotion: 'happy',
}, },
@ -543,8 +547,8 @@ describe('emotions', () => {
it('allows a user to add more than one emotion', async () => { it('allows a user to add more than one emotion', async () => {
const expectedEmotions = [ const expectedEmotions = [
{ emotion: 'happy', User: { id: authorParams.id } }, { emotion: 'happy', User: { id: user.id } },
{ emotion: 'surprised', User: { id: authorParams.id } }, { emotion: 'surprised', User: { id: user.id } },
] ]
const expectedResponse = { const expectedResponse = {
data: { Post: [{ emotions: expect.arrayContaining(expectedEmotions) }] }, data: { Post: [{ emotions: expect.arrayContaining(expectedEmotions) }] },
@ -567,7 +571,7 @@ describe('emotions', () => {
const expected = { const expected = {
data: { data: {
AddPostEmotions: { AddPostEmotions: {
from: addPostEmotionsVariables.from, from: { id: owner.id },
to: addPostEmotionsVariables.to, to: addPostEmotionsVariables.to,
emotion: 'happy', emotion: 'happy',
}, },
@ -582,7 +586,7 @@ describe('emotions', () => {
describe('RemovePostEmotions', () => { describe('RemovePostEmotions', () => {
let removePostEmotionsVariables, postsEmotionsQueryVariables let removePostEmotionsVariables, postsEmotionsQueryVariables
const removePostEmotionsMutation = ` const removePostEmotionsMutation = gql`
mutation($to: _PostInput!, $data: _EMOTEDInput!) { mutation($to: _PostInput!, $data: _EMOTEDInput!) {
RemovePostEmotions(to: $to, data: $data) RemovePostEmotions(to: $to, data: $data)
} }
@ -650,13 +654,13 @@ describe('emotions', () => {
let PostsEmotionsCountByEmotionVariables let PostsEmotionsCountByEmotionVariables
let PostsEmotionsByCurrentUserVariables let PostsEmotionsByCurrentUserVariables
const PostsEmotionsCountByEmotionQuery = ` const PostsEmotionsCountByEmotionQuery = gql`
query($postId: ID!, $data: _EMOTEDInput!) { query($postId: ID!, $data: _EMOTEDInput!) {
PostsEmotionsCountByEmotion(postId: $postId, data: $data) PostsEmotionsCountByEmotion(postId: $postId, data: $data)
} }
` `
const PostsEmotionsByCurrentUserQuery = ` const PostsEmotionsByCurrentUserQuery = gql`
query($postId: ID!) { query($postId: ID!) {
PostsEmotionsByCurrentUser(postId: $postId) PostsEmotionsByCurrentUser(postId: $postId)
} }

View File

@ -91,7 +91,7 @@ type Mutation {
language: String language: String
categoryIds: [ID] categoryIds: [ID]
): Post ): Post
AddPostEmotions(from: _UserInput!, to: _PostInput!, data: _EMOTEDInput!): EMOTED AddPostEmotions(to: _PostInput!, data: _EMOTEDInput!): EMOTED
RemovePostEmotions(to: _PostInput!, data: _EMOTEDInput!): Boolean! RemovePostEmotions(to: _PostInput!, data: _EMOTEDInput!): Boolean!
} }

View File

@ -128,11 +128,10 @@ export default function Factory(options = {}) {
this.lastResponse = await cleanDatabase({ driver: this.neo4jDriver }) this.lastResponse = await cleanDatabase({ driver: this.neo4jDriver })
return this return this
}, },
async emote({ from, to, data }) { async emote({ to, data }) {
const mutation = ` const mutation = `
mutation { mutation {
AddPostEmotions( AddPostEmotions(
from: { id: "${from}" },
to: { id: "${to}" }, to: { id: "${to}" },
data: { emotion: ${data} } data: { emotion: ${data} }
) { ) {

View File

@ -61,8 +61,8 @@ export default () => {
} }
`, `,
AddPostEmotionsMutation: gql` AddPostEmotionsMutation: gql`
mutation($from: _UserInput!, $to: _PostInput!, $data: _EMOTEDInput!) { mutation($to: _PostInput!, $data: _EMOTEDInput!) {
AddPostEmotions(from: $from, to: $to, data: $data) { AddPostEmotions(to: $to, data: $data) {
emotion emotion
from { from {
id id