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,
|
2018-08-23 15:39:09 +02:00
|
|
|
modifyPermission,
|
|
|
|
|
modifyPermissionSuccess,
|
2018-08-28 13:39:32 +02:00
|
|
|
getModifyPermissionFailure,
|
|
|
|
|
isModifyPermissionPending,
|
2018-08-23 15:39:09 +02:00
|
|
|
MODIFY_PERMISSION_FAILURE,
|
|
|
|
|
MODIFY_PERMISSION_PENDING,
|
2018-08-23 10:16:54 +02:00
|
|
|
FETCH_PERMISSIONS,
|
2018-08-21 16:16:15 +02:00
|
|
|
FETCH_PERMISSIONS_PENDING,
|
2018-08-21 16:21:41 +02:00
|
|
|
FETCH_PERMISSIONS_SUCCESS,
|
2018-08-23 15:39:09 +02:00
|
|
|
FETCH_PERMISSIONS_FAILURE,
|
2018-08-28 13:39:32 +02:00
|
|
|
MODIFY_PERMISSION_SUCCESS,
|
|
|
|
|
MODIFY_PERMISSION
|
2018-08-21 16:16:15 +02:00
|
|
|
} from "./permissions";
|
2018-08-23 11:11:40 +02:00
|
|
|
import type { Permission, PermissionCollection } from "../types/Permissions";
|
2018-08-21 16:16:15 +02:00
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-23 11:11:40 +02:00
|
|
|
const hitchhiker_puzzle42Permissions: PermissionCollection = [
|
2018-08-23 10:16:54 +02:00
|
|
|
hitchhiker_puzzle42Permission_user_eins,
|
|
|
|
|
hitchhiker_puzzle42Permission_user_zwei
|
|
|
|
|
];
|
2018-08-21 16:16:15 +02:00
|
|
|
|
2018-08-28 08:49:09 +02:00
|
|
|
const hitchhiker_puzzle42RepoPermissions = {
|
|
|
|
|
_embedded: {
|
|
|
|
|
permissions: hitchhiker_puzzle42Permissions
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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",
|
2018-08-28 08:49:09 +02:00
|
|
|
hitchhiker_puzzle42RepoPermissions
|
2018-08-23 10:16:54 +02:00
|
|
|
);
|
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-28 08:49:09 +02:00
|
|
|
payload: hitchhiker_puzzle42RepoPermissions,
|
2018-08-23 10:16:54 +02:00
|
|
|
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-23 15:39:09 +02:00
|
|
|
|
|
|
|
|
it("should successfully modify user_eins permission", () => {
|
|
|
|
|
fetchMock.putOnce(
|
|
|
|
|
hitchhiker_puzzle42Permission_user_eins._links.update.href,
|
|
|
|
|
{
|
|
|
|
|
status: 204
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let editedPermission = { ...hitchhiker_puzzle42Permission_user_eins };
|
|
|
|
|
editedPermission.type = "OWNER";
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
return store
|
2018-08-28 13:40:44 +02:00
|
|
|
.dispatch(modifyPermission(editedPermission, "hitchhiker", "puzzle42"))
|
2018-08-23 15:39:09 +02:00
|
|
|
.then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(MODIFY_PERMISSION_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(MODIFY_PERMISSION_SUCCESS);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should successfully modify user_eins permission and call the callback", () => {
|
|
|
|
|
fetchMock.putOnce(
|
|
|
|
|
hitchhiker_puzzle42Permission_user_eins._links.update.href,
|
|
|
|
|
{
|
|
|
|
|
status: 204
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let editedPermission = { ...hitchhiker_puzzle42Permission_user_eins };
|
|
|
|
|
editedPermission.type = "OWNER";
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
let called = false;
|
|
|
|
|
const callback = () => {
|
|
|
|
|
called = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return store
|
|
|
|
|
.dispatch(
|
|
|
|
|
modifyPermission(editedPermission, "hitchhiker", "puzzle42", callback)
|
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(MODIFY_PERMISSION_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(MODIFY_PERMISSION_SUCCESS);
|
|
|
|
|
expect(called).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should fail modifying on HTTP 500", () => {
|
|
|
|
|
fetchMock.putOnce(
|
|
|
|
|
hitchhiker_puzzle42Permission_user_eins._links.update.href,
|
|
|
|
|
{
|
|
|
|
|
status: 500
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let editedPermission = { ...hitchhiker_puzzle42Permission_user_eins };
|
|
|
|
|
editedPermission.type = "OWNER";
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
|
|
|
|
|
return store
|
2018-08-28 13:40:44 +02:00
|
|
|
.dispatch(modifyPermission(editedPermission, "hitchhiker", "puzzle42"))
|
2018-08-23 15:39:09 +02:00
|
|
|
.then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(MODIFY_PERMISSION_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(MODIFY_PERMISSION_FAILURE);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
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(
|
2018-08-28 08:49:09 +02:00
|
|
|
hitchhiker_puzzle42RepoPermissions,
|
2018-08-23 10:16:54 +02:00
|
|
|
"hitchhiker",
|
|
|
|
|
"puzzle42"
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(newState["hitchhiker/puzzle42"]).toBe(
|
|
|
|
|
hitchhiker_puzzle42Permissions
|
2018-08-23 09:10:23 +02:00
|
|
|
);
|
|
|
|
|
});
|
2018-08-23 15:39:09 +02:00
|
|
|
|
|
|
|
|
it("should update permission", () => {
|
|
|
|
|
const oldState = {
|
2018-08-28 13:39:32 +02:00
|
|
|
"hitchhiker/puzzle42": [hitchhiker_puzzle42Permission_user_eins]
|
2018-08-23 15:39:09 +02:00
|
|
|
};
|
|
|
|
|
let permissionEdited = { ...hitchhiker_puzzle42Permission_user_eins };
|
|
|
|
|
permissionEdited.type = "OWNER";
|
2018-08-28 13:39:32 +02:00
|
|
|
let expectedState = {
|
|
|
|
|
"hitchhiker/puzzle42": [permissionEdited]
|
|
|
|
|
};
|
2018-08-23 15:39:09 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
oldState,
|
|
|
|
|
modifyPermissionSuccess(permissionEdited, "hitchhiker", "puzzle42")
|
|
|
|
|
);
|
2018-08-28 13:39:32 +02:00
|
|
|
expect(newState["hitchhiker/puzzle42"]).toEqual(
|
|
|
|
|
expectedState["hitchhiker/puzzle42"]
|
|
|
|
|
);
|
2018-08-23 15:39:09 +02:00
|
|
|
});
|
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: {
|
2018-08-23 12:25:00 +02:00
|
|
|
[FETCH_PERMISSIONS + "/hitchhiker/puzzle42"]: true
|
2018-08-23 10:16:54 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-08-23 12:25:00 +02:00
|
|
|
expect(isFetchPermissionsPending(state, "hitchhiker", "puzzle42")).toEqual(
|
|
|
|
|
true
|
|
|
|
|
);
|
2018-08-23 10:16:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return false, when fetch permissions is not pending", () => {
|
2018-08-23 12:25:00 +02:00
|
|
|
expect(isFetchPermissionsPending({}, "hitchiker", "puzzle42")).toEqual(
|
|
|
|
|
false
|
|
|
|
|
);
|
2018-08-23 10:16:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return error when fetch permissions did fail", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
failure: {
|
2018-08-23 12:25:00 +02:00
|
|
|
[FETCH_PERMISSIONS + "/hitchhiker/puzzle42"]: error
|
2018-08-23 10:16:54 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-08-23 12:25:00 +02:00
|
|
|
expect(getFetchPermissionsFailure(state, "hitchhiker", "puzzle42")).toEqual(
|
|
|
|
|
error
|
|
|
|
|
);
|
2018-08-23 10:16:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return undefined when fetch permissions did not fail", () => {
|
2018-08-23 12:25:00 +02:00
|
|
|
expect(getFetchPermissionsFailure({}, "hitchhiker", "puzzle42")).toBe(
|
|
|
|
|
undefined
|
|
|
|
|
);
|
2018-08-23 10:16:54 +02:00
|
|
|
});
|
2018-08-28 13:39:32 +02:00
|
|
|
|
|
|
|
|
it("should return true, when modify permission is pending", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
pending: {
|
|
|
|
|
[MODIFY_PERMISSION + "/hitchhiker/puzzle42/user_eins"]: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
expect(
|
|
|
|
|
isModifyPermissionPending(state, "hitchhiker", "puzzle42", "user_eins")
|
|
|
|
|
).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return false, when modify permissions is not pending", () => {
|
|
|
|
|
expect(
|
|
|
|
|
isModifyPermissionPending({}, "hitchiker", "puzzle42", "user_eins")
|
|
|
|
|
).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return error when modify permissions did fail", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
failure: {
|
|
|
|
|
[MODIFY_PERMISSION + "/hitchhiker/puzzle42/user_eins"]: error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
expect(
|
|
|
|
|
getModifyPermissionFailure(state, "hitchhiker", "puzzle42", "user_eins")
|
|
|
|
|
).toEqual(error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return undefined when modify permissions did not fail", () => {
|
|
|
|
|
expect(
|
|
|
|
|
getModifyPermissionFailure({}, "hitchhiker", "puzzle42", "user_eins")
|
|
|
|
|
).toBe(undefined);
|
|
|
|
|
});
|
2018-08-23 10:16:54 +02:00
|
|
|
});
|