Skip to content

Commit

Permalink
[req-changes] Added backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Jun 28, 2023
1 parent 77e9a86 commit 590b6b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export default class MobilePhoneVerification extends React.Component {
})
.then((data) => data.active)
.catch((error) => {
if (error.response && error.response.status === 404) {
// This is kept for backward compatibility with older versions of OpenWISP RADIUS
// that does not have API endpoint for checking phone token status.
return false;
}
const errorText = getErrorText(error);
logError(error, errorText);
toast.error(errorText);
Expand All @@ -198,6 +203,7 @@ export default class MobilePhoneVerification extends React.Component {
...(errorText ? {nonField: errorText} : {nonField: ""}),
},
});
return errorText;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,36 @@ describe("Mobile Phone Token verification: standard flow", () => {
).toBe(0);
});

it("should not show error if active phone token returns 404", async () => {
// This is kept for backward compatibility with older versions of OpenWISP RADIUS
// that does not have API endpoint for checking phone token status.
MobilePhoneVerification.prototype.activePhoneToken.mockRestore();
jest
.spyOn(MobilePhoneVerification.prototype, "createPhoneToken")
.mockReturnValue(true);
axios.mockRestore();
axios.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 404,
statusText: "NOT FOUND",
data: {
non_field_errors: ["Not Found"],
},
},
}),
);
validateToken.mockReturnValue(true);
jest.spyOn(toast, "error");
wrapper = createShallowComponent(props);
await tick();
expect(logError.mock.calls.length).toBe(0);
expect(toast.error.mock.calls.length).toBe(0);
expect(
MobilePhoneVerification.prototype.createPhoneToken.mock.calls.length,
).toBe(1);
});

it("should show error on if active phone token check fails", async () => {
MobilePhoneVerification.prototype.activePhoneToken.mockRestore();
axios.mockImplementationOnce(() =>
Expand Down
9 changes: 6 additions & 3 deletions server/controllers/mobile-phone-token-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ export const mobilePhoneTokenStatus = (req, res) => {
}
return org.slug === reqOrg;
});
// return 404 for invalid organization slug or org not listed in config
// Return 401 for invalid organization slug or org not listed in config.
// This is different from other implementation because the frontend
// expects 404 only when the phone token status API endpoint is
// not implemented in OpenWISP RADIUS.
if (!validSlug) {
res.status(404).type("application/json").send({
response_code: "INTERNAL_SERVER_ERROR",
res.status(401).type("application/json").send({
response_code: "UNAUTHORIZED",
});
}
};
Expand Down

0 comments on commit 590b6b9

Please sign in to comment.