2018-08-21 16:16:15 +02:00
|
|
|
// @flow
|
|
|
|
|
import configureMockStore from "redux-mock-store";
|
|
|
|
|
import thunk from "redux-thunk";
|
|
|
|
|
import fetchMock from "fetch-mock";
|
2018-08-23 09:10:23 +02:00
|
|
|
import reducer, {
|
2018-08-21 16:16:15 +02:00
|
|
|
fetchPermissions,
|
2018-08-23 09:10:23 +02:00
|
|
|
fetchPermissionsSuccess,
|
2018-08-23 10:16:54 +02:00
|
|
|
getPermissionsOfRepo,
|
|
|
|
|
isFetchPermissionsPending,
|
|
|
|
|
getFetchPermissionsFailure,
|
|
|
|
|
FETCH_PERMISSIONS,
|
2018-08-21 16:16:15 +02:00
|
|
|
FETCH_PERMISSIONS_PENDING,
|
2018-08-21 16:21:41 +02:00
|
|
|
FETCH_PERMISSIONS_SUCCESS,
|
|
|
|
|
FETCH_PERMISSIONS_FAILURE
|
2018-08-21 16:16:15 +02:00
|
|
|
} from "./permissions";
|
|
|
|
|
import type { Permission, Permissions } from "../types/Permissions";
|
|
|
|
|
|
2018-08-23 10:16:54 +02:00
|
|
|
const hitchhiker_puzzle42Permission_user_eins: Permission = {
|
2018-08-21 16:16:15 +02:00
|
|
|
name: "user_eins",
|
|
|
|
|
type: "READ",
|
|
|
|
|
groupPermission: true,
|
|
|
|
|
_links: {
|
|
|
|
|
self: {
|
|
|
|
|
href:
|
2018-08-23 10:16:54 +02:00
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_eins"
|
2018-08-21 16:16:15 +02:00
|
|
|
},
|
|
|
|
|
delete: {
|
|
|
|
|
href:
|
2018-08-23 10:16:54 +02:00
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_eins"
|
2018-08-21 16:16:15 +02:00
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
href:
|
2018-08-23 10:16:54 +02:00
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_eins"
|
2018-08-21 16:16:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-23 10:16:54 +02:00
|
|
|
const hitchhiker_puzzle42Permission_user_zwei: Permission = {
|
|
|
|
|
name: "user_zwei",
|
|
|
|
|
type: "WRITE",
|
|
|
|
|
groupPermission: true,
|
|
|
|
|
_links: {
|
|
|
|
|
self: {
|
|
|
|
|
href:
|
|
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_zwei"
|
|
|
|
|
},
|
|
|
|
|
delete: {
|
|
|
|
|
href:
|
|
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_zwei"
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
href:
|
|
|
|
|
"http://localhost:8081/scm/api/rest/v2/repositories/hitchhiker/puzzle42/permissions/user_zwei"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hitchhiker_puzzle42Permissions: Permissions = [
|
|
|
|
|
hitchhiker_puzzle42Permission_user_eins,
|
|
|
|
|
hitchhiker_puzzle42Permission_user_zwei
|
|
|
|
|
];
|
2018-08-21 16:16:15 +02:00
|
|
|
|
|
|
|
|
describe("permission fetch", () => {
|
|
|
|
|
const REPOS_URL = "/scm/api/rest/v2/repositories";
|
|
|
|
|
const mockStore = configureMockStore([thunk]);
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-23 10:16:54 +02:00
|
|
|
it("should successfully fetch permissions to repo hitchhiker/puzzle42", () => {
|
|
|
|
|
fetchMock.getOnce(
|
|
|
|
|
REPOS_URL + "/hitchhiker/puzzle42/permissions",
|
|
|
|
|
hitchhiker_puzzle42Permissions
|
|
|
|
|
);
|
2018-08-21 16:16:15 +02:00
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PERMISSIONS_PENDING,
|
|
|
|
|
payload: {
|
2018-08-23 10:16:54 +02:00
|
|
|
namespace: "hitchhiker",
|
|
|
|
|
name: "puzzle42"
|
2018-08-21 16:16:15 +02:00
|
|
|
},
|
2018-08-23 10:16:54 +02:00
|
|
|
itemId: "hitchhiker/puzzle42"
|
2018-08-21 16:16:15 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PERMISSIONS_SUCCESS,
|
2018-08-23 10:16:54 +02:00
|
|
|
payload: hitchhiker_puzzle42Permissions,
|
|
|
|
|
itemId: "hitchhiker/puzzle42"
|
2018-08-21 16:16:15 +02:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2018-08-23 10:16:54 +02:00
|
|
|
return store
|
|
|
|
|
.dispatch(fetchPermissions("hitchhiker", "puzzle42"))
|
|
|
|
|
.then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
2018-08-21 16:16:15 +02:00
|
|
|
});
|
2018-08-21 16:21:41 +02:00
|
|
|
|
|
|
|
|
it("should dispatch FETCH_PERMISSIONS_FAILURE, it the request fails", () => {
|
2018-08-23 10:16:54 +02:00
|
|
|
fetchMock.getOnce(REPOS_URL + "/hitchhiker/puzzle42/permissions", {
|
2018-08-21 16:21:41 +02:00
|
|
|
status: 500
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2018-08-23 10:16:54 +02:00
|
|
|
return store
|
|
|
|
|
.dispatch(fetchPermissions("hitchhiker", "puzzle42"))
|
|
|
|
|
.then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(FETCH_PERMISSIONS_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(FETCH_PERMISSIONS_FAILURE);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
2018-08-21 16:21:41 +02:00
|
|
|
});
|
2018-08-21 16:16:15 +02:00
|
|
|
});
|
2018-08-23 09:10:23 +02:00
|
|
|
|
2018-08-23 10:16:54 +02:00
|
|
|
describe("permissions reducer", () => {
|
2018-08-23 09:10:23 +02:00
|
|
|
it("should return empty object, if state and action is undefined", () => {
|
|
|
|
|
expect(reducer()).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return the same state, if the action is undefined", () => {
|
|
|
|
|
const state = { x: true };
|
|
|
|
|
expect(reducer(state)).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return the same state, if the action is unknown to the reducer", () => {
|
|
|
|
|
const state = { x: true };
|
|
|
|
|
expect(reducer(state, { type: "EL_SPECIALE" })).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should store the permissions on FETCH_PERMISSION_SUCCESS", () => {
|
|
|
|
|
const newState = reducer(
|
|
|
|
|
{},
|
2018-08-23 10:16:54 +02:00
|
|
|
fetchPermissionsSuccess(
|
|
|
|
|
hitchhiker_puzzle42Permissions,
|
|
|
|
|
"hitchhiker",
|
|
|
|
|
"puzzle42"
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(newState["hitchhiker/puzzle42"]).toBe(
|
|
|
|
|
hitchhiker_puzzle42Permissions
|
2018-08-23 09:10:23 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-08-23 10:16:54 +02:00
|
|
|
|
|
|
|
|
describe("permissions selectors", () => {
|
|
|
|
|
const error = new Error("something goes wrong");
|
|
|
|
|
|
|
|
|
|
it("should return the permissions of one repository", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
permissions: {
|
|
|
|
|
"hitchhiker/puzzle42": hitchhiker_puzzle42Permissions
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const repoPermissions = getPermissionsOfRepo(
|
|
|
|
|
state,
|
|
|
|
|
"hitchhiker",
|
|
|
|
|
"puzzle42"
|
|
|
|
|
);
|
|
|
|
|
expect(repoPermissions).toEqual(hitchhiker_puzzle42Permissions);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return true, when fetch permissions is pending", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
pending: {
|
|
|
|
|
[FETCH_PERMISSIONS]: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
expect(isFetchPermissionsPending(state)).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return false, when fetch permissions is not pending", () => {
|
|
|
|
|
expect(isFetchPermissionsPending({})).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return error when fetch permissions did fail", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
failure: {
|
|
|
|
|
[FETCH_PERMISSIONS]: error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
expect(getFetchPermissionsFailure(state)).toEqual(error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return undefined when fetch permissions did not fail", () => {
|
|
|
|
|
expect(getFetchPermissionsFailure({})).toBe(undefined);
|
|
|
|
|
});
|
|
|
|
|
});
|