mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-12 23:35:58 +00:00
Merge pull request #223 from Human-Connection/204-tests-for-rewarding-badges
204 tests for rewarding badges
This commit is contained in:
commit
a1848e8dda
@ -6,6 +6,7 @@ import statistics from './resolvers/statistics.js'
|
|||||||
import reports from './resolvers/reports.js'
|
import reports from './resolvers/reports.js'
|
||||||
import posts from './resolvers/posts.js'
|
import posts from './resolvers/posts.js'
|
||||||
import moderation from './resolvers/moderation.js'
|
import moderation from './resolvers/moderation.js'
|
||||||
|
import rewards from './resolvers/rewards.js'
|
||||||
|
|
||||||
export const typeDefs = fs
|
export const typeDefs = fs
|
||||||
.readFileSync(
|
.readFileSync(
|
||||||
@ -21,7 +22,8 @@ export const resolvers = {
|
|||||||
Mutation: {
|
Mutation: {
|
||||||
...userManagement.Mutation,
|
...userManagement.Mutation,
|
||||||
...reports.Mutation,
|
...reports.Mutation,
|
||||||
|
...posts.Mutation,
|
||||||
...moderation.Mutation,
|
...moderation.Mutation,
|
||||||
...posts.Mutation
|
...rewards.Mutation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,6 +56,11 @@ const permissions = shield({
|
|||||||
CreateBadge: isAdmin,
|
CreateBadge: isAdmin,
|
||||||
UpdateBadge: isAdmin,
|
UpdateBadge: isAdmin,
|
||||||
DeleteBadge: isAdmin,
|
DeleteBadge: isAdmin,
|
||||||
|
AddUserBadges: isAdmin,
|
||||||
|
// AddBadgeRewarded: isAdmin,
|
||||||
|
// RemoveBadgeRewarded: isAdmin,
|
||||||
|
reward: isAdmin,
|
||||||
|
unreward: isAdmin,
|
||||||
// addFruitToBasket: isAuthenticated
|
// addFruitToBasket: isAuthenticated
|
||||||
follow: isAuthenticated,
|
follow: isAuthenticated,
|
||||||
unfollow: isAuthenticated,
|
unfollow: isAuthenticated,
|
||||||
|
|||||||
47
backend/src/resolvers/rewards.js
Normal file
47
backend/src/resolvers/rewards.js
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
export default {
|
||||||
|
Mutation: {
|
||||||
|
reward: async (_object, params, context, _resolveInfo) => {
|
||||||
|
const { fromBadgeId, toUserId } = params
|
||||||
|
const session = context.driver.session()
|
||||||
|
|
||||||
|
let sessionRes = await session.run(
|
||||||
|
`MATCH (badge:Badge {id: $badgeId}), (rewardedUser:User {id: $rewardedUserId})
|
||||||
|
MERGE (badge)-[:REWARDED]->(rewardedUser)
|
||||||
|
RETURN rewardedUser {.id}`,
|
||||||
|
{
|
||||||
|
badgeId: fromBadgeId,
|
||||||
|
rewardedUserId: toUserId
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const [rewardedUser] = sessionRes.records.map(record => {
|
||||||
|
return record.get('rewardedUser')
|
||||||
|
})
|
||||||
|
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
return rewardedUser.id
|
||||||
|
},
|
||||||
|
|
||||||
|
unreward: async (_object, params, context, _resolveInfo) => {
|
||||||
|
const { fromBadgeId, toUserId } = params
|
||||||
|
const session = context.driver.session()
|
||||||
|
|
||||||
|
let sessionRes = await session.run(
|
||||||
|
`MATCH (badge:Badge {id: $badgeId})-[reward:REWARDED]->(rewardedUser:User {id: $rewardedUserId})
|
||||||
|
DELETE reward
|
||||||
|
RETURN rewardedUser {.id}`,
|
||||||
|
{
|
||||||
|
badgeId: fromBadgeId,
|
||||||
|
rewardedUserId: toUserId
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const [rewardedUser] = sessionRes.records.map(record => {
|
||||||
|
return record.get('rewardedUser')
|
||||||
|
})
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
return rewardedUser.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
232
backend/src/resolvers/rewards.spec.js
Normal file
232
backend/src/resolvers/rewards.spec.js
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
import Factory from '../seed/factories'
|
||||||
|
import { GraphQLClient } from 'graphql-request'
|
||||||
|
import { host, login } from '../jest/helpers'
|
||||||
|
|
||||||
|
const factory = Factory()
|
||||||
|
|
||||||
|
describe('rewards', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await factory.create('User', {
|
||||||
|
id: 'u1',
|
||||||
|
role: 'user',
|
||||||
|
email: 'user@example.org',
|
||||||
|
password: '1234'
|
||||||
|
})
|
||||||
|
await factory.create('User', {
|
||||||
|
id: 'u2',
|
||||||
|
role: 'moderator',
|
||||||
|
email: 'moderator@example.org'
|
||||||
|
})
|
||||||
|
await factory.create('User', {
|
||||||
|
id: 'u3',
|
||||||
|
role: 'admin',
|
||||||
|
email: 'admin@example.org'
|
||||||
|
})
|
||||||
|
await factory.create('Badge', {
|
||||||
|
id: 'b6',
|
||||||
|
key: 'indiegogo_en_rhino',
|
||||||
|
type: 'crowdfunding',
|
||||||
|
status: 'permanent',
|
||||||
|
icon: '/img/badges/indiegogo_en_rhino.svg'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await factory.cleanDatabase()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('RewardBadge', () => {
|
||||||
|
const mutation = `
|
||||||
|
mutation(
|
||||||
|
$from: ID!
|
||||||
|
$to: ID!
|
||||||
|
) {
|
||||||
|
reward(fromBadgeId: $from, toUserId: $to)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
const variables = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
let client
|
||||||
|
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated admin', () => {
|
||||||
|
let client
|
||||||
|
beforeEach(async () => {
|
||||||
|
const headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rewards a badge to user', async () => {
|
||||||
|
const variables = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
reward: 'u1'
|
||||||
|
}
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).resolves.toEqual(expected)
|
||||||
|
})
|
||||||
|
it('rewards a second different badge to same user', async () => {
|
||||||
|
await factory.create('Badge', {
|
||||||
|
id: 'b1',
|
||||||
|
key: 'indiegogo_en_racoon',
|
||||||
|
type: 'crowdfunding',
|
||||||
|
status: 'permanent',
|
||||||
|
icon: '/img/badges/indiegogo_en_racoon.svg'
|
||||||
|
})
|
||||||
|
const variables = {
|
||||||
|
from: 'b1',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
reward: 'u1'
|
||||||
|
}
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).resolves.toEqual(expected)
|
||||||
|
})
|
||||||
|
it('rewards the same badge as well to another user', async () => {
|
||||||
|
const variables1 = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
await client.request(mutation, variables1)
|
||||||
|
|
||||||
|
const variables2 = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u2'
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
reward: 'u2'
|
||||||
|
}
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables2)
|
||||||
|
).resolves.toEqual(expected)
|
||||||
|
})
|
||||||
|
it('returns the original reward if a reward is attempted a second time', async () => {
|
||||||
|
const variables = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
await client.request(mutation, variables)
|
||||||
|
await client.request(mutation, variables)
|
||||||
|
|
||||||
|
const query = `{
|
||||||
|
User( id: "u1" ) {
|
||||||
|
badgesCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
const expected = { User: [{ badgesCount: 1 }] }
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
client.request(query)
|
||||||
|
).resolves.toEqual(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated moderator', () => {
|
||||||
|
const variables = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
let client
|
||||||
|
beforeEach(async () => {
|
||||||
|
const headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('rewards bage to user', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('RemoveReward', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await factory.relate('User', 'Badges', { from: 'b6', to: 'u1' })
|
||||||
|
})
|
||||||
|
const variables = {
|
||||||
|
from: 'b6',
|
||||||
|
to: 'u1'
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
unreward: 'u1'
|
||||||
|
}
|
||||||
|
|
||||||
|
const mutation = `
|
||||||
|
mutation(
|
||||||
|
$from: ID!
|
||||||
|
$to: ID!
|
||||||
|
) {
|
||||||
|
unreward(fromBadgeId: $from, toUserId: $to)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
let client
|
||||||
|
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated admin', () => {
|
||||||
|
let client
|
||||||
|
beforeEach(async () => {
|
||||||
|
const headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('removes a badge from user', async () => {
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).resolves.toEqual(expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails to remove a not existing badge from user', async () => {
|
||||||
|
await client.request(mutation, variables)
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).rejects.toThrow('Cannot read property \'id\' of undefined')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated moderator', () => {
|
||||||
|
let client
|
||||||
|
beforeEach(async () => {
|
||||||
|
const headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('removes bage from user', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
await expect(
|
||||||
|
client.request(mutation, variables)
|
||||||
|
).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@ -25,6 +25,8 @@ type Mutation {
|
|||||||
report(id: ID!, description: String): Report
|
report(id: ID!, description: String): Report
|
||||||
disable(id: ID!): ID
|
disable(id: ID!): ID
|
||||||
enable(id: ID!): ID
|
enable(id: ID!): ID
|
||||||
|
reward(fromBadgeId: ID!, toUserId: ID!): ID
|
||||||
|
unreward(fromBadgeId: ID!, toUserId: ID!): ID
|
||||||
"Shout the given Type and ID"
|
"Shout the given Type and ID"
|
||||||
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
|
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
|
||||||
MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId})
|
MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId})
|
||||||
@ -269,6 +271,14 @@ enum FollowTypeEnum {
|
|||||||
Project
|
Project
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Reward {
|
||||||
|
id: ID!
|
||||||
|
user: User @relation(name: "REWARDED", direction: "IN")
|
||||||
|
rewarderId: ID
|
||||||
|
createdAt: String
|
||||||
|
badge: Badge @relation(name: "REWARDED", direction: "OUT")
|
||||||
|
}
|
||||||
|
|
||||||
type Organization {
|
type Organization {
|
||||||
id: ID!
|
id: ID!
|
||||||
createdBy: User @relation(name: "CREATED_ORGA", direction: "IN")
|
createdBy: User @relation(name: "CREATED_ORGA", direction: "IN")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user