Write test/refactor tests/resolvers/middleware

- write tests for userMiddleware
- checks the functionality of nodes/locations middleware
- refactor to not allow users to update to remove their name
  debatable whether we want that or not, but we do not allow users to
create accounts with no name, so we should be consistent, before we were
using neode to validate this, but we have are removing neode from
production code, so we must validate ourselves
- collate UpdateUser mutations to one
This commit is contained in:
mattwr18 2019-12-12 18:14:47 +01:00
parent 3e15ecdfa2
commit d375ebe7d9
12 changed files with 293 additions and 90 deletions

View File

@ -7,7 +7,7 @@ import sluggify from './sluggifyMiddleware'
import excerpt from './excerptMiddleware' import excerpt from './excerptMiddleware'
import xss from './xssMiddleware' import xss from './xssMiddleware'
import permissions from './permissionsMiddleware' import permissions from './permissionsMiddleware'
import user from './userMiddleware' import user from './user/userMiddleware'
import includedFields from './includedFieldsMiddleware' import includedFields from './includedFieldsMiddleware'
import orderBy from './orderByMiddleware' import orderBy from './orderByMiddleware'
import validation from './validation/validationMiddleware' import validation from './validation/validationMiddleware'

View File

@ -70,7 +70,6 @@ const createOrUpdateLocations = async (userId, locationName, driver) => {
if (isEmpty(locationName)) { if (isEmpty(locationName)) {
return return
} }
const res = await fetch( const res = await fetch(
`https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent( `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(
locationName, locationName,
@ -111,33 +110,44 @@ const createOrUpdateLocations = async (userId, locationName, driver) => {
if (data.context) { if (data.context) {
await asyncForEach(data.context, async ctx => { await asyncForEach(data.context, async ctx => {
await createLocation(session, ctx) await createLocation(session, ctx)
try {
await session.run( await session.writeTransaction(transaction => {
'MATCH (parent:Location {id: $parentId}), (child:Location {id: $childId}) ' + return transaction.run(
'MERGE (child)<-[:IS_IN]-(parent) ' + `
'RETURN child.id, parent.id', MATCH (parent:Location {id: $parentId}), (child:Location {id: $childId})
MERGE (child)<-[:IS_IN]-(parent)
RETURN child.id, parent.id
`,
{ {
parentId: parent.id, parentId: parent.id,
childId: ctx.id, childId: ctx.id,
}, },
) )
})
parent = ctx parent = ctx
} finally {
session.close()
}
}) })
} }
// delete all current locations from user // delete all current locations from user and add new location
await session.run('MATCH (u:User {id: $userId})-[r:IS_IN]->(l:Location) DETACH DELETE r', { try {
userId: userId, await session.writeTransaction(transaction => {
}) return transaction.run(
// connect user with location `
await session.run( MATCH (user:User {id: $userId})-[relationship:IS_IN]->(location:Location)
'MATCH (u:User {id: $userId}), (l:Location {id: $locationId}) MERGE (u)-[:IS_IN]->(l) RETURN l.id, u.id', DETACH DELETE relationship
{ WITH user
userId: userId, MATCH (location:Location {id: $locationId})
locationId: data.id, MERGE (user)-[:IS_IN]->(location)
}, RETURN location.id, user.id
`,
{ userId: userId, locationId: data.id },
) )
})
} finally {
session.close() session.close()
} }
}
export default createOrUpdateLocations export default createOrUpdateLocations

View File

@ -1,10 +1,10 @@
import createOrUpdateLocations from './nodes/locations' import createOrUpdateLocations from '../nodes/locations'
export default { export default {
Mutation: { Mutation: {
SignupVerification: async (resolve, root, args, context, info) => { SignupVerification: async (resolve, root, args, context, info) => {
const result = await resolve(root, args, context, info) const result = await resolve(root, args, context, info)
await createOrUpdateLocations(args.id, args.locationName, context.driver) await createOrUpdateLocations(result.id, args.locationName, context.driver)
return result return result
}, },
UpdateUser: async (resolve, root, args, context, info) => { UpdateUser: async (resolve, root, args, context, info) => {

View File

@ -0,0 +1,213 @@
import { gql } from '../../helpers/jest'
import Factory from '../../seed/factories'
import { getNeode, getDriver } from '../../bootstrap/neo4j'
import { createTestClient } from 'apollo-server-testing'
import createServer from '../../server'
const factory = Factory()
const neode = getNeode()
const driver = getDriver()
let authenticatedUser, mutate, variables
const signupVerificationMutation = gql`
mutation(
$name: String!
$password: String!
$email: String!
$nonce: String!
$termsAndConditionsAgreedVersion: String!
$locationName: String
) {
SignupVerification(
name: $name
password: $password
email: $email
nonce: $nonce
termsAndConditionsAgreedVersion: $termsAndConditionsAgreedVersion
locationName: $locationName
) {
locationName
}
}
`
const updateUserMutation = gql`
mutation($id: ID!, $name: String!, $locationName: String) {
UpdateUser(id: $id, name: $name, locationName: $locationName) {
locationName
}
}
`
let newlyCreatedNodesWithLocales = [
{
city: {
lng: 41.1534,
nameES: 'Hamburg',
nameFR: 'Hamburg',
nameIT: 'Hamburg',
nameEN: 'Hamburg',
type: 'place',
namePT: 'Hamburg',
nameRU: 'Хамбург',
nameDE: 'Hamburg',
nameNL: 'Hamburg',
name: 'Hamburg',
namePL: 'Hamburg',
id: 'place.5977106083398860',
lat: -74.5763,
},
state: {
namePT: 'Nova Jérsia',
nameRU: 'Нью-Джерси',
nameDE: 'New Jersey',
nameNL: 'New Jersey',
nameES: 'Nueva Jersey',
name: 'New Jersey',
namePL: 'New Jersey',
nameFR: 'New Jersey',
nameIT: 'New Jersey',
id: 'region.14919479731700330',
nameEN: 'New Jersey',
type: 'region',
},
country: {
namePT: 'Estados Unidos',
nameRU: 'Соединённые Штаты Америки',
nameDE: 'Vereinigte Staaten',
nameNL: 'Verenigde Staten van Amerika',
nameES: 'Estados Unidos',
namePL: 'Stany Zjednoczone',
name: 'United States of America',
nameFR: 'États-Unis',
nameIT: "Stati Uniti d'America",
id: 'country.9053006287256050',
nameEN: 'United States of America',
type: 'country',
},
},
]
beforeAll(() => {
const { server } = createServer({
context: () => {
return {
user: authenticatedUser,
neode,
driver,
}
},
})
mutate = createTestClient(server).mutate
})
beforeEach(() => {
variables = {}
authenticatedUser = null
})
afterEach(() => {
factory.cleanDatabase()
})
describe('userMiddleware', () => {
describe('SignupVerification', () => {
beforeEach(async () => {
variables = {
...variables,
name: 'John Doe',
password: '123',
email: 'john@example.org',
nonce: '123456',
termsAndConditionsAgreedVersion: '0.1.0',
locationName: 'Hamburg, New Jersey, United States of America',
}
const args = {
email: 'john@example.org',
nonce: '123456',
}
await neode.model('EmailAddress').create(args)
})
it('creates a Location node with localised city/state/country names', async () => {
await mutate({ mutation: signupVerificationMutation, variables })
const locations = await neode.cypher(
`MATCH (city:Location)-[:IS_IN]->(state:Location)-[:IS_IN]->(country:Location) return city, state, country`,
)
expect(
locations.records.map(record => {
return {
city: record.get('city').properties,
state: record.get('state').properties,
country: record.get('country').properties,
}
}),
).toEqual(newlyCreatedNodesWithLocales)
})
})
describe('UpdateUser', () => {
let user, userParams
beforeEach(async () => {
newlyCreatedNodesWithLocales = [
{
city: {
lng: 53.55,
nameES: 'Hamburgo',
nameFR: 'Hambourg',
nameIT: 'Amburgo',
nameEN: 'Hamburg',
type: 'region',
namePT: 'Hamburgo',
nameRU: 'Гамбург',
nameDE: 'Hamburg',
nameNL: 'Hamburg',
namePL: 'Hamburg',
name: 'Hamburg',
id: 'region.10793468240398860',
lat: 10,
},
country: {
namePT: 'Alemanha',
nameRU: 'Германия',
nameDE: 'Deutschland',
nameNL: 'Duitsland',
nameES: 'Alemania',
name: 'Germany',
namePL: 'Niemcy',
nameFR: 'Allemagne',
nameIT: 'Germania',
id: 'country.10743216036480410',
nameEN: 'Germany',
type: 'country',
},
},
]
userParams = {
id: 'updating-user',
}
user = await factory.create('User', userParams)
authenticatedUser = await user.toJson()
})
it('creates a Location node with localised city/state/country names', async () => {
variables = {
...variables,
id: 'updating-user',
name: 'Updating user',
locationName: 'Hamburg, Germany',
}
await mutate({ mutation: updateUserMutation, variables })
const locations = await neode.cypher(
`MATCH (city:Location)-[:IS_IN]->(country:Location) return city, country`,
)
expect(
locations.records.map(record => {
return {
city: record.get('city').properties,
country: record.get('country').properties,
}
}),
).toEqual(newlyCreatedNodesWithLocales)
})
})
})

View File

@ -128,9 +128,11 @@ export const validateNotifyUsers = async (label, reason) => {
} }
const validateUpdateUser = async (resolve, root, params, context, info) => { const validateUpdateUser = async (resolve, root, params, context, info) => {
if (!params.name || params.name.length < USERNAME_MIN_LENGTH) const { name } = params
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} character long!`) if (typeof name === 'string' && name.trim().length > USERNAME_MIN_LENGTH)
return resolve(root, params, context, info) return resolve(root, params, context, info)
if (typeof name !== 'string' || name.trim().length < USERNAME_MIN_LENGTH)
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} character long!`)
} }
export default { export default {

View File

@ -26,7 +26,7 @@ enum _UserOrdering {
type User { type User {
id: ID! id: ID!
actorId: String actorId: String
name: String name: String!
email: String! @cypher(statement: "MATCH (this)-[: PRIMARY_EMAIL]->(e: EmailAddress) RETURN e.email") email: String! @cypher(statement: "MATCH (this)-[: PRIMARY_EMAIL]->(e: EmailAddress) RETURN e.email")
slug: String! slug: String!
avatar: String avatar: String

View File

@ -46,7 +46,7 @@
<script> <script>
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import { allowEmbedIframesMutation } from '~/graphql/User.js' import { updateUserMutation } from '~/graphql/User.js'
export default { export default {
name: 'embed-component', name: 'embed-component',
@ -129,9 +129,10 @@ export default {
async updateEmbedSettings(allowEmbedIframes) { async updateEmbedSettings(allowEmbedIframes) {
try { try {
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: allowEmbedIframesMutation(), mutation: updateUserMutation(),
variables: { variables: {
id: this.currentUser.id, id: this.currentUser.id,
name: this.currentUser.name,
allowEmbedIframes, allowEmbedIframes,
}, },
update: (store, { data: { UpdateUser } }) => { update: (store, { data: { UpdateUser } }) => {

View File

@ -140,23 +140,35 @@ export const unfollowUserMutation = i18n => {
` `
} }
export const allowEmbedIframesMutation = () => { export const updateUserMutation = () => {
return gql` return gql`
mutation($id: ID!, $allowEmbedIframes: Boolean) { mutation(
UpdateUser(id: $id, allowEmbedIframes: $allowEmbedIframes) { $id: ID!
$slug: String
$name: String
$locationName: String
$about: String
$allowEmbedIframes: Boolean
$locale: String
) {
UpdateUser(
id: $id
slug: $slug
name: $name
locationName: $locationName
about: $about
allowEmbedIframes: $allowEmbedIframes
showShoutsPublicly: $showShoutsPublicly
locale: $locale
) {
id id
slug
name
locationName
about
allowEmbedIframes allowEmbedIframes
}
}
`
}
export const showShoutsPubliclyMutation = () => {
return gql`
mutation($id: ID!, $showShoutsPublicly: Boolean) {
UpdateUser(id: $id, showShoutsPublicly: $showShoutsPublicly) {
id
showShoutsPublicly showShoutsPublicly
locale
} }
} }
` `
@ -169,14 +181,3 @@ export const checkSlugAvailableQuery = gql`
} }
} }
` `
export const localeMutation = () => {
return gql`
mutation($id: ID!, $locale: String) {
UpdateUser(id: $id, locale: $locale) {
id
locale
}
}
`
}

View File

@ -37,7 +37,7 @@
<script> <script>
import axios from 'axios' import axios from 'axios'
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import { allowEmbedIframesMutation } from '~/graphql/User.js' import { updateUserMutation } from '~/graphql/User.js'
export default { export default {
head() { head() {
@ -69,9 +69,10 @@ export default {
async submit() { async submit() {
try { try {
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: allowEmbedIframesMutation(), mutation: updateUserMutation(),
variables: { variables: {
id: this.currentUser.id, id: this.currentUser.id,
name: this.currentUser.name,
allowEmbedIframes: !this.disabled, allowEmbedIframes: !this.disabled,
}, },
update: (store, { data: { UpdateUser } }) => { update: (store, { data: { UpdateUser } }) => {

View File

@ -41,40 +41,14 @@
</template> </template>
<script> <script>
import gql from 'graphql-tag'
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import { CancelToken } from 'axios' import { CancelToken } from 'axios'
import UniqueSlugForm from '~/components/utils/UniqueSlugForm' import UniqueSlugForm from '~/components/utils/UniqueSlugForm'
import { updateUserMutation } from '~/graphql/User'
let timeout let timeout
const mapboxToken = process.env.MAPBOX_TOKEN const mapboxToken = process.env.MAPBOX_TOKEN
/*
const query = gql`
query getUser($id: ID) {
User(id: $id) {
id
name
locationName
about
}
}
`
*/
const mutation = gql`
mutation($id: ID!, $slug: String, $name: String, $locationName: String, $about: String) {
UpdateUser(id: $id, slug: $slug, name: $name, locationName: $locationName, about: $about) {
id
slug
name
locationName
about
}
}
`
export default { export default {
data() { data() {
return { return {
@ -120,7 +94,7 @@ export default {
locationName = locationName && (locationName.label || locationName) locationName = locationName && (locationName.label || locationName)
try { try {
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation, mutation: updateUserMutation(),
variables: { variables: {
id: this.currentUser.id, id: this.currentUser.id,
name, name,

View File

@ -10,7 +10,7 @@
<script> <script>
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import { showShoutsPubliclyMutation } from '~/graphql/User' import { updateUserMutation } from '~/graphql/User'
export default { export default {
data() { data() {
@ -36,9 +36,10 @@ export default {
async submit() { async submit() {
try { try {
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: showShoutsPubliclyMutation(), mutation: updateUserMutation(),
variables: { variables: {
id: this.currentUser.id, id: this.currentUser.id,
name: this.currentUser.name,
showShoutsPublicly: this.shoutsAllowed, showShoutsPublicly: this.shoutsAllowed,
}, },
update: (_, { data: { UpdateUser } }) => { update: (_, { data: { UpdateUser } }) => {