Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 162 additions & 9 deletions src/tests/views/Account/Account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,67 @@
* 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: '<div><slot /></div>' },
}))

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),
generateUrl: vi.fn((path: string) => `/index.php${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: {
Expand All @@ -48,16 +90,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({
Expand All @@ -66,11 +110,120 @@ 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('links to the Nextcloud contact settings through a semantic link', async () => {
const wrapper = createWrapper()
await flushPromises()

const link = wrapper.get('.user-contact-edit a')
expect(link.text()).toBe('Edit contact information')
expect(link.attributes('href')).toBe('/index.php/settings/user/profile-contact')
})

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')
expect(wrapper.find('.user-contact-edit a').exists()).toBe(true)
})

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)
expect(wrapper.find('.user-contact-edit a').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()

const loadingValues = wrapper.findAll('.user-contact dd')
expect(loadingValues).toHaveLength(2)
for (const value of loadingValues) {
const text = value.element.textContent ?? ''
expect(text).toContain('Loading …')
expect(text).not.toContain('Loading …')
expect(text).not.toContain('Loading...')
}
expect(wrapper.text()).not.toContain('ada@example.com')
expect(wrapper.find('.user-contact-edit a').exists()).toBe(true)

resolveRequest(accountResponse('ada@example.com', '+1234567890'))
await flushPromises()

expect(wrapper.find('.user-contact').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'])
})
})
87 changes: 87 additions & 0 deletions src/views/Account/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
<div class="user-display-name icon-user">
<p>{{ user.displayName }}</p>
</div>
<p v-if="accountError" class="account-error" role="status">
<!-- TRANSLATORS Shown when the account contact-detail request fails. -->
{{ t('libresign', 'Email and phone details could not be loaded.') }}
</p>
<dl v-else class="user-contact">
<dt>{{ t('libresign', 'Email') }}</dt>
<dd v-if="loadingAccount">
<!-- TRANSLATORS Account email is loading; the ellipsis indicates an operation in progress and must be preserved. -->
{{ t('libresign', 'Loading …') }}
</dd>
<dd v-else>
{{ emailAddress }}
</dd>
<dt>{{ t('libresign', 'Phone') }}</dt>
<dd v-if="loadingAccount">
<!-- TRANSLATORS Account phone number is loading; the ellipsis indicates an operation in progress and must be preserved. -->
{{ t('libresign', 'Loading …') }}
</dd>
<dd v-else>
{{ phoneNumber }}
</dd>
</dl>
<p v-if="currentUser" class="user-contact-edit">
<!-- TRANSLATORS Opens the signed-in user's contact settings in Nextcloud, not an editor inside LibreSign. -->
<a :href="contactSettingsUrl">{{ t('libresign', 'Edit contact information') }}</a>

@vitormattos vitormattos Nov 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add TRANSLATORS context hints for the new context-sensitive strings in this component?

  • Line 26 — Edit contact information: Clarify that this link opens the signed-in user's contact settings in Nextcloud, not an editor inside LibreSign.
  • Lines 86–87 — Not provided: Explain that this is the fallback for an absent email address or phone number.
  • Line 17 — Email and phone details could not be loaded.: Explain that this appears when loading account contact details fails.

Please place each hint immediately above its corresponding t() call, following the Vue/TypeScript syntax in the LibreSign translation guide. Generic Email and Phone labels do not need extra hints.

</p>
</div>
<div class="user-password">
<h3>{{ t('libresign', 'Certificate') }}</h3>
Expand All @@ -31,14 +57,19 @@

<script setup lang="ts">
import { t } from '@nextcloud/l10n'
import { computed, onMounted, ref } from 'vue'

import { getCurrentUser } from '@nextcloud/auth'
import axios from '@nextcloud/axios'
import { generateOcsUrl, generateUrl } from '@nextcloud/router'

import NcAppContent from '@nextcloud/vue/components/NcAppContent'

import Documents from './partials/Documents.vue'
import ManagePassword from './partials/ManagePassword.vue'
import Signatures from './partials/Signatures.vue'
import UserImage from './partials/UserImage.vue'
import type { components } from '../../types/openapi/openapi'

defineOptions({
name: 'Account',
Expand All @@ -49,12 +80,46 @@ type User = {
displayName: string
}

type AccountMeResponse = components['schemas']['AccountMeResponse']
type OcsResponseData<T> = {
ocs: {
data: T
}
}

const currentUser = getCurrentUser()
const user: User = {
uid: currentUser?.uid ?? '',
displayName: currentUser?.displayName ?? '',
}

const account = ref<AccountMeResponse | null>(null)
const loadingAccount = ref(Boolean(currentUser))
const accountError = ref(false)

// TRANSLATORS Fallback when the account has no email address configured.
const emailAddress = computed(() => account.value?.account.emailAddress?.trim() || t('libresign', 'Not provided'))
// TRANSLATORS Fallback when the account has no phone number configured.
const phoneNumber = computed(() => account.value?.settings.phoneNumber?.trim() || t('libresign', 'Not provided'))
const contactSettingsUrl = generateUrl('/settings/user/profile-contact')

onMounted(async () => {
if (!currentUser) {
return
}

try {
const response = await axios.get<OcsResponseData<AccountMeResponse>>(
generateOcsUrl('/apps/libresign/api/v1/account/me'),
)
account.value = response.data.ocs.data
} catch {
accountError.value = true
} finally {
loadingAccount.value = false
}
})

defineExpose({
user,
t,
Expand Down Expand Up @@ -112,6 +177,28 @@ defineExpose({
margin-left: 12px;
padding-left: 22px;
}

.user-contact{
margin: 0 12px;

dt{
font-weight: bold;
}

dd{
margin: 0 0 12px;
overflow-wrap: anywhere;
}
}

.account-error{
margin: 0 12px;
overflow-wrap: anywhere;
}

.user-contact-edit{
margin: 0 12px;
}
}

.user-password{
Expand Down
Loading