Skip to content

Commit

Permalink
[BUGFIX] [Admin] Gérer le cas des imports OIDC Providers suivants qui…
Browse files Browse the repository at this point in the history
… produisent des 500 (PIX-12333)

 #9038
  • Loading branch information
pix-service-auto-merge authored May 27, 2024
2 parents 9eda0c6 + c729eb6 commit bd6b0ae
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<p>{{t "components.administration.organizations-batch-update.description"}}</p>
<br />
<PixButtonUpload
@id="oidc-providers-file-upload"
@id="organizations-batch-update-file-upload"
@onChange={{this.updateOrganizationsInBatch}}
@variant="secondary"
accept=".csv"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';

/**
* @typedef {import ('../usecases/index.js').OidcProviderRepository} OidcProviderRepository
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* @module OidcProviderRepository
*/

import { knex } from '../../../../db/knex-database-connection.js';
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
import { AlreadyExistingEntityError } from '../../../shared/domain/errors.js';
import * as knexUtils from '../../../shared/infrastructure/utils/knex-utils.js';
import { OidcProvider } from '../../domain/models/OidcProvider.js';

/**
* @module OidcProviderRepository
*/

const OIDC_PROVIDERS_TABLE_NAME = 'oidc-providers';

/**
Expand Down Expand Up @@ -37,7 +39,15 @@ const create = async function (
dependencies = { domainTransaction: DomainTransaction.emptyTransaction() },
) {
const knexConn = dependencies.domainTransaction.knexTransaction ?? knex;
return knexConn(OIDC_PROVIDERS_TABLE_NAME).insert(oidcProviderProperties).returning('*');
try {
const result = await knexConn(OIDC_PROVIDERS_TABLE_NAME).insert(oidcProviderProperties).returning('*');
return result;
} catch (err) {
if (knexUtils.isUniqConstraintViolated(err)) {
throw new AlreadyExistingEntityError();
}
throw err;
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
import { oidcProviderRepository } from '../../../../../src/identity-access-management/infrastructure/repositories/oidc-provider-repository.js';
import { databaseBuilder, expect, knex } from '../../../../test-helper.js';
import { AlreadyExistingEntityError } from '../../../../../src/shared/domain/errors.js';
import { catchErr, databaseBuilder, expect, knex } from '../../../../test-helper.js';

describe('Integration | Identity Access Management | Infrastructure | Repositories | OidcProvider', function () {
describe('#create', function () {
it('stores an OIDC Provider in the database', async function () {
// given
const oidcProviderProperties = {
accessTokenLifespan: '7d',
clientId: 'client',
encryptedClientSecret: '#%@!!!!!!!!!!!!!',
shouldCloseSession: true,
identityProvider: 'OIDC_EXAMPLE_NET',
openidConfigurationUrl: 'https://oidc.example.net/.well-known/openid-configuration',
organizationName: 'OIDC Example',
redirectUri: 'https://app.dev.pix.org/connexion/oidc-example-net',
scope: 'openid profile',
slug: 'oidc-example-net',
source: 'oidcexamplenet',
};
context('when the OidcProvider doesn’t already exist', function () {
it('stores an OIDC Provider in the database', async function () {
// given
const oidcProviderProperties = {
accessTokenLifespan: '7d',
clientId: 'client',
encryptedClientSecret: '#%@!!!!!!!!!!!!!',
shouldCloseSession: true,
identityProvider: 'OIDC_EXAMPLE_NET',
openidConfigurationUrl: 'https://oidc.example.net/.well-known/openid-configuration',
organizationName: 'OIDC Example',
redirectUri: 'https://app.dev.pix.org/connexion/oidc-example-net',
scope: 'openid profile',
slug: 'oidc-example-net',
source: 'oidcexamplenet',
};

// when
const savedOidcProvider = await oidcProviderRepository.create(oidcProviderProperties);
// when
const savedOidcProvider = await oidcProviderRepository.create(oidcProviderProperties);

// then
const oidcProvider = await knex('oidc-providers').where({ identityProvider: 'OIDC_EXAMPLE_NET' }).first('id');
expect(oidcProvider.id).to.equal(savedOidcProvider[0].id);
// then
const oidcProvider = await knex('oidc-providers').where({ identityProvider: 'OIDC_EXAMPLE_NET' }).first('id');
expect(oidcProvider.id).to.equal(savedOidcProvider[0].id);
});
});

context('when the OidcProvider already exists', function () {
it('throws an AlreadyExistingEntityError', async function () {
// given
const buildOidcProviderProperties = {
accessTokenLifespan: '7d',
clientId: 'client',
clientSecret: 'plainTextSecret',
shouldCloseSession: true,
identityProvider: 'OIDC_EXAMPLE_NET',
openidConfigurationUrl: 'https://oidc.example.net/.well-known/openid-configuration',
organizationName: 'OIDC Example',
redirectUri: 'https://app.dev.pix.org/connexion/oidc-example-net',
scope: 'openid profile',
slug: 'oidc-example-net',
source: 'oidcexamplenet',
};
await databaseBuilder.factory.buildOidcProvider(buildOidcProviderProperties);
await databaseBuilder.commit();

// eslint-disable-next-line no-unused-vars
const { clientSecret, ...oidcProviderProperties } = buildOidcProviderProperties;
oidcProviderProperties.encryptedClientSecret = '#%@!!!!!!!!!!!!!';

// when
const error = await catchErr(oidcProviderRepository.create)(oidcProviderProperties);

// then
expect(error).to.be.instanceOf(AlreadyExistingEntityError);
});
});
});

Expand Down

0 comments on commit bd6b0ae

Please sign in to comment.