<commit_before>'use strict'

const { expect } = require('chai')
const { connect, People, Organization, Activity } = require('../lib/model')
const { merge, pick, map } = require('lodash/fp')
const { agent } = require('./http-utils')


describe('REST', () => {

	let connection

	before(() => connect().then(conn => connection = conn))
	after(() => connection && connection.close())

	describeModelRestApi('/people', People, ({ organization }) => ({
		name: '<NAME>',
		academicMemberships: [ { organization } ] // Without this, user cannot be seen from the creator
	}), () => ({
		name: '<NAME> (modified)'
	}))

	describeModelRestApi('/organizations', Organization, () => ({
		name: 'Missing & co'
	}), () => ({
		name: 'Missing & co (modified)'
	}))

	describeModelRestApi('/activities', Activity, ({ organization }) => ({
		name: 'Find someone',
		activityType: 'projetderecherche',
		organizations: [ { organization } ] // Without this, activity cannot be seen from the creator
	}), () => ({
		name: 'Find someone (modified)'
	}))

})


function describeModelRestApi (root, Model, create, update) {
	const query = agent()

	describe(`${root} (basics)`, () => {

		let initialCount = (Model === People || Model === Organization) ? 1 : 0 // increment initial count if People or Organization, as we add some credentials
		let doc = null
		let adminLogin = 'test.test'
		let admin = null
		let organization = null

		before(() => Model.remove())

		// Add an organization for permissions
		before(() =>
			(new Organization({
				name: 'Test Organization',
				latestChangeBy: 'UnitTests'
			})).save().then(o => organization = o)
		)
		// Cleanup organization
		after(() => Organization.findById(organization.id).then(o => o ? o.remove() : Promise.reject('Organization not found for cleanup')))

		// Add a user for authentication
		before(() =>
			(new People({
				name: '<NAME>',
				ldapUid: adminLogin,
				latestChangeBy: 'UnitTests',
				// Make this user central admin
				isariAuthorizedCenters: [{
					organization,
					isariRole: 'central_admin'
				}]
			})).save().then(o => admin = o)
		)
		// Log out at the end, but do it BEFORE deleting user
		after(() => query('post', '/auth/logout').then(({ body, status }) => {
			expect(status).to.equal(200)
			expect(body).to.eql({ was: adminLogin })
		}))
		// Cleanup user
		after(() => People.findOne({ ldapUid: adminLogin }).then(o => o ? o.remove() : Promise.reject('Admin not found for cleanup')))

		// Authenticate first
		it('should authenticate', () =>
			query('post', '/auth/login', { login: adminLogin, password: '<PASSWORD>' }).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('object').and.have.property('login').to.equal(adminLogin)
				expect(body).to.have.property('people').to.be.an('object').to.have.property('ldapUid').to.equal(adminLogin)
			})
		)

		it('should be authenticated', () =>
			query('get', '/auth/myself').then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('object').and.have.property('login').to.equal(adminLogin)
				expect(body).to.have.property('people').to.be.an('object').to.have.property('ldapUid').to.equal(adminLogin)
			})
		)

		it('GET / (empty)', () =>
			query('get', root).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('array').and.have.length(0 + initialCount)
			})
		)

		it('POST / (create)', () =>
			query('post', root, create({ admin, organization }))
			.then(({ body, status }) => {
				expect(status).to.equal(201)
				expect(body).to.be.an('object').and.have.property('id')
				doc = body
			})
		)

		it('GET / (1 element)', () =>
			query('get', root).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('array').and.have.length(1 + initialCount)
				if (Model === People) {
					body = body.filter(({ ldapUid }) => ldapUid !== adminLogin)
				}
				if (Model === Organization) {
					body = body.filter(({ id }) => id !== organization.id)
				}
				expect(map(pick('id'), body)).to.eql([pick('id', doc)])
			})
		)

		it('GET /:id (find)', () =>
			query('get', root + '/' + doc.id)
			.then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(pick('id', body)).to.eql(pick('id', doc))
			})
		)

		it('PUT /:id (update)', () => {
			const updates = update({ admin, organization })
			return query('put', root + '/' + doc.id, updates)
			.then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(pick('id', body)).to.eql(pick('id', merge(doc, updates)))<commit_msg>refactor(tests): back to omit instead of pick, as now objects have a more predictible format<commit_after>'use strict'

const { expect } = require('chai')
const { connect, People, Organization, Activity } = require('../lib/model')
const { merge, omit, map } = require('lodash/fp')
const { agent } = require('./http-utils')


describe('REST', () => {

	let connection

	before(() => connect().then(conn => connection = conn))
	after(() => connection && connection.close())

	describeModelRestApi('/people', People, ({ organization }) => ({
		name: '<NAME>',
		academicMemberships: [ { organization } ] // Without this, user cannot be seen from the creator
	}), () => ({
		name: '<NAME> (modified)'
	}))

	describeModelRestApi('/organizations', Organization, () => ({
		name: 'Missing & co'
	}), () => ({
		name: 'Missing & co (modified)'
	}))

	describeModelRestApi('/activities', Activity, ({ organization }) => ({
		name: 'Find someone',
		activityType: 'projetderecherche',
		organizations: [ { organization } ] // Without this, activity cannot be seen from the creator
	}), () => ({
		name: 'Find someone (modified)'
	}))

})


function describeModelRestApi (root, Model, create, update) {
	const query = agent()

	describe(`${root} (basics)`, () => {

		let initialCount = (Model === People || Model === Organization) ? 1 : 0 // increment initial count if People or Organization, as we add some credentials
		let doc = null
		let adminLogin = 'test.test'
		let admin = null
		let organization = null

		before(() => Model.remove())

		// Add an organization for permissions
		before(() =>
			(new Organization({
				name: 'Test Organization',
				latestChangeBy: 'UnitTests'
			})).save().then(o => organization = o)
		)
		// Cleanup organization
		after(() => Organization.findById(organization.id).then(o => o ? o.remove() : Promise.reject('Organization not found for cleanup')))

		// Add a user for authentication
		before(() =>
			(new People({
				name: '<NAME>',
				ldapUid: adminLogin,
				latestChangeBy: 'UnitTests',
				// Make this user central admin
				isariAuthorizedCenters: [{
					organization,
					isariRole: 'central_admin'
				}]
			})).save().then(o => admin = o)
		)
		// Log out at the end, but do it BEFORE deleting user
		after(() => query('post', '/auth/logout').then(({ body, status }) => {
			expect(status).to.equal(200)
			expect(body).to.eql({ was: adminLogin })
		}))
		// Cleanup user
		after(() => People.findOne({ ldapUid: adminLogin }).then(o => o ? o.remove() : Promise.reject('Admin not found for cleanup')))

		// Authenticate first
		it('should authenticate', () =>
			query('post', '/auth/login', { login: adminLogin, password: '<PASSWORD>' }).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('object').and.have.property('login').to.equal(adminLogin)
				expect(body).to.have.property('people').to.be.an('object').to.have.property('ldapUid').to.equal(adminLogin)
			})
		)

		it('should be authenticated', () =>
			query('get', '/auth/myself').then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('object').and.have.property('login').to.equal(adminLogin)
				expect(body).to.have.property('people').to.be.an('object').to.have.property('ldapUid').to.equal(adminLogin)
			})
		)

		it('GET / (empty)', () =>
			query('get', root).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('array').and.have.length(0 + initialCount)
			})
		)

		it('POST / (create)', () =>
			query('post', root, create({ admin, organization }))
			.then(({ body, status }) => {
				expect(status).to.equal(201)
				expect(body).to.be.an('object').and.have.property('id')
				doc = body
			})
		)

		it('GET / (1 element)', () =>
			query('get', root).then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(body).to.be.an('array').and.have.length(1 + initialCount)
				if (Model === People) {
					body = body.filter(({ ldapUid }) => ldapUid !== adminLogin)
				}
				if (Model === Organization) {
					body = body.filter(({ id }) => id !== organization.id)
				}
				expect(map(omit(['updatedAt', 'opts']), body)).to.eql([omit(['updatedAt'], doc)])
			})
		)

		it('GET /:id (find)', () =>
			query('get', root + '/' + doc.id)
			.then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(omit(['updatedAt', 'opts'], body)).to.eql(omit(['updatedAt'], doc))
			})
		)

		it('PUT /:id (update)', () => {
			const updates = update({ admin, organization })
			return query('put', root + '/' + doc.id, updates)
			.then(({ body, status }) => {
				expect(status).to.equal(200)
				expect(omit(['updatedAt', 'opts'], body)).to.eql(omit(['updatedAt'], merge(doc, updates)))
{'fraction_non_alphanumeric': 0.20962716235091866, 'fraction_numerical': 0.006231868486085742, 'mean_word_length': 3.6493506493506493, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 87, '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': '3667121', 'n_tokens_mistral': 3467, 'n_tokens_neox': 2970, 'n_words': 668}