From 40ac9a9e22f1a02e5fed0cd177c3b2693683bdd8 Mon Sep 17 00:00:00 2001 From: chirag-gupta7 Date: Sat, 14 Nov 2026 22:32:12 +0530 Subject: [PATCH 1/4] feat(account): display existing email and phone details Read account.emailAddress and settings.phoneNumber from the existing /api/v1/account/me OCS endpoint. Missing values fall back to a localized label, failures show a safe message, and no request is made without an authenticated user. Signed-off-by: chirag-gupta7 --- src/tests/views/Account/Account.spec.ts | 151 ++++++++++++++++++++++-- src/views/Account/Account.vue | 63 ++++++++++ 2 files changed, 205 insertions(+), 9 deletions(-) diff --git a/src/tests/views/Account/Account.spec.ts b/src/tests/views/Account/Account.spec.ts index 37aec4cc71..28e87b0668 100644 --- a/src/tests/views/Account/Account.spec.ts +++ b/src/tests/views/Account/Account.spec.ts @@ -3,11 +3,16 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -import { describe, expect, it, vi } from 'vitest' -import { mount } from '@vue/test-utils' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { flushPromises, mount } from '@vue/test-utils' import Account from '../../../views/Account/Account.vue' +const { getCurrentUserMock, axiosGetMock } = vi.hoisted(() => ({ + getCurrentUserMock: vi.fn(), + axiosGetMock: vi.fn(), +})) + vi.mock('@nextcloud/vue/components/NcAppContent', () => ({ default: { name: 'NcAppContent', template: '
' }, })) @@ -15,13 +20,49 @@ vi.mock('@nextcloud/vue/components/NcAppContent', () => ({ vi.mock('@nextcloud/l10n', () => globalThis.mockNextcloudL10n()) vi.mock('@nextcloud/auth', () => ({ - getCurrentUser: vi.fn(() => ({ - uid: 'ada', - displayName: 'Ada Lovelace', - })), + getCurrentUser: () => getCurrentUserMock(), +})) + +vi.mock('@nextcloud/axios', () => ({ + default: { + get: axiosGetMock, + }, })) +vi.mock('@nextcloud/router', () => ({ + generateOcsUrl: vi.fn((path: string) => path), +})) + +const accountResponse = (emailAddress: string, phoneNumber: string) => ({ + data: { + ocs: { + data: { + account: { + uid: 'ada', + displayName: 'Ada Lovelace', + emailAddress, + }, + settings: { + canRequestSign: false, + hasSignatureFile: false, + phoneNumber, + }, + }, + }, + }, +}) + describe('Account.vue', () => { + beforeEach(() => { + getCurrentUserMock.mockReset() + getCurrentUserMock.mockReturnValue({ + uid: 'ada', + displayName: 'Ada Lovelace', + }) + axiosGetMock.mockReset() + axiosGetMock.mockResolvedValue(accountResponse('ada@example.com', '+1234567890')) + }) + function createWrapper() { return mount(Account, { global: { @@ -48,16 +89,18 @@ describe('Account.vue', () => { }) } - it('renders the current user details and certificate section', () => { + it('renders the current user details and certificate section', async () => { const wrapper = createWrapper() + await flushPromises() expect(wrapper.text()).toContain('Details') expect(wrapper.text()).toContain('Certificate') expect(wrapper.text()).toContain('Ada Lovelace') }) - it('passes the current user to UserImage', () => { + it('passes the current user to UserImage', async () => { const wrapper = createWrapper() + await flushPromises() const userImage = wrapper.findComponent({ name: 'UserImage' }) expect(userImage.props('user')).toEqual({ @@ -66,11 +109,101 @@ describe('Account.vue', () => { }) }) - it('renders the account partial sections', () => { + it('renders the account partial sections', async () => { + const wrapper = createWrapper() + await flushPromises() + + expect(wrapper.find('.manage-password-stub').exists()).toBe(true) + expect(wrapper.find('.signatures-stub').exists()).toBe(true) + expect(wrapper.find('.documents-stub').exists()).toBe(true) + }) + + it('renders the email and phone from the account API', async () => { + const wrapper = createWrapper() + await flushPromises() + + expect(wrapper.text()).toContain('Email') + expect(wrapper.text()).toContain('ada@example.com') + expect(wrapper.text()).toContain('Phone') + expect(wrapper.text()).toContain('+1234567890') + expect(wrapper.text()).toContain('Ada Lovelace') + expect(wrapper.find('.manage-password-stub').exists()).toBe(true) + }) + + it('shows a fallback when the email is missing', async () => { + axiosGetMock.mockResolvedValue(accountResponse('', '+1234567890')) + const wrapper = createWrapper() + await flushPromises() + + const values = wrapper.findAll('.user-contact dd').map((value) => value.text()) + expect(values).toEqual(['Not provided', '+1234567890']) + expect(wrapper.text()).not.toContain('undefined') + expect(wrapper.text()).not.toContain('null') + }) + + it('shows a fallback when the phone is missing', async () => { + axiosGetMock.mockResolvedValue(accountResponse('ada@example.com', '')) + const wrapper = createWrapper() + await flushPromises() + + const values = wrapper.findAll('.user-contact dd').map((value) => value.text()) + expect(values).toEqual(['ada@example.com', 'Not provided']) + expect(wrapper.text()).not.toContain('undefined') + expect(wrapper.text()).not.toContain('null') + }) + + it('keeps the details section readable when both values are missing', async () => { + axiosGetMock.mockResolvedValue(accountResponse('', '')) const wrapper = createWrapper() + await flushPromises() + + const values = wrapper.findAll('.user-contact dd').map((value) => value.text()) + expect(values).toEqual(['Not provided', 'Not provided']) + expect(wrapper.text()).toContain('Details') + expect(wrapper.text()).toContain('Certificate') + expect(wrapper.text()).not.toContain('undefined') + expect(wrapper.text()).not.toContain('null') + }) + it('shows a safe error state without breaking the account page', async () => { + axiosGetMock.mockRejectedValue(new Error('Internal Server Error at /var/www/libresign')) + const wrapper = createWrapper() + await flushPromises() + + expect(wrapper.text()).toContain('Email and phone details could not be loaded.') + expect(wrapper.text()).not.toContain('Internal Server Error') + expect(wrapper.text()).not.toContain('/var/www/libresign') expect(wrapper.find('.manage-password-stub').exists()).toBe(true) expect(wrapper.find('.signatures-stub').exists()).toBe(true) expect(wrapper.find('.documents-stub').exists()).toBe(true) }) + + it('shows a loading state until the account request resolves', async () => { + let resolveRequest: (response: unknown) => void = () => {} + axiosGetMock.mockImplementation(() => new Promise((resolve) => { + resolveRequest = resolve + })) + + const wrapper = createWrapper() + + expect(wrapper.text()).toContain('Loading …') + expect(wrapper.text()).not.toContain('ada@example.com') + + resolveRequest(accountResponse('ada@example.com', '+1234567890')) + await flushPromises() + + expect(wrapper.text()).not.toContain('Loading …') + expect(wrapper.text()).toContain('ada@example.com') + expect(wrapper.text()).toContain('+1234567890') + }) + + it('does not fetch account details without an authenticated user', async () => { + getCurrentUserMock.mockReturnValue(null) + const wrapper = createWrapper() + await flushPromises() + + expect(axiosGetMock).not.toHaveBeenCalled() + const values = wrapper.findAll('.user-contact dd').map((value) => value.text()) + expect(values).toEqual(['Not provided', 'Not provided']) + }) }) diff --git a/src/views/Account/Account.vue b/src/views/Account/Account.vue index d38ce63efe..5530ee980a 100644 --- a/src/views/Account/Account.vue +++ b/src/views/Account/Account.vue @@ -13,6 +13,15 @@

{{ user.displayName }}

+ +
+
{{ t('libresign', 'Email') }}
+
{{ loadingAccount ? t('libresign', 'Loading …') : emailAddress }}
+
{{ t('libresign', 'Phone') }}
+
{{ loadingAccount ? t('libresign', 'Loading …') : phoneNumber }}
+

{{ t('libresign', 'Certificate') }}

@@ -31,14 +40,19 @@