diff options
author | winh <winnie@gitlab.com> | 2017-05-22 17:51:18 +0200 |
---|---|---|
committer | winh <winnie@gitlab.com> | 2017-05-22 20:37:41 +0200 |
commit | 729a5a5b89c24aa3d3d991815f4dcb05270f640f (patch) | |
tree | 7e57d5a818921d56e69ccc194e1cf30147742648 | |
parent | e15aee2b51e4f03972fba490e2974a761acccf6c (diff) | |
download | gitlab-ce-winh-current-user-frontend-api.tar.gz |
Add frontend API method for current user (!11608)winh-current-user-frontend-api
-rw-r--r-- | app/assets/javascripts/api.js | 9 | ||||
-rw-r--r-- | spec/javascripts/api_spec.js | 39 |
2 files changed, 48 insertions, 0 deletions
diff --git a/app/assets/javascripts/api.js b/app/assets/javascripts/api.js index 6680834a8d1..45c5dd718a6 100644 --- a/app/assets/javascripts/api.js +++ b/app/assets/javascripts/api.js @@ -13,6 +13,7 @@ const Api = { dockerfilePath: '/api/:version/templates/dockerfiles/:key', issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key', usersPath: '/api/:version/users.json', + currentUserPath: '/api/:version/user.json', group(groupId, callback) { const url = Api.buildUrl(Api.groupPath) @@ -149,6 +150,14 @@ const Api = { }); }, + currentUser() { + const url = Api.buildUrl(this.currentUserPath); + return Api.wrapAjaxCall({ + url, + dataType: 'json', + }); + }, + buildUrl(url) { let urlRoot = ''; if (gon.relative_url_root != null) { diff --git a/spec/javascripts/api_spec.js b/spec/javascripts/api_spec.js index 867322ce8ae..38dd1f5aa04 100644 --- a/spec/javascripts/api_spec.js +++ b/spec/javascripts/api_spec.js @@ -278,4 +278,43 @@ describe('Api', () => { .catch(done.fail); }); }); + + describe('currentUser', () => { + it('fetches current user', (done) => { + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user.json`; + spyOn(jQuery, 'ajax').and.callFake((request) => { + expect(request.url).toEqual(expectedUrl); + expect(request.dataType).toEqual('json'); + return sendDummyResponse(); + }); + + Api.currentUser() + .then((response) => { + expect(response).toBe(dummyResponse); + }) + .then(done) + .catch(done.fail); + }); + + it('throws if fetching current user fails', (done) => { + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user.json`; + const expectedError = 'current user hides behind a bush'; + spyOn(jQuery, 'ajax').and.callFake((request) => { + expect(request.url).toEqual(expectedUrl); + expect(request.dataType).toEqual('json'); + + const deferred = $.Deferred(); + deferred.reject(null, null, expectedError); + return deferred.promise(); + }); + + Api.currentUser() + .then(done.fail) + .catch((error) => { + expect(error.message).toBe(`${expectedUrl}: ${expectedError}`); + }) + .then(done) + .catch(done.fail); + }); + }); }); |