TypeScript · 2783 bytes Raw Blame History
1 import { test, beforeAll, afterAll, expect } from 'vitest';
2 import { createServer } from '../index.js';
3 import type { FastifyInstance } from 'fastify';
4
5 let app: FastifyInstance;
6 let authToken: string;
7
8 beforeAll(async () => {
9 // Set test environment variables
10 process.env.NODE_ENV = 'test';
11 process.env.JWT_SECRET = 'test-secret-for-testing-only-min-32-chars-long';
12 process.env.ZEPHYRFS_NODE_URL = 'http://localhost:8080';
13
14 app = await createServer();
15 await app.ready();
16 });
17
18 afterAll(async () => {
19 await app?.close();
20 });
21
22 test('Health check endpoint', async () => {
23 const response = await app.inject({
24 method: 'GET',
25 url: '/api/health',
26 });
27
28 expect(response.statusCode).toBe(200);
29 const body = JSON.parse(response.body);
30 expect(body.status).toBeDefined();
31 expect(body.timestamp).toBeDefined();
32 });
33
34 test('Login with valid credentials', async () => {
35 const response = await app.inject({
36 method: 'POST',
37 url: '/api/auth/login',
38 payload: {
39 username: 'admin',
40 password: 'admin',
41 },
42 });
43
44 expect(response.statusCode).toBe(200);
45 const body = JSON.parse(response.body);
46 expect(body.token).toBeDefined();
47 expect(body.refreshToken).toBeDefined();
48 expect(body.user.username).toBe('admin');
49
50 authToken = body.token;
51 });
52
53 test('Login with invalid credentials', async () => {
54 const response = await app.inject({
55 method: 'POST',
56 url: '/api/auth/login',
57 payload: {
58 username: 'invalid',
59 password: 'invalid',
60 },
61 });
62
63 expect(response.statusCode).toBe(401);
64 });
65
66 test('API info endpoint', async () => {
67 const response = await app.inject({
68 method: 'GET',
69 url: '/api/',
70 headers: {
71 authorization: `Bearer ${authToken}`,
72 },
73 });
74
75 expect(response.statusCode).toBe(200);
76 const body = JSON.parse(response.body);
77 expect(body.name).toBe('ZephyrFS Web API');
78 expect(body.endpoints).toBeDefined();
79 });
80
81 test('Get current user info', async () => {
82 const response = await app.inject({
83 method: 'GET',
84 url: '/api/auth/me',
85 headers: {
86 authorization: `Bearer ${authToken}`,
87 },
88 });
89
90 expect(response.statusCode).toBe(200);
91 const body = JSON.parse(response.body);
92 expect(body.username).toBe('admin');
93 });
94
95 test('Unauthorized request without token', async () => {
96 const response = await app.inject({
97 method: 'GET',
98 url: '/api/files',
99 });
100
101 expect(response.statusCode).toBe(401);
102 });
103
104 test('WebDAV discovery endpoint', async () => {
105 const response = await app.inject({
106 method: 'GET',
107 url: '/api/webdav',
108 });
109
110 expect(response.statusCode).toBe(200);
111 const body = JSON.parse(response.body);
112 expect(body.service).toBe('ZephyrFS WebDAV');
113 expect(body.url).toBeDefined();
114 expect(body.instructions).toBeDefined();
115 });