194 lines
6.7 KiB
TypeScript
194 lines
6.7 KiB
TypeScript
import { AuthorizationRole } from './authorization-role.aggregate'
|
|
import { UserId, AdminUserId } from '@/domain/value-objects'
|
|
import { RoleType, AuthorizationStatus, MonthlyTargetType } from '@/domain/enums'
|
|
import { DomainError } from '@/shared/exceptions'
|
|
|
|
describe('AuthorizationRole Aggregate', () => {
|
|
describe('createCommunityAuth', () => {
|
|
it('should create community authorization', () => {
|
|
const auth = AuthorizationRole.createCommunityAuth({
|
|
userId: UserId.create('user-1', '1'),
|
|
communityName: '量子社区',
|
|
})
|
|
|
|
expect(auth.roleType).toBe(RoleType.COMMUNITY)
|
|
expect(auth.status).toBe(AuthorizationStatus.PENDING)
|
|
expect(auth.displayTitle).toBe('量子社区')
|
|
expect(auth.benefitActive).toBe(false)
|
|
expect(auth.getInitialTarget()).toBe(10)
|
|
expect(auth.domainEvents.length).toBe(1)
|
|
expect(auth.domainEvents[0].eventType).toBe('authorization.community.requested')
|
|
})
|
|
})
|
|
|
|
describe('createAuthProvinceCompany', () => {
|
|
it('should create auth province company authorization', () => {
|
|
const auth = AuthorizationRole.createAuthProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
})
|
|
|
|
expect(auth.roleType).toBe(RoleType.AUTH_PROVINCE_COMPANY)
|
|
expect(auth.status).toBe(AuthorizationStatus.PENDING)
|
|
expect(auth.displayTitle).toBe('授权湖南省')
|
|
expect(auth.benefitActive).toBe(false)
|
|
expect(auth.getInitialTarget()).toBe(500)
|
|
expect(auth.requireLocalPercentage).toBe(5.0)
|
|
expect(auth.needsLadderAssessment()).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('createAuthCityCompany', () => {
|
|
it('should create auth city company authorization', () => {
|
|
const auth = AuthorizationRole.createAuthCityCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
cityCode: '430100',
|
|
cityName: '长沙市',
|
|
})
|
|
|
|
expect(auth.roleType).toBe(RoleType.AUTH_CITY_COMPANY)
|
|
expect(auth.status).toBe(AuthorizationStatus.PENDING)
|
|
expect(auth.displayTitle).toBe('授权长沙市')
|
|
expect(auth.benefitActive).toBe(false)
|
|
expect(auth.getInitialTarget()).toBe(100)
|
|
})
|
|
})
|
|
|
|
describe('createProvinceCompany', () => {
|
|
it('should create official province company with active benefits', () => {
|
|
const adminId = AdminUserId.create('admin-1', '101')
|
|
const auth = AuthorizationRole.createProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
adminId,
|
|
})
|
|
|
|
expect(auth.roleType).toBe(RoleType.PROVINCE_COMPANY)
|
|
expect(auth.status).toBe(AuthorizationStatus.AUTHORIZED)
|
|
expect(auth.displayTitle).toBe('湖南省')
|
|
expect(auth.benefitActive).toBe(true)
|
|
expect(auth.getInitialTarget()).toBe(0) // No initial target for official company
|
|
})
|
|
})
|
|
|
|
describe('activateBenefit', () => {
|
|
it('should activate benefit and emit event', () => {
|
|
const auth = AuthorizationRole.createCommunityAuth({
|
|
userId: UserId.create('user-1', '1'),
|
|
communityName: '量子社区',
|
|
})
|
|
auth.clearDomainEvents()
|
|
|
|
auth.activateBenefit()
|
|
|
|
expect(auth.benefitActive).toBe(true)
|
|
expect(auth.status).toBe(AuthorizationStatus.AUTHORIZED)
|
|
expect(auth.currentMonthIndex).toBe(1)
|
|
expect(auth.domainEvents.length).toBe(1)
|
|
expect(auth.domainEvents[0].eventType).toBe('authorization.benefit.activated')
|
|
})
|
|
|
|
it('should throw error if already active', () => {
|
|
const auth = AuthorizationRole.createProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
adminId: AdminUserId.create('admin-1', '101'),
|
|
})
|
|
|
|
expect(() => auth.activateBenefit()).toThrow(DomainError)
|
|
})
|
|
})
|
|
|
|
describe('deactivateBenefit', () => {
|
|
it('should deactivate benefit and reset month index', () => {
|
|
const auth = AuthorizationRole.createProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
adminId: AdminUserId.create('admin-1', '101'),
|
|
})
|
|
auth.clearDomainEvents()
|
|
|
|
auth.deactivateBenefit('考核不达标')
|
|
|
|
expect(auth.benefitActive).toBe(false)
|
|
expect(auth.currentMonthIndex).toBe(0)
|
|
expect(auth.domainEvents.length).toBe(1)
|
|
expect(auth.domainEvents[0].eventType).toBe('authorization.benefit.deactivated')
|
|
})
|
|
})
|
|
|
|
describe('revoke', () => {
|
|
it('should revoke authorization', () => {
|
|
const auth = AuthorizationRole.createProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
adminId: AdminUserId.create('admin-1', '101'),
|
|
})
|
|
auth.clearDomainEvents()
|
|
|
|
auth.revoke(AdminUserId.create('admin-2', '102'), '违规操作')
|
|
|
|
expect(auth.status).toBe(AuthorizationStatus.REVOKED)
|
|
expect(auth.benefitActive).toBe(false)
|
|
expect(auth.revokeReason).toBe('违规操作')
|
|
expect(auth.domainEvents.length).toBe(1)
|
|
expect(auth.domainEvents[0].eventType).toBe('authorization.role.revoked')
|
|
})
|
|
|
|
it('should throw error if already revoked', () => {
|
|
const auth = AuthorizationRole.createProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
adminId: AdminUserId.create('admin-1', '101'),
|
|
})
|
|
auth.revoke(AdminUserId.create('admin-2', '102'), '违规操作')
|
|
|
|
expect(() => auth.revoke(AdminUserId.create('admin-3', '103'), '再次撤销')).toThrow(
|
|
DomainError,
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('exemptLocalPercentageCheck', () => {
|
|
it('should exempt from percentage check', () => {
|
|
const auth = AuthorizationRole.createAuthProvinceCompany({
|
|
userId: UserId.create('user-1', '1'),
|
|
provinceCode: '430000',
|
|
provinceName: '湖南省',
|
|
})
|
|
|
|
expect(auth.exemptFromPercentageCheck).toBe(false)
|
|
expect(auth.needsLocalPercentageCheck()).toBe(true)
|
|
|
|
auth.exemptLocalPercentageCheck(AdminUserId.create('admin-1', '101'))
|
|
|
|
expect(auth.exemptFromPercentageCheck).toBe(true)
|
|
expect(auth.needsLocalPercentageCheck()).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('incrementMonthIndex', () => {
|
|
it('should increment month index', () => {
|
|
const auth = AuthorizationRole.createCommunityAuth({
|
|
userId: UserId.create('user-1', '1'),
|
|
communityName: '量子社区',
|
|
})
|
|
auth.activateBenefit()
|
|
|
|
expect(auth.currentMonthIndex).toBe(1)
|
|
|
|
auth.incrementMonthIndex()
|
|
expect(auth.currentMonthIndex).toBe(2)
|
|
|
|
auth.incrementMonthIndex()
|
|
expect(auth.currentMonthIndex).toBe(3)
|
|
})
|
|
})
|
|
})
|