diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 31c373fc7..8e3e2311a 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -167,6 +167,7 @@ const permissions = shield( // RemoveBadgeRewarded: isAdmin, reward: isAdmin, unreward: isAdmin, + // why is this here? will we support buying/selling fruit?? // addFruitToBasket: isAuthenticated follow: isAuthenticated, unfollow: isAuthenticated, @@ -180,6 +181,7 @@ const permissions = shield( DeleteUser: isDeletingOwnAccount, requestPasswordReset: allow, resetPassword: allow, + AddPostEmotions: isAuthenticated, }, User: { email: isMyOwn, diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 0c8dfb7f0..74f88d62e 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -44,6 +44,7 @@ export default { delete params.categoryIds params = await fileUpload(params, { file: 'imageUpload', url: 'image' }) params.id = params.id || uuid() + let createPostCypher = `CREATE (post:Post {params}) WITH post MATCH (author:User {id: $userId}) diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 233be450a..49cb2123b 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -383,3 +383,77 @@ describe('DeletePost', () => { }) }) }) + +describe('AddPostEmotions', () => { + let addPostEmotionsVariables + const addPostEmotionsMutation = ` + mutation($from: _UserInput!, $to: _PostInput!, $data: _EMOTEDInput!) { + AddPostEmotions(from: $from, to: $to, data: $data) { + from { + id + } + to { + id + } + emotion + } + } + ` + describe('emotions', () => { + beforeEach(async () => { + const asAuthor = Factory() + authorParams = { + id: 'u25', + email: 'wanna-add-emotions@example.org', + password: '1234', + } + await asAuthor.create('User', authorParams) + await asAuthor.authenticateAs(authorParams) + await asAuthor.create('Post', { + id: 'p1376', + title: postTitle, + content: postContent, + }) + addPostEmotionsVariables = { + from: { id: authorParams.id }, + to: { id: 'p1376' }, + data: { emotion: 'happy' }, + } + }) + // it('supports setting emotions for a post', () => {}) + + describe('unauthenticated', () => { + it('throws authorization error', async () => { + client = new GraphQLClient(host) + await expect( + client.request(addPostEmotionsMutation, { + from: { id: 'u25' }, + to: { id: 'p1376' }, + data: { emotion: 'happy' }, + }), + ).rejects.toThrow('Not Authorised') + }) + }) + + describe('authenticated as author', () => { + let headers + beforeEach(async () => { + headers = await login(authorParams) + client = new GraphQLClient(host, { headers }) + }) + + it('adds an emotion to the post', async () => { + const expected = { + AddPostEmotions: { + from: addPostEmotionsVariables.from, + to: addPostEmotionsVariables.to, + emotion: 'happy', + }, + } + await expect( + client.request(addPostEmotionsMutation, addPostEmotionsVariables), + ).resolves.toEqual(expected) + }) + }) + }) +}) diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index d254a9a9c..30cdd71d4 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -50,6 +50,10 @@ type Post { ) emotions: [EMOTED] + emotionsCount: Int! + @cypher( + statement: "MATCH (this)<-[emoted:EMOTED]-(:User) RETURN COUNT(DISTINCT emoted)" + ) } type Mutation {