From 5d436748fa9cd2e58804f3f8245c5263af302c05 Mon Sep 17 00:00:00 2001 From: LeoX DEV Date: Fri, 1 Dec 2023 19:08:06 +0100 Subject: [PATCH] Added authentication test --- package-lock.json | 13 +++++++++++++ package.json | 1 + src/http.js | 15 +++++++++++++++ test/auth.js | 11 +++++++++++ 4 files changed, 40 insertions(+) create mode 100644 test/auth.js diff --git a/package-lock.json b/package-lock.json index a909aa3..c65b5b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "axios": "^1.6.2" }, "devDependencies": { + "dotenv": "^16.3.1", "mocha": "^10.2.0", "nyc": "^15.1.0" } @@ -1043,6 +1044,18 @@ "node": ">=0.3.1" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "dev": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/electron-to-chromium": { "version": "1.4.600", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.600.tgz", diff --git a/package.json b/package.json index 647ace3..ec9ea98 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "axios": "^1.6.2" }, "devDependencies": { + "dotenv": "^16.3.1", "mocha": "^10.2.0", "nyc": "^15.1.0" } diff --git a/src/http.js b/src/http.js index 44b5cf0..6be7d3d 100644 --- a/src/http.js +++ b/src/http.js @@ -7,6 +7,7 @@ class Session { #username = ''; #password = ''; #cookies = ''; + #user = null; /** * Creates a new session. @@ -29,6 +30,14 @@ class Session { Object.keys(params).forEach(key => formData.append(key, params[key])); const result = (await post('https://www.donald.org/api/mitglieder/login', formData, this)).data; if (result.error !== null) throw new Error(`[${result.error.code} ${result.error.message}] ${result.error.reason}`); + this.#user = { + id: result.id, + name: result.name, + email: result.email, + expires: new Date(result.expires), + mifümi: result.properties.mifümi, + groups: Object.values(result.groups) + }; return this; }; @@ -46,6 +55,12 @@ class Session { }; set cookies(cookies) {}; + + get user() { + if (this.#user != null) return this.#user; else throw new Error('User not logged in'); + }; + + set user(user) {}; }; function get(url, session) { diff --git a/test/auth.js b/test/auth.js new file mode 100644 index 0000000..8e9254c --- /dev/null +++ b/test/auth.js @@ -0,0 +1,11 @@ +require('dotenv').config(); +const donald = require('..'); +const assert = require('assert'); + +const session = new donald.Session(process.env.USRNAME, process.env.PASSWD); + +describe('auth', () => { + it('should authenticate successfully', async () => { + await session.login().catch(err => assert.fail(err)); + }); +}); \ No newline at end of file