|
|
@@ -37,6 +37,10 @@ describe "AuthenticationController", ->
|
|
|
ipMatcherAffiliation: sinon.stub()
|
|
|
"../V1/V1Api": @V1Api = request: sinon.stub()
|
|
|
"../../models/User": { User: @UserModel }
|
|
|
+ "../../../../modules/oauth2-server/app/js/Oauth2Server": @Oauth2Server =
|
|
|
+ Request: sinon.stub()
|
|
|
+ Response: sinon.stub()
|
|
|
+ server: authenticate: sinon.stub()
|
|
|
@user =
|
|
|
_id: ObjectId()
|
|
|
email: @email = "USER@example.com"
|
|
|
@@ -402,90 +406,115 @@ describe "AuthenticationController", ->
|
|
|
|
|
|
describe "requireOauth", ->
|
|
|
beforeEach ->
|
|
|
+ @res.sendStatus = sinon.stub()
|
|
|
@res.send = sinon.stub()
|
|
|
@res.status = sinon.stub().returns(@res)
|
|
|
@middleware = @AuthenticationController.requireOauth()
|
|
|
|
|
|
- describe "when token not provided", ->
|
|
|
+ describe "when Oauth2Server authenticates", ->
|
|
|
beforeEach ->
|
|
|
+ @token =
|
|
|
+ accessToken: "token"
|
|
|
+ user: "user"
|
|
|
+ @Oauth2Server.server.authenticate.yields null, @token
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
- it "should return 401 error", ->
|
|
|
- @res.status.should.have.been.calledWith 401
|
|
|
+ it "should set oauth_token on request", ->
|
|
|
+ @req.oauth_token.should.equal @token
|
|
|
|
|
|
- describe "when token provided", ->
|
|
|
- beforeEach ->
|
|
|
- @V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
- @req.token = "foo"
|
|
|
- @middleware(@req, @res, @next)
|
|
|
+ it "should set oauth on request", ->
|
|
|
+ @req.oauth.access_token.should.equal @token.accessToken
|
|
|
|
|
|
- it "should make request to v1 api with token", ->
|
|
|
- @V1Api.request.should.have.been.calledWith {
|
|
|
- expectedStatusCodes: [401]
|
|
|
- json: token: "foo"
|
|
|
- method: "POST"
|
|
|
- uri: "/api/v1/sharelatex/oauth_authorize"
|
|
|
- }
|
|
|
-
|
|
|
- describe "when v1 api returns error", ->
|
|
|
- beforeEach ->
|
|
|
- @V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
- @req.token = "foo"
|
|
|
- @middleware(@req, @res, @next)
|
|
|
+ it "should set oauth_user on request", ->
|
|
|
+ @req.oauth_user.should.equal "user"
|
|
|
|
|
|
- it "should return status", ->
|
|
|
- @next.should.have.been.calledWith "error"
|
|
|
+ it "should call next", ->
|
|
|
+ @next.should.have.been.calledOnce
|
|
|
|
|
|
- describe "when v1 api status code is not 200", ->
|
|
|
+ describe "when Oauth2Server does not authenticate", ->
|
|
|
beforeEach ->
|
|
|
- @V1Api.request = sinon.stub().yields(null, {statusCode: 401}, {})
|
|
|
- @req.token = "foo"
|
|
|
- @middleware(@req, @res, @next)
|
|
|
+ @Oauth2Server.server.authenticate.yields code: 401
|
|
|
|
|
|
- it "should return status", ->
|
|
|
- @res.status.should.have.been.calledWith 401
|
|
|
+ describe "when token not provided", ->
|
|
|
+ beforeEach ->
|
|
|
+ @middleware(@req, @res, @next)
|
|
|
|
|
|
- describe "when v1 api returns authorized profile and access token", ->
|
|
|
- beforeEach ->
|
|
|
- @oauth_authorize =
|
|
|
- access_token: "access_token"
|
|
|
- user_profile: id: "overleaf-id"
|
|
|
- @V1Api.request = sinon.stub().yields(null, {statusCode: 200}, @oauth_authorize)
|
|
|
- @req.token = "foo"
|
|
|
+ it "should return 401 error", ->
|
|
|
+ @res.sendStatus.should.have.been.calledWith 401
|
|
|
|
|
|
- describe "in all cases", ->
|
|
|
+ describe "when token provided", ->
|
|
|
beforeEach ->
|
|
|
+ @V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
+ @req.token = "foo"
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
- it "should find user", ->
|
|
|
- @UserModel.findOne.should.have.been.calledWithMatch { "overleaf.id": "overleaf-id" }
|
|
|
+ it "should make request to v1 api with token", ->
|
|
|
+ @V1Api.request.should.have.been.calledWith {
|
|
|
+ expectedStatusCodes: [401]
|
|
|
+ json: token: "foo"
|
|
|
+ method: "POST"
|
|
|
+ uri: "/api/v1/sharelatex/oauth_authorize"
|
|
|
+ }
|
|
|
|
|
|
- describe "when user find returns error", ->
|
|
|
+ describe "when v1 api returns error", ->
|
|
|
beforeEach ->
|
|
|
- @UserModel.findOne = sinon.stub().yields("error")
|
|
|
+ @V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
+ @req.token = "foo"
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
- it "should return error", ->
|
|
|
+ it "should return status", ->
|
|
|
@next.should.have.been.calledWith "error"
|
|
|
|
|
|
- describe "when user is not found", ->
|
|
|
+ describe "when v1 api status code is not 200", ->
|
|
|
beforeEach ->
|
|
|
- @UserModel.findOne = sinon.stub().yields(null, null)
|
|
|
+ @V1Api.request = sinon.stub().yields(null, {statusCode: 401}, {})
|
|
|
+ @req.token = "foo"
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
- it "should return unauthorized", ->
|
|
|
+ it "should return status", ->
|
|
|
@res.status.should.have.been.calledWith 401
|
|
|
|
|
|
- describe "when user is found", ->
|
|
|
+ describe "when v1 api returns authorized profile and access token", ->
|
|
|
beforeEach ->
|
|
|
- @UserModel.findOne = sinon.stub().yields(null, "user")
|
|
|
- @middleware(@req, @res, @next)
|
|
|
+ @oauth_authorize =
|
|
|
+ access_token: "access_token"
|
|
|
+ user_profile: id: "overleaf-id"
|
|
|
+ @V1Api.request = sinon.stub().yields(null, {statusCode: 200}, @oauth_authorize)
|
|
|
+ @req.token = "foo"
|
|
|
+
|
|
|
+ describe "in all cases", ->
|
|
|
+ beforeEach ->
|
|
|
+ @middleware(@req, @res, @next)
|
|
|
+
|
|
|
+ it "should find user", ->
|
|
|
+ @UserModel.findOne.should.have.been.calledWithMatch { "overleaf.id": "overleaf-id" }
|
|
|
+
|
|
|
+ describe "when user find returns error", ->
|
|
|
+ beforeEach ->
|
|
|
+ @UserModel.findOne = sinon.stub().yields("error")
|
|
|
+ @middleware(@req, @res, @next)
|
|
|
+
|
|
|
+ it "should return error", ->
|
|
|
+ @next.should.have.been.calledWith "error"
|
|
|
+
|
|
|
+ describe "when user is not found", ->
|
|
|
+ beforeEach ->
|
|
|
+ @UserModel.findOne = sinon.stub().yields(null, null)
|
|
|
+ @middleware(@req, @res, @next)
|
|
|
+
|
|
|
+ it "should return unauthorized", ->
|
|
|
+ @res.status.should.have.been.calledWith 401
|
|
|
+
|
|
|
+ describe "when user is found", ->
|
|
|
+ beforeEach ->
|
|
|
+ @UserModel.findOne = sinon.stub().yields(null, "user")
|
|
|
+ @middleware(@req, @res, @next)
|
|
|
|
|
|
- it "should add user to request", ->
|
|
|
- @req.oauth_user.should.equal "user"
|
|
|
+ it "should add user to request", ->
|
|
|
+ @req.oauth_user.should.equal "user"
|
|
|
|
|
|
- it "should add access_token to request", ->
|
|
|
- @req.oauth.access_token.should.equal "access_token"
|
|
|
+ it "should add access_token to request", ->
|
|
|
+ @req.oauth.access_token.should.equal "access_token"
|
|
|
|
|
|
describe "requireGlobalLogin", ->
|
|
|
beforeEach ->
|