Do not allow to find by name in findUser()
This commit is contained in:
parent
09f9640be1
commit
ab4a75bdd9
|
@ -110,7 +110,7 @@ const UserProfile = {
|
|||
const maybeName = userNameOrId.name
|
||||
|
||||
// Check if user data is already loaded in store
|
||||
const user = this.$store.getters.findUser(maybeId || maybeName)
|
||||
const user = maybeId ? this.$store.getters.findUser(maybeId) : this.$store.getters.findUserByName(maybeName)
|
||||
if (user) {
|
||||
loadById(user.id)
|
||||
} else {
|
||||
|
|
|
@ -16,9 +16,6 @@ export const mergeOrAdd = (arr, obj, item) => {
|
|||
// This is a new item, prepare it
|
||||
arr.push(item)
|
||||
obj[item.id] = item
|
||||
if (item.screen_name && !item.screen_name.includes('@')) {
|
||||
obj[item.screen_name.toLowerCase()] = item
|
||||
}
|
||||
return { item, new: true }
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +159,11 @@ export const mutations = {
|
|||
if (user.relationship) {
|
||||
state.relationships[user.relationship.id] = user.relationship
|
||||
}
|
||||
mergeOrAdd(state.users, state.usersObject, user)
|
||||
const res = mergeOrAdd(state.users, state.usersObject, user)
|
||||
const item = res.item
|
||||
if (res.new && item.screen_name && !item.screen_name.includes('@')) {
|
||||
state.usersByNameObject[item.screen_name.toLowerCase()] = item
|
||||
}
|
||||
})
|
||||
},
|
||||
updateUserRelationship (state, relationships) {
|
||||
|
@ -239,12 +240,10 @@ export const mutations = {
|
|||
|
||||
export const getters = {
|
||||
findUser: state => query => {
|
||||
const result = state.usersObject[query]
|
||||
// In case it's a screen_name, we can try searching case-insensitive
|
||||
if (!result && typeof query === 'string') {
|
||||
return state.usersObject[query.toLowerCase()]
|
||||
}
|
||||
return result
|
||||
return state.usersObject[query]
|
||||
},
|
||||
findUserByName: state => query => {
|
||||
return state.usersByNameObject[query.toLowerCase()]
|
||||
},
|
||||
findUserByUrl: state => query => {
|
||||
return state.users
|
||||
|
@ -263,6 +262,7 @@ export const defaultState = {
|
|||
currentUser: false,
|
||||
users: [],
|
||||
usersObject: {},
|
||||
usersByNameObject: {},
|
||||
signUpPending: false,
|
||||
signUpErrors: [],
|
||||
relationships: {}
|
||||
|
|
|
@ -15,6 +15,7 @@ const actions = {
|
|||
|
||||
const testGetters = {
|
||||
findUser: state => getters.findUser(state.users),
|
||||
findUserByName: state => getters.findUserByName(state.users),
|
||||
relationship: state => getters.relationship(state.users),
|
||||
mergedConfig: state => ({
|
||||
colors: '',
|
||||
|
@ -95,6 +96,7 @@ const externalProfileStore = createStore({
|
|||
credentials: ''
|
||||
},
|
||||
usersObject: { 100: extUser },
|
||||
usersByNameObject: {},
|
||||
users: [extUser],
|
||||
relationships: {}
|
||||
}
|
||||
|
@ -163,7 +165,8 @@ const localProfileStore = createStore({
|
|||
currentUser: {
|
||||
credentials: ''
|
||||
},
|
||||
usersObject: { 100: localUser, testuser: localUser },
|
||||
usersObject: { 100: localUser },
|
||||
usersByNameObject: { testuser: localUser },
|
||||
users: [localUser],
|
||||
relationships: {}
|
||||
}
|
||||
|
|
|
@ -57,24 +57,27 @@ describe('The users module', () => {
|
|||
})
|
||||
|
||||
describe('findUser', () => {
|
||||
it('returns user with matching screen_name', () => {
|
||||
it('does not return user with matching screen_name', () => {
|
||||
const user = { screen_name: 'Guy', id: '1' }
|
||||
const state = {
|
||||
usersObject: {
|
||||
1: user,
|
||||
1: user
|
||||
},
|
||||
usersByNameObject: {
|
||||
guy: user
|
||||
}
|
||||
}
|
||||
const name = 'Guy'
|
||||
const expected = { screen_name: 'Guy', id: '1' }
|
||||
expect(getters.findUser(state)(name)).to.eql(expected)
|
||||
expect(getters.findUser(state)(name)).to.eql(undefined)
|
||||
})
|
||||
|
||||
it('returns user with matching id', () => {
|
||||
const user = { screen_name: 'Guy', id: '1' }
|
||||
const state = {
|
||||
usersObject: {
|
||||
1: user,
|
||||
1: user
|
||||
},
|
||||
usersByNameObject: {
|
||||
guy: user
|
||||
}
|
||||
}
|
||||
|
@ -83,4 +86,35 @@ describe('The users module', () => {
|
|||
expect(getters.findUser(state)(id)).to.eql(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('findUserByName', () => {
|
||||
it('returns user with matching screen_name', () => {
|
||||
const user = { screen_name: 'Guy', id: '1' }
|
||||
const state = {
|
||||
usersObject: {
|
||||
1: user
|
||||
},
|
||||
usersByNameObject: {
|
||||
guy: user
|
||||
}
|
||||
}
|
||||
const name = 'Guy'
|
||||
const expected = { screen_name: 'Guy', id: '1' }
|
||||
expect(getters.findUserByName(state)(name)).to.eql(expected)
|
||||
})
|
||||
|
||||
it('does not return user with matching id', () => {
|
||||
const user = { screen_name: 'Guy', id: '1' }
|
||||
const state = {
|
||||
usersObject: {
|
||||
1: user
|
||||
},
|
||||
usersByNameObject: {
|
||||
guy: user
|
||||
}
|
||||
}
|
||||
const id = '1'
|
||||
expect(getters.findUserByName(state)(id)).to.eql(undefined)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue