Get instance from window.location if it is not provided in the username field

This commit is contained in:
Angelina Filippova 2019-09-02 23:58:17 +00:00 committed by Maxim Filippov
parent 19e728bfa0
commit 210d19314e
4 changed files with 46 additions and 44 deletions

View file

@ -1,10 +1,11 @@
const users = [ const users = [
{ username: 'bob', password: '123456' } { username: 'bob', password: '123456', authHost: 'pleroma' }
] ]
export async function loginByUsername(username, password) { export async function loginByUsername(username, password, authHost) {
const user = users.find(user => user.username === username) const user = users.find(user => user.username === username)
const verifyPassword = user.password === password const verifyPassword = user.password === password
const verifyHost = user.authHost === authHost
const data = { const data = {
'token_type': 'Bearer', 'token_type': 'Bearer',
'scope': 'read write follow', 'scope': 'read write follow',
@ -14,7 +15,7 @@ export async function loginByUsername(username, password) {
'access_token': 'bar123' 'access_token': 'bar123'
} }
return verifyPassword return verifyPassword && verifyHost
? Promise.resolve({ data }) ? Promise.resolve({ data })
: Promise.reject({ message: 'Invalid credentials' }) : Promise.reject({ message: 'Invalid credentials' })
} }

View file

@ -45,9 +45,7 @@
</template> </template>
<script> <script>
import { Message } from 'element-ui'
import SvgIcon from '@/components/SvgIcon' import SvgIcon from '@/components/SvgIcon'
import i18n from '@/lang'
export default { export default {
name: 'Login', name: 'Login',
@ -82,33 +80,20 @@ export default {
}, },
handleLogin() { handleLogin() {
this.loading = true this.loading = true
if (this.checkUsername()) { const loginData = this.getLoginData()
const loginData = this.getLoginData() this.$store.dispatch('LoginByUsername', loginData).then(() => {
this.$store.dispatch('LoginByUsername', loginData).then(() => {
this.loading = false
this.$router.push({ path: this.redirect || '/users/index' })
}).catch(() => {
this.loading = false
})
} else {
Message({
message: i18n.t('login.errorMessage'),
type: 'error',
duration: 7000
})
this.$store.dispatch('addErrorLog', { message: i18n.t('login.errorMessage') })
this.loading = false this.loading = false
} this.$router.push({ path: this.redirect || '/users/index' })
}, }).catch(() => {
checkUsername() { this.loading = false
return this.loginForm.username.includes('@') })
}, },
getLoginData() { getLoginData() {
const [username, authHost] = this.loginForm.username.split('@') const [username, authHost] = this.loginForm.username.split('@')
return { return {
username: username.trim(), username: username.trim(),
authHost: authHost.trim(), authHost: authHost ? authHost.trim() : window.location.host,
password: this.loginForm.password password: this.loginForm.password
} }
} }

View file

@ -31,13 +31,19 @@ describe('Login', () => {
router.beforeEach(beforeEachRoute) router.beforeEach(beforeEachRoute)
}) })
it('throws error if username does not contain authHost', () => { it('takes authHost from window.location if it is not provided in username', () => {
const wrapper = mount(Login, { const wrapper = mount(Login, {
store, store,
router, router,
localVue localVue
}) })
Object.defineProperty(window, 'location', {
value: {
...window.location,
host: 'pleroma'
}
});
const errorLog = store.state.errorLog.logs const errorLog = store.state.errorLog.logs
expect(errorLog.length).toBe(0) expect(errorLog.length).toBe(0)
const submitButton = wrapper.find('button') const submitButton = wrapper.find('button')
@ -49,10 +55,7 @@ describe('Login', () => {
submitButton.trigger('click') submitButton.trigger('click')
const updatedErrorLog = store.state.errorLog.logs const updatedErrorLog = store.state.errorLog.logs
expect(updatedErrorLog.length).toBe(1) expect(updatedErrorLog.length).toBe(0)
expect(updatedErrorLog[0].message).toEqual(
'Username must contain username and host, e.g. john@pleroma.social'
)
}) })
it('throws error if password is incorrect', async (done) => { it('throws error if password is incorrect', async (done) => {
@ -92,7 +95,7 @@ describe('Login', () => {
const submitButton = wrapper.find('button') const submitButton = wrapper.find('button')
expect(wrapper.vm.$route.path).toBe('/login') expect(wrapper.vm.$route.path).toBe('/login')
wrapper.find(usernameInput).element.value = 'bob@apple' wrapper.find(usernameInput).element.value = 'bob@pleroma'
wrapper.find(usernameInput).trigger('input') wrapper.find(usernameInput).trigger('input')
wrapper.find(passwordInput).element.value = '123456' wrapper.find(passwordInput).element.value = '123456'
wrapper.find(passwordInput).trigger('input') wrapper.find(passwordInput).trigger('input')

View file

@ -27,7 +27,8 @@ describe('Search and filter users', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -39,7 +40,8 @@ describe('Search and filter users', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
wrapper.vm.handleDebounceSearchInput = (query) => { wrapper.vm.handleDebounceSearchInput = (query) => {
@ -76,7 +78,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -99,7 +102,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -114,7 +118,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -133,7 +138,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
expect(store.state.users.fetchedUsers.length).toEqual(3) expect(store.state.users.fetchedUsers.length).toEqual(3)
@ -148,7 +154,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -173,7 +180,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -192,7 +200,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -209,7 +218,8 @@ describe('Users actions', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -245,7 +255,8 @@ describe('Creates new account', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
@ -269,7 +280,8 @@ describe('Creates new account', () => {
const wrapper = mount(Users, { const wrapper = mount(Users, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
await flushPromises() await flushPromises()
expect(wrapper.vm.usersCount).toEqual(3) expect(wrapper.vm.usersCount).toEqual(3)
@ -302,7 +314,8 @@ describe('Creates new account', () => {
const wrapper = mount(NewAccountDialog, { const wrapper = mount(NewAccountDialog, {
store, store,
localVue, localVue,
sync: false sync: false,
stubs: ['router-link']
}) })
const validateEmailRule = { validator: wrapper.vm.validateEmail, field: 'email', fullField: 'email', type: 'string' } const validateEmailRule = { validator: wrapper.vm.validateEmail, field: 'email', fullField: 'email', type: 'string' }