forked from breeku/trackmania-api-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-helper.js
69 lines (65 loc) · 2.12 KB
/
test-helper.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
const {
loginUbi,
loginTrackmaniaUbi,
loginTrackmaniaNadeo,
} = require('./build/main/lib/api/auth.js')
require('dotenv').config()
const { promises: fs } = require('fs')
const login = async base64 => {
try {
const { ticket } = await loginUbi(base64) // login to ubi, level 0
const ubiTokens = await loginTrackmaniaUbi(ticket) // login to trackmania, level 1
const nadeoTokens = await loginTrackmaniaNadeo(
ubiTokens.accessToken,
'NadeoLiveServices',
) // login to trackmania nadeoliveservices, level 2
console.log('logged in')
return {
ticket,
lv1accessToken: ubiTokens.accessToken,
lv2accessToken: nadeoTokens.accessToken,
accountId: nadeoTokens.accountId,
}
} catch (err) {
console.error(err.response.data)
}
}
;(async () => {
const mode = process.argv[2]
console.log(mode)
if (mode === 'CREATE') {
const email = process.env.EMAIL
const password = process.env.PASSWORD
const base64 = Buffer.from(email + ':' + password).toString('base64')
const credentials = JSON.stringify(await login(base64))
await fs.writeFile('./src/config/test.json', credentials)
console.log('writed file')
} else if (mode === 'RESET') {
await fs.writeFile(
'./src/config/test.json',
JSON.stringify({
ticket: null,
lv1accessToken: null,
lv2accessToken: null,
accountId: null,
}),
)
console.log('reset file')
} else if (mode === 'CHECK') {
// TODO: check also build
const data = JSON.parse(await fs.readFile('./src/config/test.json', 'utf8'))
if (
data.ticket ||
data.lv1accessToken ||
data.lv2refreshToken ||
data.accountId
) {
console.error('Config was not reset. do npm run config:reset')
process.exit(1)
} else {
console.log('File is reset')
}
} else {
console.error('Unknown command')
}
})()