From c5f72cfa3a3bcd6d87e692cb6ef8892e3c25f138 Mon Sep 17 00:00:00 2001 From: Velang RN Date: Mon, 22 Aug 2022 11:22:37 -0700 Subject: [PATCH] Addressing the code review comments WRT axios --- testing/src/package.json | 5 +++-- testing/src/uploadResult.ts | 34 ++++++++++++++++------------------ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/testing/src/package.json b/testing/src/package.json index d5be8a9b45..accf732d54 100644 --- a/testing/src/package.json +++ b/testing/src/package.json @@ -16,7 +16,7 @@ "posttest": "npm run combine-reports && npm run generate-report", "test": "npm run scripts || npm run posttest", "Run headless for all modes": "npm run test", - "uploadResultTQ" : "npx ts-code uploadResult.ts" + "uploadResultTQ" : "ts-code uploadResult.ts" }, "devDependencies": { "@types/jest": "^28.1.0", @@ -30,6 +30,7 @@ "typescript": "^4.6.2", "webpack": "^5.70.0", "form-data": "^4.0.0", - "ts-node": "^10.9.1" + "ts-node": "^10.9.1", + "axios": "^0.27.2" } } diff --git a/testing/src/uploadResult.ts b/testing/src/uploadResult.ts index 0f571b4de0..293856c8cd 100644 --- a/testing/src/uploadResult.ts +++ b/testing/src/uploadResult.ts @@ -6,16 +6,17 @@ const TQ_USERNAME = process.env.TQ_USER_NAME; const TQ_PASSWORD = process.env.TQ_PASSWORD; const TQ_SECRET = process.env.TQ_SECRET; const TQ_RUN_ID = process.env.TQ_RUN_ID; //( default id is 26606. Will need to change once we have integrated TQ account for the team) +const TQ_API_URL = 'https://api.testquality.com/api' -const getAuthToken = async (): Promise> => { - const url = "https://api.testquality.com/api/oauth/access_token"; - const body = JSON.stringify({ +async function getAuthToken (): Promise> { + const auth_url = `${TQ_API_URL}/oauth/access_token`; + const body = { grant_type: "password", client_id: "2", client_secret: TQ_SECRET, username: TQ_USERNAME, password: TQ_PASSWORD, - }); + }; const settings = { headers: { "Content-Type": "application/json", @@ -23,7 +24,7 @@ const getAuthToken = async (): Promise> => { }; try { console.log("Fetching the token from TestQuality"); - const response = await axios.post(url, body, settings); + const response = await axios.post(auth_url, body, settings); console.log("Token Fetched"); return response.data["access_token"]; } catch (error) { @@ -34,26 +35,23 @@ const getAuthToken = async (): Promise> => { async function uploadResultFile() { const auth = await getAuthToken(); - const url = `https://api.testquality.com/api/plan/${TQ_RUN_ID}/junit_xml`; + const url = `${TQ_API_URL}/plan/${TQ_RUN_ID}/junit_xml`; var formData = new FormData(); var resultFile = fs.createReadStream("test-results.xml"); formData.append("file", resultFile, "test-results.xml"); try { - if (String(auth).length > 1) { - const settings = { - headers: { - ...formData.getHeaders(), - Authorization: `Bearer ${auth}`, - }, - }; - const response = await axios.post(url, formData, settings); - console.log("Successfully uploaded the test results file to TestQuality"); - return true; - } + const settings = { + headers: { + ...formData.getHeaders(), + Authorization: `Bearer ${auth}`, + }, + }; + await axios.post(url, formData, settings); + console.log("Successfully uploaded the test results file to TestQuality"); + return true; } catch (error) { console.error(error); throw error; } } - uploadResultFile();