Skip to content

Commit

Permalink
Improv algo (#48)
Browse files Browse the repository at this point in the history
* suppression du badge de build status de Travis CI

* Rewrite tests to be more demanding

* Fix test and bahevior when there is no CodNatJurBenefCA to anonymize the LibOrgaBenef

* bump xmldom

* Change occultation string to something more generic
Also, make it a argument to the anonymize function with a default value

* PJRef > NomPJ elements are anonymized

* Occultation of all Champ_Editeurs

* anonymise all <Proprietaire>s in all <MEMBREASA>s

* 1.2.0
  • Loading branch information
DavidBruant committed Apr 7, 2021
1 parent 07eb4f2 commit 2302188
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# anonymisation-document-budgetaire [![Build Status](https://travis-ci.org/dtc-innovation/anonymisation-document-budgetaire.svg?branch=master)](https://travis-ci.org/dtc-innovation/anonymisation-document-budgetaire)
# anonymisation-document-budgetaire

Outil d'anonymisation de fichiers DocumentBudgetaire

Expand Down
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
const ANONYMIZED_NAME = "Nom anonymisé";

// http://odm-budgetaire.org/doc-schema/CommunAnnexe_xsd_Complex_Type_ATCodNatJurBenef.html#ATCodNatJurBenef_V
const CodNatJurBenefPersonnesPhysiques = 'P3';

export default function(doc){
export default function(doc, OCCULTATION_STRING = "Occultation Article L311-7 CRPA"){
const concours = Array.from(doc.getElementsByTagName('CONCOURS'));

concours.forEach(c => {
const natJurEl = c.getElementsByTagName('CodNatJurBenefCA')[0];

if(natJurEl && natJurEl.getAttribute('V') === CodNatJurBenefPersonnesPhysiques){
if(!natJurEl || natJurEl.getAttribute('V') === CodNatJurBenefPersonnesPhysiques){
const libOrgaBenef = c.getElementsByTagName('LibOrgaBenef')[0];
libOrgaBenef.setAttribute('V', ANONYMIZED_NAME);
libOrgaBenef.setAttribute('V', OCCULTATION_STRING);
}
})

const prets = Array.from(doc.getElementsByTagName('PRET'))

prets.forEach(c => {
const nomBenefPret = c.getElementsByTagName('NomBenefPret')[0];

if(nomBenefPret){
nomBenefPret.setAttribute('V', ANONYMIZED_NAME);
nomBenefPret.setAttribute('V', OCCULTATION_STRING);
}
})

const BlocBudget = doc.getElementsByTagName('BlocBudget')[0];
const PJRefs = Array.from(BlocBudget.getElementsByTagName('PJRef'));
PJRefs.forEach(pjref => {
const NomPJ = pjref.getElementsByTagName('NomPJ')[0];

if(NomPJ){
NomPJ.setAttribute('V', OCCULTATION_STRING);
}
})

const Champ_Editeurs = Array.from(doc.getElementsByTagName('Champ_Editeur'));
Champ_Editeurs.forEach(ce => {
ce.setAttribute('V', OCCULTATION_STRING);
})

const MEMBREASAs = Array.from(doc.getElementsByTagName('MEMBREASA'))
MEMBREASAs.forEach(masa => {
const Proprietaire = masa.getElementsByTagName('Proprietaire')[0];

if(Proprietaire){
Proprietaire.setAttribute('V', OCCULTATION_STRING);
}
})
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anon-doc-budg",
"version": "1.1.1",
"version": "1.2.0",
"description": "Outil d'anonymisation de fichiers DocumentBudgetaire",
"type": "module",
"engines": {
Expand All @@ -23,7 +23,7 @@
"fs-extra": "^7.0.1",
"p-limit": "^3.1.0",
"xml-buffer-tostring": "^0.2.0",
"xmldom": "^0.1.27"
"xmldom": "^0.5.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
Expand Down
111 changes: 95 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import {DOMParser, XMLSerializer} from 'xmldom';

import anonymize from '../index.js';

const defaultBlocBudget = `<BlocBudget>
<NatDec V="09"/>
<Exer V="2016"/>
<BudgPrec V="1"/>
<ReprRes V="1"/>
<NatFonc V="3"/>
<CodTypBud V="P"/>
</BlocBudget>`

function makeDocBudg(annexes){

function makeDocBudg(annexes = '', blocBudget = defaultBlocBudget){
const xmlStr = `<?xml version="1.0" encoding="UTF-8"?>
<DocumentBudgetaire xsi:schemaLocation="http://www.minefi.gouv.fr/cp/demat/docbudgetaire Actes_budgetaires___Schema_Annexes_Bull_V15\DocumentBudgetaire.xsd" xmlns="http://www.minefi.gouv.fr/cp/demat/docbudgetaire" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<VersionSchema V="81"/>
Expand All @@ -23,14 +32,7 @@ function makeDocBudg(annexes){
<CodBud V="00"/>
<Nomenclature V="M14-M14_COM_500_3500"/>
</EnTeteBudget>
<BlocBudget>
<NatDec V="09"/>
<Exer V="2016"/>
<BudgPrec V="1"/>
<ReprRes V="1"/>
<NatFonc V="3"/>
<CodTypBud V="P"/>
</BlocBudget>
${blocBudget}
<InformationsGenerales/>
<LigneBudget>
<CodRD V="D"/>
Expand Down Expand Up @@ -76,7 +78,7 @@ describe('anonymize', () => {

anonymize(doc);

expect( doc.getElementsByTagName('LibOrgaBenef')[0].getAttribute('V') ).to.not.equal(NAME);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(NAME);
})

it('should anonymize the document if there are several physical person names', () => {
Expand Down Expand Up @@ -105,10 +107,10 @@ describe('anonymize', () => {
const libOrgaBenefs = doc.getElementsByTagName('LibOrgaBenef');

expect( libOrgaBenefs[0].getAttribute('V') ).to.equal( libOrgaBenefs[1].getAttribute('V') );
expect( libOrgaBenefs[0].getAttribute('V') ).to.not.equal( NAME_1 );
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(NAME_1);
})

it('should not do anything if there is no <CodNatJurBenefCA> in the <CONCOURS>', () => {
it('should anonymize if there is no <CodNatJurBenefCA> in the <CONCOURS>', () => {
const NAME = "Asso dtc";

const annexes = `<Annexes>
Expand All @@ -124,7 +126,7 @@ describe('anonymize', () => {

anonymize(doc);

expect( doc.getElementsByTagName('LibOrgaBenef')[0].getAttribute('V') ).to.equal(NAME);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(NAME);
})

it('should not do anything if there are subs, but no physical person', () => {
Expand Down Expand Up @@ -187,7 +189,7 @@ describe('anonymize', () => {

})

it(`should anonimize all <NomBenefPret>s in all <PRET>s`, () => {
it(`should anonymize all <NomBenefPret>s in all <PRET>s`, () => {
const NAME = 'David Bruant';

const annexes = `<Annexes>
Expand All @@ -207,9 +209,86 @@ describe('anonymize', () => {

anonymize(doc);

const nomBenefPret = doc.getElementsByTagName('NomBenefPret')[0].getAttribute('V')
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(NAME);
})


it('should occult all Budget > BlocBudget > PJRef > NomPJ[V]', () => {
const PJ_NAME_1 = "Yo.pdf";
const PJ_NAME_2 = "Document joint";

const blocBudget = `<BlocBudget>
<NatDec V="09"/>
<Exer V="2016"/>
<BudgPrec V="1"/>
<ReprRes V="1"/>
<NatFonc V="3"/>
<CodTypBud V="P"/>
<PJRef>
<NomPJ V="${PJ_NAME_1}"/>
</PJRef>
<PJRef>
<NomPJ V="${PJ_NAME_2}"/>
</PJRef>
</BlocBudget>`

const doc = makeDocBudg(undefined, blocBudget);

anonymize(doc);

expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(PJ_NAME_1);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(PJ_NAME_2);
})


it('should occult all Champ_Editeur[V]', () => {
const CHAMP_EDITEUR_1 = "srbdtyndu,yu";
const CHAMP_EDITEUR_2 = "16151651681653";
const CHAMP_EDITEUR_3 = "ù$*ù*ù$";

const annexes = `<Annexes>
<DATA_EMPRUNT>
<EMPRUNT>
<Champ_Editeur V="${CHAMP_EDITEUR_1}"/>
</EMPRUNT>
</DATA_EMPRUNT>
<DATA_TRESORERIE>
<TRESORERIE>
<Champ_Editeur V="${CHAMP_EDITEUR_2}"/>
</TRESORERIE>
</DATA_TRESORERIE>
<DATA_CHARGE>
<CHARGE>
<Champ_Editeur V="${CHAMP_EDITEUR_3}"/>
</CHARGE>
</DATA_CHARGE>
</Annexes>`;

const doc = makeDocBudg(annexes);

anonymize(doc);

expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(CHAMP_EDITEUR_1);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(CHAMP_EDITEUR_2);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(CHAMP_EDITEUR_3);
})

it(`should anonymize all <Proprietaire>s in all <MEMBREASA>s`, () => {
const NAME = 'David Bruant';

const annexes = `<Annexes>
<DATA_MEMBRESASA>
<MEMBREASA>
<Proprietaire V="${NAME}"/>
</MEMBREASA>
</DATA_MEMBRESASA>
</Annexes>`;

const doc = makeDocBudg(annexes);

anonymize(doc);

expect( nomBenefPret ).to.not.equal(NAME);
expect( (new XMLSerializer()).serializeToString(doc) ).to.not.include(NAME);
})

});
Expand Down

0 comments on commit 2302188

Please sign in to comment.