Refactor user factory

This commit is contained in:
roschaefer 2020-01-28 01:52:20 +01:00
parent 63939fa9e0
commit 2a79c53765
24 changed files with 611 additions and 362 deletions

View File

@ -147,55 +147,90 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
louie, louie,
dagobert, dagobert,
] = await Promise.all([ ] = await Promise.all([
factory.create('User', { factory.create(
id: 'u1', 'User',
name: 'Peter Lustig', {
slug: 'peter-lustig', id: 'u1',
role: 'admin', name: 'Peter Lustig',
email: 'admin@example.org', slug: 'peter-lustig',
}), role: 'admin',
factory.create('User', { },
id: 'u2', {
name: 'Bob der Baumeister', email: 'admin@example.org',
slug: 'bob-der-baumeister', },
role: 'moderator', ),
email: 'moderator@example.org', factory.create(
}), 'User',
factory.create('User', { {
id: 'u3', id: 'u2',
name: 'Jenny Rostock', name: 'Bob der Baumeister',
slug: 'jenny-rostock', slug: 'bob-der-baumeister',
role: 'user', role: 'moderator',
email: 'user@example.org', },
}), {
factory.create('User', { email: 'moderator@example.org',
id: 'u4', },
name: 'Huey', ),
slug: 'huey', factory.create(
role: 'user', 'User',
email: 'huey@example.org', {
}), id: 'u3',
factory.create('User', { name: 'Jenny Rostock',
id: 'u5', slug: 'jenny-rostock',
name: 'Dewey', role: 'user',
slug: 'dewey', },
role: 'user', {
email: 'dewey@example.org', email: 'user@example.org',
}), },
factory.create('User', { ),
id: 'u6', factory.create(
name: 'Louie', 'User',
slug: 'louie', {
role: 'user', id: 'u4',
email: 'louie@example.org', name: 'Huey',
}), slug: 'huey',
factory.create('User', { role: 'user',
id: 'u7', },
name: 'Dagobert', {
slug: 'dagobert', email: 'huey@example.org',
role: 'user', },
email: 'dagobert@example.org', ),
}), factory.create(
'User',
{
id: 'u5',
name: 'Dewey',
slug: 'dewey',
role: 'user',
},
{
email: 'dewey@example.org',
},
),
factory.create(
'User',
{
id: 'u6',
name: 'Louie',
slug: 'louie',
role: 'user',
},
{
email: 'louie@example.org',
},
),
factory.create(
'User',
{
id: 'u7',
name: 'Dagobert',
slug: 'dagobert',
role: 'user',
},
{
email: 'dagobert@example.org',
},
),
]) ])
await Promise.all([ await Promise.all([

View File

@ -40,10 +40,11 @@ export default function Factory(options = {}) {
factories, factories,
lastResponse: null, lastResponse: null,
neodeInstance, neodeInstance,
async create(node, args = {}) { async create(node, args = {}, options = {}) {
const { factory } = this.factories[node](args) const { factory } = this.factories[node](args)
this.lastResponse = await factory({ this.lastResponse = await factory({
args, args,
options,
neodeInstance, neodeInstance,
factoryInstance: this, factoryInstance: this,
}) })

View File

@ -13,7 +13,7 @@ Factory.define('socialMedia')
export default function create() { export default function create() {
return { return {
factory: async ({ args, neodeInstance }) => { factory: async ({ args }) => {
return Factory.build('socialMedia', args) return Factory.build('socialMedia', args)
}, },
} }

View File

@ -1,44 +1,54 @@
import faker from 'faker' import faker from 'faker'
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
import encryptPassword from '../helpers/encryptPassword'
import slugify from 'slug' import slugify from 'slug'
import { Factory } from 'rosie'
import { getNeode } from '../db/neo4j'
import { hashSync } from 'bcryptjs'
const neode = getNeode()
Factory.define('userWithoutEmailAddress')
.option('password', '1234')
.attrs({
id: uuid,
name: faker.name.findName,
email: faker.internet.email,
password: '1234',
role: 'user',
avatar: faker.internet.avatar,
about: faker.lorem.paragraph,
termsAndConditionsAgreedVersion: '0.0.1',
termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z',
allowEmbedIframes: false,
showShoutsPublicly: false,
locale: 'en',
})
.attr('slug', ['slug', 'name'], (slug, name) => {
return slug || slugify(name, { lower: true })
})
.attr('encryptedPassword', ['password'], password => {
return hashSync(password, 10)
})
.after(async (buildObject, options) => {
return neode.create('User', buildObject)
})
Factory.define('user')
.option('email', faker.internet.exampleEmail)
.extend('userWithoutEmailAddress')
.after(async (buildObject, options) => {
const [user, email] = await Promise.all([
buildObject,
neode.create('EmailAddress', { email: options.email }),
])
await Promise.all([user.relateTo(email, 'primaryEmail'), email.relateTo(user, 'belongsTo')])
return user
})
export default function create() { export default function create() {
return { return {
factory: async ({ args, neodeInstance, factoryInstance }) => { factory: async ({ args, options }) => {
const defaults = { return Factory.build('user', args, options)
id: uuid(),
name: faker.name.findName(),
email: faker.internet.email(),
password: '1234',
role: 'user',
avatar: faker.internet.avatar(),
about: faker.lorem.paragraph(),
termsAndConditionsAgreedVersion: '0.0.1',
termsAndConditionsAgreedAt: '2019-08-01T10:47:19.212Z',
allowEmbedIframes: false,
showShoutsPublicly: false,
locale: 'en',
}
defaults.slug = slugify(defaults.name, { lower: true })
args = {
...defaults,
...args,
}
args = await encryptPassword(args)
const user = await neodeInstance.create('User', args)
let email
if (typeof args.email === 'object') {
// probably a neode node
email = args.email
} else {
email = await factoryInstance.create('EmailAddress', { email: args.email })
}
await user.relateTo(email, 'primaryEmail')
await email.relateTo(user, 'belongsTo')
return user
}, },
} }
} }

View File

@ -65,14 +65,19 @@ describe('decode', () => {
describe('and corresponding user in the database', () => { describe('and corresponding user in the database', () => {
let user let user
beforeEach(async () => { beforeEach(async () => {
user = await factory.create('User', { user = await factory.create(
role: 'user', 'User',
name: 'Jenny Rostock', {
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/sasha_shestakov/128.jpg', role: 'user',
id: 'u3', name: 'Jenny Rostock',
email: 'user@example.org', avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/sasha_shestakov/128.jpg',
slug: 'jenny-rostock', id: 'u3',
}) slug: 'jenny-rostock',
},
{
email: 'user@example.org',
},
)
}) })
it('returns user object except email', async () => { it('returns user object except email', async () => {

View File

@ -48,13 +48,18 @@ beforeAll(() => {
}) })
beforeEach(async () => { beforeEach(async () => {
hashtagingUser = await neode.create('User', { hashtagingUser = await neode.create(
id: 'you', 'User',
name: 'Al Capone', {
slug: 'al-capone', id: 'you',
email: 'test@example.org', name: 'Al Capone',
password: '1234', slug: 'al-capone',
}) },
{
password: '1234',
email: 'test@example.org',
},
)
await neode.create('Category', { await neode.create('Category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',

View File

@ -53,13 +53,18 @@ beforeAll(async () => {
}) })
beforeEach(async () => { beforeEach(async () => {
notifiedUser = await neode.create('User', { notifiedUser = await neode.create(
id: 'you', 'User',
name: 'Al Capone', {
slug: 'al-capone', id: 'you',
email: 'test@example.org', name: 'Al Capone',
password: '1234', slug: 'al-capone',
}) },
{
email: 'test@example.org',
password: '1234',
},
)
await neode.create('Category', { await neode.create('Category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',
@ -143,13 +148,18 @@ describe('notifications', () => {
describe('commenter is not me', () => { describe('commenter is not me', () => {
beforeEach(async () => { beforeEach(async () => {
commentContent = 'Commenters comment.' commentContent = 'Commenters comment.'
commentAuthor = await neode.create('User', { commentAuthor = await neode.create(
id: 'commentAuthor', 'User',
name: 'Mrs Comment', {
slug: 'mrs-comment', id: 'commentAuthor',
email: 'commentauthor@example.org', name: 'Mrs Comment',
password: '1234', slug: 'mrs-comment',
}) },
{
email: 'commentauthor@example.org',
password: '1234',
},
)
}) })
it('sends me a notification', async () => { it('sends me a notification', async () => {
@ -224,13 +234,18 @@ describe('notifications', () => {
}) })
beforeEach(async () => { beforeEach(async () => {
postAuthor = await neode.create('User', { postAuthor = await neode.create(
id: 'postAuthor', 'User',
name: 'Mrs Post', {
slug: 'mrs-post', id: 'postAuthor',
email: 'post-author@example.org', name: 'Mrs Post',
password: '1234', slug: 'mrs-post',
}) },
{
email: 'post-author@example.org',
password: '1234',
},
)
}) })
describe('mentions me in a post', () => { describe('mentions me in a post', () => {
@ -428,23 +443,33 @@ describe('notifications', () => {
beforeEach(async () => { beforeEach(async () => {
commentContent = commentContent =
'One mention about me with <a data-mention-id="you" class="mention" href="/profile/you" target="_blank">@al-capone</a>.' 'One mention about me with <a data-mention-id="you" class="mention" href="/profile/you" target="_blank">@al-capone</a>.'
commentAuthor = await neode.create('User', { commentAuthor = await neode.create(
id: 'commentAuthor', 'User',
name: 'Mrs Comment', {
slug: 'mrs-comment', id: 'commentAuthor',
email: 'comment-author@example.org', name: 'Mrs Comment',
password: '1234', slug: 'mrs-comment',
}) },
{
email: 'comment-author@example.org',
password: '1234',
},
)
}) })
it('sends only one notification with reason mentioned_in_comment', async () => { it('sends only one notification with reason mentioned_in_comment', async () => {
postAuthor = await neode.create('User', { postAuthor = await neode.create(
id: 'MrPostAuthor', 'User',
name: 'Mr Author', {
slug: 'mr-author', id: 'MrPostAuthor',
email: 'post-author@example.org', name: 'Mr Author',
password: '1234', slug: 'mr-author',
}) },
{
email: 'post-author@example.org',
password: '1234',
},
)
await createCommentOnPostAction() await createCommentOnPostAction()
const expected = expect.objectContaining({ const expected = expect.objectContaining({
@ -514,13 +539,18 @@ describe('notifications', () => {
await postAuthor.relateTo(notifiedUser, 'blocked') await postAuthor.relateTo(notifiedUser, 'blocked')
commentContent = commentContent =
'One mention about me with <a data-mention-id="you" class="mention" href="/profile/you" target="_blank">@al-capone</a>.' 'One mention about me with <a data-mention-id="you" class="mention" href="/profile/you" target="_blank">@al-capone</a>.'
commentAuthor = await neode.create('User', { commentAuthor = await neode.create(
id: 'commentAuthor', 'User',
name: 'Mrs Comment', {
slug: 'mrs-comment', id: 'commentAuthor',
email: 'comment-author@example.org', name: 'Mrs Comment',
password: '1234', slug: 'mrs-comment',
}) },
{
email: 'comment-author@example.org',
password: '1234',
},
)
}) })
it('sends no notification', async () => { it('sends no notification', async () => {

View File

@ -34,28 +34,48 @@ describe('authorization', () => {
describe('given two existing users', () => { describe('given two existing users', () => {
beforeEach(async () => { beforeEach(async () => {
;[owner, anotherRegularUser, administrator, moderator] = await Promise.all([ ;[owner, anotherRegularUser, administrator, moderator] = await Promise.all([
factory.create('User', { factory.create(
email: 'owner@example.org', 'User',
name: 'Owner', {
password: 'iamtheowner', name: 'Owner',
}), },
factory.create('User', { {
email: 'another.regular.user@example.org', email: 'owner@example.org',
name: 'Another Regular User', password: 'iamtheowner',
password: 'else', },
}), ),
factory.create('User', { factory.create(
email: 'admin@example.org', 'User',
name: 'Admin', {
password: 'admin', name: 'Another Regular User',
role: 'admin', },
}), {
factory.create('User', { email: 'another.regular.user@example.org',
email: 'moderator@example.org', password: 'else',
name: 'Moderator', },
password: 'moderator', ),
role: 'moderator', factory.create(
}), 'User',
{
name: 'Admin',
role: 'admin',
},
{
email: 'admin@example.org',
password: 'admin',
},
),
factory.create(
'User',
{
name: 'Moderator',
role: 'moderator',
},
{
email: 'moderator@example.org',
password: 'moderator',
},
),
]) ])
variables = {} variables = {}
}) })

View File

@ -31,10 +31,14 @@ beforeEach(async () => {
const admin = await factory.create('User', { const admin = await factory.create('User', {
role: 'admin', role: 'admin',
}) })
await factory.create('User', { await factory.create(
email: 'someone@example.org', 'User',
password: '1234', {},
}) {
email: 'someone@example.org',
password: '1234',
},
)
await factory.create('Category', { await factory.create('Category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',

View File

@ -19,11 +19,16 @@ beforeAll(async () => {
// For performance reasons we do this only once // For performance reasons we do this only once
const users = await Promise.all([ const users = await Promise.all([
factory.create('User', { id: 'u1', role: 'user' }), factory.create('User', { id: 'u1', role: 'user' }),
factory.create('User', { factory.create(
id: 'm1', 'User',
role: 'moderator', {
password: '1234', id: 'm1',
}), role: 'moderator',
},
{
password: '1234',
},
),
factory.create('User', { factory.create('User', {
id: 'u2', id: 'u2',
role: 'user', role: 'user',

View File

@ -63,7 +63,7 @@ describe('AddEmailAddress', () => {
describe('authenticated', () => { describe('authenticated', () => {
beforeEach(async () => { beforeEach(async () => {
user = await factory.create('User', { id: '567', email: 'user@example.org' }) user = await factory.create('User', { id: '567' }, { email: 'user@example.org' })
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
}) })
@ -128,7 +128,7 @@ describe('AddEmailAddress', () => {
describe('but if another user owns an `EmailAddress` already with that email', () => { describe('but if another user owns an `EmailAddress` already with that email', () => {
it('throws UserInputError because of unique constraints', async () => { it('throws UserInputError because of unique constraints', async () => {
await factory.create('User', { email: 'new-email@example.org' }) await factory.create('User', {}, { email: 'new-email@example.org' })
await expect(mutate({ mutation, variables })).resolves.toMatchObject({ await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { AddEmailAddress: null }, data: { AddEmailAddress: null },
errors: [{ message: 'A user account with this email already exists.' }], errors: [{ message: 'A user account with this email already exists.' }],
@ -169,7 +169,7 @@ describe('VerifyEmailAddress', () => {
describe('authenticated', () => { describe('authenticated', () => {
beforeEach(async () => { beforeEach(async () => {
user = await factory.create('User', { id: '567', email: 'user@example.org' }) user = await factory.create('User', { id: '567' }, { email: 'user@example.org' })
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
}) })

View File

@ -73,20 +73,30 @@ beforeAll(async () => {
beforeEach(async () => { beforeEach(async () => {
user1 = await factory user1 = await factory
.create('User', { .create(
id: 'u1', 'User',
name: 'user1', {
email: 'test@example.org', id: 'u1',
password: '1234', name: 'user1',
}) },
{
email: 'test@example.org',
password: '1234',
},
)
.then(user => user.toJson()) .then(user => user.toJson())
user2 = await factory user2 = await factory
.create('User', { .create(
id: 'u2', 'User',
name: 'user2', {
email: 'test2@example.org', id: 'u2',
password: '1234', name: 'user2',
}) },
{
email: 'test2@example.org',
password: '1234',
},
)
.then(user => user.toJson()) .then(user => user.toJson())
authenticatedUser = user1 authenticatedUser = user1

View File

@ -80,19 +80,29 @@ describe('moderate resources', () => {
closed: false, closed: false,
} }
authenticatedUser = null authenticatedUser = null
moderator = await factory.create('User', { moderator = await factory.create(
id: 'moderator-id', 'User',
name: 'Moderator', {
email: 'moderator@example.org', id: 'moderator-id',
password: '1234', name: 'Moderator',
role: 'moderator', role: 'moderator',
}) },
nonModerator = await factory.create('User', { {
id: 'non-moderator', email: 'moderator@example.org',
name: 'Non Moderator', password: '1234',
email: 'non.moderator@example.org', },
password: '1234', )
}) nonModerator = await factory.create(
'User',
{
id: 'non-moderator',
name: 'Non Moderator',
},
{
email: 'non.moderator@example.org',
password: '1234',
},
)
}) })
afterEach(async () => { afterEach(async () => {

View File

@ -45,9 +45,13 @@ afterEach(async () => {
describe('passwordReset', () => { describe('passwordReset', () => {
describe('given a user', () => { describe('given a user', () => {
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { await factory.create(
email: 'user@example.org', 'User',
}) {},
{
email: 'user@example.org',
},
)
}) })
describe('requestPasswordReset', () => { describe('requestPasswordReset', () => {
@ -123,11 +127,16 @@ describe('resetPassword', () => {
describe('given a user', () => { describe('given a user', () => {
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { await factory.create(
email: 'user@example.org', 'User',
role: 'user', {
password: '1234', role: 'user',
}) },
{
email: 'user@example.org',
password: '1234',
},
)
}) })
describe('invalid email', () => { describe('invalid email', () => {

View File

@ -56,12 +56,17 @@ beforeAll(async () => {
beforeEach(async () => { beforeEach(async () => {
variables = {} variables = {}
user = await factory.create('User', { user = await factory.create(
id: 'current-user', 'User',
name: 'TestUser', {
email: 'test@example.org', id: 'current-user',
password: '1234', name: 'TestUser',
}) },
{
email: 'test@example.org',
password: '1234',
},
)
await Promise.all([ await Promise.all([
neode.create('Category', { neode.create('Category', {
id: 'cat9', id: 'cat9',
@ -96,12 +101,17 @@ describe('Post', () => {
let followedUser, happyPost, cryPost let followedUser, happyPost, cryPost
beforeEach(async () => { beforeEach(async () => {
;[followedUser] = await Promise.all([ ;[followedUser] = await Promise.all([
factory.create('User', { factory.create(
id: 'followed-by-me', 'User',
email: 'followed@example.org', {
name: 'Followed User', id: 'followed-by-me',
password: '1234', name: 'Followed User',
}), },
{
email: 'followed@example.org',
password: '1234',
},
),
]) ])
;[happyPost, cryPost] = await Promise.all([ ;[happyPost, cryPost] = await Promise.all([
factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }), factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }),

View File

@ -1,4 +1,5 @@
import Factory from '../../factories' import Factory from '../../factories'
import { Factory as RosieFactory } from 'rosie'
import { gql } from '../../helpers/jest' import { gql } from '../../helpers/jest'
import { getDriver, getNeode } from '../../db/neo4j' import { getDriver, getNeode } from '../../db/neo4j'
import createServer from '../../server' import createServer from '../../server'
@ -58,11 +59,16 @@ describe('Signup', () => {
describe('as admin', () => { describe('as admin', () => {
beforeEach(async () => { beforeEach(async () => {
const admin = await factory.create('User', { const admin = await factory.create(
role: 'admin', 'User',
email: 'admin@example.org', {
password: '1234', role: 'admin',
}) },
{
email: 'admin@example.org',
password: '1234',
},
)
authenticatedUser = await admin.toJson() authenticatedUser = await admin.toJson()
}) })
@ -90,9 +96,9 @@ describe('Signup', () => {
}) })
describe('if the email already exists', () => { describe('if the email already exists', () => {
let email let emailAddress
beforeEach(async () => { beforeEach(async () => {
email = await factory.create('EmailAddress', { emailAddress = await factory.create('EmailAddress', {
email: 'someuser@example.org', email: 'someuser@example.org',
verifiedAt: null, verifiedAt: null,
}) })
@ -100,7 +106,8 @@ describe('Signup', () => {
describe('and the user has registered already', () => { describe('and the user has registered already', () => {
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { email }) const user = await RosieFactory.build('userWithoutEmailAddress')
await emailAddress.relateTo(user, 'belongsTo')
}) })
it('throws UserInputError error because of unique constraint violation', async () => { it('throws UserInputError error because of unique constraint violation', async () => {

View File

@ -84,24 +84,39 @@ describe('file a report on a resource', () => {
describe('authenticated', () => { describe('authenticated', () => {
beforeEach(async () => { beforeEach(async () => {
currentUser = await factory.create('User', { currentUser = await factory.create(
id: 'current-user-id', 'User',
role: 'user', {
email: 'test@example.org', id: 'current-user-id',
password: '1234', role: 'user',
}) },
otherReportingUser = await factory.create('User', { {
id: 'other-reporting-user-id', email: 'test@example.org',
role: 'user', password: '1234',
email: 'reporting@example.org', },
password: '1234', )
}) otherReportingUser = await factory.create(
await factory.create('User', { 'User',
id: 'abusive-user-id', {
role: 'user', id: 'other-reporting-user-id',
name: 'abusive-user', role: 'user',
email: 'abusive-user@example.org', },
}) {
email: 'reporting@example.org',
password: '1234',
},
)
await factory.create(
'User',
{
id: 'abusive-user-id',
role: 'user',
name: 'abusive-user',
},
{
email: 'abusive-user@example.org',
},
)
await instance.create('Category', { await instance.create('Category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',
@ -515,24 +530,39 @@ describe('file a report on a resource', () => {
beforeEach(async () => { beforeEach(async () => {
authenticatedUser = null authenticatedUser = null
moderator = await factory.create('User', { moderator = await factory.create(
id: 'moderator-1', 'User',
role: 'moderator', {
email: 'moderator@example.org', id: 'moderator-1',
password: '1234', role: 'moderator',
}) },
currentUser = await factory.create('User', { {
id: 'current-user-id', email: 'moderator@example.org',
role: 'user', password: '1234',
email: 'current.user@example.org', },
password: '1234', )
}) currentUser = await factory.create(
abusiveUser = await factory.create('User', { 'User',
id: 'abusive-user-1', {
role: 'user', id: 'current-user-id',
name: 'abusive-user', role: 'user',
email: 'abusive-user@example.org', },
}) {
email: 'current.user@example.org',
password: '1234',
},
)
abusiveUser = await factory.create(
'User',
{
id: 'abusive-user-1',
role: 'user',
name: 'abusive-user',
},
{
email: 'abusive-user@example.org',
},
)
await instance.create('Category', { await instance.create('Category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',

View File

@ -31,22 +31,37 @@ describe('rewards', () => {
}) })
beforeEach(async () => { beforeEach(async () => {
regularUser = await factory.create('User', { regularUser = await factory.create(
id: 'regular-user-id', 'User',
role: 'user', {
email: 'user@example.org', id: 'regular-user-id',
password: '1234', role: 'user',
}) },
moderator = await factory.create('User', { {
id: 'moderator-id', email: 'user@example.org',
role: 'moderator', password: '1234',
email: 'moderator@example.org', },
}) )
administrator = await factory.create('User', { moderator = await factory.create(
id: 'admin-id', 'User',
role: 'admin', {
email: 'admin@example.org', id: 'moderator-id',
}) role: 'moderator',
},
{
email: 'moderator@example.org',
},
)
administrator = await factory.create(
'User',
{
id: 'admin-id',
role: 'admin',
},
{
email: 'admin@example.org',
},
)
badge = await factory.create('Badge', { badge = await factory.create('Badge', {
id: 'indiegogo_en_rhino', id: 'indiegogo_en_rhino',
type: 'crowdfunding', type: 'crowdfunding',
@ -172,10 +187,15 @@ describe('rewards', () => {
}, },
errors: undefined, errors: undefined,
} }
await factory.create('User', { await factory.create(
id: 'regular-user-2-id', 'User',
email: 'regular2@email.com', {
}) id: 'regular-user-2-id',
},
{
email: 'regular2@email.com',
},
)
await mutate({ await mutate({
mutation: rewardMutation, mutation: rewardMutation,
variables, variables,

View File

@ -47,19 +47,29 @@ describe('shout and unshout posts', () => {
query = createTestClient(server).query query = createTestClient(server).query
}) })
beforeEach(async () => { beforeEach(async () => {
currentUser = await factory.create('User', { currentUser = await factory.create(
id: 'current-user-id', 'User',
name: 'Current User', {
email: 'current.user@example.org', id: 'current-user-id',
password: '1234', name: 'Current User',
}) },
{
email: 'current.user@example.org',
password: '1234',
},
)
postAuthor = await factory.create('User', { postAuthor = await factory.create(
id: 'id-of-another-user', 'User',
name: 'Another User', {
email: 'another.user@example.org', id: 'id-of-another-user',
password: '1234', name: 'Another User',
}) },
{
email: 'another.user@example.org',
password: '1234',
},
)
}) })
afterEach(async () => { afterEach(async () => {
await factory.cleanDatabase() await factory.cleanDatabase()

View File

@ -2,40 +2,46 @@ import { createTestClient } from 'apollo-server-testing'
import createServer from '../../server' import createServer from '../../server'
import Factory from '../../factories' import Factory from '../../factories'
import { gql } from '../../helpers/jest' import { gql } from '../../helpers/jest'
import { getNeode, getDriver } from '../../db/neo4j' import { getDriver } from '../../db/neo4j'
const driver = getDriver() const driver = getDriver()
const factory = Factory() const factory = Factory()
const neode = getNeode()
describe('SocialMedia', () => { describe('SocialMedia', () => {
let socialMediaAction, someUser, ownerNode, owner let socialMediaAction, someUser, ownerNode, owner
const ownerParams = {
email: 'pippi@example.com',
password: '1234',
name: 'Pippi Langstrumpf',
}
const userParams = {
email: 'kalle@example.com',
password: 'abcd',
name: 'Kalle Blomqvist',
}
const url = 'https://twitter.com/pippi-langstrumpf' const url = 'https://twitter.com/pippi-langstrumpf'
const newUrl = 'https://twitter.com/bullerby' const newUrl = 'https://twitter.com/bullerby'
const setUpSocialMedia = async () => { const setUpSocialMedia = async () => {
const socialMediaNode = await neode.create('SocialMedia', { url }) const socialMediaNode = await factory.create('SocialMedia', { url })
await socialMediaNode.relateTo(ownerNode, 'ownedBy') await socialMediaNode.relateTo(ownerNode, 'ownedBy')
return socialMediaNode.toJson() return socialMediaNode.toJson()
} }
beforeEach(async () => { beforeEach(async () => {
const someUserNode = await neode.create('User', userParams) const someUserNode = await factory.create(
'User',
{
name: 'Kalle Blomqvist',
},
{
email: 'kalle@example.com',
password: 'abcd',
},
)
someUser = await someUserNode.toJson() someUser = await someUserNode.toJson()
ownerNode = await neode.create('User', ownerParams) ownerNode = await factory.create(
'User',
{
name: 'Pippi Langstrumpf',
},
{
email: 'pippi@example.com',
password: '1234',
},
)
owner = await ownerNode.toJson() owner = await ownerNode.toJson()
socialMediaAction = async (user, mutation, variables) => { socialMediaAction = async (user, mutation, variables) => {

View File

@ -127,15 +127,20 @@ describe('currentUser', () => {
describe('authenticated', () => { describe('authenticated', () => {
describe('and corresponding user in the database', () => { describe('and corresponding user in the database', () => {
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { await factory.create(
id: 'u3', 'User',
// the `id` is the only thing that has to match the decoded JWT bearer token {
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg', id: 'u3',
email: 'test@example.org', // the `id` is the only thing that has to match the decoded JWT bearer token
name: 'Matilde Hermiston', avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg',
slug: 'matilde-hermiston', name: 'Matilde Hermiston',
role: 'user', slug: 'matilde-hermiston',
}) role: 'user',
},
{
email: 'test@example.org',
},
)
const userBearerToken = encode({ id: 'u3' }) const userBearerToken = encode({ id: 'u3' })
req = { headers: { authorization: `Bearer ${userBearerToken}` } } req = { headers: { authorization: `Bearer ${userBearerToken}` } }
}) })
@ -172,10 +177,13 @@ describe('login', () => {
beforeEach(async () => { beforeEach(async () => {
variables = { email: 'test@example.org', password: '1234' } variables = { email: 'test@example.org', password: '1234' }
user = await factory.create('User', { user = await factory.create(
...variables, 'User',
id: 'acb2d923-f3af-479e-9f00-61b12e864666', {
}) id: 'acb2d923-f3af-479e-9f00-61b12e864666',
},
variables,
)
}) })
describe('ask for a `token`', () => { describe('ask for a `token`', () => {

View File

@ -37,7 +37,7 @@ afterEach(async () => {
describe('User', () => { describe('User', () => {
describe('query by email address', () => { describe('query by email address', () => {
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { name: 'Johnny', email: 'any-email-address@example.org' }) await factory.create('User', { name: 'Johnny' }, { email: 'any-email-address@example.org' })
}) })
const userQuery = gql` const userQuery = gql`
@ -57,11 +57,16 @@ describe('User', () => {
describe('as admin', () => { describe('as admin', () => {
beforeEach(async () => { beforeEach(async () => {
const admin = await factory.create('User', { const admin = await factory.create(
role: 'admin', 'User',
email: 'admin@example.org', {
password: '1234', role: 'admin',
}) },
{
email: 'admin@example.org',
password: '1234',
},
)
authenticatedUser = await admin.toJson() authenticatedUser = await admin.toJson()
}) })
@ -91,19 +96,9 @@ describe('User', () => {
}) })
describe('UpdateUser', () => { describe('UpdateUser', () => {
let userParams, variables let variables
beforeEach(async () => { beforeEach(async () => {
userParams = {
email: 'user@example.org',
password: '1234',
id: 'u47',
name: 'John Doe',
termsAndConditionsAgreedVersion: null,
termsAndConditionsAgreedAt: null,
allowEmbedIframes: false,
}
variables = { variables = {
id: 'u47', id: 'u47',
name: 'John Doughnut', name: 'John Doughnut',
@ -133,18 +128,33 @@ describe('UpdateUser', () => {
` `
beforeEach(async () => { beforeEach(async () => {
user = await factory.create('User', userParams) user = await factory.create(
'User',
{
id: 'u47',
name: 'John Doe',
termsAndConditionsAgreedVersion: null,
termsAndConditionsAgreedAt: null,
allowEmbedIframes: false,
},
{
email: 'user@example.org',
},
)
}) })
describe('as another user', () => { describe('as another user', () => {
beforeEach(async () => { beforeEach(async () => {
const someoneElseParams = { const someoneElse = await factory.create(
email: 'someone-else@example.org', 'User',
password: '1234', {
name: 'James Doe', name: 'James Doe',
} },
{
email: 'someone-else@example.org',
},
)
const someoneElse = await factory.create('User', someoneElseParams)
authenticatedUser = await someoneElse.toJson() authenticatedUser = await someoneElse.toJson()
}) })
@ -272,11 +282,15 @@ describe('DeleteUser', () => {
about: 'along with my about', about: 'along with my about',
id: 'u343', id: 'u343',
}) })
await factory.create('User', { await factory.create(
email: 'friends-account@example.org', 'User',
password: '1234', {
id: 'not-my-account', id: 'not-my-account',
}) },
{
email: 'friends-account@example.org',
},
)
}) })
describe('unauthenticated', () => { describe('unauthenticated', () => {

View File

@ -146,7 +146,7 @@ describe('userMiddleware', () => {
}) })
describe('UpdateUser', () => { describe('UpdateUser', () => {
let user, userParams let user
beforeEach(async () => { beforeEach(async () => {
newlyCreatedNodesWithLocales = [ newlyCreatedNodesWithLocales = [
{ {
@ -182,10 +182,9 @@ describe('userMiddleware', () => {
}, },
}, },
] ]
userParams = { user = await factory.create('User', {
id: 'updating-user', id: 'updating-user',
} })
user = await factory.create('User', userParams)
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
}) })

View File

@ -13,8 +13,9 @@ function createUser (slug) {
debug(`creating user ${slug}`) debug(`creating user ${slug}`)
return factory.create('User', { return factory.create('User', {
name: slug, name: slug,
email: 'example@test.org', }, {
password: '1234' password: '1234'
email: 'example@test.org',
}) })
// await login({ email: 'example@test.org', password: '1234' }) // await login({ email: 'example@test.org', password: '1234' })
} }