diff --git a/backend/src/graphql/resolver/TransactionResolver.test.ts b/backend/src/graphql/resolver/TransactionResolver.test.ts index 857159a97..d130a802e 100644 --- a/backend/src/graphql/resolver/TransactionResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionResolver.test.ts @@ -13,6 +13,7 @@ import { Transaction } from '@entity/Transaction' import { User } from '@entity/User' import { ApolloServerTestClient } from 'apollo-server-testing' import { GraphQLError } from 'graphql' +import { v4 as uuidv4 } from 'uuid' import { cleanDB, testEnvironment } from '@test/helpers' import { logger } from '@test/testSetup' @@ -71,12 +72,8 @@ let fedForeignCom: DbFederatedCommunity describe('send coins', () => { beforeAll(async () => { - peter = await userFactory(testEnv, peterLustig) - bob = await userFactory(testEnv, bobBaumeister) - await userFactory(testEnv, stephenHawking) - await userFactory(testEnv, garrickOllivander) homeCom = DbCommunity.create() - homeCom.communityUuid = '7f474922-b6d8-4b64-8cd0-ebf0a1d875aa' + homeCom.communityUuid = uuidv4() homeCom.creationDate = new Date('2000-01-01') homeCom.description = 'homeCom description' homeCom.foreign = false @@ -87,7 +84,7 @@ describe('send coins', () => { homeCom = await DbCommunity.save(homeCom) foreignCom = DbCommunity.create() - foreignCom.communityUuid = '7f474922-b6d8-4b64-8cd0-cea0a1d875bb' + foreignCom.communityUuid = uuidv4() foreignCom.creationDate = new Date('2000-06-06') foreignCom.description = 'foreignCom description' foreignCom.foreign = true @@ -98,6 +95,11 @@ describe('send coins', () => { foreignCom.authenticatedAt = new Date('2000-06-12') foreignCom = await DbCommunity.save(foreignCom) + peter = await userFactory(testEnv, peterLustig) + bob = await userFactory(testEnv, bobBaumeister) + await userFactory(testEnv, stephenHawking) + await userFactory(testEnv, garrickOllivander) + bobData = { email: 'bob@baumeister.de', password: 'Aa12345_', diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 5ad05fc05..a9c50553e 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -5,6 +5,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-argument */ import { Connection } from '@dbTools/typeorm' +import { Community as DbCommunity } from '@entity/Community' import { Event as DbEvent } from '@entity/Event' import { TransactionLink } from '@entity/TransactionLink' import { User } from '@entity/User' @@ -2502,6 +2503,39 @@ describe('UserResolver', () => { }) describe('user', () => { + let homeCom1: DbCommunity + let foreignCom1: DbCommunity + + beforeAll(async () => { + homeCom1 = DbCommunity.create() + homeCom1.foreign = false + homeCom1.url = 'http://localhost/api' + homeCom1.publicKey = Buffer.from('publicKey-HomeCommunity') + homeCom1.privateKey = Buffer.from('privateKey-HomeCommunity') + homeCom1.communityUuid = uuidv4() // 'HomeCom-UUID' + homeCom1.authenticatedAt = new Date() + homeCom1.name = 'HomeCommunity-name' + homeCom1.description = 'HomeCommunity-description' + homeCom1.creationDate = new Date() + await DbCommunity.insert(homeCom1) + + foreignCom1 = DbCommunity.create() + foreignCom1.foreign = true + foreignCom1.url = 'http://stage-2.gradido.net/api' + foreignCom1.publicKey = Buffer.from('publicKey-stage-2_Community') + foreignCom1.privateKey = Buffer.from('privateKey-stage-2_Community') + foreignCom1.communityUuid = uuidv4() // 'Stage2-Com-UUID' + foreignCom1.authenticatedAt = new Date() + foreignCom1.name = 'Stage-2_Community-name' + foreignCom1.description = 'Stage-2_Community-description' + foreignCom1.creationDate = new Date() + await DbCommunity.insert(foreignCom1) + }) + + afterAll(async () => { + await DbCommunity.clear() + }) + beforeEach(() => { jest.clearAllMocks() }) @@ -2548,6 +2582,7 @@ describe('UserResolver', () => { query: userQuery, variables: { identifier: 'identifier_is_no_valid_alias!', + communityIdentifier: homeCom1.communityUuid, }, }), ).resolves.toEqual( @@ -2569,14 +2604,44 @@ describe('UserResolver', () => { query: userQuery, variables: { identifier: uuid, + communityIdentifier: homeCom1.communityUuid, }, }), ).resolves.toEqual( expect.objectContaining({ - errors: [new GraphQLError('No user found to given identifier')], + errors: [new GraphQLError('No user found to given identifier(s)')], }), ) - expect(logger.error).toBeCalledWith('No user found to given identifier', uuid) + expect(logger.error).toBeCalledWith( + 'No user found to given identifier(s)', + uuid, + homeCom1.communityUuid, + ) + }) + }) + + describe('identifier is found via email, but not matching community', () => { + it('returns user', async () => { + await expect( + query({ + query: userQuery, + variables: { + identifier: 'bibi@bloxberg.de', + communityIdentifier: foreignCom1.communityUuid, + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [ + new GraphQLError('Found user to given contact, but belongs to other community'), + ], + }), + ) + expect(logger.error).toBeCalledWith( + 'Found user to given contact, but belongs to other community', + 'bibi@bloxberg.de', + foreignCom1.communityUuid, + ) }) }) @@ -2587,15 +2652,16 @@ describe('UserResolver', () => { query: userQuery, variables: { identifier: 'bibi@bloxberg.de', + communityIdentifier: homeCom1.communityUuid, }, }), ).resolves.toEqual( expect.objectContaining({ data: { - user: { + user: expect.objectContaining({ firstName: 'Bibi', lastName: 'Bloxberg', - }, + }), }, errors: undefined, }), @@ -2610,15 +2676,16 @@ describe('UserResolver', () => { query: userQuery, variables: { identifier: user.gradidoID, + communityIdentifier: homeCom1.communityUuid, }, }), ).resolves.toEqual( expect.objectContaining({ data: { - user: { + user: expect.objectContaining({ firstName: 'Bibi', lastName: 'Bloxberg', - }, + }), }, errors: undefined, }), @@ -2633,15 +2700,16 @@ describe('UserResolver', () => { query: userQuery, variables: { identifier: 'bibi', + communityIdentifier: homeCom1.communityUuid, }, }), ).resolves.toEqual( expect.objectContaining({ data: { - user: { + user: expect.objectContaining({ firstName: 'Bibi', lastName: 'Bloxberg', - }, + }), }, errors: undefined, }), diff --git a/backend/src/graphql/resolver/util/communities.ts b/backend/src/graphql/resolver/util/communities.ts index fd52f0c06..0c0023a19 100644 --- a/backend/src/graphql/resolver/util/communities.ts +++ b/backend/src/graphql/resolver/util/communities.ts @@ -17,7 +17,7 @@ export async function isHomeCommunity(communityIdentifier: string): Promise { return await DbCommunity.findOneOrFail({ - where: [{ foreign: true }], + where: [{ foreign: false }], }) } diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index 3fa5591a2..65b0ff3bb 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -5,6 +5,7 @@ import { ApolloServerTestClient } from 'apollo-server-testing' import { RoleNames } from '@enum/RoleNames' +import { getHomeCommunity } from '@/graphql/resolver/util/communities' import { setUserRole } from '@/graphql/resolver/util/modifyUserRole' import { createUser, setPassword } from '@/seeds/graphql/mutations' import { UserInterface } from '@/seeds/users/UserInterface' @@ -43,6 +44,15 @@ export const userFactory = async ( } await dbUser.save() } + try { + const homeCom = await getHomeCommunity() + if (homeCom.communityUuid) { + dbUser.communityUuid = homeCom.communityUuid + await User.save(dbUser) + } + } catch (err) { + // no homeCommunity exists + } // get last changes of user from database dbUser = await User.findOneOrFail({