solve tests for updateUserInfo with gmsLocation

This commit is contained in:
Claus-Peter Huebner 2024-02-07 21:03:46 +01:00
parent 7c0bc5ae35
commit 028337b291
7 changed files with 69 additions and 102 deletions

View File

@ -15,6 +15,8 @@ import { ApolloServerTestClient } from 'apollo-server-testing'
import { GraphQLError } from 'graphql'
import { v4 as uuidv4 } from 'uuid'
import { GmsPublishLocationType } from '@enum/GmsPublishLocationType'
import { GmsPublishNameType } from '@enum/GmsPublishNameType'
import { cleanDB, testEnvironment } from '@test/helpers'
import { logger } from '@test/testSetup'
@ -523,6 +525,9 @@ describe('send coins', () => {
mutation: updateUserInfos,
variables: {
alias: 'bob',
gmsAllowed: true,
gmsPublishName: GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS,
gmsPublishLocation: GmsPublishLocationType.GMS_LOCATION_TYPE_RANDOM,
},
})
await mutate({

View File

@ -71,6 +71,8 @@ import { stephenHawking } from '@/seeds/users/stephen-hawking'
import { printTimeDuration } from '@/util/time'
import { objectValuesToArray } from '@/util/utilities'
import { Location2Point } from './util/Location2Point'
jest.mock('@/emails/sendEmailVariants', () => {
const originalModule = jest.requireActual('@/emails/sendEmailVariants')
return {
@ -1333,27 +1335,15 @@ describe('UserResolver', () => {
})
})
describe.only('with gms location', () => {
describe('with gms location', () => {
const loc = new Location()
loc.longitude = 9.573224
loc.latitude = 49.679437
console.log('with gms location:', loc)
it('updates the user in DB', async () => {
const usr = await User.find()
console.log('usr=', usr)
await mutate({
mutation: updateUserInfos,
variables: {
/*
firstName: usr[0].firstName,
lastName: usr[0].lastName,
alias: usr[0].alias,
language: usr[0].language,
password: usr[0].password,
passwordNew: usr[0].password,
hideAmountGDD: usr[0].hideAmountGDD,
hideAmountGDT: usr[0].hideAmountGDT,
*/
gmsAllowed: true,
gmsPublishName: GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS,
gmsLocation: loc,
@ -1364,7 +1354,7 @@ describe('UserResolver', () => {
expect.objectContaining({
gmsAllowed: true,
gmsPublishName: GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS,
location: loc,
location: Location2Point(loc),
gmsPublishLocation: GmsPublishLocationType.GMS_LOCATION_TYPE_RANDOM,
}),
])

View File

@ -71,10 +71,10 @@ import { getUserCreations } from './util/creations'
import { findUserByIdentifier } from './util/findUserByIdentifier'
import { findUsers } from './util/findUsers'
import { getKlicktippState } from './util/getKlicktippState'
import { Location2Point } from './util/Location2Point'
import { setUserRole, deleteUserRole } from './util/modifyUserRole'
import { sendUserToGms } from './util/sendUserToGms'
import { validateAlias } from './util/validateAlias'
import { Location2Point } from './util/Location2Point'
const LANGUAGES = ['de', 'en', 'es', 'fr', 'nl']
const DEFAULT_LANGUAGE = 'de'
@ -556,83 +556,72 @@ export class UserResolver {
}: UpdateUserInfosArgs,
@Ctx() context: Context,
): Promise<boolean> {
console.log(
`updateUserInfos(${firstName}, ${lastName}, ${alias}, ${language}, ${password}, ${passwordNew}, ${hideAmountGDD}, ${hideAmountGDT}, ${gmsAllowed}, ${gmsPublishName}, ${gmsLocation}, ${gmsPublishLocation})`,
)
logger.info(
`updateUserInfos(${firstName}, ${lastName}, ${alias}, ${language}, ***, ***, ${hideAmountGDD}, ${hideAmountGDT}, ${gmsAllowed}, ${gmsPublishName}, ${gmsLocation}, ${gmsPublishLocation})...`,
)
const user = getUser(context)
console.log('getUser:', user)
try {
if (firstName) {
user.firstName = firstName
}
if (lastName) {
user.lastName = lastName
}
if (alias && (await validateAlias(alias))) {
user.alias = alias
}
if (language) {
if (!isLanguage(language)) {
throw new LogError('Given language is not a valid language', language)
}
user.language = language
i18n.setLocale(language)
}
if (password && passwordNew) {
// Validate Password
if (!isValidPassword(passwordNew)) {
throw new LogError(
'Please enter a valid password with at least 8 characters, upper and lower case letters, at least one number and one special character!',
)
}
if (!verifyPassword(user, password)) {
throw new LogError(`Old password is invalid`)
}
// Save new password hash and newly encrypted private key
user.passwordEncryptionType = PasswordEncryptionType.GRADIDO_ID
user.password = encryptPassword(user, passwordNew)
}
// Save hideAmountGDD value
if (hideAmountGDD !== undefined) {
user.hideAmountGDD = hideAmountGDD
}
// Save hideAmountGDT value
if (hideAmountGDT !== undefined) {
user.hideAmountGDT = hideAmountGDT
}
console.log('gmsAllowed:', user.gmsAllowed, gmsAllowed)
user.gmsAllowed = gmsAllowed
console.log('gmsPublishName:', user.gmsPublishName, gmsPublishName)
user.gmsPublishName = gmsPublishName
if (gmsLocation) {
console.log('1. gmsLocation:', user.location, gmsLocation)
user.location = Location2Point(gmsLocation)
console.log('2. gmsLocation:', user.location)
}
console.log('gmsPublishLocation:', user.gmsPublishLocation, gmsPublishLocation)
user.gmsPublishLocation = gmsPublishLocation
console.log('vor commit user:', user)
} catch (err) {
console.log('error:', err)
// try {
if (firstName) {
user.firstName = firstName
}
if (lastName) {
user.lastName = lastName
}
if (alias && (await validateAlias(alias))) {
user.alias = alias
}
if (language) {
if (!isLanguage(language)) {
throw new LogError('Given language is not a valid language', language)
}
user.language = language
i18n.setLocale(language)
}
if (password && passwordNew) {
// Validate Password
if (!isValidPassword(passwordNew)) {
throw new LogError(
'Please enter a valid password with at least 8 characters, upper and lower case letters, at least one number and one special character!',
)
}
if (!verifyPassword(user, password)) {
throw new LogError(`Old password is invalid`)
}
// Save new password hash and newly encrypted private key
user.passwordEncryptionType = PasswordEncryptionType.GRADIDO_ID
user.password = encryptPassword(user, passwordNew)
}
// Save hideAmountGDD value
if (hideAmountGDD !== undefined) {
user.hideAmountGDD = hideAmountGDD
}
// Save hideAmountGDT value
if (hideAmountGDT !== undefined) {
user.hideAmountGDT = hideAmountGDT
}
user.gmsAllowed = gmsAllowed
user.gmsPublishName = gmsPublishName
if (gmsLocation) {
user.location = Location2Point(gmsLocation)
}
user.gmsPublishLocation = gmsPublishLocation
// } catch (err) {
// console.log('error:', err)
// }
const queryRunner = getConnection().createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction('REPEATABLE READ')
try {
await queryRunner.manager.save(user).catch((error) => {
console.log('Error on saving user:', error)
throw new LogError('Error saving user', error)
})

View File

@ -3,7 +3,6 @@ import { Point } from '@dbTools/typeorm'
import { Location } from '@model/Location'
export function Location2Point(location: Location): Point {
console.log('in Location2Point:', location)
let pointStr: string
if (location.longitude && location.latitude) {
pointStr = '{ "type": "Point", "coordinates": ['
@ -14,19 +13,15 @@ export function Location2Point(location: Location): Point {
} else {
pointStr = '{ "type": "Point", "coordinates": [] }'
}
console.log('pointStr:', pointStr)
const point = JSON.parse(pointStr) as Point
console.log('point:', point)
return point
}
export function Point2Location(point: Point): Location {
console.log('in Point2Location:', point)
const location = new Location()
if (point.type === 'Point' && point.coordinates.length === 2) {
location.longitude = point.coordinates[0]
location.latitude = point.coordinates[1]
}
console.log('location:', location)
return location
}

View File

@ -2,6 +2,7 @@
import { GraphQLScalarType, Kind } from 'graphql'
import { Location } from '@model/Location'
import { LogError } from '@/server/LogError'
export const LocationScalar = new GraphQLScalarType({
name: 'Location',
@ -9,37 +10,32 @@ export const LocationScalar = new GraphQLScalarType({
'The `Location` scalar type to represent longitude and latitude values of a geo location',
serialize(value: Location) {
console.log('serialize LocationScalar:', value)
return value
},
parseValue(value): Location {
console.log('parseValue LocationScalar:', value)
try {
const loc = new Location()
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
loc.longitude = value.longitude
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
loc.latitude = value.latitude
console.log('parsed:', loc)
return loc
} catch (err) {
console.log('Error:', err)
throw new LogError('Error:', err)
}
return new Location()
// return new Location()
},
parseLiteral(ast): Location {
console.log('parseLiteral LocationScalar:', ast)
if (ast.kind !== Kind.STRING) {
throw new TypeError(`${String(ast)} is not a valid Location value.`)
}
let loc = new Location()
try {
loc = JSON.parse(ast.value) as Location
console.log('parsed:', loc)
} catch (err) {
console.log('Error:', err)
throw new LogError('Error:', err)
}
return loc
},

View File

@ -16,10 +16,8 @@ export function isValidLocation(validationOptions?: ValidationOptions) {
validate(value: Location) {
// console.log('isValidLocation:', value, value.getPoint())
if (!value || Location2Point(value).type === 'Point') {
console.log('isValidLocation: true')
return true
}
console.log('isValidLocation: false')
return false
},
defaultMessage(args: ValidationArguments) {

View File

@ -8,12 +8,9 @@ import { ValueTransformer } from 'typeorm/decorator/options/ValueTransformer'
*/
export const GeometryTransformer: ValueTransformer = {
to: (geojson: Geometry): string | null => {
console.log('GeometryTransformer to: geojson=', geojson)
if (geojson) {
const wkxg = wkx_Geometry.parseGeoJSON(geojson)
console.log('GeometryTransformer to: wkxg=', wkxg)
const str = wkxg.toWkt()
console.log('GeometryTransformer to: str=', str)
return str
}
return null
@ -21,14 +18,11 @@ export const GeometryTransformer: ValueTransformer = {
from: (wkb: string): Record<string, any> | null => {
// wkb ? wkx_Geometry.parse(wkb).toGeoJSON() : undefined
console.log('GeometryTransformer from: wbk=', wkb)
if (!wkb) {
return null
}
const record = wkx_Geometry.parse(wkb)
console.log('GeometryTransformer from: record=', record)
const str = record.toGeoJSON()
console.log('GeometryTransformer from: str=', str)
return str
},
}