Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/actions/__tests__/admin-access-actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @jest-environment jsdom
*/
import configureStore from "redux-mock-store";
import thunk from "redux-thunk";
import flushPromises from "flush-promises";
import {
postRequest,
putRequest
} from "openstack-uicore-foundation/lib/utils/actions";
import { saveAdminAccess } from "../admin-access-actions";
import * as methods from "../../utils/methods";

jest.mock("openstack-uicore-foundation/lib/utils/actions", () => ({
__esModule: true,
...jest.requireActual("openstack-uicore-foundation/lib/utils/actions"),
postRequest: jest.fn(),
putRequest: jest.fn()
}));

const requestMock =
(requestActionCreator, receiveActionCreator) => () => (dispatch) => {
if (requestActionCreator && typeof requestActionCreator === "function") {
dispatch(requestActionCreator({}));
}
return new Promise((resolve) => {
if (typeof receiveActionCreator === "function") {
dispatch(receiveActionCreator({ response: { id: 1 } }));
} else {
dispatch(receiveActionCreator);
}
resolve({ response: { id: 1 } });
});
};

describe("saveAdminAccess", () => {
const middlewares = [thunk];
const mockStore = configureStore(middlewares);

beforeEach(() => {
jest.spyOn(methods, "getAccessTokenSafely").mockResolvedValue("TOKEN");
postRequest.mockImplementation(requestMock);
putRequest.mockImplementation(requestMock);
});

afterEach(() => {
jest.restoreAllMocks();
});

describe("create path (entity has no id)", () => {
it("returns a Promise", async () => {
const store = mockStore({});
const result = store.dispatch(
saveAdminAccess({ title: "Group A", members: [], summits: [] })
);
expect(result).toBeInstanceOf(Promise);
await expect(result).resolves.toBeUndefined();
});

it("dispatches ADMIN_ACCESS_ADDED then STOP_LOADING on success", async () => {
const store = mockStore({});
store.dispatch(
saveAdminAccess({ title: "Group A", members: [], summits: [] })
);
await flushPromises();

const actionTypes = store.getActions().map((a) => a.type);
expect(actionTypes).toContain("ADMIN_ACCESS_ADDED");
expect(actionTypes).toContain("STOP_LOADING");
expect(actionTypes.indexOf("STOP_LOADING")).toBeGreaterThan(
actionTypes.indexOf("ADMIN_ACCESS_ADDED")
);
});
});

describe("update path (entity has id)", () => {
it("returns a Promise", async () => {
const store = mockStore({});
const result = store.dispatch(
saveAdminAccess({ id: 1, title: "Group A", members: [], summits: [] })
);
expect(result).toBeInstanceOf(Promise);
await expect(result).resolves.toBeUndefined();
});

it("dispatches ADMIN_ACCESS_UPDATED then STOP_LOADING on success", async () => {
const store = mockStore({});
store.dispatch(
saveAdminAccess({ id: 1, title: "Group A", members: [], summits: [] })
);
await flushPromises();

const actionTypes = store.getActions().map((a) => a.type);
expect(actionTypes).toContain("ADMIN_ACCESS_UPDATED");
expect(actionTypes).toContain("STOP_LOADING");
expect(actionTypes.indexOf("STOP_LOADING")).toBeGreaterThan(
actionTypes.indexOf("ADMIN_ACCESS_UPDATED")
);
});
});
});
83 changes: 37 additions & 46 deletions src/actions/admin-access-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import {
createAction,
stopLoading,
startLoading,
showMessage,
showSuccessMessage,
authErrorHandler,
escapeFilterValue
} from "openstack-uicore-foundation/lib/utils/actions";
import history from "../history";
import { getAccessTokenSafely } from "../utils/methods";
import { DEFAULT_PER_PAGE } from "../utils/constants";

Expand Down Expand Up @@ -95,7 +93,7 @@ export const getAdminAccesses =
createAction(RECEIVE_ADMIN_ACCESSES),
`${window.API_BASE_URL}/api/v1/summit-administrator-groups`,
authErrorHandler,
{ order, orderDir, term }
{ order, orderDir, term, page, perPage }
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
Expand Down Expand Up @@ -128,52 +126,45 @@ export const resetAdminAccessForm = () => (dispatch) => {
dispatch(createAction(RESET_ADMIN_ACCESS_FORM)({}));
};

export const saveAdminAccess =
(entity, noAlert = false) =>
async (dispatch) => {
const accessToken = await getAccessTokenSafely();
export const saveAdminAccess = (entity) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();

dispatch(startLoading());
dispatch(startLoading());

const normalizedEntity = normalizeEntity(entity);
const params = { access_token: accessToken };

if (entity.id) {
putRequest(
createAction(UPDATE_ADMIN_ACCESS),
createAction(ADMIN_ACCESS_UPDATED),
`${window.API_BASE_URL}/api/v1/summit-administrator-groups/${entity.id}`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch).then(() => {
if (!noAlert)
dispatch(showSuccessMessage(T.translate("admin_access.saved")));
else dispatch(stopLoading());
});
} else {
const successMessage = {
title: T.translate("general.done"),
html: T.translate("admin_access.created"),
type: "success"
};

postRequest(
createAction(UPDATE_ADMIN_ACCESS),
createAction(ADMIN_ACCESS_ADDED),
`${window.API_BASE_URL}/api/v1/summit-administrator-groups`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch).then((payload) => {
dispatch(
showMessage(successMessage, () => {
history.push(`/app/admin-access/${payload.response.id}`);
})
);
const normalizedEntity = normalizeEntity(entity);
const params = { access_token: accessToken };

if (entity.id) {
return putRequest(
createAction(UPDATE_ADMIN_ACCESS),
createAction(ADMIN_ACCESS_UPDATED),
`${window.API_BASE_URL}/api/v1/summit-administrator-groups/${entity.id}`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch)
.then(() => {
dispatch(showSuccessMessage(T.translate("admin_access.saved")));
})
.finally(() => {
dispatch(stopLoading());
});
}
};
}
return postRequest(
createAction(UPDATE_ADMIN_ACCESS),
createAction(ADMIN_ACCESS_ADDED),
`${window.API_BASE_URL}/api/v1/summit-administrator-groups`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch)
.then(() => {
dispatch(showSuccessMessage(T.translate("admin_access.created")));
})
.finally(() => {
dispatch(stopLoading());
});
};

export const deleteAdminAccess = (adminAccessId) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
Expand Down
Loading
Loading