-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17564 from itisAliRH/login-refactors
Login components refactors
- Loading branch information
Showing
15 changed files
with
1,039 additions
and
944 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,67 @@ | ||
<template> | ||
<b-form @submit.prevent="submit"> | ||
<b-alert v-if="!!message" :variant="variant" show> | ||
{{ message }} | ||
</b-alert> | ||
<b-card header="Change your password"> | ||
<b-form-group v-if="expiredUser" label="Current Password"> | ||
<b-form-input v-model="current" type="password" /> | ||
</b-form-group> | ||
<b-form-group label="New Password"> <b-form-input v-model="password" type="password" /> </b-form-group> | ||
<b-form-group label="Confirm password"> <b-form-input v-model="confirm" type="password" /> </b-form-group> | ||
<b-button type="submit">Save new password</b-button> | ||
</b-card> | ||
</b-form> | ||
</template> | ||
<script> | ||
<script setup lang="ts"> | ||
import axios from "axios"; | ||
import BootstrapVue from "bootstrap-vue"; | ||
import { withPrefix } from "utils/redirect"; | ||
import Vue from "vue"; | ||
import { BAlert, BButton, BCard, BForm, BFormGroup, BFormInput } from "bootstrap-vue"; | ||
import { ref } from "vue"; | ||
import { useRouter } from "vue-router/composables"; | ||
import { withPrefix } from "@/utils/redirect"; | ||
interface Props { | ||
token?: string; | ||
expiredUser?: string; | ||
messageText?: string; | ||
messageVariant?: string; | ||
} | ||
const props = defineProps<Props>(); | ||
const router = useRouter(); | ||
const confirm = ref(null); | ||
const current = ref(null); | ||
const password = ref(null); | ||
const message = ref(props.messageText); | ||
const variant = ref(props.messageVariant); | ||
Vue.use(BootstrapVue); | ||
async function submit() { | ||
try { | ||
await axios.post(withPrefix("/user/change_password"), { | ||
token: props.token, | ||
id: props.expiredUser, | ||
current: current.value, | ||
password: password.value, | ||
confirm: confirm.value, | ||
}); | ||
export default { | ||
props: { | ||
token: { | ||
type: String, | ||
default: null, | ||
}, | ||
expiredUser: { | ||
type: String, | ||
default: null, | ||
}, | ||
messageText: { | ||
type: String, | ||
default: null, | ||
}, | ||
messageVariant: { | ||
type: String, | ||
default: null, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
password: null, | ||
confirm: null, | ||
current: null, | ||
message: this.messageText, | ||
variant: this.messageVariant, | ||
}; | ||
}, | ||
methods: { | ||
submit() { | ||
axios | ||
.post(withPrefix("/user/change_password"), { | ||
token: this.token, | ||
id: this.expiredUser, | ||
current: this.current, | ||
password: this.password, | ||
confirm: this.confirm, | ||
}) | ||
.then((response) => { | ||
window.location = withPrefix(`/`); | ||
}) | ||
.catch((error) => { | ||
this.variant = "danger"; | ||
const message = error.response && error.response.data && error.response.data.err_msg; | ||
this.message = message || "Password change failed for an unknown reason."; | ||
}); | ||
}, | ||
}, | ||
}; | ||
router.push("/"); | ||
} catch (error: any) { | ||
variant.value = "danger"; | ||
const errMsg = error.response && error.response.data && error.response.data.err_msg; | ||
message.value = errMsg || "Password change failed for an unknown reason."; | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<BForm @submit.prevent="submit"> | ||
<BAlert v-if="!!message" :variant="variant" show> | ||
{{ message }} | ||
</BAlert> | ||
|
||
<BCard header="Change your password"> | ||
<BFormGroup v-if="expiredUser" label="Current Password"> | ||
<BFormInput v-model="current" type="password" /> | ||
</BFormGroup> | ||
|
||
<BFormGroup label="New Password"> | ||
<BFormInput v-model="password" type="password" /> | ||
</BFormGroup> | ||
|
||
<BFormGroup label="Confirm password"> | ||
<BFormInput v-model="confirm" type="password" /> | ||
</BFormGroup> | ||
|
||
<BButton type="submit">Save new password</BButton> | ||
</BCard> | ||
</BForm> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,82 @@ | ||
import { getLocalVue } from "@tests/jest/helpers"; | ||
import { mount } from "@vue/test-utils"; | ||
import axios from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
import { getLocalVue } from "tests/jest/helpers"; | ||
|
||
import MountTarget from "./LoginForm"; | ||
import MountTarget from "./LoginForm.vue"; | ||
|
||
const localVue = getLocalVue(true); | ||
|
||
async function mountLoginForm() { | ||
const wrapper = mount(MountTarget as object, { | ||
propsData: { | ||
sessionCsrfToken: "sessionCsrfToken", | ||
}, | ||
localVue, | ||
stubs: { | ||
ExternalLogin: true, | ||
}, | ||
}); | ||
|
||
return wrapper; | ||
} | ||
|
||
describe("LoginForm", () => { | ||
let wrapper; | ||
let axiosMock; | ||
let axiosMock: MockAdapter; | ||
|
||
beforeEach(() => { | ||
axiosMock = new MockAdapter(axios); | ||
wrapper = mount(MountTarget, { | ||
propsData: { | ||
sessionCsrfToken: "sessionCsrfToken", | ||
}, | ||
localVue, | ||
stubs: { | ||
ExternalLogin: true, | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
axiosMock.reset(); | ||
}); | ||
|
||
it("basics", async () => { | ||
const wrapper = await mountLoginForm(); | ||
|
||
const cardHeader = wrapper.find(".card-header"); | ||
expect(cardHeader.text()).toBe("Welcome to Galaxy, please log in"); | ||
|
||
const inputs = wrapper.findAll("input"); | ||
expect(inputs.length).toBe(2); | ||
|
||
const usernameField = inputs.at(0); | ||
expect(usernameField.attributes("type")).toBe("text"); | ||
|
||
await usernameField.setValue("test_user"); | ||
|
||
const pwdField = inputs.at(1); | ||
expect(pwdField.attributes("type")).toBe("password"); | ||
|
||
await pwdField.setValue("test_pwd"); | ||
|
||
const submitButton = wrapper.find("button[type='submit']"); | ||
await submitButton.trigger("submit"); | ||
const postedData = JSON.parse(axiosMock.history.post[0].data); | ||
|
||
const postedData = JSON.parse(axiosMock.history.post?.[0]?.data); | ||
expect(postedData.login).toBe("test_user"); | ||
expect(postedData.password).toBe("test_pwd"); | ||
}); | ||
|
||
it("props", async () => { | ||
const $register = "[id='register-toggle']"; | ||
const wrapper = await mountLoginForm(); | ||
|
||
console.log(wrapper.html()); | ||
|
||
const $register = "#register-toggle"; | ||
expect(wrapper.findAll($register).length).toBe(0); | ||
|
||
await wrapper.setProps({ | ||
allowUserCreation: true, | ||
enableOidc: true, | ||
showWelcomeWithLogin: true, | ||
welcomeUrl: "welcome_url", | ||
}); | ||
|
||
const register = wrapper.find($register); | ||
expect(register.text()).toBeLocalizationOf("Register here."); | ||
(expect(register.text()) as any).toBeLocalizationOf("Register here."); | ||
|
||
const welcomePage = wrapper.find("iframe"); | ||
expect(welcomePage.attributes("src")).toBe("welcome_url"); | ||
}); | ||
|
@@ -65,33 +85,46 @@ describe("LoginForm", () => { | |
const external_email = "[email protected]"; | ||
const provider_id = "test_provider"; | ||
const provider_label = "Provider"; | ||
await wrapper.setData({ | ||
connectExternalEmail: external_email, | ||
connectExternalProvider: provider_id, | ||
connectExternalLabel: provider_label, | ||
}); | ||
|
||
const originalLocation = window.location; | ||
jest.spyOn(window, "location", "get").mockImplementation(() => ({ | ||
...originalLocation, | ||
search: `?connect_external_email=${external_email}&connect_external_provider=${provider_id}&connect_external_label=${provider_label}`, | ||
})); | ||
|
||
const wrapper = await mountLoginForm(); | ||
|
||
expect(wrapper.find(".card-header").exists()).toBe(false); | ||
|
||
const alert = wrapper.find(".alert"); | ||
expect(alert.classes()).toContain("alert-info"); | ||
expect(alert.text()).toContain(`There already exists a user with the email ${external_email}`); | ||
expect(alert.text()).toContain(`In order to associate this account with ${provider_label}`); | ||
|
||
const inputs = wrapper.findAll("input"); | ||
expect(inputs.length).toBe(2); | ||
|
||
const usernameField = inputs.at(0); | ||
expect(usernameField.attributes("type")).toBe("text"); | ||
expect(usernameField.element.disabled).toBe(true); | ||
expect(usernameField.element.value).not.toBe(""); | ||
expect(usernameField.element.value).toContain(external_email); | ||
expect((usernameField.element as HTMLInputElement).disabled).toBe(true); | ||
expect((usernameField.element as HTMLInputElement).value).not.toBe(""); | ||
expect((usernameField.element as HTMLInputElement).value).toContain(external_email); | ||
|
||
const pwdField = inputs.at(1); | ||
expect(pwdField.attributes("type")).toBe("password"); | ||
expect(pwdField.element.value).toBe(""); | ||
expect((pwdField.element as HTMLInputElement).value).toBe(""); | ||
|
||
await pwdField.setValue("test_pwd"); | ||
|
||
const submitButton = wrapper.find("button[type='submit']"); | ||
|
||
await submitButton.trigger("submit"); | ||
const postedData = JSON.parse(axiosMock.history.post[0].data); | ||
|
||
const postedData = JSON.parse(axiosMock.history.post?.[0]?.data); | ||
expect(postedData.login).toBe(external_email); | ||
expect(postedData.password).toBe("test_pwd"); | ||
const postedURL = axiosMock.history.post[0].url; | ||
|
||
const postedURL = axiosMock.history.post?.[0]?.url; | ||
expect(postedURL).toBe("/user/login"); | ||
}); | ||
}); |
Oops, something went wrong.