From a70f983503c05c74fd33e6e9940f838319a57188 Mon Sep 17 00:00:00 2001
From: Eric Eastwood <contact@ericeastwood.com>
Date: Tue, 29 Aug 2017 01:21:59 -0500
Subject: [PATCH] Add OAuth access token authentication support

Continuation of https://github.com/repo-utils/gitlab/pull/28
---
 lib/gitlab.js       |  9 ++++++-
 test/config.js      |  1 +
 test/gitlab.test.js | 58 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/lib/gitlab.js b/lib/gitlab.js
index 9871775..78cb4dc 100644
--- a/lib/gitlab.js
+++ b/lib/gitlab.js
@@ -27,12 +27,14 @@ module.exports = Gitlab;
  * @param {Object} options
  *  - {String} api, api root url, e.g.: 'http://gitlab.com/api/v3'
  *  - {String} privateToken, You can find or reset your private token in your profile.
+ *  - {String} accessToken, Obtained via OAuth
  */
 function Gitlab(options) {
   options = options || {};
   options.api = options.api || 'https://gitlab.com/api/v3';
   RESTFulClient.call(this, options);
   this.privateToken = options.privateToken;
+  this.accessToken = options.accessToken;
 
   this.addResources(resources);
 
@@ -45,7 +47,12 @@ function Gitlab(options) {
 util.inherits(Gitlab, RESTFulClient);
 
 Gitlab.prototype.setAuthentication = function (req) {
-  req.params.data.private_token = req.params.data.private_token || this.privateToken;
+  var accessToken = req.params.data.access_token || this.accessToken;
+  if (accessToken) {
+    req.params.data.access_token = accessToken;
+  } else {
+    req.params.data.private_token = req.params.data.private_token || this.privateToken;
+  }
   return req;
 };
 
diff --git a/test/config.js b/test/config.js
index 5bfb4a1..841d69e 100644
--- a/test/config.js
+++ b/test/config.js
@@ -1,5 +1,6 @@
 module.exports = {
   api: process.env.NODE_GITLAB_API || 'https://gitlab.com/api/v3',
   privateToken: process.env.NODE_GITLAB_TOKEN || 'enEWf516mA168tP6BiVe',
+  accessToken: process.env.NODE_GITLAB_ACCESS_TOKEN || 'dbbf3e41770035b126fe203138c94007f7bc15c9ce2dd18766d243eda904dfb3',
   requestTimeout: 30000,
 };
diff --git a/test/gitlab.test.js b/test/gitlab.test.js
index 50db1cb..3f545e5 100644
--- a/test/gitlab.test.js
+++ b/test/gitlab.test.js
@@ -16,8 +16,66 @@
 
 var should = require('should');
 var client = require('./client');
+var gitlab = require('../');
 
 describe('gitlab.test.js', function () {
+  describe('setAuthentication', function () {
+    var req = {};
+    beforeEach(function () {
+      req = {
+        params: {
+          data: {}
+        }
+      }
+    });
+
+    it('should default to using a private token', function() {
+      var privateToken = 'private';
+      gitlab.prototype.setAuthentication.call({
+        privateToken: privateToken
+      }, req);
+
+      req.params.data.private_token.should.equal(privateToken);
+      req.params.data.should.not.have.keys('access_token');
+    });
+
+    it('should use access token if provided', function() {
+      var accessToken = 'access';
+      gitlab.prototype.setAuthentication.call({
+        accessToken: accessToken
+      }, req);
+
+      req.params.data.access_token.should.equal(accessToken);
+      req.params.data.should.not.have.keys('private_token');
+    });
+
+    it('should prefer already passed private token on the request object', function() {
+      var privateToken = 'private';
+      var existingPrivateToken = 'already-private';
+
+      req.params.data.private_token = existingPrivateToken;
+      gitlab.prototype.setAuthentication.call({
+        privateToken: privateToken
+      }, req);
+
+      req.params.data.private_token.should.equal(existingPrivateToken);
+      req.params.data.should.not.have.keys('access_token');
+    });
+
+    it('should prefer already passed access token on the request object', function() {
+      var accessToken = 'access';
+      var existingAccessToken = 'already-access';
+
+      req.params.data.access_token = existingAccessToken;
+      gitlab.prototype.setAuthentication.call({
+        accessToken: accessToken
+      }, req);
+
+      req.params.data.access_token.should.equal(existingAccessToken);
+      req.params.data.should.not.have.keys('private_token');
+    });
+  });
+
   describe('Client.request()', function () {
     it('should request success', function (done) {
       client.request('get', '/projects', {}, function (err, projects) {