diff --git a/app/waf/src/controllers/wafpolicy.controller.ts b/app/waf/src/controllers/wafpolicy.controller.ts index fd1533d48..a583d1c30 100644 --- a/app/waf/src/controllers/wafpolicy.controller.ts +++ b/app/waf/src/controllers/wafpolicy.controller.ts @@ -227,9 +227,11 @@ export class WafpolicyController extends BaseController { wafpolicy.id, ); } catch (error) { - throw new HttpErrors.unprocessableEntity( - 'check wafpolicy from asg service failed', - ); + if (error.code === 404) { + throw new HttpErrors.NotFound(`ASG error: ${error.message}`); + } else { + throw new HttpErrors.UnprocessableEntity(`ASG error: ${error.message}`); + } } if (!resp || !resp[0]) { diff --git a/app/waf/test/acceptance/wafpolicy.controller.acceptance.ts b/app/waf/test/acceptance/wafpolicy.controller.acceptance.ts index 909e96167..517bac4a4 100644 --- a/app/waf/test/acceptance/wafpolicy.controller.acceptance.ts +++ b/app/waf/test/acceptance/wafpolicy.controller.acceptance.ts @@ -236,6 +236,62 @@ describe('WafpolicyController', () => { }, ); + it( + 'get ' + + prefix + + '/wafpolicies/${id}/adcs/${adcId}: ASG fails when check wafpolicy', + async () => { + const wafpolicy = await givenWafpolicyData(wafapp, { + tenantId: 'a random id', + public: true, + }); + + const adc = await givenAdcData(wafapp, { + management: { + trustedDeviceId: uuid(), + }, + }); + + checkWafpolicyStub.throws(new Error('OMG')); + + const resp = await client + .get(prefix + `/wafpolicies/${wafpolicy.id}/adcs/${adc.id}`) + .set('X-Auth-Token', ExpectedData.userToken) + .set('tenant-id', ExpectedData.tenantId) + .expect(422); + + expect(resp.body.error.message).equal('ASG error: OMG'); + }, + ); + + it( + 'get ' + prefix + '/wafpolicies/${id}/adcs/${adcId}: ASG returns not found', + async () => { + const wafpolicy = await givenWafpolicyData(wafapp, { + tenantId: 'a random id', + public: true, + }); + + const adc = await givenAdcData(wafapp, { + management: { + trustedDeviceId: uuid(), + }, + }); + + let err = new Error('Not found'); + Object.assign(err, {code: 404}); + checkWafpolicyStub.throws(err); + + const resp = await client + .get(prefix + `/wafpolicies/${wafpolicy.id}/adcs/${adc.id}`) + .set('X-Auth-Token', ExpectedData.userToken) + .set('tenant-id', ExpectedData.tenantId) + .expect(404); + + expect(resp.body.error.message).equal('ASG error: Not found'); + }, + ); + it('get ' + prefix + '/wafpolicies: of all', async () => { const wafpolicy = await givenWafpolicyData(wafapp); diff --git a/test/onboard-collection.json b/test/onboard-collection.json index be8f3ae2d..23b7300d0 100644 --- a/test/onboard-collection.json +++ b/test/onboard-collection.json @@ -1992,33 +1992,37 @@ "script": { "id": "8a7c44c1-f82e-4877-b56b-406c1f5efb7d", "exec": [ - "pm.test('response code is 200', function () {", - " pm.response.to.have.status(200);", - "});", - "", - "let resp = pm.response.json();", - "", - "console.log(resp.wafpolicyondevice.state);", - "", - "let retry = Number(pm.environment.get('UPLOAD_RETRY'));", - "if (retry === 0) {", - " pm.test('Upload WAF policy timeout', function () {", - " pm.expect(false).to.be.true;", - " });", - "}", - "pm.environment.set('UPLOAD_RETRY', --retry);", - "", - "pm.test('WAF policy state is not ERROR', function () {", - " pm.expect(resp.wafpolicyondevice.state).to.not.have.string('ERROR');", + "pm.test('response code is 200 or 404', function () {", + " console.log(pm.response.code);", + " pm.expect(pm.response.code).to.be.oneOf([200, 404]);", "});", "", - "if (resp.wafpolicyondevice.state !== 'AVAILABLE') {", + "if (pm.response.code === 404) {", + " // ASG may return 404 sometimes during uploading", " postman.setNextRequest('wait-wafpolicy-upload');", " setTimeout(function(){}, 10000)", - "}", + "} else {", + " let resp = pm.response.json();", "", + " console.log(resp.wafpolicyondevice.state);", "", - "" + " let retry = Number(pm.environment.get('UPLOAD_RETRY'));", + " if (retry === 0) {", + " pm.test('Upload WAF policy timeout', function () {", + " pm.expect(false).to.be.true;", + " });", + " }", + " pm.environment.set('UPLOAD_RETRY', --retry);", + "", + " pm.test('WAF policy state is not ERROR', function () {", + " pm.expect(resp.wafpolicyondevice.state).to.not.have.string('ERROR');", + " });", + "", + " if (resp.wafpolicyondevice.state !== 'AVAILABLE') {", + " postman.setNextRequest('wait-wafpolicy-upload');", + " setTimeout(function(){}, 10000)", + " }", + "}" ], "type": "text/javascript" }