Skip to content

Commit

Permalink
refactor(condo): DOMA-10042 simplified test for duplicate tins, made …
Browse files Browse the repository at this point in the history
…more grouping for tests
  • Loading branch information
YEgorLu committed Sep 3, 2024
1 parent d46b9f1 commit e5a050a
Showing 1 changed file with 116 additions and 151 deletions.
267 changes: 116 additions & 151 deletions apps/condo/domains/resident/schema/SuggestServiceProviderService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
* Generated by `createservice organization.SuggestServiceProviderService --type queries`
*/

const { faker } = require('@faker-js/faker')
const { values } = require('lodash')

const {
expectToThrowAuthenticationErrorToResult,
expectToThrowAccessDeniedErrorToResult,
Expand All @@ -16,13 +13,13 @@ const {
TestUtils,
ResidentTestMixin,
MeterTestMixin,
AcquiringTestMixin,
} = require('@condo/domains/billing/utils/testSchema/testUtils')
const { RUSSIAN_COUNTRY, DEFAULT_ENGLISH_COUNTRY } = require('@condo/domains/common/constants/countries')
const { RUSSIAN_COUNTRY } = require('@condo/domains/common/constants/countries')
const { COLD_WATER_METER_RESOURCE_ID } = require('@condo/domains/meter/constants/constants')
const { createTestMeterResourceOwner, updateTestMeterResourceOwner, MeterResource, MeterResourceOwner } = require('@condo/domains/meter/utils/testSchema')
const { SERVICE_PROVIDER_TYPE } = require('@condo/domains/organization/constants/common')
const { createTestOrganization, updateTestOrganization, generateTin } = require('@condo/domains/organization/utils/testSchema')
const { createTestProperty, updateTestProperty } = require('@condo/domains/property/utils/testSchema')
const { generateTin } = require('@condo/domains/organization/utils/testSchema')
const { suggestServiceProviderByTestClient } = require('@condo/domains/resident/utils/testSchema')


Expand All @@ -39,19 +36,6 @@ const removeMeterResourceOwners = async (utils) => {
}
}

const generateServiceProviders = async (client, count, extraAttrs) => {
const serviceProviders = []
const attrs = {
type: SERVICE_PROVIDER_TYPE,
...extraAttrs,
}
for (let i = 0; i < count; i++) {
const serviceProvider = createTestOrganization(client, attrs).then(pair => pair[0])
serviceProviders.push(serviceProvider)
}
return Promise.all(serviceProviders)
}

describe('SuggestServiceProviderService', () => {

let utils
Expand All @@ -63,51 +47,59 @@ describe('SuggestServiceProviderService', () => {
})

describe('Permissions check', () => {
test('anonymous: can not execute', async () => {
await expectToThrowAuthenticationErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.anonymous, {
search: String(generateTin(RUSSIAN_COUNTRY)),

describe('Can not execute', () => {

test('anonymous: can not execute', async () => {
await expectToThrowAuthenticationErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.anonymous, {
search: String(generateTin(RUSSIAN_COUNTRY)),
})
})
})
})
test('user: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.user, {
search: String(generateTin(RUSSIAN_COUNTRY)),
test('user: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.user, {
search: String(generateTin(RUSSIAN_COUNTRY)),
})
})
})
})
test('employee: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.employee['billing'], {
search: String(generateTin(RUSSIAN_COUNTRY)),
test('employee: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.employee['billing'], {
search: String(generateTin(RUSSIAN_COUNTRY)),
})
})
})
})
test('service user: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.service, {
search: String(generateTin(RUSSIAN_COUNTRY)),
test('service user: can not execute', async () => {
await expectToThrowAccessDeniedErrorToResult(async () => {
await suggestServiceProviderByTestClient(utils.clients.service, {
search: String(generateTin(RUSSIAN_COUNTRY)),
})
})
})

})
test('admin: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.admin, {
search: utils.organization.tin.substr(0, 5),

describe('Can execute', () => {
test('admin: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.admin, {
search: utils.organization.tin.substr(0, 5),
})
expect(organizations).not.toHaveLength(0)
})
expect(organizations).not.toHaveLength(0)
})
test('support: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.support, {
search: utils.organization.tin.substr(0, 5),
test('support: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.support, {
search: utils.organization.tin.substr(0, 5),
})
expect(organizations).not.toHaveLength(0)
})
expect(organizations).not.toHaveLength(0)
})
test('resident: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: utils.organization.tin.substr(0, 5),
test('resident: can execute', async () => {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: utils.organization.tin.substr(0, 5),
})
expect(organizations).not.toHaveLength(0)
})
expect(organizations).not.toHaveLength(0)
})
})

Expand All @@ -124,118 +116,91 @@ describe('SuggestServiceProviderService', () => {
return [tin, name]
}

test('should find organization with finished acquiring context', async () => {
for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})
describe('Should find', () => {

test('should find organization with meter resource owning', async () => {
await utils.updateAcquiringContext({ status: CONTEXT_IN_PROGRESS_STATUS })
await addMeterResourceOwner(utils)
test('should find organization with finished acquiring context', async () => {
for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})

for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})
test('should find organization with meter resource owning', async () => {
await utils.updateAcquiringContext({ status: CONTEXT_IN_PROGRESS_STATUS })
await addMeterResourceOwner(utils)

test('should find organization with finished acquiring context and meter resource owning', async () => {
await addMeterResourceOwner(utils)
for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})

for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})
test('should find organization with finished acquiring context and meter resource owning', async () => {
await addMeterResourceOwner(utils)

for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})

test('should not find organization without acquiring context in Finished status and without meter owning', async () => {
await utils.updateAcquiringContext({ status: CONTEXT_IN_PROGRESS_STATUS })

for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).not.toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})

test('should merge organizations with same tin', async () => {
const tin = generateTin(faker.helpers.arrayElement([RUSSIAN_COUNTRY, DEFAULT_ENGLISH_COUNTRY]))
const serviceProviders = await generateServiceProviders(utils.clients.admin, 5, { tin: tin })
const names = serviceProviders.map(provider => provider.name)

const toCleanup = {
organizations: {
items: [...serviceProviders],
cleanupFn: (id) => updateTestOrganization(utils.clients.admin, id, { deletedAt: new Date().toISOString() }),
},
properties: {
items: [],
cleanupFn: (id) => updateTestProperty(utils.clients.admin, id, { deletedAt: new Date().toISOString() }),
},
meterResourceOwners: {
items: [],
cleanupFn: (id) => updateTestMeterResourceOwner(utils.clients.admin, id, { deletedAt: new Date().toISOString() }),
},
}

try {
const resource = await MeterResource.getOne(utils.clients.admin, { id: COLD_WATER_METER_RESOURCE_ID })
for (const serviceProvider of serviceProviders) {
const [organization] = await createTestOrganization(utils.clients.admin)
const [property] = await createTestProperty(utils.clients.admin, organization)
const [meterResourceOwner] = await createTestMeterResourceOwner(utils.clients.admin, serviceProvider, resource, {
address: property.address,
})
describe('Should not find', () => {

test('should not find organization without acquiring context in Finished status and without meter owning', async () => {
await utils.updateAcquiringContext({ status: CONTEXT_IN_PROGRESS_STATUS })

toCleanup.organizations.items.push(organization)
toCleanup.properties.items.push(property)
toCleanup.meterResourceOwners.items.push(meterResourceOwner)
for (const tinOrName of getPartialTinAndName()) {
const [organizations] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: tinOrName,
})
expect(organizations).not.toContainEqual({
tin: utils.organization.tin,
name: utils.organization.name,
})
}
})

const searchParams = [
tin.substring(0, 5),
names[0].substring(0, names[0].length / 2),
]
})

for (const searchParam of searchParams) {
const [suggestedProviders] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: searchParam,
})
const uniqueTins = new Set(suggestedProviders.map((provider) => provider.tin))
describe('Duplicates', () => {

test('should merge organizations with same tin', async () => {
const utils2 = new TestUtils([AcquiringTestMixin])
await utils2.init()
await utils2.updateOrganization({
tin: utils.organization.tin,
type: SERVICE_PROVIDER_TYPE,
})
await utils2.updateAcquiringContext({ status: CONTEXT_FINISHED_STATUS })

const [suggestedProviders] = await suggestServiceProviderByTestClient(utils.clients.resident, {
search: utils.organization.tin,
})
expect(utils.organization.tin).toEqual(utils2.organization.tin)
expect(utils.organization.id).not.toEqual(utils2.organization.id)
expect(suggestedProviders).toHaveLength(1)
})

expect(uniqueTins.size).toBeGreaterThanOrEqual(1)
expect(uniqueTins).toContain(tin)
}

} finally {
const cleanupPromises = values(toCleanup)
.flatMap(category =>
category.items.map(item => category.cleanupFn(item.id))
)
await Promise.all(cleanupPromises)
.catch(err => console.error('could not cleanup properly', err))
}
})

})

})

0 comments on commit e5a050a

Please sign in to comment.