var test = require('tape')
  , WSKey = require('./')
  , wsk = new WSKey('abc123', 'SECRETSHH')
  , user = { principalID: 'principalID', principalIDNS: 'principalIDNS' }
  , url = 'https://www.oclc.org/test/?cee=dee and bee&aye=bee'
  ;

test('normalized request is normalized', function(t) {
    var conf = 'abc123\n123456\n99999\n\nGET\nwww.oclc.org\n443\n/wskey\naye=bee\ncee=dee%20and%20bee\n'
      , debug = { time: 123456, nonce: 99999 }
      , norm = wsk._normalizeRequest('get', url, {_debug: debug})
      ;

    t.equal(norm, conf);
    t.end();
});

test('hmac\'d sig is hmac\'d', function(t) {
    var sig = 'Yy9/wwejQt0Pk6yHS7ziRO+c+APCvtO29Egg/1NfY6Q='
      , debug = { time: 123456, nonce: 99999 }
      , header = wsk.HMACSignature('get', url, {_debug: debug} )
      ;

    t.ok(header.indexOf('signature="' + sig + '"') > -1, 'signature matches that in header');
    t.end();
});

test('user gets added when passed', function(t) {
    var header = wsk.HMACSignature('GET', url, {'user': user});

    t.ok(header.indexOf('principalID="principalID"') > -1, 'principalID exists in header');
    t.ok(header.indexOf('principalIDNS="principalIDNS"') > -1, 'principalIDNS exists in header');
    t.end();
});

test('user gets passed when initialized', function(t) {
  var w = new WSKey('abc123', 'SECRETSHH', {user: user});
  var h = w.HMACSignature('GET', url);

  t.ok(h.indexOf('principalID="principalID"') > -1);
  t.ok(h.indexOf('principalIDNS="principalIDNS"') > -1);

  t.end();
});

test('addUser works', function(t) {
  wsk.addUser(user);
  t.deepEquals(user, wsk.user, 'addUser assigns user');

  wsk.addUser();
  t.deepEquals({}, wsk.user, 'passing no user zeros-out WSKey.user');
  t.end();
});
@@ -487,31 +487,21 @@
 ', url, 
-%7B_
 debug
-: debug%7D
 )%0A      
@@ -750,24 +750,13 @@
 rl, 
-%7B_
 debug
-: debug%7D 
 )%0A  
@@ -872,32 +872,36 @@
 d();%0A%7D);%0A%0Atest('
+new 
 user gets added 
@@ -898,19 +898,8 @@
 ets 
-added when 
 pass
@@ -909,32 +909,95 @@
 , function(t) %7B%0A
+    var newUser = %7B principalID: 'zyx', principalIDNS: 'wvu' %7D%0A
     var header =
@@ -1031,22 +1031,15 @@
 rl, 
-%7B'user': u
+newU
 ser
-%7D
 );%0A%0A
@@ -1080,30 +1080,34 @@
 ID=%22
-principalID%22') %3E -1, '
+zyx%22') %3E -1, 'new user%5C's 
 prin
@@ -1114,21 +1114,17 @@
 cipalID 
-exist
+i
 s in hea
@@ -1174,32 +1174,34 @@
 NS=%22
-principalIDNS%22') %3E -1, '
+wvu%22') %3E -1, 'new user%5C's 
 prin
@@ -1214,13 +1214,9 @@
 DNS 
-exist
+i
 s in
@@ -1347,20 +1347,12 @@
 H', 
-%7B
 user
-: user%7D
 );%0A 
@@ -1386,24 +1386,61 @@
 ET', url);%0A%0A
+  t.ok(w.hasUser(), 'user exists');%0A%0A
   t.ok(h.ind
@@ -1477,16 +1477,44 @@
 %22') %3E -1
+, 'principalID is in header'
 );%0A  t.o
@@ -1562,16 +1562,46 @@
 %22') %3E -1
+, 'principalIDNS is in header'
 );%0A%0A  t.
@@ -1648,16 +1648,73 @@
 on(t) %7B%0A
+  t.notOk(wsk.hasUser(), 'WSKey initially has no user')%0A%0A
   wsk.ad
@@ -1872,20 +1872,538 @@
 er');%0A  t.end();%0A%7D);
+%0A%0Aif ( process.env.WSKEY && process.env.WSKEY_SECRET && process.env.INST && process.env.SCOPE ) %7B%0A  test('requestAccessToken', function(t) %7B%0A    var wk = new WSKey(process.env.WSKEY, process.env.WSKEY_SECRET)%0A      , authInstID = process.env.INST%0A      , scope = process.env.SCOPE.split(',')%0A      ;%0A%0A    wk.requestAccessToken(%7B%0A      authenticatingInstitutionID: authInstID,%0A      scope: scope%0A    %7D, function(err, resp, auth) %7B%0A      t.ok(err === null)%0A      t.ok(auth.match(/%5EBearer/))%0A      t.end()%0A    %7D);%0A  %7D);%0A%7D

update tests + add accessToken tests