<commit_before>const assert = require('assert');
const chai = require('chai');

const app = require('../../../src/app');
const userModel = require('../../../src/services/user/model');

const registrationService = app.service('registration');
const registrationPinService = app.service('registrationPins');

describe('registration service', () => {
	it('registered the registration service', () => {
		assert.ok(registrationService);
	});

	it('processes registration by student correctly', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '<PASSWORD>',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput);
			})
			.then((response) => {
				chai.expect(response.user).to.have.property('_id');
				chai.expect(response.account).to.have.property('_id');
				chai.expect(response.consent).to.have.property('_id');
				chai.expect(response.consent).to.have.property('userConsent');
				chai.expect(response.parent).to.equal(null);
			});
	});

	it('processes registration by parent correctly', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					birthDate: '15.10.2014',
					email: `<EMAIL>`,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
					parent_email: email,
					parent_firstName: 'Moritz',
					parent_lastName: 'Mustermann',
				};
				return registrationService.create(registrationInput);
			})
			.then((response) => {
				chai.expect(response.user).to.have.property('_id');
				chai.expect(response.account).to.have.property('_id');
				chai.expect(response.consent).to.have.property('_id');
				chai.expect(response.consent.parentConsents.length).to.be.at.least(1);
				chai.expect(response.parent).to.have.property('_id');
				chai.expect(response.user.parents).to.include(response.parent._id);
				chai.expect(response.parent.children).to.include(response.user._id);
			});
	});

	it('fails with invalid pin', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				let pin = Number(registrationPin.pin);
				pin = pin === 9999 ? 1000 : pin + 1;
				// make sure we pass a wrong pin
				return registrationService.create({
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: String(pin),
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
				});
			}).catch((err) => {
				chai.expect(err).to.not.equal(undefined);
				chai.expect(err.message).to.equal('Ungültige Pin, bitte überprüfe die Eingabe.');
			});
	});

	it('fails if parent and student email are the same', () => registrationService.create({
		classOrSchoolId: '0000d186816abba584714c5f',
		email: '<EMAIL>',
		parent_email: '<EMAIL>',
		birthDate: '18.02.2015',
	}).catch((err) => {
		chai.expect(err.message).to.equal('Bitte gib eine unterschiedliche E-Mail-Adresse für dein Kind an.');
	}));

	it('undoes changes on fail', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput).catch((err) => {
					// no password given, should result in an error.
					chai.expect(err.message).to.equal('Fehler beim Erstellen des Accounts.');
					// a user has been created before the error. Lets check if he was deleted...
					return userModel.userModel.findOne({ email });
				}).then((users) => {
					chai.expect(users).to.equal(null);
				});
			});
	});

	it('processes teachers correctly', () => {
		const email = `<EMAIL>`;
		let hash;
		let user;
		const hashData = {
			toHash: email,
			save: true,
		};
		return app.service('hash').create(hashData)
			.then((newHash) => {
				hash = newHash;
				return userModel.userModel.create({
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					schoolId: '0000d186816abba584714c5f',
					roles: ['0000d186816abba584714c98'], // teacher
					importHash: hash,
				});
			})
			.then((newUser) => {
				user = newUser;
				return registrationPinService.create({ email });
			})
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					importHash: hash,
					userId: user._id,
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput).then((response) => {
					chai.expect(response.user).to.have.property('_id');
					chai.expect(response.account).to.have.property('_id');
					chai.expect(response.consent).to.have.property('_id');
					chai.expect(response.consent).to.have.property('userConsent');
					chai.expect(response.parent).to.equal(null);
				});
			});
	});
});
<commit_msg>Fix registration test<commit_after>const assert = require('assert');
const chai = require('chai');

const app = require('../../../src/app');
const userModel = require('../../../src/services/user/model');

const registrationService = app.service('registration');
const registrationPinService = app.service('registrationPins');

describe('registration service', () => {
	it('registered the registration service', () => {
		assert.ok(registrationService);
	});

	it('processes registration by student correctly', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput);
			})
			.then((response) => {
				chai.expect(response.user).to.have.property('_id');
				chai.expect(response.account).to.have.property('_id');
				chai.expect(response.consent).to.have.property('_id');
				chai.expect(response.consent).to.have.property('userConsent');
				chai.expect(response.parent).to.equal(null);
			});
	});

	it('processes registration by parent correctly', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					birthDate: '15.10.2014',
					email: `<EMAIL>`,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
					parent_email: email,
					parent_firstName: 'Moritz',
					parent_lastName: 'Mustermann',
				};
				return registrationService.create(registrationInput);
			})
			.then((response) => {
				chai.expect(response.user).to.have.property('_id');
				chai.expect(response.account).to.have.property('_id');
				chai.expect(response.consent).to.have.property('_id');
				chai.expect(response.consent.parentConsents.length).to.be.at.least(1);
				chai.expect(response.parent).to.have.property('_id');
				chai.expect(response.user.parents[0].toString()).to.equal(response.parent._id.toString());
				chai.expect(response.parent.children[0].toString()).to.include(response.user._id.toString());
			});
	});

	it('fails with invalid pin', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				let pin = Number(registrationPin.pin);
				pin = pin === 9999 ? 1000 : pin + 1;
				// make sure we pass a wrong pin
				return registrationService.create({
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: String(pin),
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
				});
			}).catch((err) => {
				chai.expect(err).to.not.equal(undefined);
				chai.expect(err.message).to.equal('Ungültige Pin, bitte überprüfe die Eingabe.');
			});
	});

	it('fails if parent and student email are the same', () => registrationService.create({
		classOrSchoolId: '0000d186816abba584714c5f',
		email: '<EMAIL>',
		parent_email: '<EMAIL>',
		birthDate: '18.02.2015',
	}).catch((err) => {
		chai.expect(err.message).to.equal('Bitte gib eine unterschiedliche E-Mail-Adresse für dein Kind an.');
	}));

	it('undoes changes on fail', () => {
		const email = `<EMAIL>`;
		return registrationPinService.create({ email })
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					birthDate: '15.10.1999',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput).catch((err) => {
					// no password given, should result in an error.
					chai.expect(err.message).to.equal('Fehler beim Erstellen des Accounts.');
					// a user has been created before the error. Lets check if he was deleted...
					return userModel.userModel.findOne({ email });
				}).then((users) => {
					chai.expect(users).to.equal(null);
				});
			});
	});

	it('processes teachers correctly', () => {
		const email = `<EMAIL>`;
		let hash;
		let user;
		const hashData = {
			toHash: email,
			save: true,
		};
		return app.service('hash').create(hashData)
			.then((newHash) => {
				hash = newHash;
				return userModel.userModel.create({
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					schoolId: '0000d186816abba58471<PASSWORD>',
					roles: ['0000d186816abba584714c98'], // teacher
					importHash: hash,
				});
			})
			.then((newUser) => {
				user = newUser;
				return registrationPinService.create({ email });
			})
			.then((registrationPin) => {
				const registrationInput = {
					classOrSchoolId: '0000d186816abba584714c5f',
					pin: registrationPin.pin,
					password_1: '<PASSWORD>!',
					password_2: '<PASSWORD>!',
					email,
					firstName: 'Max',
					lastName: 'Mustermann',
					importHash: hash,
					userId: user._id,
					privacyConsent: true,
					termsOfUseConsent: true,
				};
				return registrationService.create(registrationInput).then((response) => {
					chai.expect(response.user).to.have.property('_id');
					chai.expect(response.account).to.have.property('_id');
					chai.expect(response.consent).to.have.property('_id');
					chai.expect(response.consent).to.have.property('userConsent');
					chai.expect(response.parent).to.equal(null);
				});
			});
	});
});

{'fraction_non_alphanumeric': 0.16477768090671316, 'fraction_numerical': 0.03217088055797733, 'mean_word_length': 3.5975951903807615, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 77, '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': '3821657', 'n_tokens_mistral': 4888, 'n_tokens_neox': 3732, 'n_words': 654}