Browse Source

Pass location.href for oauth redirect. Fixed tests to use mock core. Added logout button.

Harry Hedger 9 years ago
parent
commit
f37df87069
2 changed files with 71 additions and 35 deletions
  1. 25 10
      src/plugins/GoogleDrive.js
  2. 46 25
      test/unit/GoogleDrive.spec.js

+ 25 - 10
src/plugins/GoogleDrive.js

@@ -16,6 +16,7 @@ export default class Google extends Plugin {
 
     this.getFile = this.getFile.bind(this)
     this.getFolder = this.getFolder.bind(this)
+    this.logout = this.logout.bind(this)
     this.renderBrowser = this.renderBrowser.bind(this)
 
     // set default options
@@ -25,7 +26,6 @@ export default class Google extends Plugin {
     this.opts = Object.assign({}, defaultOptions, opts)
     this.currentFolder = 'root'
     this.isAuthenticated = false
-    this.el = this.render(this.core.state)
   }
 
   focus () {
@@ -155,14 +155,28 @@ export default class Google extends Plugin {
     /**
      * Leave this here
      */
-    // fetch(`${this.opts.host}/google/logout`, {
-    //   method: 'get',
-    //   credentials: 'include',
-    //   headers: {
-    //     'Accept': 'application/json',
-    //     'Content-Type': 'application/json'
-    //   }
-    // }).then(res => console.log(res))
+    fetch(`${this.opts.host}/google/logout?redirect=${location.href}`, {
+      method: 'get',
+      credentials: 'include',
+      headers: {
+        'Accept': 'application/json',
+        'Content-Type': 'application/json'
+      }
+    })
+      .then((res) => res.json())
+      .then((res) => {
+        if (res.ok) {
+          console.log('ok')
+          const newState = {
+            authenticated: false,
+            files: [],
+            folders: [],
+            directory: 'root'
+          }
+
+          this.updateState(newState)
+        }
+      })
   }
 
   update (state) {
@@ -199,7 +213,7 @@ export default class Google extends Plugin {
   }
 
   renderAuth () {
-    const link = this.opts.host ? `${this.opts.host}/connect/google` : '#'
+    const link = `${this.opts.host}/connect/google?state=${location.href}`
     return yo`
       <div>
         <h1>Authenticate With Google Drive</h1>
@@ -214,6 +228,7 @@ export default class Google extends Plugin {
 
     return yo`
       <div>
+        <button onclick=${this.logout}/>Logout</button>
         <ul>${folders}</ul>
         <ul>${files}</ul>
       </div>

+ 46 - 25
test/unit/GoogleDrive.spec.js

@@ -3,72 +3,87 @@ import nock from 'nock'
 import Utils from '../../src/core/Utils'
 import Google from '../../src/plugins/GoogleDrive'
 
-test('checkAuthentication success', function (t) {
-  t.plan(1)
+var defaultCore = {
+  state: {
+    googleDrive: {
+      authenticated: false,
+      files: [],
+      folders: [],
+      directory: 'root'
+    }
+  }
+}
 
+test('checkAuthentication success', function (t) {
   nock('http://localhost:3020')
     .get('/google/authorize')
     .reply(200, {
       isAuthenticated: true
     })
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
+
   GoogleDrive.checkAuthentication()
     .then((isAuthed) => {
       t.equal(isAuthed, true)
+      t.end()
     })
 })
 
 test('checkAuthentication fail', function (t) {
-  t.plan(1)
-
   nock('http://localhost:3020')
     .get('/google/authorize')
     .reply(200, {
       isAuthenticated: false
     })
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
+
   GoogleDrive.checkAuthentication()
     .then((isAuthed) => {
       t.equal(isAuthed, false)
+      t.end()
     })
 })
 
 test('getFile: success', function (t) {
-  t.plan(1)
-
   nock('http://localhost:3020')
-    .post('/google/get')
-    .reply(201, (uri, requestBody) => {
-      return {
-        ok: true,
-        id: '12345'
-      }
+    .get('/google/get?fileId=12345')
+    .reply(201, {
+      ok: true,
+      id: '12345'
     })
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
 
   GoogleDrive.getFile('12345')
     .then((result) => {
       t.equal(result.ok, true)
+      t.end()
     })
 })
 
 test('getFile: fileId not a string', function (t) {
-  t.plan(1)
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
   var result = GoogleDrive.getFile()
 
   t.equal(result instanceof Error, true)
+
+  t.end()
 })
 
 test('getFolder: success', function (t) {
-  t.plan(1)
-
   nock('http://localhost:3020')
-  .get('/google/list')
+  .get('/google/list?dir=root')
   .reply(200, {
     items: [{
       mimeType: 'application/vnd.google-apps.folder'
@@ -81,8 +96,11 @@ test('getFolder: success', function (t) {
     }]
   })
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
-  GoogleDrive.getFolder('/')
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
+
+  GoogleDrive.getFolder('root')
     .then((res) => {
       const allFolders = Utils.every(res.folders, function (folder) {
         return folder.mimeType === 'application/vnd.google-apps.folder'
@@ -93,19 +111,22 @@ test('getFolder: success', function (t) {
       })
 
       t.equal(allFolders && allFiles, true)
+      t.end()
     })
 })
 
 test('getFolder: fail', function (t) {
-  t.plan(1)
-
   nock('http://localhost:3020')
   .get('/google/list')
   .reply(500, 'Not authenticated')
 
-  var GoogleDrive = new Google(null, {host: 'http://localhost:3020'})
+  var core = Object.assign({}, defaultCore)
+
+  var GoogleDrive = new Google(core, {host: 'http://localhost:3020'})
+
   GoogleDrive.getFolder('/')
     .then((err) => {
       t.equal(err instanceof Error, true)
+      t.end()
     })
 })