Tests for read_only_mode/*

pull/1348/head
Rick Carlino 2019-08-01 08:01:15 -05:00
parent cc172bf585
commit 404facb183
6 changed files with 76 additions and 23 deletions

View File

@ -23,7 +23,7 @@ import { arrayUnwrap } from "../resources/util";
import { findByUuid } from "../resources/reducer_support";
import { assign, noop } from "lodash";
import { t } from "../i18next_wrapper";
import { appIsReadonly } from "../read_only_mode";
import { appIsReadonly } from "../read_only_mode/app_is_read_only";
export function edit(tr: TaggedResource, changes: Partial<typeof tr.body>):
ReduxAction<EditResourceParams> {

View File

@ -1,5 +1,5 @@
import { buildResourceIndex } from "../../__test_support__/resource_index_builder";
import { appIsReadonly } from "..";
import { appIsReadonly } from "../app_is_read_only";
import { fakeWebAppConfig } from "../../__test_support__/fake_state/resources";
describe("appIsReadonly", () => {
@ -24,9 +24,3 @@ describe("appIsReadonly", () => {
expect(result).toBe(false);
});
});
// describe("readOnlyInterceptor", () => {
// it("resolves the config when app is not read-only", () => {
// });
// });

View File

@ -0,0 +1,42 @@
let mockReadonlyState = true;
jest.mock("../app_is_read_only", () => ({
appIsReadonly: jest.fn(() => mockReadonlyState)
}));
import { DeepPartial } from "redux";
import { AxiosRequestConfig } from "axios";
import { readOnlyInterceptor } from "..";
import { warning } from "../../toast/toast";
describe("readOnlyInterceptor", () => {
it("resolves the config when app is not read-only", async () => {
const conf: DeepPartial<AxiosRequestConfig> = {};
const result = await readOnlyInterceptor(conf as AxiosRequestConfig);
expect(result).toBe(conf);
});
it("rejects non-GET HTTP requests when app is read-only", (done) => {
mockReadonlyState = true;
const conf: DeepPartial<AxiosRequestConfig> = { method: "PUT" };
readOnlyInterceptor(conf as AxiosRequestConfig)
.then(fail, (result) => {
expect(result).toBe(conf);
expect(warning)
.toHaveBeenCalledWith("Refusing to modify data in read-only mode");
done();
});
});
it("allows HTTP GET requests when app is read-only", (done) => {
mockReadonlyState = true;
const conf: DeepPartial<AxiosRequestConfig> = {
method: "GET"
};
readOnlyInterceptor(conf as AxiosRequestConfig)
.then((result) => {
expect(result).toBe(conf);
done();
}, fail);
});
});

View File

@ -0,0 +1,17 @@
import React from "react";
import { ReadOnlyIcon } from "..";
import { shallow } from "enzyme";
describe("<ReadOnlyIcon/>", () => {
it("shows nothing when unlocked", () => {
expect(ReadOnlyIcon({ locked: false }))
.toEqual(<div />);
});
it("hows the pencil icon when locked", () => {
const result = shallow(<ReadOnlyIcon locked={true} />);
expect(result.find(".fa-pencil").length).toBe(1);
expect(result.find(".fa-ban").length).toBe(1);
});
});

View File

@ -0,0 +1,14 @@
import { getWebAppConfig } from "../resources/getters";
import { ResourceIndex } from "../resources/interfaces";
/** Returns `true` if the user is allowed to modify account data.
* This is a helper function of the "readonly" account lock. */
export function appIsReadonly(index: ResourceIndex) {
const conf = getWebAppConfig(index);
if (conf) {
return conf.body.user_interface_read_only_mode;
} else {
// Assume user is allowed to change data if no
// configs are available.
return true;
}
}

View File

@ -1,22 +1,8 @@
import { getWebAppConfig } from "../resources/getters";
import { AxiosRequestConfig } from "axios";
import { ResourceIndex } from "../resources/interfaces";
import { store } from "../redux/store";
import { warning } from "../toast/toast";
import React from "react";
/** Returns `true` if the user is allowed to modify account data.
* This is a helper function of the "readonly" account lock. */
export function appIsReadonly(index: ResourceIndex) {
const conf = getWebAppConfig(index);
if (conf) {
return conf.body.user_interface_read_only_mode;
} else {
// Assume user is allowed to change data if no
// configs are available.
return true;
}
}
import { appIsReadonly } from "./app_is_read_only";
export const readOnlyInterceptor = (config: AxiosRequestConfig) => {
const method = (config.method || "get").toLowerCase();