Skip to content

Commit

Permalink
Addressing the code review comments WRT axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Velang RN authored and Velang RN committed Aug 22, 2022
1 parent 34712cb commit c5f72cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
5 changes: 3 additions & 2 deletions testing/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
34 changes: 16 additions & 18 deletions testing/src/uploadResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ 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<AxiosResponse<any>> => {
const url = "https://api.testquality.com/api/oauth/access_token";
const body = JSON.stringify({
async function getAuthToken (): Promise<AxiosResponse<String>> {
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",
},
};
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) {
Expand All @@ -34,26 +35,23 @@ const getAuthToken = async (): Promise<AxiosResponse<any>> => {

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();

0 comments on commit c5f72cf

Please sign in to comment.