mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 07:06:36 +08:00
feat(cloudflare): add DNS pointing integration
Implement Cloudflare connection management, pointing groups and members, asynchronous A-record reconciliation, node IP triggers, admin APIs, management pages, migrations, tests, and documentation.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { createElement } from 'react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import CloudflarePage from '@/app/(main)/cloudflare/page';
|
||||
import { CloudflareService } from '@/lib/services/openflare';
|
||||
|
||||
vi.mock('@/lib/services/openflare', async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import('@/lib/services/openflare')>();
|
||||
return {
|
||||
...actual,
|
||||
CloudflareService: { ...actual.CloudflareService, getOverview: vi.fn() },
|
||||
};
|
||||
});
|
||||
|
||||
describe('Cloudflare overview', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(CloudflareService.getOverview).mockResolvedValue({
|
||||
connection: {
|
||||
configured: false,
|
||||
ready: false,
|
||||
source: '',
|
||||
dns_account_id: null,
|
||||
status: '',
|
||||
verified_at: null,
|
||||
},
|
||||
group_count: 0,
|
||||
member_count: 0,
|
||||
ok_count: 0,
|
||||
pending_count: 0,
|
||||
error_count: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('shows the token readiness gate and phase-one limitation', async () => {
|
||||
const client = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false, gcTime: 0 } },
|
||||
});
|
||||
render(
|
||||
createElement(
|
||||
QueryClientProvider,
|
||||
{ client },
|
||||
createElement(CloudflarePage),
|
||||
),
|
||||
);
|
||||
|
||||
expect(await screen.findByText('Cloudflare 连接尚未就绪')).toBeVisible();
|
||||
expect(screen.getByText(/一期不提供自动故障切换/)).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import apiClient from '@/lib/services/core/api-client';
|
||||
import { CloudflareService } from '@/lib/services/openflare/cloudflare.service';
|
||||
|
||||
vi.mock('@/lib/services/core/api-client', () => ({
|
||||
default: { get: vi.fn(), post: vi.fn(), put: vi.fn() },
|
||||
}));
|
||||
|
||||
function response<T>(data: T) {
|
||||
return {
|
||||
data: { error_msg: '', data },
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: {},
|
||||
config: { headers: {} },
|
||||
} as AxiosResponse;
|
||||
}
|
||||
|
||||
describe('CloudflareService', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(apiClient.get).mockReset();
|
||||
vi.mocked(apiClient.post).mockReset();
|
||||
vi.mocked(apiClient.put).mockReset();
|
||||
});
|
||||
|
||||
it('uses the Cloudflare management API paths', async () => {
|
||||
vi.mocked(apiClient.get).mockResolvedValue(response([]));
|
||||
vi.mocked(apiClient.post).mockResolvedValue(
|
||||
response({ task_id: 'task-1' }),
|
||||
);
|
||||
vi.mocked(apiClient.put).mockResolvedValue(response({ configured: true }));
|
||||
|
||||
await CloudflareService.saveConnection({
|
||||
source: 'standalone',
|
||||
dns_account_id: 0,
|
||||
api_token: 'secret',
|
||||
});
|
||||
await CloudflareService.listGroups();
|
||||
await CloudflareService.syncMember(7, 9);
|
||||
|
||||
expect(apiClient.put).toHaveBeenCalledWith(
|
||||
'/api/v1/d/cloudflare/connection',
|
||||
expect.objectContaining({ source: 'standalone' }),
|
||||
undefined,
|
||||
);
|
||||
expect(apiClient.get).toHaveBeenCalledWith(
|
||||
'/api/v1/d/cloudflare/groups',
|
||||
expect.objectContaining({ params: undefined }),
|
||||
);
|
||||
expect(apiClient.post).toHaveBeenCalledWith(
|
||||
'/api/v1/d/cloudflare/groups/7/members/9/sync',
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user