Node.js Oauth/Zoom API post请求-Mocha集成测试失败,出现400(客户端错误),而应用程序本身似乎正常工作
我无法理解为什么集成测试不能使用下面的代码。该准则的工作原理如下;“makeOauthRequest”函数向Oauth API发出请求以检索访问令牌。“createMeeting”函数接收访问令牌并使用它向Zoom API发出post请求 如果我在应用程序文件本身中调用“createMeeting”函数,调用的调用与在测试文件中的调用相同(使用“createMeeting”(“Please Work API”,“2021-04-26T19:20:00Z”)),它将工作并成功发出post请求。然而,根据下面的测试文件,当我通过Mocha进行相同的调用时,它抛出了一个400错误。有人能帮我理解为什么存在这种差异吗 我已经在测试文件中记录了环境变量,以检查在理解它们时测试是否有问题,但它们似乎没有问题Node.js Oauth/Zoom API post请求-Mocha集成测试失败,出现400(客户端错误),而应用程序本身似乎正常工作,node.js,oauth-2.0,oauth,mocha.js,integration-testing,Node.js,Oauth 2.0,Oauth,Mocha.js,Integration Testing,我无法理解为什么集成测试不能使用下面的代码。该准则的工作原理如下;“makeOauthRequest”函数向Oauth API发出请求以检索访问令牌。“createMeeting”函数接收访问令牌并使用它向Zoom API发出post请求 如果我在应用程序文件本身中调用“createMeeting”函数,调用的调用与在测试文件中的调用相同(使用“createMeeting”(“Please Work API”,“2021-04-26T19:20:00Z”)),它将工作并成功发出post请求。然而
// Test file
const funcs = require("../auto/autoNeg.js");
const assert = require("chai").assert;
require("dotenv").config({ path : '/Users/frederickgodsell/codefiles/mokapot/auto/.env'});
describe('Simple integration', function() {
it('Integration works', async function() {
const resp = await createMeeting("Please Work API", "2021-04-26T19:20:00Z");
console.log(resp)
});
});
// Fails with 'Error: Request failed with status code 400'
// App file
const axios = require("axios").default;
require("dotenv").config();
const fs = require("fs");
const jsonString = fs.readFileSync(`/Users/frederickgodsell/codefiles/mokapot/refresh.json`);
const fileData = JSON.parse(jsonString);
const refTok = fileData.refresh_token;
const oathOptions = {
headers: {
Authorization:
"Basic " +
Buffer.from(process.env.ID + ":" + process.env.SECRET).toString("base64"),
},
params: {
grant_type: "refresh_token",
refresh_token: refTok,
},
};
makeOauthRequest = async () => {
try {
const oauthPostRequest = await axios.post(
"https://zoom.us/oauth/token",
{},
oathOptions
);
let accessPlusRefresh = JSON.stringify(
await oauthPostRequest.data,
null,
2
);
fs.writeFile(`/Users/frederickgodsell/codefiles/mokapot/refresh.json`, accessPlusRefresh, (error) => {
if (error) {
console.log("Error writing file", error);
} else {
console.log("JSON file sucessfully updated");
}
});
// console.log(oauthPostRequest.data)
return oauthPostRequest.data;
} catch (error) {
console.log(error);
}
};
createMeeting = async (topic, startTime) => {
let meetingBody = {
topic: topic,
start_time: startTime,
};
let response = await makeOauthRequest();
let accessToken = await response.access_token;
const meetingOptions = {
headers: {
authorization: `Bearer ${await accessToken}`,
"Content-Type": "application/json",
},
};
if (!topic || !startTime) {
console.log('Please provide both a topic and a start time')
return 'Please provide both a topic and a start time'
}
try {
let meetingResponse = await axios.post(
`${process.env.API_URL}`,
meetingBody,
meetingOptions
);
console.log(`Meetin booked. UUID : ${meetingResponse.data.uuid}`);
return meetingResponse;
} catch (err) {
console.log(err.response.data);
}
};
module.exports.createMeeting = createMeeting;
module.exports.makeOauthRequest = makeOauthRequest;
module.exports.oathOptions = oathOptions;
module.exports.refTok = refTok;
// package.json file for good measure
{
"name": "mokapot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha -r dotenv/config"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"chai": "^4.3.4",
"dotenv": "^8.2.0",
"mocha": "^8.3.2",
"nock": "^13.0.11",
"sinon": "^9.2.4",
"supertest": "^6.1.3"
}
}