From 1095851dfe00cc725665fb44e1c8c84810c8240f Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Fri, 1 May 2020 12:43:18 +0200 Subject: [PATCH] [misc] test/unit: fix typos and assertion of error messages Sinon does not check the contents of the passed error when checked via sinon.stub().calledWith. ``` callback = sinon.stub() callback(new Error("some message")) .calledWith(new Error("completely different message")) === true ``` Cherry-pick plus an additional patch for the joinProject bail-out. (cherry picked from commit d9570fee70701a5f431c39fdbec5f8bc5a7843fe) --- .../coffee/AuthorizationManagerTests.coffee | 60 +++++++------------ .../unit/coffee/WebApiManagerTests.coffee | 6 +- .../coffee/WebsocketControllerTests.coffee | 6 +- 3 files changed, 28 insertions(+), 44 deletions(-) diff --git a/services/real-time/test/unit/coffee/AuthorizationManagerTests.coffee b/services/real-time/test/unit/coffee/AuthorizationManagerTests.coffee index 9856684247..46b9e8be9a 100644 --- a/services/real-time/test/unit/coffee/AuthorizationManagerTests.coffee +++ b/services/real-time/test/unit/coffee/AuthorizationManagerTests.coffee @@ -27,19 +27,19 @@ describe 'AuthorizationManager', -> @AuthorizationManager.assertClientCanViewProject @client, (error) -> expect(error).to.be.null done() - + it "should allow the readAndWrite privilegeLevel", (done) -> @client.params.privilege_level = "readAndWrite" @AuthorizationManager.assertClientCanViewProject @client, (error) -> expect(error).to.be.null done() - + it "should allow the owner privilegeLevel", (done) -> @client.params.privilege_level = "owner" @AuthorizationManager.assertClientCanViewProject @client, (error) -> expect(error).to.be.null done() - + it "should return an error with any other privilegeLevel", (done) -> @client.params.privilege_level = "unknown" @AuthorizationManager.assertClientCanViewProject @client, (error) -> @@ -52,19 +52,19 @@ describe 'AuthorizationManager', -> @AuthorizationManager.assertClientCanEditProject @client, (error) -> error.message.should.equal "not authorized" done() - + it "should allow the readAndWrite privilegeLevel", (done) -> @client.params.privilege_level = "readAndWrite" @AuthorizationManager.assertClientCanEditProject @client, (error) -> expect(error).to.be.null done() - + it "should allow the owner privilegeLevel", (done) -> @client.params.privilege_level = "owner" @AuthorizationManager.assertClientCanEditProject @client, (error) -> expect(error).to.be.null done() - + it "should return an error with any other privilegeLevel", (done) -> @client.params.privilege_level = "unknown" @AuthorizationManager.assertClientCanEditProject @client, (error) -> @@ -84,20 +84,16 @@ describe 'AuthorizationManager', -> @client.params.privilege_level = "unknown" it "should not allow access", () -> - @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "even when authorised at the doc level", -> beforeEach (done) -> @AuthorizationManager.addAccessToDoc @client, @doc_id, done it "should not allow access", () -> - @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "when authorised at the project level", -> beforeEach () -> @@ -105,10 +101,8 @@ describe 'AuthorizationManager', -> describe "and not authorised at the document level", -> it "should not allow access", () -> - @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "and authorised at the document level", -> beforeEach (done) -> @@ -126,10 +120,8 @@ describe 'AuthorizationManager', -> @AuthorizationManager.removeAccessToDoc @client, @doc_id, done it "should deny access", () -> - @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "assertClientCanEditProjectAndDoc", -> beforeEach () -> @@ -142,20 +134,16 @@ describe 'AuthorizationManager', -> @client.params.privilege_level = "readOnly" it "should not allow access", () -> - @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "even when authorised at the doc level", -> beforeEach (done) -> @AuthorizationManager.addAccessToDoc @client, @doc_id, done it "should not allow access", () -> - @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "when authorised at the project level", -> beforeEach () -> @@ -163,10 +151,8 @@ describe 'AuthorizationManager', -> describe "and not authorised at the document level", -> it "should not allow access", () -> - @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" describe "and authorised at the document level", -> beforeEach (done) -> @@ -184,7 +170,5 @@ describe 'AuthorizationManager', -> @AuthorizationManager.removeAccessToDoc @client, @doc_id, done it "should deny access", () -> - @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback - @callback - .calledWith(new Error("not authorised")) - .should.equal true + @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) -> + err.message.should.equal "not authorized" diff --git a/services/real-time/test/unit/coffee/WebApiManagerTests.coffee b/services/real-time/test/unit/coffee/WebApiManagerTests.coffee index a87522c0a9..e65ba93859 100644 --- a/services/real-time/test/unit/coffee/WebApiManagerTests.coffee +++ b/services/real-time/test/unit/coffee/WebApiManagerTests.coffee @@ -60,7 +60,7 @@ describe 'WebApiManager', -> it "should call the callback with an error", -> @callback - .calledWith(new Error("non-success code from web: 500")) + .calledWith(sinon.match({message: "non-success status code from web: 500"})) .should.equal true describe "with no data from web", -> @@ -70,7 +70,7 @@ describe 'WebApiManager', -> it "should call the callback with an error", -> @callback - .calledWith(new Error("no data returned from joinProject request")) + .calledWith(sinon.match({message: "no data returned from joinProject request"})) .should.equal true describe "when the project is over its rate limit", -> @@ -80,5 +80,5 @@ describe 'WebApiManager', -> it "should call the callback with a TooManyRequests error code", -> @callback - .calledWith(new CodedError("rate-limit hit when joining project", "TooManyRequests")) + .calledWith(sinon.match({message: "rate-limit hit when joining project", code: "TooManyRequests"})) .should.equal true diff --git a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee index 79b22dbc81..498b425281 100644 --- a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee +++ b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee @@ -119,7 +119,7 @@ describe 'WebsocketController', -> it "should return an error", -> @callback - .calledWith(new Error("not authorized")) + .calledWith(sinon.match({message: "not authorized"})) .should.equal true it "should not log an error", -> @@ -143,7 +143,7 @@ describe 'WebsocketController', -> it "should return an error", -> @callback - .calledWith(new Error("subscribe failed")) + .calledWith(sinon.match({message: "subscribe failed"})) .should.equal true @callback.args[0][0].message.should.equal "subscribe failed" @@ -369,7 +369,7 @@ describe 'WebsocketController', -> @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback it "should call the callback with an error", -> - @callback.calledWith(@err).should.equal true + @callback.calledWith(sinon.match({message: "not authorized"})).should.equal true it "should not call the DocumentUpdaterManager", -> @DocumentUpdaterManager.getDocument.called.should.equal false