From e6d05f0e9b2254e3934e5bd4863e29fd252ceca0 Mon Sep 17 00:00:00 2001 From: Sj3rd <33723199+Sj3rd@users.noreply.github.com> Date: Mon, 25 Mar 2019 20:46:37 +0100 Subject: [PATCH 1/2] Implement absences tested and fully function (as expected ;)) --- src/magister.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/magister.js b/src/magister.js index 1902382..94291d7 100644 --- a/src/magister.js +++ b/src/magister.js @@ -86,6 +86,28 @@ class Magister { get token() { return this.http._token } + + /** + * @param {Date} from Time is ignored. + * @param {Date} [to=from] Time is ignored + * @returns {Promise} + */ + absences() { + // extract dates + const dates = _(arguments).filter(_.isDate).sortBy().value() + const from = dates[0] + const to = dates[1] || dates[0] + + const fromUrl = util.urlDateConvert(from) + const toUrl = util.urlDateConvert(to) + + // fetch absences + const absencesUrl = `${this._personUrl}/absenties?van=${fromUrl}&tot=${toUrl}` + return this._privileges.needs('absenties', 'read') + .then(() => this.http.get(absencesUrl)) + .then(res => res.json()) + .then(res => res.Items.map(a => new AbsenceInfo(this, a))) + } /** * @returns {Promise} From cba89590cf349acbcf7b76f3a5b40ebc65f20ef1 Mon Sep 17 00:00:00 2001 From: Sj3rd <33723199+Sj3rd@users.noreply.github.com> Date: Mon, 25 Mar 2019 20:59:37 +0100 Subject: [PATCH 2/2] Update test.js to use authCode Cant login without ;) --- test/test.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index f6194ce..004c121 100644 --- a/test/test.js +++ b/test/test.js @@ -12,23 +12,26 @@ const options = { school: {}, username: undefined, password: undefined, + authCode: undefined } try { const parsed = require('./options.json') const isParent = parsed.parent != null - const { school, username, password } = parsed[isParent ? 'parent' : 'child'] + const { school, username, password, authCode } = parsed[isParent ? 'parent' : 'child'] options.isParent = isParent options.school = school options.username = username options.password = password + options.authCode = authCode } catch (e) { // For Travis CI we use environment variables. options.isParent = process.env.TEST_ISPARENT options.school.url = process.env.TEST_SCHOOLURL options.username = process.env.TEST_USERNAME options.password = process.env.TEST_PASSWORD + options.authCode = process.env.TEST_AUTHCODE } -if (!options.school.url || !options.username || !options.password) { +if (!options.school.url || !options.username || !options.password || !options.authCode) { throw new Error('No login information found.') } @@ -173,6 +176,21 @@ describe('Magister', function() { }) }) + describe('absence', function () { + it('should fetch absences', function () { + return m.absences(new Date()).then(r => { + expect(r).to.be.an('array') + + for (const absence of r) { + expect(absence).to.be.an.instanceof(magisterjs.AbsenceInfo) + expect(absence.appointment).to.be.a('object') + } + + return [] + }) + }) + }) + describe('activity', function () { // TODO: add test for `ActivityElement#signup`