Home Identifier Source Test Repository

tests/core/client.js

  1. import test from 'selenium-webdriver/testing';
  2. import express from 'express';
  3.  
  4. let client,
  5. driver,
  6. server = express();
  7.  
  8. before(function (done) {
  9. server.listen(3000, done);
  10. });
  11.  
  12. after(function (done) {
  13. // Deauthorize the user AFTER all tests have been ran,
  14. // or else authentication will fail...
  15. client.deauthorizeUser(function (err, res) {
  16. should.not.exist(err);
  17. done();
  18. });
  19. });
  20.  
  21. describe('Core Suite', function () {
  22. describe('Authentication', function () {
  23. it('should authenticate the user when returning from the OAuth gateway', function (done) {
  24. server.get('/authorize', function (req, res) {
  25. res.send(`<a id="authorize_link" href="${BufferClient.getAuthorizationUrl(app.client_id, app.redirect_url)}">Authorize</a>`);
  26. })
  27. server.get('/verify', function (req, res) {
  28. client = new BufferClient({
  29. access_token: req.query.code,
  30. client_id: app.client_id,
  31. client_secret: app.client_secret,
  32. redirect_url: app.redirect_url,
  33. authenticated: false
  34. }, function (err, res) {
  35. client._authenticated.should.be.true;
  36. client._access_token.should.not.equal(req.query.code);
  37.  
  38. // Clear out any previously queued updates before running tests
  39. client.getProfiles(function (err, res) {
  40. var profile = client.getProfile(app.profile_id);
  41. profile.getPendingUpdates(function (err, res) {
  42. async.forEachOf(profile.pending_updates, function (update, key, next) {
  43. update.destroy(next);
  44. }, function () {
  45. done();
  46. });
  47. });
  48. });
  49. });
  50. });
  51.  
  52. this.timeout(100000);
  53.  
  54. driver = new seleniumWebdriver
  55. .Builder()
  56. .forBrowser('phantomjs')
  57. .build();
  58.  
  59. // Click through to the authorization page
  60. driver.get('http://localhost:3000/authorize');
  61. driver.findElement(By.tagName('a')).click();
  62.  
  63. // Log in to Buffer
  64. driver.findElement(By.id('email')).sendKeys('buffertest@joemck.ie');
  65. driver.findElement(By.id('password')).sendKeys('password');
  66. driver.findElement(By.name('signin')).click();
  67.  
  68. driver.findElement(By.className('allow')).click();
  69. driver.quit();
  70. });
  71.  
  72. describe('Method: getAuthorizationUrl', function () {
  73. it('should include the client ID', function (done) {
  74. BufferClient.getAuthorizationUrl(app.client_id, app.redirect_url).should.include(app.client_id);
  75. done();
  76. });
  77. it('should include the redirect URI', function (done) {
  78. BufferClient.getAuthorizationUrl(app.client_id, app.redirect_url).should.include(encodeURIComponent(app.redirect_url));
  79. done();
  80. });
  81. });
  82. });
  83.  
  84. describe('Instantiation', function () {
  85. it('should assign the authenticated flag', function (done) {
  86. client.should.have.property('_authenticated');
  87. done();
  88. });
  89. it('should assign the client ID', function (done) {
  90. client.should.have.property('_client_id');
  91. done();
  92. });
  93. it('should assign the client secret', function (done) {
  94. client.should.have.property('_client_secret');
  95. done();
  96. });
  97. it('should assign the access token', function (done) {
  98. client.should.have.property('_access_token');
  99. done();
  100. });
  101. it('should assign the redirect_url', function (done) {
  102. client.should.have.property('_access_token');
  103. done();
  104. });
  105. it('should assign the API version', function (done) {
  106. client.should.have.property('_api_version');
  107. done();
  108. });
  109. it('should assign the API protocol as HTTPS', function (done) {
  110. client.should.have.property('_protocol', 'https');
  111. done();
  112. });
  113. it('should assign the hostname as "api.bufferapp.com"', function (done) {
  114. client.should.have.property('_hostname', 'api.bufferapp.com');
  115. done();
  116. });
  117. it('should assign the host in the format "#{protocol}://#{hostname}"', function (done) {
  118. client.should.have.property('_host', `${client._protocol}://${client._hostname}`);
  119. done();
  120. });
  121. it('should set the options to be passed to querystring.stringify()', function (done) {
  122. client.should.have.property('_stringify_options');
  123. client._stringify_options.should.eql({
  124. arrayFormat: 'index'
  125. });
  126. done();
  127. });
  128. it('should generate the OAuth client', function(done) {
  129. client.should.have.property('client');
  130. done();
  131. });
  132. it('should retrieve the Buffer configuration', function (done) {
  133. client.should.have.property('config');
  134. done();
  135. });
  136. });
  137.  
  138. describe('API Interaction', function () {
  139. describe('Method: get', function () {
  140. it('should respond', function (done) {
  141. expect(client).to.respondTo('get');
  142. done();
  143. });
  144. it('should not throw an error', function (done) {
  145. client.get('info/configuration.json', function (err, res) {
  146. should.not.exist(err);
  147. done();
  148. });
  149. });
  150. it('should use the GET method', function (done) {
  151. client.get('info/configuration.json', function (err, res, response) {
  152. response.req.method.should.equal('GET');
  153. done();
  154. });
  155. });
  156. });
  157. describe('Method: post', function () {
  158. it('should respond', function (done) {
  159. expect(client).to.respondTo('post');
  160. done();
  161. });
  162. it('should not throw an error', function (done) {
  163. client.post(`profiles/${app.profile_id}/updates/shuffle.json`, function (err, res, response) {
  164. should.not.exist(err);
  165. done();
  166. });
  167. });
  168. it('should use the POST method', function (done) {
  169. client.post(`profiles/${app.profile_id}/updates/shuffle.json`, function (err, res, response) {
  170. response.req.method.should.equal('POST');
  171. done();
  172. });
  173. });
  174. });
  175. });
  176.  
  177. describe('Profiles', function () {
  178. describe('Method: getProfiles', function () {
  179. it('should respond', function (done) {
  180. expect(client).to.respondTo('getProfiles');
  181. done();
  182. });
  183. it('should not throw an error', function (done) {
  184. client.getProfiles(function (err, res) {
  185. should.not.exist(err);
  186. done();
  187. });
  188. });
  189. it('should associate the profiles with the client', function (done) {
  190. client.getProfiles(function (err, res) {
  191. client._profiles.should.exist;
  192. done();
  193. });
  194. });
  195. it('should instantiate each profile with the Profile object', function (done) {
  196. client._profiles[app.profile_id].should.be.an.instanceOf(Profile);
  197. done();
  198. });
  199. });
  200.  
  201. describe('Method: getProfile', function () {
  202. it('should respond', function (done) {
  203. expect(client).to.respondTo('getProfile');
  204. done();
  205. });
  206. it('should retrieve the list of profiles', function (done) {
  207. client.getProfile(app.profile_id).should.be.instanceOf(Profile);
  208. done();
  209. });
  210. });
  211. });
  212. });