!1 Merge back to master

Merge pull request !1 from dev branch
This commit is contained in:
hekk 2018-05-27 21:32:55 +08:00
parent 30c963a5f0
commit 61a1c93f0f
89 changed files with 15859 additions and 2 deletions

View file

@ -0,0 +1,34 @@
import { login, logout } from "../api_auth";
import { config } from "../../config";
const serverAddr = config.serverAddr;
const testId = config.testId;
const testPwd = config.testPwd;
export function testAuth() {
return testLogin()
.then(testLogout)
.catch(err => {
console.error("auth: fail", err);
});
}
export function testLogin() {
return login(serverAddr, testId, testPwd).then(ok => {
if (ok === true) {
console.log("login api: ok");
} else {
throw new Error("login api: failed");
}
});
}
export function testLogout() {
return logout(serverAddr).then(ok => {
if (ok === true) {
console.log("logout api: ok");
} else {
throw new Error("logout api: failed");
}
});
}

View file

@ -0,0 +1,167 @@
import { FileUploader } from "../api_upload";
import {
del,
list,
shadowId,
publishId,
setDownLimit,
addLocalFiles
} from "../api_share";
import { testLogin, testLogout } from "./api_auth_test";
const fileName = "filename";
function upload(fileName) {
return new Promise(resolve => {
const onStart = () => true;
const onProgress = () => true;
const onFinish = () => resolve();
const onError = err => {
throw new Error(JSON.stringify(err));
};
const file = new File(["foo"], fileName, {
type: "text/plain"
});
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
uploader.uploadFile(file);
});
}
function getIdFromList(list, fileName) {
if (list == null) {
throw new Error("list: list fail");
}
// TODO: should verify file name
const filterInfo = list.find(info => {
return info.PathLocal.includes(fileName);
});
if (filterInfo == null) {
console.error(list);
throw new Error("list: file name not found");
} else {
return filterInfo.Id;
}
}
function delWithName(fileName) {
return list().then(infoList => {
const infoToDel = infoList.find(info => {
return info.PathLocal.includes(fileName);
});
if (infoToDel == null) {
console.warn("delWithName: name not found");
} else {
return del(infoToDel.Id);
}
});
}
export function testShadowPublishId() {
return testLogin()
.then(() => upload(fileName))
.then(list)
.then(infoList => {
return getIdFromList(infoList, fileName);
})
.then(shareId => {
return shadowId(shareId).then(secretId => {
if (shareId === secretId) {
throw new Error("shadowId: id not changed");
} else {
return secretId;
}
});
})
.then(secretId => {
return list().then(infoList => {
const info = infoList.find(info => {
return info.Id === secretId;
});
if (info.PathLocal.includes(fileName)) {
console.log("shadowId api: ok", secretId);
return secretId;
} else {
throw new Error("shadowId pai: file not found", infoList, fileName);
}
});
})
.then(secretId => {
return publishId(secretId).then(publicId => {
if (publicId === secretId) {
// TODO: it is not enough to check they are not equal
throw new Error("publicId: id not changed");
} else {
console.log("publishId api: ok", publicId);
return publicId;
}
});
})
.then(shareId => del(shareId))
.then(testLogout)
.catch(err => {
console.error(err);
delWithName(fileName);
});
}
export function testSetDownLimit() {
const downLimit = 777;
return testLogin()
.then(() => upload(fileName))
.then(list)
.then(infoList => {
return getIdFromList(infoList, fileName);
})
.then(shareId => {
return setDownLimit(shareId, downLimit).then(ok => {
if (!ok) {
throw new Error("setDownLimit: failed");
} else {
return shareId;
}
});
})
.then(shareId => {
return list().then(infoList => {
const info = infoList.find(info => {
return info.Id == shareId;
});
if (info.DownLimit === downLimit) {
console.log("setDownLimit api: ok");
return shareId;
} else {
throw new Error("setDownLimit api: limit unchanged");
}
});
})
.then(shareId => del(shareId))
.then(testLogout)
.catch(err => {
console.error(err);
delWithName(fileName);
});
}
// TODO: need to add local file and test
export function testAddLocalFiles() {
return testLogin()
.then(() => addLocalFiles())
.then(ok => {
if (ok) {
console.log("addLocalFiles api: ok");
} else {
throw new Error("addLocalFiles api: failed");
}
})
.then(() => testLogout())
.catch(err => {
console.error(err);
});
}

View file

@ -0,0 +1,25 @@
import { testAuth } from "./api_auth_test";
import { testUploadOneFile } from "./api_upload_test";
import {
testAddLocalFiles,
testSetDownLimit,
testShadowPublishId
} from "./api_share_test";
import { testUpDownBatch } from "./api_up_down_batch_test";
console.log("Test started");
const fileName = `test_filename${Date.now()}`;
const file = new File(["foo"], fileName, {
type: "text/plain"
});
testAuth()
.then(testShadowPublishId)
.then(() => testUploadOneFile(file, fileName))
.then(testSetDownLimit)
.then(testAddLocalFiles)
.then(testUpDownBatch)
.then(() => {
console.log("Tests are finished");
});

View file

@ -0,0 +1,97 @@
import axios from "axios";
import md5 from "md5";
import { config } from "../../config";
import { testUpload } from "./api_upload_test";
import { list, del } from "../api_share";
import { testLogin, testLogout } from "./api_auth_test";
export function testUpDownBatch() {
const fileInfos = [
{
fileName: "test_2MB_1",
content: new Array(1024 * 1024 * 2).join("x")
},
{
fileName: "test_1MB_1",
content: new Array(1024 * 1024 * 1).join("x")
},
{
fileName: "test_2MB_2",
content: new Array(1024 * 1024 * 2).join("x")
},
{
fileName: "test_1B",
content: `${new Array(3).join("o")}${new Array(3).join("x")}`
}
];
return testLogin()
.then(() => {
const promises = fileInfos.map(info => {
const file = new File([info.content], info.fileName, {
type: "text/plain"
});
return testUpAndDownOneFile(file, info.fileName);
});
return Promise.all(promises);
})
.then(() => {
testLogout();
})
.catch(err => console.error(err));
}
export function testUpAndDownOneFile(file, fileName) {
return delTestFile(fileName)
.then(() => testUpload(file))
.then(shareId => testDownload(shareId, file))
.catch(err => console.error(err));
}
function delTestFile(fileName) {
return list().then(infos => {
const info = infos.find(info => {
return info.PathLocal === fileName;
});
if (info == null) {
console.log("up-down: file not found", fileName);
} else {
return del(info.Id);
}
});
}
function testDownload(shareId, file) {
return axios
.get(`${config.serverAddr}/download?shareid=${shareId}`)
.then(response => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = event => {
const upHash = md5(event.target.result);
const downHash = md5(response.data);
if (upHash !== downHash) {
console.error(
"up&down: hash unmatch",
file.name,
upHash,
downHash,
upHash.length,
downHash.length
);
} else {
console.log("up&down: ok: hash match", file.name, upHash, downHash);
resolve();
}
};
reader.onerror = err => reject(err);
reader.readAsText(file);
});
});
}

View file

@ -0,0 +1,63 @@
import { FileUploader } from "../api_upload";
import { list, del } from "../api_share";
import { testLogin, testLogout } from "./api_auth_test";
function verify(fileName) {
return list()
.then(list => {
if (list == null) {
throw new Error("upload: list fail");
}
// TODO: should verify file name
const filterInfo = list.find(info => {
return info.PathLocal.includes(fileName);
});
if (filterInfo == null) {
console.error(list);
throw new Error("upload: file name not found");
} else {
return filterInfo.Id;
}
})
.then(shareId => {
console.log("upload api: ok");
del(shareId);
})
.then(testLogout)
.catch(err => {
throw err;
});
}
export function testUpload(file) {
const onStart = () => true;
const onProgress = () => true;
const onFinish = () => true;
const onError = err => {
throw new Error(JSON.stringify(err));
};
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
return uploader.uploadFile(file).catch(err => {
console.error(err);
});
}
export function testUploadOneFile(file, fileName) {
const onStart = () => true;
const onProgress = () => true;
const onFinish = () => true;
const onError = err => {
throw new Error(JSON.stringify(err));
};
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
return testLogin()
.then(() => uploader.uploadFile(file))
.then(() => verify(fileName))
.catch(err => {
console.error(err);
});
}