|
const axios = require('axios'); |
|
const tough = require('tough-cookie'); |
|
const querystring = require('querystring'); |
|
const FormData = require('form-data'); |
|
const fs = require('fs'); |
|
|
|
const config = { |
|
'username': 'myusername', |
|
'password': 'Password', |
|
'domain': 'https://testdomain.io' |
|
} |
|
|
|
class TestCase { |
|
constructor() { |
|
const me = this; |
|
this.cookieJar = new tough.CookieJar(); |
|
const Cookie = tough.Cookie; |
|
this.csrftoken = null; |
|
this.api = axios.create({ |
|
baseURL: config.domain, |
|
xsrfCookieName: 'csrftoken', // This only work in browser |
|
xsrfHeaderName: 'X-CSRFToken', // This only work in browser |
|
withCredentials: true, |
|
maxRedirects: 0, |
|
validateStatus: function(status) { |
|
return status >= 200 && status < 303; |
|
}, |
|
headers: { |
|
'x-requested-with': 'XMLHttpRequest', |
|
'referer': config.domain |
|
} |
|
}); |
|
// Add cookie support for Node |
|
this.api.interceptors.request.use(function (config) { |
|
if (me.csrftoken) { |
|
config.headers['x-csrftoken'] = me.csrftoken; |
|
} |
|
me.cookieJar.getCookies(config.baseURL, function(err, cookies) { |
|
config.headers['cookie'] = cookies.join('; '); |
|
}); |
|
return config; |
|
}); |
|
this.api.interceptors.response.use(function (response) { |
|
if (response.headers['set-cookie']) { |
|
let cookies = []; |
|
if (response.headers['set-cookie'] instanceof Array) { |
|
cookies = response.headers['set-cookie'].map(Cookie.parse); |
|
} else { |
|
cookies = [Cookie.parse(response.headers['set-cookie'])]; |
|
} |
|
cookies.forEach(function(c) { |
|
me.cookieJar.setCookie(c, response.config.baseURL, function(err, c){}); |
|
// Store previous csrftoken |
|
if (c.key === 'csrftoken') { |
|
me.csrftoken = c.value; |
|
} |
|
console.log('Got Cookie', c.key, c.value); |
|
}); |
|
} |
|
return response; |
|
}); |
|
this.loggedIn = false; |
|
} |
|
|
|
async login() { |
|
// Login and aquire credentials |
|
console.log('Login'); |
|
let req = await this.api.get('/accounts/login/'); |
|
console.log('CSRF', this.csrftoken); |
|
req = await this.api.post('/accounts/login/', querystring.stringify({ |
|
'csrfmiddlewaretoken': this.csrftoken, |
|
'email': config.username, |
|
'password': config.password |
|
})); |
|
this.loggedIn = true; |
|
} |
|
|
|
async tearDown() { |
|
// Logout? |
|
} |
|
|
|
async createSomething(title) { |
|
if (!this.loggedIn) { |
|
await this.login(); |
|
} |
|
let res = await this.api.post(`/api/something/add/`, querystring.stringify({ |
|
'title': title |
|
})); |
|
console.log(req.data); |
|
} |
|
|
|
async uploadFile(filePath) { |
|
if (!this.loggedIn) { |
|
await this.login(); |
|
} |
|
const form = new FormData(); |
|
form.append('file', fs.createReadStream(filePath), { |
|
filename: 'upload.file' |
|
}); |
|
let req = await this.api.post( |
|
`/api/file-upload/`, form, { |
|
headers: form.getHeaders() |
|
}); |
|
console.log(req.data); |
|
} |
|
} |
|
|
|
|
|
/* MAIN */ |
|
(async function(){ |
|
const test = new TestCase(); |
|
await test.createSomething('Test title -' + Math.random()); |
|
await test.uploadFile('some-random-file.csv'); |
|
})().catch(e => { |
|
console.log('Error!!!', e); |
|
}); |