<commit_before>import sinon from 'sinon';
import { expect, request } from 'chai';

import User from '../../src/models/user';
import Playlist from '../../src/models/playlist';
import api from '../../src/server';
import logger from '../../src/utils/logger';
import { dropDBs, loadFixture, withLogin } from '../utils';

describe('Playlist controller', () => {
	const otherUserId = '4342306d8e147f10f16aceaf';
	let user;
	let playlist;
	
	before(async () => {
		await dropDBs();
		await loadFixture('initial-data', 'playlists');

		user = await User.findOne();
		playlist = await Playlist.findOne();
	});
	
	describe('listing all entries', () => {
		it('should return 200 for valid request', async () => {
			const response = await withLogin(request(api).get('/playlists'));

			expect(response).to.have.status(200);
		});
		
		it('should filter results if query is provided', async () => {
			const fileters = [
				{ episode: '5b0ad37026dc3db38194e286' },
				{ type: 'recommended' },
				{ type: 'featured' },
			];
			const response = await withLogin(request(api).get('/playlists').query());

			expect(response).to.have.status(200);
		});
		
		it('should return 403 when retrieving entries created by other users', async () => {
			const response = await withLogin(request(api).get('/playlists').query({ user: otherUserId }));

			expect(response).to.have.status(403);
		});

		it('should return 500 for invalid request', async () => {
			//XXX: silensing error logs to have nice clean test output
			sinon.stub(logger, 'error');
			const response = await withLogin(request(api).get('/playlists').query({ episode: { $gte: '' } }));

			expect(response).to.have.status(500);
			logger.error.restore();
		});
	});
	
	describe('retrieving entry by id', () => {
		it('should return 200 for valid request', async () => {
			const response = await withLogin(request(api).get(`/playlists/${playlist._id}`));

			expect(response).to.have.status(200);
		});

		it('should return 404 for invalid id', async () => {
			const response = await withLogin(request(api).get('/playlists/5b0f306d8e147f10deadbeef'));

			expect(response).to.have.status(404);
		});

		it('should return 403 when retrieving entries created by other users', async () => {
			const otherPlaylist = await Playlist.create({
				user: otherUserId,
				name: 'other playlist',<commit_msg>Add basic checks for output of get and list handlers<commit_after>import sinon from 'sinon';
import { expect, request } from 'chai';

import User from '../../src/models/user';
import Playlist from '../../src/models/playlist';
import api from '../../src/server';
import logger from '../../src/utils/logger';
import { dropDBs, loadFixture, withLogin } from '../utils';

describe('Playlist controller', () => {
	const otherUserId = '4342306d8e147f10f16aceaf';
	let user;
	let playlist;
	
	before(async () => {
		await dropDBs();
		await loadFixture('initial-data', 'playlists');

		user = await User.findOne();
		playlist = await Playlist.findOne();
	});
	
	describe('listing all entries', () => {
		it('should return 200 for valid request', async () => {
			const response = await withLogin(request(api).get('/playlists'));

			expect(response).to.have.status(200);

			const keys = ['_id', 'user', 'name', 'episodes'];
			for (const entry of response.body) {
				expect(Object.keys(entry)).to.include.members(keys);
			}
		});
		
		it('should filter results if query is provided', async () => {
			const fileters = [
				{ episode: '5b0ad37026dc3db38194e286' },
				{ type: 'recommended' },
				{ type: 'featured' },
			];
			const response = await withLogin(request(api).get('/playlists').query());

			expect(response).to.have.status(200);

			const keys = ['_id', 'user', 'name', 'episodes'];
			for (const entry of response.body) {
				expect(Object.keys(entry)).to.include.members(keys);
			}
		});
		
		it('should return 403 when retrieving entries created by other users', async () => {
			const response = await withLogin(request(api).get('/playlists').query({ user: otherUserId }));

			expect(response).to.have.status(403);
		});

		it('should return 500 for invalid request', async () => {
			//XXX: silensing error logs to have nice clean test output
			sinon.stub(logger, 'error');
			const response = await withLogin(request(api).get('/playlists').query({ episode: { $gte: '' } }));

			expect(response).to.have.status(500);
			logger.error.restore();
		});
	});
	
	describe('retrieving entry by id', () => {
		it('should return 200 for valid request', async () => {
			const response = await withLogin(request(api).get(`/playlists/${playlist._id}`));

			expect(response).to.have.status(200);
			const keys = ['_id', 'user', 'name', 'episodes'];
			expect(Object.keys(response.body)).to.include.members(keys);

			expect(response.body._id).to.be.equal(String(playlist._id));
			expect(response.body.user._id).to.be.equal(String(playlist.user._id));
			expect(response.body.name).to.be.equal(playlist.name);
			//XXX: mocha seems to hang if we try to do a full compare
			expect(response.body.episodes.map(e => e._id)).to.have.all.members(playlist.episodes.map(e => String(e._id)));
		});

		it('should return 404 for invalid id', async () => {
			const response = await withLogin(request(api).get('/playlists/5b0f306d8e147f10deadbeef'));

			expect(response).to.have.status(404);
		});

		it('should return 403 when retrieving entries created by other users', async () => {
			const otherPlaylist = await Playlist.create({
				user: otherUserId,
				name: 'other playlist',
{'fraction_non_alphanumeric': 0.1754607878568847, 'fraction_numerical': 0.028189374774123598, 'mean_word_length': 4.018132366273798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2624594', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1715, 'n_words': 420}