Skip to content

Commit

Permalink
Added authentication test
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Dec 1, 2023
1 parent 4629f93 commit 5d43674
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"axios": "^1.6.2"
},
"devDependencies": {
"dotenv": "^16.3.1",
"mocha": "^10.2.0",
"nyc": "^15.1.0"
}
Expand Down
15 changes: 15 additions & 0 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Session {
#username = '';
#password = '';
#cookies = '';
#user = null;

/**
* Creates a new session.
Expand All @@ -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;
};

Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
@@ -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));
});
});

0 comments on commit 5d43674

Please sign in to comment.