Added method to change password as a user

This commit is contained in:
Philipp Czora
2018-11-06 17:29:08 +01:00
parent 26e23ead21
commit 8c13f38c99
4 changed files with 55 additions and 26 deletions

View File

@@ -9,7 +9,7 @@ import {
} from "@scm-manager/ui-components";
import * as userValidator from "./userValidation";
import { translate } from "react-i18next";
import { updatePassword } from "./updatePassword";
import { setPassword } from "./changePassword";
type Props = {
user: User,
@@ -79,7 +79,7 @@ class SetUserPassword extends React.Component<Props, State> {
const { user } = this.props;
const { password } = this.state;
this.setLoadingState();
updatePassword(user._links.password.href, password)
setPassword(user._links.password.href, password)
.then(result => {
if (result.error) {
this.setErrorState(result.error);

View File

@@ -0,0 +1,32 @@
//@flow
import { apiClient } from "@scm-manager/ui-components";
const CONTENT_TYPE_PASSWORD_OVERWRITE =
"application/vnd.scmm-passwordOverwrite+json;v=2";
const CONTENT_TYPE_PASSWORD_CHANGE =
"application/vnd.scmm-passwordChange+json;v=2";
export function setPassword(url: string, password: string) {
return apiClient
.put(url, { newPassword: password }, CONTENT_TYPE_PASSWORD_OVERWRITE)
.then(response => {
return response;
})
.catch(err => {
return { error: err };
});
}
export function updatePassword(
url: string,
oldPassword: string,
newPassword: string
) {
return apiClient
.put(url, { oldPassword, newPassword }, CONTENT_TYPE_PASSWORD_CHANGE)
.then(response => {
return response;
})
.catch(err => {
return { error: err };
});
}

View File

@@ -1,15 +0,0 @@
//@flow
import { apiClient } from "@scm-manager/ui-components";
const CONTENT_TYPE_PASSWORD_OVERWRITE =
"application/vnd.scmm-passwordOverwrite+json;v=2";
export function updatePassword(url: string, password: string) {
return apiClient
.put(url, { newPassword: password }, CONTENT_TYPE_PASSWORD_OVERWRITE)
.then(response => {
return response;
})
.catch(err => {
return { error: err };
});
}

View File

@@ -1,23 +1,35 @@
//@flow
import fetchMock from "fetch-mock";
import { updatePassword } from "./updatePassword";
import { setPassword, updatePassword } from "./changePassword";
describe("get content type", () => {
const PASSWORD_URL = "/users/testuser/password";
const password = "testpw123";
describe("password change", () => {
const SET_PASSWORD_URL = "/users/testuser/password";
const CHANGE_PASSWORD_URL = "/me/password";
const oldPassword = "old";
const newPassword = "testpw123";
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
it("should update password", done => {
fetchMock.put("/api/v2" + PASSWORD_URL, 204);
updatePassword(PASSWORD_URL, password).then(content => {
// TODO: Verify content type
it("should set password", done => {
fetchMock.put("/api/v2" + SET_PASSWORD_URL, 204);
setPassword(SET_PASSWORD_URL, newPassword).then(content => {
done();
});
});
// TODO: Verify content type
it("should update password", done => {
fetchMock.put("/api/v2" + CHANGE_PASSWORD_URL, 204);
updatePassword(CHANGE_PASSWORD_URL, oldPassword, newPassword).then(
content => {
done();
}
);
});
});