const assert = require('assert');
const { expect } = require('chai');
const app = require('../../../src/app');
const testObjects = require('../helpers/testObjects')(app);

describe('systemId service', () => {
	it('registered the systems service', () => {
		assert.ok(app.service('systems'));
	});

	it('only shows systems of the current school', async () => {
		const usersSystem = await testObjects.createTestSystem();
		const otherSystem = await testObjects.createTestSystem();
		const usersSchool = await testObjects.createTestSchool({ systems: [usersSystem._id] });
		await testObjects.createTestSchool({ systems: [otherSystem._id] });

		const user = await testObjects.createTestUser({ roles: ['administrator'], schoolId: [usersSchool._id] });
		const params = await testObjects.generateRequestParamsFromUser(user);

		const result = await app.service('systems').find(params);
		expect(result.total).to.equal(1);
		expect(result.data[0]._id.toString()).to.equal(usersSystem._id.toString());
	});

	it('fails without proper permissions', async () => {
		const usersSystem = await testObjects.createTestSystem();
		const otherSystem = await testObjects.createTestSystem();
		const usersSchool = await testObjects.createTestSchool({ systems: [usersSystem._id] });
		await testObjects.createTestSchool({ systems: [otherSystem._id] });

		const user = await testObjects.createTestUser({ roles: ['student'], schoolId: [usersSchool._id] });
		const params = await testObjects.generateRequestParamsFromUser(user);

		try {
			await app.service('systems').find(params);
			throw new Error('should have failed');
		} catch (err) {
			expect(err.message).to.not.equal('should have failed');
			expect(err.code).to.equal(403);
			expect(err.message).to.equal('You don\'t have one of the permissions: SYSTEM_EDIT.');
		}
	});
});

@@ -296,16 +296,21 @@
 ;%0A%0A%09it('
+FIND 
 only sho
@@ -350,32 +350,32 @@
 , async () =%3E %7B%0A
-
 %09%09const usersSys
@@ -1005,16 +1005,21 @@
 ;%0A%0A%09it('
+FIND 
 fails wi
@@ -1723,16 +1723,16 @@
 l(403);%0A
-
 %09%09%09expec
@@ -1820,13 +1820,752 @@
 %09%09%7D%0A%09%7D);
+%0A%0A%09it('GET fails for different school', async () =%3E %7B%0A%09%09const otherSystem = await testObjects.createTestSystem();%0A%09%09const usersSchool = await testObjects.createTestSchool(%7B%7D);%0A%09%09await testObjects.createTestSchool(%7B systems: %5BotherSystem._id%5D %7D);%0A%0A%09%09const user = await testObjects.createTestUser(%7B roles: %5B'administrator'%5D, schoolId: %5BusersSchool._id%5D %7D);%0A%09%09const params = await testObjects.generateRequestParamsFromUser(user);%0A%0A%09%09try %7B%0A%09%09%09await app.service('systems').get(otherSystem._id, params);%0A%09%09%09throw new Error('should have failed');%0A%09%09%7D catch (err) %7B%0A%09%09%09expect(err.message).to.not.equal('should have failed');%0A%09%09%09expect(err.code).to.equal(403);%0A%09%09%09expect(err.message).to.equal('You are not allowed to access this system.');%0A%09%09%7D%0A%09%7D);
 %0A%7D);%0A

add test for GET