'use strict';

require('should');

var CluestrClient = require('../lib/cluestr.js/index.js');

/**
 * Check environment is set up.
 */
before(function() {
  var keys = ['CLUESTR_ID', 'CLUESTR_SECRET', 'ACCESS_TOKEN'];
  keys.forEach(function(key) {
    if(!process.env[key]) {
      process.env[key] = 'fake';
      //throw new Error("To run this test suite, you need to specify environment variable " + key);
    }
  });
});


describe('getAccessToken()', function() {
  var cluestrClient = new CluestrClient(process.env.CLUESTR_ID, process.env.CLUESTR_SECRET);
  it('should crash on invalid values', function(done) {

    cluestrClient.getAccessToken("fake_code", "fake_uri",   function(err) {
      err.toString().should.include('401');
      err.toString().should.include('Client authentication failed');

      done();
    });
  });
});


describe('sendDocument()', function() {
  var cluestrClient = new CluestrClient(process.env.CLUESTR_ID, process.env.CLUESTR_SECRET);

  it('should require an accessToken', function(done) {
    cluestrClient.sendDocument({}, function(err) {
      err.toString().should.include('accessToken');
      done();
    });
  });

  it('should require an identifier', function(done) {
    cluestrClient.setAccessToken(process.env.ACCESS_TOKEN);
    cluestrClient.sendDocument({}, function(err) {
      err.toString().should.include('identifier');
      done();
    });
  });

  it('should allow for noHydrate parameter', function(done) {
    cluestrClient.setAccessToken(process.env.ACCESS_TOKEN);
    cluestrClient.sendDocument({}, false, function(err) {
      err.toString().should.include('identifier');
      done();
    });
  });


});

@@ -280,43 +280,8 @@
     
-process.env%5Bkey%5D = 'fake';%0A      //
 thro
@@ -739,35 +739,50 @@
 de('
-Client authentication faile
+The provided authorization grant is invali
 d');
@@ -851,37 +851,97 @@
  function() %7B%0A  
-var c
+it('should require an accessToken', function(done) %7B%0A    var rawC
 luestrClient = n
@@ -1014,44 +1014,329 @@
 T);%0A
-%0A  it('should require an accessToken
+    rawCluestrClient.sendDocument(%7B%7D, function(err) %7B%0A      err.toString().should.include('accessToken');%0A      done();%0A    %7D);%0A  %7D);%0A%0A  var cluestrClient = new CluestrClient(process.env.CLUESTR_ID, process.env.CLUESTR_SECRET);%0A  cluestrClient.setAccessToken(process.env.ACCESS_TOKEN);%0A%0A  it('should require an identifier
 ', f
@@ -1435,35 +1435,34 @@
 ld.include('
-accessToken
+identifier
 ');%0A      do
@@ -1495,35 +1495,43 @@
 'should 
-require an identifi
+allow for noHydrate paramet
 er', fun
@@ -1630,32 +1630,39 @@
 sendDocument(%7B%7D,
+ false,
  function(err) %7B
@@ -1759,37 +1759,26 @@
 uld 
-allow for noHydrate para
+work as docu
 me
+n
 te
-r
+d
 ', f
@@ -1845,32 +1845,181 @@
 .ACCESS_TOKEN);%0A
+%0A    var datas = %7B%0A      identifier: 'test-identifier',%0A      binary_document_type: 'file',%0A      metadatas: %7B%0A        'foo': 'bar'%0A      %7D,%0A    %7D;%0A%0A
     cluestrClien
@@ -2029,33 +2029,29 @@
 endDocument(
-%7B%7D, false
+datas
 , function(e
@@ -2056,92 +2056,344 @@
 (err
-) %7B%0A      err.toString().should.include('identifier');%0A      done(
+, document) %7B%0A      if(err) %7B%0A        throw err;%0A      %7D%0A      document.should.have.property('_type', 'Document');%0A      document.should.have.property('binary_document_type', '5252ce4ce4cfcd16f55cfa3b');%0A      document.should.have.property('metadatas');%0A      document.metadatas.should.eql(datas.metadatas
 );%0A
+%0A 
     
-%7D
+ done(
 );%0A  
+ 
+ 
 %7D);%0A
-%0A
+  %7D);
 %0A%7D);

Correct send