-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtils.js
149 lines (139 loc) · 4.79 KB
/
Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*eslint-disable */
const fs = require('fs');
const YAMLJS = require('yamljs');
const chai = require('chai');
chai.should();
function loadYAMLConfigFile(file) {
return YAMLJS.parse(fs.readFileSync(file).toString());
}
const yamlConfig = loadYAMLConfigFile('./config.yaml');
const utils = {
projectId: process.env.PROJECTID || yamlConfig.projectId,
apiGateway: process.env.APIGATEWAY || yamlConfig.apiGateway,
loginKey: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
loginName: process.env.LOGINNAME || yamlConfig.login.loginname,
loginPass: process.env.PASSWORD || yamlConfig.login.password.toString(),
};
/**
* return the current month + date in a 4 digit format
*/
function getMonthAndDate() {
const date = new Date();
const month = (date.getMonth() + 1) < 10 ? (`0${date.getMonth() + 1}`) : (date.getMonth() + 1);
const day = date.getDate();
return month + day;
}
/**
* sleep for a desiganted time passed as a parameter.
* @return {Promise} returns a promise.
* @param {long} time sleep time, in milliseconds.
*/
function sleep(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
/**
*
* @param {string} password encode the password.
*/
function encode(password) {
const loginKey = utils.loginKey;
let output = '';
let chr1,
chr2,
chr3 = '';
let enc1,
enc2,
enc3,
enc4 = '';
let i = 0;
do {
chr1 = password.charCodeAt(i++);
chr2 = password.charCodeAt(i++);
chr3 = password.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + loginKey.charAt(enc1) + loginKey.charAt(enc2)
+ loginKey.charAt(enc3) + loginKey.charAt(enc4);
} while (i < password.length);
return output;
}
/**
* login function, pass in [message] to get expected results, successful login by default if no msg is passed.
* @param {JSON} reqBody login message, including username and password (and verification code, if required).
* @param {string} msg
*/
function login(reqBody, msg) {
console.log('login',reqBody)
if (!arguments[1]) { msg = 'success'; }
global.user = {};
if (!reqBody.username || !reqBody.password) {
throw new Error('reqBody格式错误,必须包含username以及加密过的password!');
}
reqBody.password = encode(reqBody.password);
const authorize = '/oauth/oauth/authorize?scope=default&redirect_uri=https://choerodon.io&response_type=token&realm=default&state=client&client_id=client';
return chai.request(utils.apiGateway)
.get(authorize)
.redirects(0)
.then((res) => {
// console.dir(res.header)
let cookie;
cookie = res.header['set-cookie'][0].split(';')[0];
if (msg == 'success') {
// console.log('login success')
// Login success, pass token to global.user.token
return chai.request(utils.apiGateway)
.post('/oauth/login')
.redirects(0)
.set('Cookie', cookie)
.set('content-type', 'application/x-www-form-urlencoded')
.send(reqBody)
.then((res) => {
// console.dir(res.header)
cookie = res.header['set-cookie'][0].split(';')[0];
return chai.request(utils.apiGateway)
.get(authorize)
.redirects(0)
.set('Cookie', cookie)
.then((res) => {
let location = res.header['location'];
if (location === undefined) {
throw new Error("Error GET TOKEN");
}
// pass user info including login name and user token to global.user
global.user.username = reqBody.username;
global.user.token = 'Bearer ' + location.split('#access_token=')[1].split('&token_type')[0];
console.log(" [Login] User " + global.user.username + " logged in!\n");
return res;
});
});
} else {
console.log(reqBody);
return chai.request(utils.apiGateway)
.post('/oauth/login')
// .redirects(0)
.set('Cookie', cookie)
.set('content-type', 'application/x-www-form-urlencoded')
.send(reqBody)
.then((res) => {
console.log(res.header);
return res;
});
}
});
}
/**
* logout function, logout the current user.
*/
function logout() {
return chai.request(utils.apiGateway)
.get('/oauth/logout')
.set('Authorization', global.user.token);
}
module.exports = { config: utils, login, logout, getMonthAndDate, sleep };