diff --git a/helpers/user.php b/helpers/user.php index 1decc2a..5dcaf79 100644 --- a/helpers/user.php +++ b/helpers/user.php @@ -194,7 +194,7 @@ /** * Get all users * - * @return array|bool Array of uUser users, false on error + * @return uUser[]|bool Array of uUser users, false on error */ public static function getAll() { try { diff --git a/js/src/user.js b/js/src/user.js new file mode 100644 index 0000000..a8e7f66 --- /dev/null +++ b/js/src/user.js @@ -0,0 +1,51 @@ +/* + * μlogger + * + * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +import uAjax from './ajax.js'; + +/** + * @class uUser + * @property {number} id + * @property {string} login + * @property {string} [password] + */ +export default class uUser { + /** + * @param {number} id + * @param {string} login + */ + constructor(id, login) { + this.id = id; + this.login = login; + } + + /** + * @throws + * @return {Promise} + */ + static fetchList() { + return uAjax.get('utils/getusers.php').then((_users) => { + const users = []; + for (const user of _users) { + users.push(new uUser(user.id, user.login)); + } + return users; + }); + } +} diff --git a/js/test/user.test.js b/js/test/user.test.js new file mode 100644 index 0000000..a0bbdf3 --- /dev/null +++ b/js/test/user.test.js @@ -0,0 +1,62 @@ +/* + * μlogger + * + * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +import uUser from '../src/user.js'; + +describe('User tests', () => { + + describe('simple tests', () => { + + it('should create uUser instance', () => { + // given + const id = 1; + const login = 'test'; + // when + const user = new uUser(id, login); + // then + expect(user.id).toBe(id); + expect(user.login).toBe(login); + }); + }); + + describe('ajax tests', () => { + const validResponse = [ { 'id': 1, 'login': 'test' }, { 'id': 2, 'login': 'test2' }, { 'id': 18, 'login': 'demo' } ]; + + beforeEach(() => { + spyOn(XMLHttpRequest.prototype, 'open').and.callThrough(); + spyOn(XMLHttpRequest.prototype, 'setRequestHeader').and.callThrough(); + spyOn(XMLHttpRequest.prototype, 'send'); + spyOnProperty(XMLHttpRequest.prototype, 'readyState').and.returnValue(XMLHttpRequest.DONE); + spyOnProperty(XMLHttpRequest.prototype, 'status').and.returnValue(200); + }); + + it('should make successful request and user array', (done) => { + // when + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify(validResponse)); + // then + uUser.fetchList() + .then((result) => { + expect(result).toEqual(jasmine.arrayContaining([ new uUser(1, 'test') ])); + expect(result.length).toBe(3); + done(); + }) + .catch(() => done.fail('reject callback called')); + }); + }); +}); diff --git a/utils/getusers.php b/utils/getusers.php new file mode 100644 index 0000000..42fb1fd --- /dev/null +++ b/utils/getusers.php @@ -0,0 +1,43 @@ +. + */ + +require_once(dirname(__DIR__) . "/helpers/auth.php"); +require_once(ROOT_DIR . "/helpers/track.php"); + +$auth = new uAuth(); + +$usersArr = []; +if (uConfig::$public_tracks || $auth->isAdmin()) { + $usersArr = uUser::getAll(); +} else if ($auth->isAuthenticated()) { + $usersArr = [ $auth->user ]; +} + +$result = []; +if ($usersArr === false) { + $result = [ "error" => true ]; +} else if (!empty($usersArr)) { + foreach ($usersArr as $user) { + $result[] = [ "id" => $user->id, "login" => $user->login ]; + } +} +header("Content-type: application/json"); +echo json_encode($result); +?>