Skip to content

Commit

Permalink
#37 update csis helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
p-a-s-c-a-l committed Sep 6, 2019
1 parent 293c98f commit fc22ea7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "clarity-map-component",
"version": "1.0.0",
"version": "2.0.0",
"private": true,
"license": "LGPL",
"dependencies": {
"@turf/boolean-within": "latest",
"csis-helpers-js": "git://github.com/clarity-h2020/csis-helpers-js.git#semver:^0.3.0",
"csis-helpers-js": "git://github.com/clarity-h2020/csis-helpers-js.git#semver:^0.3.1",
"history": "^4.9.0",
"leaflet": "^1.5.1",
"leaflet-draw": "^1.0.4",
Expand Down
40 changes: 33 additions & 7 deletions src/__tests__/CSISRemoteHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,40 @@ log.enableAll();
* Set auth headers for live API test
*/
beforeAll(async (done) => {

jest.setTimeout(60000);
let cookie = process.env.COOKIE;

if (process.env.ORIGIN) {
CSISRemoteHelpers.csisClient.defaults.headers.common['Origin'] = process.env.Origin;
}

//axios.defaults.withCredentials = true;
if (process.env.COOKIE && process.env.ORIGIN) {
log.info('headers.js fixture found, executing remote API tests');
if (process.env.CSIS_USERNAME && process.env.CSIS_PASSWORD) {
try {
const apiResponse = await CSISRemoteHelpers.login('https://csis.myclimateservice.eu', process.env.CSIS_USERNAME, process.env.CSIS_PASSWORD);
// Unfortunately, this does not work!
// See https://github.com/axios/axios/issues/295#issuecomment-381485257
if(apiResponse.headers['set-cookie'] && Array.isArray(apiResponse.headers['set-cookie']) && apiResponse.headers['set-cookie'].length > 0) {
cookie = apiResponse.headers['set-cookie'][0];
log.debug(`login cookie retrieved: ${cookie}`);
} else {
log.warn('login with credentials from process.env.CSIS_USERNAME && process.env.CSIS_PASSWORD failed, please check file .env.test.local')
}

} catch(error) {
log.error('login to remote CSIS API failed', error);
}
}

if (cookie) {
log.info('cookie available, executing remote API tests');
// this will fail when a new instance of axios has been created in CSISRemoteHelpers
// because the instance is created with the *previous* defaults! :o
//axios.defaults.headers.common[header[0]] = header[1];

// therefore we change the instance in CSISRemoteHelpers :o
CSISRemoteHelpers.csisClient.defaults.headers.common['Cookie'] = process.env.COOKIE;
CSISRemoteHelpers.csisClient.defaults.headers.common['Origin'] = process.env.Origin;
CSISRemoteHelpers.csisClient.defaults.headers.common['Cookie'] = cookie;
} else {
log.warn('no headers ENV VAR (.env.test.local) found, skipping remote API tests');
}
Expand All @@ -43,10 +67,12 @@ beforeAll(async (done) => {
afterAll(() => {
//delete axios.defaults.withCredentials;
delete CSISRemoteHelpers.csisClient.defaults.headers.common[axios.defaults.xsrfHeaderName];
if (process.env.COOKIE && process.env.ORIGIN) {
if ((process.env.CSIS_USERNAME && process.env.CSIS_PASSWORD) || (process.env.COOKIE && process.env.ORIGIN)) {
delete CSISRemoteHelpers.csisClient.defaults.headers.common['Cookie'];
delete CSISRemoteHelpers.csisClient.defaults.headers.common['Origin'];
}

jest.setTimeout(5000);
});


Expand All @@ -56,7 +82,7 @@ describe('Remote API tests without authentication', () => {
const token1 = await CSISRemoteHelpers.getXCsrfToken();
const token2 = await CSISRemoteHelpers.getXCsrfToken();
// if not logged in by session cookie, token will be different for each request
if (!process.env.COOKIE || !process.env.ORIGIN) {
if ((!process.env.COOKIE || !process.env.ORIGIN) && (!process.env.CSIS_USERNAME || !process.env.CSIS_PASSWORD)) {
expect(token1).not.toEqual(token2);
} else {
expect(token1).toEqual(token2);
Expand All @@ -67,7 +93,7 @@ describe('Remote API tests without authentication', () => {

describe('Remote API tests with authentication', () => {

if (!process.env.COOKIE || !process.env.ORIGIN) {
if ((!process.env.COOKIE || !process.env.ORIGIN) && (!process.env.CSIS_USERNAME || !process.env.CSIS_PASSWORD)) {
it.only('no headers.js fixture found, skipping remote API tests', () => {
log.warn('no headers.js fixture found, skipping remote API tests');
});
Expand Down

0 comments on commit fc22ea7

Please sign in to comment.