Skip to content

Commit d6d0782

Browse files
fix(settings): guard isAdmin access when getCurrentUser returns null
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 35ee07e commit d6d0782

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/components/Settings/Settings.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default {
5656
},
5757
data() {
5858
return {
59-
isAdmin: getCurrentUser().isAdmin,
59+
isAdmin: getCurrentUser()?.isAdmin ?? false,
6060
}
6161
},
6262
methods: {

src/tests/components/Settings/Settings.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,54 @@ describe('Settings', () => {
258258
})
259259
})
260260

261+
describe('RULE: unauthenticated users (signing via email link) do not crash the component', () => {
262+
const createUnauthenticatedWrapper = () => {
263+
getCurrentUserMock.mockReturnValue(null)
264+
265+
return mount(Settings, {
266+
global: {
267+
stubs: {
268+
NcAppNavigationItem: {
269+
name: 'NcAppNavigationItem',
270+
props: ['name', 'to', 'href', 'icon'],
271+
template: '<li><slot name="icon" /><span class="item-name">{{ name }}</span><slot /></li>',
272+
},
273+
AccountIcon: { template: '<div class="account-icon"></div>' },
274+
StarIcon: { template: '<div class="star-icon"></div>' },
275+
TuneIcon: { template: '<div class="tune-icon"></div>' },
276+
},
277+
mocks: { t },
278+
},
279+
})
280+
}
281+
282+
it('mounts without throwing when getCurrentUser returns null', () => {
283+
expect(() => createUnauthenticatedWrapper()).not.toThrow()
284+
})
285+
286+
it('isAdmin is false when getCurrentUser returns null', () => {
287+
wrapper = createUnauthenticatedWrapper()
288+
289+
expect(getWrapper().vm.isAdmin).toBe(false)
290+
})
291+
292+
it('hides the Administration link when user is unauthenticated', () => {
293+
wrapper = createUnauthenticatedWrapper()
294+
const items = getItems()
295+
const adminItem = findItemByName(items, 'Administration')
296+
297+
expect(adminItem).toBeUndefined()
298+
})
299+
300+
it('shows 2 navigation items for unauthenticated user', () => {
301+
wrapper = createUnauthenticatedWrapper()
302+
const items = getItems()
303+
304+
// Account + Rate = 2
305+
expect(items.length).toBe(2)
306+
})
307+
})
308+
261309
describe('RULE: navigation items count depends on admin status', () => {
262310
it('shows 2 items for non-admin', () => {
263311
wrapper = createWrapper(false)

0 commit comments

Comments
 (0)