From 110b867ed581edef0b7a70cac14830f7281bd4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 17 Dec 2023 12:52:23 -0500 Subject: [PATCH] meta --- test/meta.js | 99 +++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 59 deletions(-) diff --git a/test/meta.js b/test/meta.js index 84452561d4..f57a1d61a1 100644 --- a/test/meta.js +++ b/test/meta.js @@ -2,13 +2,14 @@ const assert = require('assert'); const async = require('async'); -const request = require('request'); + const nconf = require('nconf'); const db = require('./mocks/databasemock'); const meta = require('../src/meta'); const User = require('../src/user'); const Groups = require('../src/groups'); +const request = require('../src/request'); describe('meta', () => { let fooUid; @@ -489,117 +490,97 @@ describe('meta', () => { }); describe('Access-Control-Allow-Origin', () => { - it('Access-Control-Allow-Origin header should be empty', (done) => { + it('Access-Control-Allow-Origin header should be empty', async () => { const jar = request.jar(); - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: {}, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: {}, + validateStatus: null, jar: jar, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], undefined); - done(); }); + + assert.equal(response.headers['access-control-allow-origin'], undefined); }); - it('should set proper Access-Control-Allow-Origin header', (done) => { + it('should set proper Access-Control-Allow-Origin header', async () => { const jar = request.jar(); const oldValue = meta.config['access-control-allow-origin']; meta.config['access-control-allow-origin'] = 'test.com, mydomain.com'; - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: { - }, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: { }, jar: jar, headers: { origin: 'mydomain.com', }, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com'); - meta.config['access-control-allow-origin'] = oldValue; - done(err); + validateStatus: null, }); + + assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com'); + meta.config['access-control-allow-origin'] = oldValue; }); - it('Access-Control-Allow-Origin header should be empty if origin does not match', (done) => { + it('Access-Control-Allow-Origin header should be empty if origin does not match', async () => { const jar = request.jar(); const oldValue = meta.config['access-control-allow-origin']; meta.config['access-control-allow-origin'] = 'test.com, mydomain.com'; - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: { - }, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: {}, jar: jar, headers: { origin: 'notallowed.com', }, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], undefined); - meta.config['access-control-allow-origin'] = oldValue; - done(err); + validateStatus: null, }); + assert.equal(response.headers['access-control-allow-origin'], undefined); + meta.config['access-control-allow-origin'] = oldValue; }); - it('should set proper Access-Control-Allow-Origin header', (done) => { + it('should set proper Access-Control-Allow-Origin header', async () => { const jar = request.jar(); const oldValue = meta.config['access-control-allow-origin-regex']; meta.config['access-control-allow-origin-regex'] = 'match\\.this\\..+\\.domain.com, mydomain\\.com'; - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: { - }, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: {}, jar: jar, headers: { origin: 'match.this.anything123.domain.com', }, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], 'match.this.anything123.domain.com'); - meta.config['access-control-allow-origin-regex'] = oldValue; - done(err); + validateStatus: null, }); + + assert.equal(response.headers['access-control-allow-origin'], 'match.this.anything123.domain.com'); + meta.config['access-control-allow-origin-regex'] = oldValue; }); - it('Access-Control-Allow-Origin header should be empty if origin does not match', (done) => { + it('Access-Control-Allow-Origin header should be empty if origin does not match', async () => { const jar = request.jar(); const oldValue = meta.config['access-control-allow-origin-regex']; meta.config['access-control-allow-origin-regex'] = 'match\\.this\\..+\\.domain.com, mydomain\\.com'; - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: { - }, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: {}, jar: jar, headers: { origin: 'notallowed.com', }, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], undefined); - meta.config['access-control-allow-origin-regex'] = oldValue; - done(err); + validateStatus: null, }); + assert.equal(response.headers['access-control-allow-origin'], undefined); + meta.config['access-control-allow-origin-regex'] = oldValue; }); - it('should not error with invalid regexp', (done) => { + it('should not error with invalid regexp', async () => { const jar = request.jar(); const oldValue = meta.config['access-control-allow-origin-regex']; meta.config['access-control-allow-origin-regex'] = '[match\\.this\\..+\\.domain.com, mydomain\\.com'; - request.get(`${nconf.get('url')}/api/search?term=bug`, { - form: { - }, - json: true, + const { response } = await request.get(`${nconf.get('url')}/api/search?term=bug`, { + data: { }, jar: jar, headers: { origin: 'mydomain.com', }, - }, (err, response, body) => { - assert.ifError(err); - assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com'); - meta.config['access-control-allow-origin-regex'] = oldValue; - done(err); + validateStatus: null, }); + assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com'); + meta.config['access-control-allow-origin-regex'] = oldValue; }); });