• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CodeHex16 / Suppl-AI / 15071463608

16 May 2025 03:02PM UTC coverage: 81.09%. First build
15071463608

Pull #55

github

web-flow
Merge 99649ab05 into 1832a13c9
Pull Request #55: Test di unita e varie optimization

333 of 436 branches covered (76.38%)

Branch coverage included in aggregate %.

651 of 790 new or added lines in 54 files covered. (82.41%)

1974 of 2409 relevant lines covered (81.94%)

1.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.52
/src/routes/api/users/+server.ts
1
import type { RequestHandler } from './$types';
2
import { json } from '@sveltejs/kit';
1✔
3
import { env } from '$env/dynamic/public';
1✔
4
import { logger } from '$lib/utils/logger';
1✔
5

6
const DATABASE_URL = env.PUBLIC_DATABASE_URL;
1✔
7

8
export const POST: RequestHandler = async ({ request, cookies }) => {
1✔
9
        logger.info('POST /api/users');
3✔
10
        try {
3✔
11
                const token = cookies.get('token');
3✔
12
                const req = await request.json();
3✔
13
                logger.debug('Request data:', req);
3✔
14

15
                logger.log('Request body:', req);
3✔
16
                if (!token) {
3✔
17
                        logger.error('Token non trovato');
1✔
18
                        return json({ error: 'Unauthorized' }, { status: 401 });
1✔
19
                }
1✔
20

21
                // Verifica se il token è valido
22
                const tokenResponse = await fetch(`http://${DATABASE_URL}/auth/verify?token=${token}`, {
2✔
23
                        method: 'GET'
2✔
24
                });
2✔
25

26
                logger.debug('Token response:', tokenResponse);
2✔
27

28
                if (!tokenResponse.ok) {
3✔
29
                        const errorData = await tokenResponse.json().catch(() => {});
1✔
30
                        logger.error('Token non valido:', errorData);
1✔
31
                        cookies.delete('token', { path: '/' });
1✔
32
                        return json(
1✔
33
                                { error: 'Token non valido', details: errorData },
1✔
34
                                { status: tokenResponse.status }
1✔
35
                        );
1✔
36
                }
1✔
37

38
                // Crea una nuovo utente nel database
39
                const response = await fetch(`http://${DATABASE_URL}/users`, {
1✔
40
                        method: 'POST',
1✔
41
                        headers: {
1✔
42
                                'Content-Type': 'application/json',
1✔
43
                                Authorization: `Bearer ${token}`
1✔
44
                        },
1✔
45
                        body: JSON.stringify({
1✔
46
                                name: req.name,
1✔
47
                                email: req.email,
1✔
48
                                scopes: [req.role]
1✔
49
                        })
1✔
50
                });
1✔
51

52
                logger.debug('Response from user creation:', response);
1✔
53

54
                if (response.status === 400) {
3!
NEW
55
                        const errorMessage = await response.json();
×
56
                        return json(
×
57
                                {
×
58
                                        error: errorMessage.detail
×
59
                                },
×
60
                                { status: response.status }
×
61
                        );
×
62
                } else if (response.status === 422) {
3!
NEW
63
                        const errorMessage = await response.json();
×
NEW
64
                        logger.log('Error message:', errorMessage);
×
65
                        return json(
×
66
                                {
×
67
                                        error: 'Dati inseriti non validi'
×
68
                                },
×
69
                                { status: response.status }
×
70
                        );
×
71
                }
×
72

73
                if (!response.ok) {
3!
NEW
74
                        const errorMessage = await response.json().catch(() => {});
×
NEW
75
                        logger.error('Errore durante l\'aggiunta dell\'utente:', errorMessage);
×
76
                        return json(
×
NEW
77
                                { error: "Errore durante l'aggiunta dell'utente", details: errorMessage },
×
78
                                { status: response.status }
×
79
                        );
×
80
                }
✔
81
                logger.info('Utente creato con successo:', req);
1✔
82
                return json({
1✔
83
                        success: true,
1✔
84
                        user: { 
1✔
85
                                name: req.name, 
1✔
86
                                email: req.email, 
1✔
87
                                role: req.role 
1✔
88
                        },
1✔
89
                        message: 'User added successfully'
1✔
90
                });
1✔
91
        } catch (error) {
3!
NEW
92
                logger.error('Errore durante la creazione della nuova chat:', error);
×
93
                return json({ error: 'Internal server error' }, { status: 500 });
×
94
        }
×
95
};
3✔
96

97
export const PUT: RequestHandler = async ({ request, cookies }) => {
1✔
98
        logger.info('PUT /api/users');
2✔
99

100
        try {
2✔
101
                const token = cookies.get('token');
2✔
102
                const req = await request.json();
2✔
103
                logger.debug('Request data:', req);
2✔
104

105
                if (!token) {
2✔
106
                        logger.error('Token non trovato');
1✔
107
                        return json({ error: 'Unauthorized' }, { status: 401 });
1✔
108
                }
1✔
109

110
                if(!req.admin_password) {
2!
NEW
111
                        logger.error('Admin Password non trovata');
×
112
                        return json({ error: 'Unauthorized' }, { status: 401 });
×
113
                }
✔
114

115
                const tokenResponse = await fetch(`http://${DATABASE_URL}/auth/verify?token=${token}`, {
1✔
116
                        method: 'GET'
1✔
117
                });
1✔
118
                logger.debug('Token response:', tokenResponse);
1✔
119
                if (!tokenResponse.ok) {
2!
NEW
120
                        const errorData = await tokenResponse.json().catch(() => {});
×
NEW
121
                        logger.error('Token non valido:', errorData);
×
122
                        cookies.delete('token', { path: '/' });
×
123
                        return json(
×
124
                                { error: 'Token non valido', details: errorData },
×
125
                                { status: tokenResponse.status }
×
126
                        );
×
127
                }
✔
128
                
129

130
                const response = await fetch(`http://${DATABASE_URL}/users/`, {
1✔
131

132
                        method: 'PATCH',
1✔
133
                        headers: {
1✔
134
                                'Content-Type': 'application/json',
1✔
135
                                Authorization: `Bearer ${token}`
1✔
136
                        },
1✔
137
                        body: JSON.stringify({
1✔
138
                                _id: req.email,
1✔
139
                                name: req.name,
1✔
140
                                scopes: [req.role],
1✔
141
                                admin_password: req.admin_password
1✔
142
                        })
1✔
143
                });
1✔
144
                logger.debug('Response from user update:', response);
1✔
145

146
                if (!response.ok) {
2!
NEW
147
                        const errorMessage = await response.json().catch(() => {});
×
NEW
148
                        logger.error('Errore durante l\'aggiornamento dell\'utente:', errorMessage);
×
149
                        if (response.status === 304) {
×
150
                                return json({ error: 'Nessuna modifica fatta', details: 'No changes made' });
×
151
                        }
×
152
                        if (response.status === 401) {
×
153
                                return json({ error: 'Password errata', details: 'Password errata' }, { status: 401 });
×
154
                        }
×
155
                        return json(
×
156
                                { error: "Errore durante l'aggiunta dell'utente", details: response },
×
157
                                { status: response.status }
×
158
                        );
×
159
                }
✔
160
                logger.info('Utente aggiornato con successo:', req);
1✔
161
                return json({
1✔
162
                        success: true,
1✔
163
                        user: { name: req.name, email: req.email, role: req.scope },
1✔
164
                        message: 'User updated successfully'
1✔
165
                });
1✔
166
        } catch (error) {
2!
NEW
167
                logger.error('Errore durante la creazione della nuova chat:', error);
×
168
                return json({ error: 'Internal server error' }, { status: 500 });
×
169
        }
×
170
};
2✔
171

172
export const DELETE: RequestHandler = async ({ request, cookies }) => {
1✔
173
        logger.info('DELETE /api/users');
2✔
174
        try {
2✔
175
                const token = cookies.get('token');
2✔
176
                const req = await request.json();
2✔
177
                logger.debug('Request data:', req);
2✔
178

179
                if (!token) {
2✔
180
                        return json({ error: 'Unauthorized' }, { status: 401 });
1✔
181
                }
1✔
182

183
                const tokenResponse = await fetch(`http://${DATABASE_URL}/auth/verify?token=${token}`, {
1✔
184
                        method: 'GET'
1✔
185
                });
1✔
186
                logger.debug('Token response:', tokenResponse);
1✔
187
                if (!tokenResponse.ok) {
2!
NEW
188
                        const errorData = await tokenResponse.json().catch(() => {});
×
NEW
189
                        logger.error('Token non valido:', errorData);
×
190
                        cookies.delete('token', { path: '/' });
×
191
                        return json(
×
192
                                { error: 'Token non valido', details: errorData },
×
193
                                { status: tokenResponse.status }
×
194
                        );
×
195
                }
✔
196

197
                const response = await fetch(`http://${DATABASE_URL}/users`, {
1✔
198
                        method: 'DELETE',
1✔
199
                        headers: {
1✔
200
                                Authorization: 'Bearer ' + cookies.get('token'),
1✔
201
                                'Content-Type': 'application/json'
1✔
202
                        },
1✔
203
                        body: JSON.stringify({
1✔
204
                                delete_user: {
1✔
205
                                        _id: req.userId
1✔
206
                                },
1✔
207
                                admin: {
1✔
208
                                        current_password: req.current_password
1✔
209
                                }
1✔
210
                        })
1✔
211
                });
1✔
212
                logger.debug('Response from user deletion:', response);
1✔
213

214
                if (!response.ok) {
2!
NEW
215
                        const errorMessage = await response.json().catch(() => {});
×
NEW
216
                        logger.error('Errore durante l\'eliminazione dell\'utente:', errorMessage);
×
217
                        if (response.status === 401) {
×
218
                                return json({ error: 'Unauthorized', details: 'Unauthorized' }, { status: 401 });
×
219
                        }
×
220
                        
221
                        return json(
×
222
                                { error: "Errore durante l'eliminazione dell'utente", details: response },
×
223
                                { status: response.status }
×
224
                        );
×
225
                }
✔
226
                logger.info('Utente eliminato con successo:', req);
1✔
227
                return json({
1✔
228
                        success: true,
1✔
229
                        message: 'User deleted successfully'
1✔
230
                });
1✔
231
        } catch (error) {
2!
NEW
232
                logger.error("Errore durante l'eliminazione dell'utente:", error);
×
233
                return json({ error: 'Internal server error' }, { status: 500 });
×
234
        }
×
235
};
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc