<commit_before>const { expect } = require('chai');
const { authenticate } = require('@feathersjs/authentication');
const { Configuration } = require('@schul-cloud/commons');

const app = require('../../../src/app');

class TestService {
	find(params) {
		return Promise.resolve({ success: true, params });
	}

	setup(localApp) {
		this.app = localApp;
	}
}

const testServiceHooks = {
	before: { all: [authenticate('api-key')] },
};

describe('api-key authentication strategy', () => {
	let server;
	const testRoute = `/foo${Date.now}`;
	let configBefore = null;

	before(async () => {
		app.use(testRoute, new TestService());
		app.service(testRoute).hooks(testServiceHooks);
		server = await app.listen(0);
		configBefore = Configuration.toObject();
		Configuration.set('CALENDAR_API_KEY', 'validKey');
	});

	after(async () => {
		Configuration.reset(configBefore);
		await server.close();
	});

	it('authenticates valid API-Key', async () => {
		const result = await app.service(testRoute).find({
			authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'resolve' },
		});
		expect(result).to.not.be.undefined;
		expect(result.success).to.eq(true);
		expect(result.params.authenticated).to.eq(true);
	});

	it('authenticates valid API-Key for Statistc Mail Trigger Route', async () => {
		const result = await app.service(testRoute).find({
			authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'statisticmails' },
		});
		expect(result).to.not.be.undefined;
		expect(result.success).to.eq(true);
		expect(result.params.authenticated).to.eq(true);
	});

	it('denies valid API-Key for other routes', async () => {
		try {
			await app.service(testRoute).find({
				authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'sync' },
			});
			throw new Error('should have failed');
		} catch (err) {
			expect(err.message).to.not.eq('should have failed');
			expect(err.code).to.equal(401);
		}
	});

	it('denies invalid API-Key', async () => {
		try {
			await app.service(testRoute).find({<commit_msg>Working on tests<commit_after>const { expect } = require('chai');
const { authenticate } = require('@feathersjs/authentication');
const { Configuration } = require('@schul-cloud/commons');

const app = require('../../../src/app');

class TestService {
	find(params) {
		return Promise.resolve({ success: true, params });
	}

	setup(localApp) {
		this.app = localApp;
	}
}

const testServiceHooks = {
	before: { all: [authenticate('api-key')] },
};

describe('api-key authentication strategy', () => {
	let server;
	const testRoute = `/foo${Date.now}`;
	let configBefore = null;

	before(async () => {
		app.use(testRoute, new TestService());
		app.service(testRoute).hooks(testServiceHooks);
		server = await app.listen(0);
		configBefore = Configuration.toObject();
		Configuration.set('CALENDAR_API_KEY', 'validKey');
	});

	after(async () => {
		Configuration.reset(configBefore);
		await server.close();
	});

	it('authenticates valid API-Key', async () => {
		const result = await app.service(testRoute).find({
			authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'resolve' },
		});
		expect(result).to.not.be.undefined;
		expect(result.success).to.eq(true);
		expect(result.params.authenticated).to.eq(true);
	});

	it('authenticates valid API-Key for Statistc Mail Trigger Route', async () => {
		const result = await app.service(testRoute).create({
			authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'statisticmails' },
		});
		expect(result).to.not.be.undefined;
		expect(result.success).to.eq(true);
		expect(result.params.authenticated).to.eq(true);
	});

	it('denies valid API-Key for other routes', async () => {
		try {
			await app.service(testRoute).find({
				authentication: { strategy: 'api-key', apiKey: 'validKey', route: 'sync' },
			});
			throw new Error('should have failed');
		} catch (err) {
			expect(err.message).to.not.eq('should have failed');
			expect(err.code).to.equal(401);
		}
	});

	it('denies invalid API-Key', async () => {
		try {
			await app.service(testRoute).find({
{'fraction_non_alphanumeric': 0.184577723378213, 'fraction_numerical': 0.0019583843329253367, 'mean_word_length': 4.440745672436751, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5930923', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1342, 'n_words': 263}