TASK22211: normalize token endpoint contract handling
This commit is contained in:
@@ -13,41 +13,35 @@
|
|||||||
* description: Success
|
* description: Success
|
||||||
*/
|
*/
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
||||||
|
|
||||||
export default async function ApiProxy(req, res) {
|
export default async function ApiProxy(req, res) {
|
||||||
const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
|
try {
|
||||||
const tenantId = process.env.TENANT;
|
const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
|
||||||
const tokenBody =
|
const tenantId = process.env.TENANT;
|
||||||
"grant_type=" +
|
const tokenBody =
|
||||||
process.env.GRANT_TYPE +
|
"grant_type=" +
|
||||||
"&client_id=" +
|
process.env.GRANT_TYPE +
|
||||||
process.env.CLIENT_ID +
|
"&client_id=" +
|
||||||
"&client_secret=" +
|
process.env.CLIENT_ID +
|
||||||
process.env.CLIENT_SECRET +
|
"&client_secret=" +
|
||||||
"&scope=" +
|
process.env.CLIENT_SECRET +
|
||||||
"https://" +
|
"&scope=" +
|
||||||
process.env.RELAYURI +
|
"https://" +
|
||||||
"/.default";
|
process.env.RELAYURI +
|
||||||
|
"/.default";
|
||||||
|
|
||||||
var config = {
|
const { data } = await axios.post(
|
||||||
method: "post",
|
`${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`,
|
||||||
headers: {
|
tokenBody
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
);
|
||||||
"Cache-Control": "no-cache",
|
|
||||||
},
|
|
||||||
url: `${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`,
|
|
||||||
body: tokenBody,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log("get token case:", config);
|
return respondSuccess(res, data);
|
||||||
|
} catch (error) {
|
||||||
return axios
|
return respondError(res, {
|
||||||
.post(`${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`, tokenBody)
|
status: 400,
|
||||||
.then(({ data }) => {
|
code: "TOKEN_FETCH_FAILED",
|
||||||
console.log;
|
message: "Unable to fetch token"
|
||||||
res.status(200).json(data);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
res.status(400);
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3611,6 +3611,70 @@ test("getappealtypesfornewappeal returns success payload contract", async () =>
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("getToken success returns token payload contract", async () => {
|
||||||
|
const mod = loadModule("pages/api/endpoint/getToken.js", {
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
axios: {
|
||||||
|
post: async () => ({
|
||||||
|
data: {
|
||||||
|
access_token: "token",
|
||||||
|
token_type: "Bearer"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
ACCESS_TOKEN_ENDPOINT: "https://login.microsoftonline.com/",
|
||||||
|
TENANT: "tenant-id",
|
||||||
|
GRANT_TYPE: "client_credentials",
|
||||||
|
CLIENT_ID: "client-id",
|
||||||
|
CLIENT_SECRET: "secret",
|
||||||
|
RELAYURI: "relay.example"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = { body: {} };
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 200);
|
||||||
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
|
||||||
|
access_token: "token",
|
||||||
|
token_type: "Bearer"
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getToken catch path returns TOKEN_FETCH_FAILED", async () => {
|
||||||
|
const mod = loadModule("pages/api/endpoint/getToken.js", {
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
axios: {
|
||||||
|
post: async () => {
|
||||||
|
throw new Error("token fetch failed");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
ACCESS_TOKEN_ENDPOINT: "https://login.microsoftonline.com/",
|
||||||
|
TENANT: "tenant-id",
|
||||||
|
GRANT_TYPE: "client_credentials",
|
||||||
|
CLIENT_ID: "client-id",
|
||||||
|
CLIENT_SECRET: "secret",
|
||||||
|
RELAYURI: "relay.example"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = { body: {} };
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 400);
|
||||||
|
assert.strictEqual(res.state.jsonBody.error.code, "TOKEN_FETCH_FAILED");
|
||||||
|
});
|
||||||
|
|
||||||
test("getsipsmedia returns success contract", async () => {
|
test("getsipsmedia returns success contract", async () => {
|
||||||
const mod = loadModule("pages/api/endpoint/getsipsmedia_api.js", {
|
const mod = loadModule("pages/api/endpoint/getsipsmedia_api.js", {
|
||||||
respondSuccess: respondSuccessMock
|
respondSuccess: respondSuccessMock
|
||||||
|
|||||||
Reference in New Issue
Block a user