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 3b4c660
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ export default class MobilePhoneVerification extends React.Component {

async activePhoneToken() {
const {orgSlug, language, userData} = this.props;
const {errors} = this.state;
const url = mobilePhoneTokenStatusUrl(orgSlug);
return axios({
method: "get",
Expand All @@ -189,15 +188,20 @@ export default class MobilePhoneVerification extends React.Component {
})
.then((data) => data.active)
.catch((error) => {
if (
error.response &&
error.response.status === 404 &&
error.response.data &&
error.response.data.response_code !== "INVALID_ORGANIZATION"
) {
// 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);
this.setState({
errors: {
...errors,
...(errorText ? {nonField: errorText} : {nonField: ""}),
},
});
return errorText;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,66 @@ 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 not execute createPhoneToken if invalid organization", async () => {
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"],
response_code: "INVALID_ORGANIZATION",
},
},
}),
);
validateToken.mockReturnValue(true);
jest.spyOn(toast, "error");
wrapper = createShallowComponent(props);
await tick();
expect(
MobilePhoneVerification.prototype.createPhoneToken.mock.calls.length,
).toBe(0);
expect(logError.mock.calls.length).toBe(1);
expect(toast.error.mock.calls.length).toBe(1);
expect(toast.error).toHaveBeenCalledWith("Not Found");
});

it("should show error on if active phone token check fails", async () => {
MobilePhoneVerification.prototype.activePhoneToken.mockRestore();
axios.mockImplementationOnce(() =>
Expand Down
15 changes: 11 additions & 4 deletions server/controllers/mobile-phone-token-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,18 @@ 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(404)
.type("application/json")
.send({
response_code: "INVALID_ORGANIZATION",
non_field_errors: ["Not found."],
});
}
};

Expand Down

0 comments on commit 3b4c660

Please sign in to comment.