UI review, stage 1: 401 on /auth/*, expiring certs, VPN netmask, formatBytes
401 from change-password/change-username no longer logs the user out; Analytics reads the plain /profiles array; VPN settings send vpn_netmask with vpn_network (the API reset it to 255.255.255.0); formatBytes guards null/NaN/negative; Certificates sorts 0 days correctly. Vitest added for formatters, CIDR parsing and the 401 interceptor. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3fac8d70d8
commit
b78de98f26
11 files changed
+672
-57
No files matched your search
@@ -9,6 +9,23 @@ const profilesApiClient = axios.create();
|
||||
const getBaseUrl = (config) => config?.api_base_url || 'http://localhost:5001/api/v1';
|
||||
const getProfilesBaseUrl = (config) => config?.profiles_api_base_url || 'http://localhost:8000';
|
||||
|
||||
// Auth endpoints answer 401 for a wrong current password or 2FA code (change-password, change-username):
|
||||
// that is a form error, not an expired session, so it must not log the user out.
|
||||
export const isAuthRequest = (url) => /(^|\/)auth\//.test(url || '');
|
||||
|
||||
// 401 on a regular API request: the session is gone, clear it and go to the login page
|
||||
export const handleUnauthorized = (error) => {
|
||||
if (error.response && error.response.status === 401 && !isAuthRequest(error.config?.url)) {
|
||||
localStorage.removeItem('ovpmon_token');
|
||||
localStorage.removeItem('ovpmon_user');
|
||||
// Redirecting using window.location for absolute refresh
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
};
|
||||
|
||||
// Add interceptors to handle Auth and Dynamic Base URLs
|
||||
const setupInterceptors = (instance, getBase) => {
|
||||
instance.interceptors.request.use((reqConfig) => {
|
||||
@@ -22,20 +39,7 @@ const setupInterceptors = (instance, getBase) => {
|
||||
return reqConfig;
|
||||
});
|
||||
|
||||
instance.interceptors.response.use(
|
||||
response => response,
|
||||
error => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
localStorage.removeItem('ovpmon_token');
|
||||
localStorage.removeItem('ovpmon_user');
|
||||
// Redirecting using window.location for absolute refresh
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
instance.interceptors.response.use(response => response, handleUnauthorized);
|
||||
};
|
||||
|
||||
setupInterceptors(apiClient, getBaseUrl);
|
||||
@@ -59,10 +63,10 @@ export function useApi() {
|
||||
|
||||
const fetchCertificates = async () => {
|
||||
const res = await profilesApiClient.get('/profiles');
|
||||
return res.data;
|
||||
// GET /profiles returns a plain array (not the {success, data} envelope of the monitoring API)
|
||||
return Array.isArray(res.data) ? res.data : [];
|
||||
};
|
||||
|
||||
|
||||
return {
|
||||
apiClient,
|
||||
profilesApiClient,
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { AxiosError } from 'axios';
|
||||
|
||||
const store = {};
|
||||
let location;
|
||||
|
||||
beforeEach(() => {
|
||||
for (const k of Object.keys(store)) delete store[k];
|
||||
store.ovpmon_token = 'tok';
|
||||
store.ovpmon_user = 'admin';
|
||||
location = { pathname: '/account', href: '/account' };
|
||||
vi.stubGlobal('localStorage', {
|
||||
getItem: (k) => (k in store ? store[k] : null),
|
||||
setItem: (k, v) => { store[k] = String(v); },
|
||||
removeItem: (k) => { delete store[k]; }
|
||||
});
|
||||
vi.stubGlobal('window', { location });
|
||||
});
|
||||
|
||||
// Make the client answer with the given status without any network
|
||||
const failWith = async (client, status, url) => {
|
||||
client.defaults.adapter = (config) => Promise.reject(
|
||||
new AxiosError('failed', 'ERR_BAD_REQUEST', config, null, { status, data: {}, config })
|
||||
);
|
||||
await client.post(url).catch(() => {});
|
||||
};
|
||||
|
||||
describe('401 handling', () => {
|
||||
it('keeps the session on 401 from /auth/* (wrong password or 2FA code)', async () => {
|
||||
const { useApi } = await import('./useApi');
|
||||
await failWith(useApi().apiClient, 401, '../auth/change-password');
|
||||
expect(store.ovpmon_token).toBe('tok');
|
||||
expect(location.href).toBe('/account');
|
||||
});
|
||||
|
||||
it('ends the session on 401 from other requests', async () => {
|
||||
const { useApi } = await import('./useApi');
|
||||
await failWith(useApi().apiClient, 401, '/stats');
|
||||
expect(store.ovpmon_token).toBeUndefined();
|
||||
expect(location.href).toBe('/login');
|
||||
});
|
||||
|
||||
it('ignores other error codes', async () => {
|
||||
const { useApi } = await import('./useApi');
|
||||
await failWith(useApi().profilesApiClient, 500, '/profiles');
|
||||
expect(store.ovpmon_token).toBe('tok');
|
||||
});
|
||||
});
|
||||
@@ -1,15 +1,17 @@
|
||||
export function useFormatters() {
|
||||
function formatBytes(bytes, decimals = 2) {
|
||||
if (bytes === 0) return '0 B';
|
||||
const value = Number(bytes);
|
||||
// null, undefined, NaN and negative values are shown as zero
|
||||
if (!Number.isFinite(value) || value <= 0) return '0 B';
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
const i = Math.min(Math.max(Math.floor(Math.log(value) / Math.log(k)), 0), sizes.length - 1);
|
||||
return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
function formatRate(rate) {
|
||||
return parseFloat(rate).toFixed(3) + ' Mbps';
|
||||
return (parseFloat(rate) || 0).toFixed(3) + ' Mbps';
|
||||
}
|
||||
|
||||
function parseServerDate(dateStr) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { useFormatters } from './useFormatters';
|
||||
|
||||
const { formatBytes, formatRate } = useFormatters();
|
||||
|
||||
describe('formatBytes', () => {
|
||||
it('formats regular values', () => {
|
||||
expect(formatBytes(1536)).toBe('1.5 KB');
|
||||
expect(formatBytes(5 * 1024 * 1024)).toBe('5 MB');
|
||||
});
|
||||
|
||||
it('shows zero for empty, non-numeric and negative values', () => {
|
||||
for (const v of [0, null, undefined, NaN, 'abc', -5, Infinity]) {
|
||||
expect(formatBytes(v)).toBe('0 B');
|
||||
}
|
||||
});
|
||||
|
||||
it('stays inside the unit table', () => {
|
||||
expect(formatBytes(0.5)).toBe('0.5 B');
|
||||
expect(formatBytes(Math.pow(1024, 6))).toMatch(/ TB$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatRate', () => {
|
||||
it('does not produce NaN', () => {
|
||||
expect(formatRate(1.23456)).toBe('1.235 Mbps');
|
||||
expect(formatRate(null)).toBe('0.000 Mbps');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user