mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
add fetch permissions and first test
This commit is contained in:
66
scm-ui/src/permissions/modules/permissions.js
Normal file
66
scm-ui/src/permissions/modules/permissions.js
Normal file
@@ -0,0 +1,66 @@
|
||||
// @flow
|
||||
import { apiClient } from "../../apiclient";
|
||||
import * as types from "../../modules/types";
|
||||
import type { Action } from "../../types/Action";
|
||||
import type { Permission, Permissions } from "../types/Permissions";
|
||||
|
||||
export const FETCH_PERMISSIONS = "scm/repos/FETCH_PERMISSIONS";
|
||||
export const FETCH_PERMISSIONS_PENDING = `${FETCH_PERMISSIONS}_${types.PENDING_SUFFIX}`;
|
||||
export const FETCH_PERMISSIONS_SUCCESS = `${FETCH_PERMISSIONS}_${types.SUCCESS_SUFFIX}`;
|
||||
export const FETCH_PERMISSIONS_FAILURE = `${FETCH_PERMISSIONS}_${types.FAILURE_SUFFIX}`;
|
||||
|
||||
|
||||
const REPOS_URL = "repositories";
|
||||
const PERMISSIONS_URL = "permissions";
|
||||
|
||||
// fetch repos
|
||||
|
||||
export function fetchPermissions(namespace: string, name: string) {
|
||||
return function(dispatch: any) {
|
||||
dispatch(fetchPermissionsPending(namespace, name));
|
||||
return apiClient
|
||||
.get(`${REPOS_URL}/${namespace}/${name}/${PERMISSIONS_URL}`)
|
||||
.then(response => response.json())
|
||||
.then(permissions => {
|
||||
dispatch(fetchPermissionsSuccess(permissions, namespace, name));
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(fetchPermissionsFailure(namespace, name, err));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchPermissionsPending(namespace: string, name: string): Action {
|
||||
return {
|
||||
type: FETCH_PERMISSIONS_PENDING,
|
||||
payload: {
|
||||
namespace,
|
||||
name
|
||||
},
|
||||
itemId: namespace + "/" + name
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchPermissionsSuccess(permissions: Permissions, namespace: string, name: string): Action {
|
||||
return {
|
||||
type: FETCH_PERMISSIONS_SUCCESS,
|
||||
payload: permissions,
|
||||
itemId: namespace + "/" + name
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchPermissionsFailure(
|
||||
namespace: string,
|
||||
name: string,
|
||||
error: Error
|
||||
): Action {
|
||||
return {
|
||||
type: FETCH_PERMISSIONS_FAILURE,
|
||||
payload: {
|
||||
namespace,
|
||||
name,
|
||||
error
|
||||
},
|
||||
itemId: namespace + "/" + name
|
||||
};
|
||||
}
|
||||
67
scm-ui/src/permissions/modules/permissions.test.js
Normal file
67
scm-ui/src/permissions/modules/permissions.test.js
Normal file
@@ -0,0 +1,67 @@
|
||||
// @flow
|
||||
import configureMockStore from "redux-mock-store";
|
||||
import thunk from "redux-thunk";
|
||||
import fetchMock from "fetch-mock";
|
||||
import {
|
||||
fetchPermissions,
|
||||
FETCH_PERMISSIONS_PENDING,
|
||||
FETCH_PERMISSIONS_SUCCESS
|
||||
} from "./permissions";
|
||||
import type { Permission, Permissions } from "../types/Permissions";
|
||||
|
||||
const s_bPermission_user_eins: Permission = {
|
||||
name: "user_eins",
|
||||
type: "READ",
|
||||
groupPermission: true,
|
||||
_links: {
|
||||
self: {
|
||||
href:
|
||||
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
|
||||
},
|
||||
delete: {
|
||||
href:
|
||||
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
|
||||
},
|
||||
update: {
|
||||
href:
|
||||
"http://localhost:8081/scm/api/rest/v2/repositories/s/b/permissions/user_eins"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const s_bPermissions: Permissions = [s_bPermission_user_eins];
|
||||
|
||||
describe("permission fetch", () => {
|
||||
const REPOS_URL = "/scm/api/rest/v2/repositories";
|
||||
const mockStore = configureMockStore([thunk]);
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.reset();
|
||||
fetchMock.restore();
|
||||
});
|
||||
|
||||
it("should successfully fetch permissions to repo s/b", () => {
|
||||
fetchMock.getOnce(REPOS_URL + "/s/b/permissions", s_bPermissions);
|
||||
|
||||
const expectedActions = [
|
||||
{
|
||||
type: FETCH_PERMISSIONS_PENDING,
|
||||
payload: {
|
||||
namespace: "s",
|
||||
name: "b"
|
||||
},
|
||||
itemId: "s/b"
|
||||
},
|
||||
{
|
||||
type: FETCH_PERMISSIONS_SUCCESS,
|
||||
payload: s_bPermissions,
|
||||
itemId: "s/b"
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store.dispatch(fetchPermissions("s", "b")).then(() => {
|
||||
expect(store.getActions()).toEqual(expectedActions);
|
||||
});
|
||||
});
|
||||
});
|
||||
11
scm-ui/src/permissions/types/Permissions.js
Normal file
11
scm-ui/src/permissions/types/Permissions.js
Normal file
@@ -0,0 +1,11 @@
|
||||
//@flow
|
||||
import type { Links } from "../../types/hal";
|
||||
|
||||
export type Permission = {
|
||||
name: string,
|
||||
type: string,
|
||||
groupPermission: boolean,
|
||||
_links: Links
|
||||
};
|
||||
|
||||
export type Permissions = Permission[];
|
||||
Reference in New Issue
Block a user