From 9f1efe6dcad68687e0c81e1f460aa49759a3b2ca Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 28 Mar 2014 13:05:16 +0000 Subject: [PATCH] Add in getProjectDetails to WebApiManager --- .../app/coffee/WebApiManager.coffee | 51 +++++++++++++------ .../WebApiManager/WebApiManagerTests.coffee | 44 +++++++++++++++- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/services/track-changes/app/coffee/WebApiManager.coffee b/services/track-changes/app/coffee/WebApiManager.coffee index fc4eda9729..555dc0179c 100644 --- a/services/track-changes/app/coffee/WebApiManager.coffee +++ b/services/track-changes/app/coffee/WebApiManager.coffee @@ -3,11 +3,9 @@ logger = require "logger-sharelatex" Settings = require "settings-sharelatex" module.exports = WebApiManager = - getUserInfo: (user_id, callback = (error, userInfo) ->) -> - url = "#{Settings.apis.web.url}/user/#{user_id}/personal_info" - logger.log user_id: user_id, "getting user info from web" + sendRequest: (url, callback = (error, body) ->) -> request.get { - url: url + url: "#{Settings.apis.web.url}#{url}" auth: user: Settings.apis.web.user pass: Settings.apis.web.pass @@ -16,17 +14,40 @@ module.exports = WebApiManager = if error? return callback(error) if res.statusCode >= 200 and res.statusCode < 300 - try - user = JSON.parse(body) - catch error - return callback(error) - callback null, { - id: user.id - email: user.email - first_name: user.first_name - last_name: user.last_name - } + return callback null, body else error = new Error("web returned a non-success status code: #{res.statusCode}") + callback error + + getUserInfo: (user_id, callback = (error, userInfo) ->) -> + url = "/user/#{user_id}/personal_info" + logger.log user_id: user_id, "getting user info from web" + WebApiManager.sendRequest url, (error, body) -> + if error? logger.error err: error, user_id: user_id, url: url, "error accessing web" - callback error \ No newline at end of file + return callback error + + try + user = JSON.parse(body) + catch error + return callback(error) + callback null, { + id: user.id + email: user.email + first_name: user.first_name + last_name: user.last_name + } + + getProjectDetails: (project_id, callback = (error, details) ->) -> + url = "/project/#{project_id}/details" + logger.log project_id: project_id, "getting project details from web" + WebApiManager.sendRequest url, (error, body) -> + if error? + logger.error err: error, project_id: project_id, url: url, "error accessing web" + return callback error + + try + project = JSON.parse(body) + catch error + return callback(error) + callback null, project \ No newline at end of file diff --git a/services/track-changes/test/unit/coffee/WebApiManager/WebApiManagerTests.coffee b/services/track-changes/test/unit/coffee/WebApiManager/WebApiManagerTests.coffee index 589dfeb7e5..aec51ab2ca 100644 --- a/services/track-changes/test/unit/coffee/WebApiManager/WebApiManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/WebApiManager/WebApiManagerTests.coffee @@ -18,12 +18,15 @@ describe "WebApiManager", -> pass: "password" @callback = sinon.stub() @user_id = "mock-user-id" + @project_id = "mock-project-id" @user_info = email: "leo@sharelatex.com" id: @user_id first_name: "Leo" last_nane: "Lion" extra_param: "blah" + @project = + features: "mock-features" describe "getUserInfo", -> describe "successfully", -> @@ -33,7 +36,6 @@ describe "WebApiManager", -> @WebApiManager.getUserInfo @user_id, @callback it 'should get the user from the web api', -> - url = @request.get .calledWith({ url: "#{@settings.apis.web.url}/user/#{@user_id}/personal_info" @@ -65,6 +67,46 @@ describe "WebApiManager", -> @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "") @WebApiManager.getUserInfo @user_id, @callback + it "should return the callback with an error", -> + @callback + .calledWith(new Error("web returned failure status code: 500")) + .should.equal true + + + describe "getProjectDetails", -> + describe "successfully", -> + beforeEach -> + @body = JSON.stringify @project + @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body) + @WebApiManager.getProjectDetails @project_id, @callback + + it 'should get the project from the web api', -> + @request.get + .calledWith({ + url: "#{@settings.apis.web.url}/project/#{@project_id}/details" + auth: + user: @settings.apis.web.user + pass: @settings.apis.web.pass + sendImmediately: true + }) + .should.equal true + + it "should call the callback with the project", -> + @callback.calledWith(null, @project).should.equal true + + describe "when the web API returns an error", -> + beforeEach -> + @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null) + @WebApiManager.getProjectDetails @project_id, @callback + + it "should return an error to the callback", -> + @callback.calledWith(@error).should.equal true + + describe "when the web returns a failure error code", -> + beforeEach -> + @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "") + @WebApiManager.getProjectDetails @project_id, @callback + it "should return the callback with an error", -> @callback .calledWith(new Error("web returned failure status code: 500"))