Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: yarn test. #2364

Merged
merged 8 commits into from May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -92,7 +92,7 @@
"eslint-config-standard-react": "^9.2.0",
"eslint-plugin-cypress": "^2.11.2",
"eslint-plugin-react": "^7.21.5",
"fetch-mock": "^7.7.3",
"fetch-mock": "^9.11.0",
"https": "^1.0.0",
"nightwatch": "^1.5.1",
"pem": "^1.14.4",
Expand Down
102 changes: 47 additions & 55 deletions src/actions/api.js
Expand Up @@ -43,7 +43,6 @@ export const requestApiInfo = (fetchUser = true) => (dispatch) => {
.apiInfo()
.then((response) => {
dispatch(act.RECEIVE_INIT_SESSION(response));

// if there is an active session, try to fetch the user information
// otherwise we make sure there are no user data saved into localstorage
if (!response.activeusersession) {
Expand Down Expand Up @@ -112,28 +111,27 @@ export const onPollUserPayment = () => (dispatch, getState) => {
});
};

export const onGetPolicy = () => (dispatch, getState) => {
export const onGetPolicy = () => async (dispatch, getState) => {
const isCMS = sel.isCMS(getState());
dispatch(act.REQUEST_POLICY());
return Promise.all([
api.policyWWW(),
!isCMS && api.policyTicketVote(),
!isCMS && api.policyComments(),
!isCMS && api.policyPi()
])
.then((response) => {
const policyOnRedux = { www: response[0] };
if (!isCMS) {
policyOnRedux.ticketvote = response[1];
policyOnRedux.comments = response[2];
policyOnRedux.pi = response[3];
}
return dispatch(act.RECEIVE_POLICY(policyOnRedux));
})
.catch((error) => {
dispatch(act.RECEIVE_POLICY(null, error));
throw error;
});
try {
const [www, ticketvote, comments, pi] = await Promise.all([
api.policyWWW(),
!isCMS && api.policyTicketVote(),
!isCMS && api.policyComments(),
!isCMS && api.policyPi()
]);
const policyOnRedux = { www };
if (!isCMS) {
policyOnRedux.ticketvote = ticketvote;
policyOnRedux.comments = comments;
policyOnRedux.pi = pi;
}
dispatch(act.RECEIVE_POLICY(policyOnRedux));
} catch (error) {
dispatch(act.RECEIVE_POLICY(null, error));
throw error;
}
};

export const withCsrf = (fn) => (dispatch, getState) => {
Expand Down Expand Up @@ -176,9 +174,7 @@ export const onCreateNewUser = ({ email, username, password }) =>
dispatch(act.REQUEST_NEW_USER({ email }));
return api
.newUser(csrf, email, username, password)
.then((response) => {
dispatch(act.RECEIVE_NEW_USER(response));
})
.then((response) => dispatch(act.RECEIVE_NEW_USER(response)))
.catch((error) => {
if (error.toString() === "Error: No available storage method found.") {
//local storage error
Expand Down Expand Up @@ -252,45 +248,41 @@ export const onSearchUser = (query, isCMS) => (dispatch) => {
// after registering, his key will be saved under his email. If so, it
// changes the storage key to his uuid.
export const onLogin = ({ email, password, code }) =>
withCsrf((dispatch, _, csrf) => {
withCsrf(async (dispatch, _, csrf) => {
dispatch(act.REQUEST_LOGIN({ email }));
return api
.login(csrf, email, password, code)
.then((response) => {
dispatch(act.RECEIVE_LOGIN(response));
const { userid, username } = response;
pki.needStorageKeyReplace(email, username).then((keyNeedsReplace) => {
if (keyNeedsReplace) {
pki.replaceStorageKey(keyNeedsReplace, userid);
}
return response;
});
})
.then(() => {
dispatch(onRequestMe());
})
.catch((error) => {
dispatch(act.RECEIVE_LOGIN(null, error));
throw error;
});
try {
const response = await api.login(csrf, email, password, code);
await dispatch(onRequestMe());
dispatch(act.RECEIVE_LOGIN(response));
const { userid, username } = response;
const keyNeedsReplace = await pki.needStorageKeyReplace(email, username);
if (keyNeedsReplace) {
pki.replaceStorageKey(keyNeedsReplace, userid);
}
return;
} catch (error) {
dispatch(act.RECEIVE_LOGIN(null, error));
throw error;
}
});

// handleLogout calls the correct logout handler according to the user selected
// option between a normal logout or a permanent logout.
// handleLogout calls the correct logout handler according to the user
// selected option between a normal logout or a permanent logout.
export const handleLogout = (isPermanent, userid) => () =>
isPermanent ? handlePermanentLogout(userid) : handleNormalLogout;

// handleNormalLogout handles all the procedure to be done once the user is logged out.
// It can be called either when the logout request has been successful or when the
// session has already expired
// handleNormalLogout handles all the procedure to be done once the user is
// logged out.
// It can be called either when the logout request has been successful or
// when the session has already expired.
export const handleNormalLogout = () => {
clearStateLocalStorage();
clearPollingPointer();
clearProposalPaymentPollingPointer();
};

// handlePermanentLogout handles the logout procedures while deleting all user related
// information from the browser storage and cache.
// handlePermanentLogout handles the logout procedures while deleting all
// user related information from the browser storage and cache.
export const handlePermanentLogout = (userid) =>
pki.removeKeys(userid).then(() => {
clearStateLocalStorage(userid);
Expand All @@ -304,11 +296,11 @@ export const onLogout = (isCMS, isPermanent) =>
dispatch(act.REQUEST_LOGOUT());
return api
.logout(csrf)
.then((response) => {
.then((response) =>
isCMS
? dispatch(act.RECEIVE_CMS_LOGOUT(response))
: dispatch(act.RECEIVE_LOGOUT(response));
})
: dispatch(act.RECEIVE_LOGOUT(response))
)
.then(() => handleLogout(isPermanent, userid))
.catch((error) => {
dispatch(act.RECEIVE_LOGOUT(null, error));
Expand Down Expand Up @@ -896,7 +888,7 @@ export const onSubmitEditedProposal = (
.then(({ record }) => {
dispatch(act.RECEIVE_EDIT_PROPOSAL({ proposal: record }));
resetNewProposalData();
return record.censorshiprecord.token;
return record?.censorshiprecord?.token;
})
.catch((error) => {
dispatch(act.RECEIVE_EDIT_PROPOSAL(null, error));
Expand Down