var request = require("supertest");
var should = require("should");
var _ = require("lodash");

var shtest = exports;

request = request("http://localhost:5101");
var gUserSession = "";
var gAdminSession = "";

//var gEmail = "test@lgdales.com";
//var gEmailUpgrade = "testUpgrade@lgdales.com";
//var gPassword = "foofoo";
//var gToken = "00000000-0000-0000-0000-000000000000";

shtest.call = function (data, cb) {
  request.post("/api").send(data)
    .set("Accept", "application/json")
    .expect(200)
    .end(function (err, res) {
      should.not.exist(err);
      res.body = res.body[0];
      cb(err, res);
    });
};

shtest.uid = function (utype) {
  if (utype === "user") {
    return gUserSession.split(":")[1];
  } else if (utype === "admin") {
    return gAdminSession.split(":")[1];
  }
  return "";
}

shtest.session = function (utype) {
  if (utype === "user") {
    return gUserSession;
  } else if (utype === "admin") {
    return gAdminSession;
  }
  return "";
}

shtest.init = function (email, password, cb) {
  var self = this;
  this.call({cmd: "reg.login", email: "shelly", password: ""},
    function (err, res) {
      if (err) {
        cb(err, res);
        return;
      }
      gAdminSession = res.body.data.session;

      // SWD probably want to add reg.create, just to be sure it is there

      self.call({cmd: "reg.login", email: email, password: password},
        function (err, res) {
          if (err) {
            cb(err, res);
            return;
          }
          gUserSession = res.body.data.session;
          cb(err, res);
        });
    });
}

shtest.adminCall = function (data, cb) {
  data.session = gAdminSession;
  this.call(data, cb);
}

shtest.userCall = function (data, cb) {
  data.session = gUserSession;
  this.call(data, cb);
}
@@ -1106,20 +1106,16 @@
 rd: %22%22%7D,
-%0A   
  functio
@@ -1125,24 +1125,24 @@
 err, res) %7B%0A
+
       if (er
@@ -1248,36 +1248,25 @@
     
-// SWD probably want to add 
+self.call(%7Bcmd: %22
 reg.
@@ -1275,39 +1275,132 @@
 eate
+%22
 , 
-just to be sure it is there%0A%0A
+email: email, password: password%7D, function (err, res) %7B%0A        // ignore error for already exists and just try login%0A  
     
@@ -1464,24 +1464,16 @@
 ssword%7D,
-%0A       
  functio
@@ -1630,32 +1630,32 @@
   cb(err, res);%0A
-
         %7D);%0A    
@@ -1650,16 +1650,26 @@
     %7D);%0A
+      %7D);%0A
     %7D);%0A

handle case were st.init user is not created;