Skip to content

Commit

Permalink
Merge pull request #2 from feup-infolab/feature/quota
Browse files Browse the repository at this point in the history
B2drop Quota
  • Loading branch information
diasp-ppb committed Feb 28, 2018
2 parents f8f8f95 + e519774 commit 0e9b538
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 87 deletions.
159 changes: 99 additions & 60 deletions src/B2Drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ B2Drop.prototype.checkIfFolderExists = function (folderPath, callback)
});
};

B2Drop.prototype.login = function (callback)
B2Drop.prototype.getAuthToken = function (callback)
{
let self = this;

Expand All @@ -79,27 +79,6 @@ B2Drop.prototype.login = function (callback)
});
};

B2Drop.prototype.logout = function (callback)
{
let self = this;

request.get({
url: Uri.logoutUri,
headers: {
requesttoken: self.requesttoken
}
},
function (error, response)
{
if (isNull(error) && response && response.statusCode === 200)
{
self.cookie = null;
}
return callback(error, response);
}
);
};

B2Drop.prototype.changeFolderSetting = function (folderUri, folderID, setting, callback)
{
let self = this;
Expand Down Expand Up @@ -144,67 +123,93 @@ B2Drop.prototype.getShareLink = function (folderUri, password, callback)
{
let self = this;

var queryString = qs.stringify({
format: "json",
password: "",
passwordChanged: "false",
permission: "31",
expireDate: "",
shareType: "3",
path: folderUri
});

request.post({
url: Uri.shareLinkRequest + "?" + queryString,
headers: {
jar: self.cookie,
requesttoken: self.requesttoken
}
},
function (error, response)
var getLink = function (callback)
{
if (!isNull(error))
{
return callback(error, response);
}

queryString = qs.stringify({
var queryString = qs.stringify({
format: "json",
path: folderUri,
reshares: "true"
password: "",
passwordChanged: "false",
permission: "31",
expireDate: "",
shareType: "3",
path: folderUri
});

request.get({
request.post({
url: Uri.shareLinkRequest + "?" + queryString,
headers: {
jar: self.cookie,
requesttoken: self.requesttoken
}
},
function (error, response, body)
function (error, response)
{
if (!isNull(error) || (response && response.statusCode !== 200))
if (!isNull(error))
{
return callback(error, response, null);
return callback(error, response);
}

var info = JSON.parse(body);
const url = info.ocs.data[0].url;
const folderID = info.ocs.data[0].id;
queryString = qs.stringify({
format: "json",
path: folderUri,
reshares: "true"
});

self.changeFolderSetting(folderUri, folderID, {permissions: "15"}, function (err, response)
request.get({
url: Uri.shareLinkRequest + "?" + queryString,
headers: {
jar: self.cookie,
requesttoken: self.requesttoken
}
},
function (error, response, body)
{
if (!isNull(error) || (response && response.statusCode !== 200))
{
return callback(error, response, url);
return callback(error, response, null);
}
self.changeFolderSetting(folderUri, folderID, {password: password}, function (err, response)

var info = JSON.parse(body);
const url = info.ocs.data[0].url;
const folderID = info.ocs.data[0].id;

self.changeFolderSetting(folderUri, folderID, {permissions: "15"}, function (err, response)
{
return callback(err, response, url);
if (!isNull(error) || (response && response.statusCode !== 200))
{
return callback(error, response, url);
}
self.changeFolderSetting(folderUri, folderID, {password: password}, function (err, response)
{
return callback(err, response, url);
});
});
});
});
});
};

if (isNull(self.requesttoken))
{
self.getAuthToken(function (err, response)
{
if (response.statusCode !== 200)
{
return callback(err, response);
}

getLink(function (err, response, url)
{
return callback(err, response, url);
});
});
}
else
{
getLink(function (err, resp)
{
return callback(err, resp);
});
}
};

B2Drop.prototype.getDirectoryContents = function (folderPath, callback)
Expand Down Expand Up @@ -345,4 +350,38 @@ B2Drop.prototype.deleteFolder = function (folderUri, callback)
});
};

B2Drop.prototype.getQuota = function (callback)
{
const self = this;

self.connection.getQuota()
.then(function (resp)
{
return callback(null, resp);
}, function (err)
{
return callback(err, null);
});
};

B2Drop.prototype.testConnection = function (callback)
{
const self = this;

self.connection.getQuota()
.then(function (resp)
{
var keys = Object.keys(resp);
if (keys.length === 2 && keys[0] === "used")
{
return callback(null, "Valid Connection");
}

return callback(resp, "Invalid Connection");
},
function (err)
{
return callback(err, "Invalid Connection");
});
};
module.exports.B2Drop = B2Drop;
102 changes: 75 additions & 27 deletions test/b2drop.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,22 @@ describe("[B2Drop]", function (done)
}, 400);
});

describe("[Login/out]", function ()
describe("[Get auth token]", function ()
{
it("Should Login and Logout", function (done)
it("Should get auth token", function (done)
{
var account = new b2drop(b2dropAccount.username, b2dropAccount.password);
account.login(function (err, response)
account.getAuthToken(function (err, response)
{
response.should.have.property("statusCode", 200);
account.logout(function (err, response)
if (!isNull(account.requesttoken))
{
response.should.have.property("statusCode", 200);
done();
});
}
else
{
done("unnable to get auth token");
}
});
});
});
Expand All @@ -111,20 +114,12 @@ describe("[B2Drop]", function (done)
it("Should succesfully get link", function (done)
{
var account = new b2drop(b2dropAccount.username, b2dropAccount.password);
account.login(function (err, res)
account.getShareLink(testPathFolder1, passwordFolder, function (err, response, shareLink)
{
res.should.have.property("statusCode", 200);
account.getShareLink(testPathFolder1, passwordFolder, function (err, response, shareLink)
{
console.log(shareLink);
response.should.have.property("statusCode", 200);
shareLink.should.contain("https://b2drop.eudat.eu/s/");
account.logout(function (err, response)
{
response.should.have.property("statusCode", 200);
done();
});
});
console.log(shareLink);
response.should.have.property("statusCode", 200);
shareLink.should.contain("https://b2drop.eudat.eu/s/");
done();
});
});
});
Expand Down Expand Up @@ -187,11 +182,6 @@ describe("[B2Drop]", function (done)
{
done("Downloaded file is not equal to mock file. Corrupted transfer?");
}

/* var renamedFilePath = util.pathInApp("/test/mockData/files/test_downloads/zipTest.doc");
fs.rename(testFile.download_location, renamedFilePath, function(err) {
});*/
});
});

Expand Down Expand Up @@ -460,10 +450,68 @@ describe("[B2Drop]", function (done)
});
});

describe("[Quota]", function ()
{
it("Should succesfully get current quota", function (done)
{
var account = new b2drop(b2dropAccount.username, b2dropAccount.password);
account.getQuota(function (err, quota)
{
should.not.exist(err);
should.exist(quota);

if (Object.keys(quota).length === 2)
{
should.exist(quota.used);
should.exist(quota.available);
done();
}
else
{
done("unnable to getQuota");
}
});
});

it ("Should fail to get current quota", function (done)
{
var account = new b2drop(b2dropAccount.username, "Random test");
account.getQuota(function (err, quota)
{
should.exist(err);
should.not.exist(quota);
done();
});
});
});

describe("[Test Connection]", function ()
{
it("Should succesfully establish connection ", function (done)
{
var account = new b2drop(b2dropAccount.username, b2dropAccount.password);
account.testConnection(function (err, resp)
{
should.not.exist(err);
should.exist(resp);
done();
});
});

it ("Should fail to establish connection", function (done)
{
var account = new b2drop(b2dropAccount.username, "Random test");
account.testConnection(function (err, resp)
{
should.exist(err);
should.exist(resp);
done();
});
});
});

after(function (done)
{
done();
});
})
;

});

0 comments on commit 0e9b538

Please sign in to comment.