mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
added tests
This commit is contained in:
parent
63769e9baf
commit
3087e16e3e
3 changed files with 120 additions and 15 deletions
103
server/__tests__/auth.test.js
Normal file
103
server/__tests__/auth.test.js
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
const request = require('supertest');
|
||||||
|
const AuthConfig = require('../config/auth.js');
|
||||||
|
|
||||||
|
const mockConfig = {
|
||||||
|
"auth": {
|
||||||
|
"passportjs": [
|
||||||
|
{
|
||||||
|
"provider1": {
|
||||||
|
"type": "oauth",
|
||||||
|
"OAUTH_AUTHORIZATION_URL": "https://www.testurl.com/oauth2/authorize",
|
||||||
|
"OAUTH_TOKEN_URL": "https://www.testurl.com/oauth2/token",
|
||||||
|
"OAUTH_CLIENT_ID": "your_oauth_client_id",
|
||||||
|
"OAUTH_CLIENT_SECRET": "your_oauth_client_secret",
|
||||||
|
"OAUTH_CALLBACK_URL": "https://localhost:3000/auth/provider/callback",
|
||||||
|
"OAUTH_ADD_SCOPE": "scopes",
|
||||||
|
"OAUTH_ROLE_TEACHER_VALUE": "teacher-claim-value",
|
||||||
|
"OAUTH_ROLE_STUDENT_VALUE": "student-claim-value"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"provider2": {
|
||||||
|
"type": "oidc",
|
||||||
|
"OIDC_CLIENT_ID": "your_oidc_client_id",
|
||||||
|
"OIDC_CLIENT_SECRET": "your_oidc_client_secret",
|
||||||
|
"OIDC_ISSUER_URL": "https://your-issuer.com",
|
||||||
|
"OIDC_CALLBACK_URL": "http://localhost:3000/auth/oidc/callback"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"simple-login": {
|
||||||
|
"enabled": true,
|
||||||
|
"name": "provider3",
|
||||||
|
"SESSION_SECRET": "your_session_secret"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Créez une instance de AuthConfig en utilisant la configuration mockée
|
||||||
|
describe('AuthConfig Class Tests', () => {
|
||||||
|
let authConfigInstance;
|
||||||
|
|
||||||
|
// Initialisez l'instance avec la configuration mockée
|
||||||
|
beforeAll(() => {
|
||||||
|
authConfigInstance = new AuthConfig();
|
||||||
|
authConfigInstance.loadConfigTest(mockConfig); // On injecte la configuration mockée
|
||||||
|
});
|
||||||
|
|
||||||
|
it('devrait retourner la configuration PassportJS', () => {
|
||||||
|
const config = authConfigInstance.getPassportJSConfig();
|
||||||
|
expect(config).toHaveProperty('provider1');
|
||||||
|
expect(config).toHaveProperty('provider2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('devrait retourner la configuration Simple Login', () => {
|
||||||
|
const config = authConfigInstance.getSimpleLoginConfig();
|
||||||
|
expect(config).toHaveProperty('name', 'provider3');
|
||||||
|
expect(config).toHaveProperty('SESSION_SECRET', 'your_session_secret');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('devrait retourner les providers OAuth', () => {
|
||||||
|
const oauthProviders = authConfigInstance.getOAuthProviders();
|
||||||
|
expect(Array.isArray(oauthProviders)).toBe(true);
|
||||||
|
expect(oauthProviders.length).toBe(1); // Il y a un seul provider OAuth
|
||||||
|
expect(oauthProviders[0]).toHaveProperty('provider1');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('devrait valider la configuration des providers', () => {
|
||||||
|
expect(() => authConfigInstance.validateProvidersConfig()).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('devrait lever une erreur si une configuration manque', () => {
|
||||||
|
const invalidMockConfig = {
|
||||||
|
"auth": {
|
||||||
|
"passportjs": [
|
||||||
|
{
|
||||||
|
"provider1": {
|
||||||
|
"type": "oauth",
|
||||||
|
"OAUTH_CLIENT_ID": "your_oauth_client_id" // Il manque des champs nécessaires
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const instanceWithInvalidConfig = new AuthConfig();
|
||||||
|
instanceWithInvalidConfig.loadConfigTest(invalidMockConfig);
|
||||||
|
|
||||||
|
// Vérifiez que l'erreur est lancée avec les champs manquants corrects
|
||||||
|
expect(() => instanceWithInvalidConfig.validateProvidersConfig()).toThrow(
|
||||||
|
new Error(`Configuration invalide pour les providers suivants : [
|
||||||
|
{
|
||||||
|
"provider": "provider1",
|
||||||
|
"missingFields": [
|
||||||
|
"OAUTH_AUTHORIZATION_URL",
|
||||||
|
"OAUTH_TOKEN_URL",
|
||||||
|
"OAUTH_CLIENT_SECRET",
|
||||||
|
"OAUTH_CALLBACK_URL"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]`)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -2,25 +2,29 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const pathAuthConfig = './auth_config.json';
|
const pathAuthConfig = './auth_config.json';
|
||||||
|
|
||||||
|
const configPath = path.join(__dirname, pathAuthConfig);
|
||||||
|
|
||||||
class AuthConfig {
|
class AuthConfig {
|
||||||
|
|
||||||
constructor(configPath) {
|
config = null;
|
||||||
this.configPath = configPath;
|
|
||||||
this.config = this.loadConfig();
|
|
||||||
this.validateProvidersConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour lire le fichier de configuration JSON
|
// Méthode pour lire le fichier de configuration JSON
|
||||||
loadConfig() {
|
loadConfig() {
|
||||||
try {
|
try {
|
||||||
const configData = fs.readFileSync(this.configPath, 'utf-8');
|
const configData = fs.readFileSync(configPath, 'utf-8');
|
||||||
return JSON.parse(configData);
|
this.config = JSON.parse(configData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erreur lors de la lecture du fichier de configuration :", error);
|
console.error("Erreur lors de la lecture du fichier de configuration :", error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Méthode pour load le fichier de test
|
||||||
|
loadConfigTest(mockConfig) {
|
||||||
|
this.config = mockConfig;
|
||||||
|
}
|
||||||
|
|
||||||
// Méthode pour retourner la configuration des fournisseurs PassportJS
|
// Méthode pour retourner la configuration des fournisseurs PassportJS
|
||||||
getPassportJSConfig() {
|
getPassportJSConfig() {
|
||||||
if (this.config && this.config.auth && this.config.auth.passportjs) {
|
if (this.config && this.config.auth && this.config.auth.passportjs) {
|
||||||
|
|
@ -177,7 +181,4 @@ class AuthConfig {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utilisation de la classe ConfigManager
|
module.exports = AuthConfig;
|
||||||
const configPath = path.join(__dirname, pathAuthConfig);
|
|
||||||
const instance = new AuthConfig(configPath);
|
|
||||||
module.exports = instance;
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,18 @@
|
||||||
const authConfig = require('../config/auth.js');
|
const AuthConfig = require('../config/auth.js');
|
||||||
|
|
||||||
class authController {
|
class authController {
|
||||||
|
|
||||||
async getActive(req, res, next) {
|
async getActive(req, res, next) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const authActive = authConfig.getActiveAuth();
|
const authC = new AuthConfig();
|
||||||
|
authC.loadConfig();
|
||||||
|
|
||||||
|
const authActive = authC.getActiveAuth();
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
authActive
|
authActive
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json(response);
|
return res.json(response);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue