1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use utoipa::ToSchema;
#[derive(Debug, Serialize, Deserialize, FromRow, Clone, PartialEq, ToSchema)]
pub struct NtfySettings {
pub id: i64,
pub user_id: i64,
pub enabled: bool,
pub topic: String,
pub server_url: String,
pub priority: i32,
pub title_template: Option<String>,
pub message_template: Option<String>,
}
pub struct NtfySettingsInput {
pub user_id: i64,
pub enabled: bool,
pub topic: String,
pub server_url: String,
pub priority: i32,
pub title_template: Option<String>,
pub message_template: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, FromRow, Clone, PartialEq, ToSchema)]
pub struct SmtpSettings {
pub id: i64,
pub user_id: i64,
pub enabled: bool,
pub email: String,
pub smtp_server: String,
pub smtp_port: i32,
pub username: Option<String>,
pub password: Option<String>,
pub use_tls: bool,
pub from_email: Option<String>,
pub from_name: Option<String>,
pub subject_template: Option<String>,
pub body_template: Option<String>,
}
pub struct SmtpSettingsInput {
pub user_id: i64,
pub enabled: bool,
pub email: String,
pub smtp_server: String,
pub smtp_port: i32,
pub username: Option<String>,
pub password: Option<String>,
pub use_tls: bool,
pub from_email: Option<String>,
pub from_name: Option<String>,
pub subject_template: Option<String>,
pub body_template: Option<String>,
}
pub struct NtfySettingsRepository<'a> {
pub db: &'a sqlx::SqlitePool,
}
impl<'a> NtfySettingsRepository<'a> {
pub async fn get_by_user(&self, user_id: i64) -> Result<Option<NtfySettings>, sqlx::Error> {
sqlx::query_as::<_, NtfySettings>("SELECT * FROM user_ntfy_settings WHERE user_id = ?")
.bind(user_id)
.fetch_optional(self.db)
.await
}
pub async fn create(&self, input: NtfySettingsInput) -> Result<NtfySettings, sqlx::Error> {
sqlx::query_as::<_, NtfySettings>(
"INSERT INTO user_ntfy_settings (user_id, enabled, topic, server_url, priority, title_template, message_template) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *"
)
.bind(input.user_id)
.bind(input.enabled)
.bind(input.topic)
.bind(input.server_url)
.bind(input.priority)
.bind(input.title_template)
.bind(input.message_template)
.fetch_one(self.db)
.await
}
pub async fn update(
&self,
id: i64,
input: NtfySettingsInput,
) -> Result<NtfySettings, sqlx::Error> {
sqlx::query_as::<_, NtfySettings>(
"UPDATE user_ntfy_settings SET enabled = ?, topic = ?, server_url = ?, priority = ?, title_template = ?, message_template = ? WHERE id = ? RETURNING *"
)
.bind(input.enabled)
.bind(input.topic)
.bind(input.server_url)
.bind(input.priority)
.bind(input.title_template)
.bind(input.message_template)
.bind(id)
.fetch_one(self.db)
.await
}
pub async fn delete(&self, id: i64) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM user_ntfy_settings WHERE id = ?")
.bind(id)
.execute(self.db)
.await?;
Ok(())
}
}
pub struct SmtpSettingsRepository<'a> {
pub db: &'a sqlx::SqlitePool,
}
impl<'a> SmtpSettingsRepository<'a> {
pub async fn get_by_user(&self, user_id: i64) -> Result<Option<SmtpSettings>, sqlx::Error> {
sqlx::query_as::<_, SmtpSettings>("SELECT * FROM user_smtp_settings WHERE user_id = ?")
.bind(user_id)
.fetch_optional(self.db)
.await
}
pub async fn create(&self, input: SmtpSettingsInput) -> Result<SmtpSettings, sqlx::Error> {
sqlx::query_as::<_, SmtpSettings>(
"INSERT INTO user_smtp_settings (user_id, enabled, email, smtp_server, smtp_port, username, password, use_tls, from_email, from_name, subject_template, body_template) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *"
)
.bind(input.user_id)
.bind(input.enabled)
.bind(input.email)
.bind(input.smtp_server)
.bind(input.smtp_port)
.bind(input.username)
.bind(input.password)
.bind(input.use_tls)
.bind(input.from_email)
.bind(input.from_name)
.bind(input.subject_template)
.bind(input.body_template)
.fetch_one(self.db)
.await
}
pub async fn update(
&self,
id: i64,
input: SmtpSettingsInput,
) -> Result<SmtpSettings, sqlx::Error> {
sqlx::query_as::<_, SmtpSettings>(
"UPDATE user_smtp_settings SET enabled = ?, email = ?, smtp_server = ?, smtp_port = ?, username = ?, password = ?, use_tls = ?, from_email = ?, from_name = ?, subject_template = ?, body_template = ? WHERE id = ? RETURNING *"
)
.bind(input.enabled)
.bind(input.email)
.bind(input.smtp_server)
.bind(input.smtp_port)
.bind(input.username)
.bind(input.password)
.bind(input.use_tls)
.bind(input.from_email)
.bind(input.from_name)
.bind(input.subject_template)
.bind(input.body_template)
.bind(id)
.fetch_one(self.db)
.await
}
pub async fn delete(&self, id: i64) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM user_smtp_settings WHERE id = ?")
.bind(id)
.execute(self.db)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::users::{UserRepository, UserRole};
use sqlx::{Executor, SqlitePool};
async fn setup_db() -> SqlitePool {
let pool = SqlitePool::connect(":memory:").await.unwrap();
pool.execute(
"CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL UNIQUE,
role TEXT NOT NULL DEFAULT 'user'
);",
)
.await
.unwrap();
pool.execute(
"CREATE TABLE user_ntfy_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT 0,
topic TEXT NOT NULL,
server_url TEXT NOT NULL,
priority INTEGER NOT NULL DEFAULT 5,
title_template TEXT,
message_template TEXT,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);",
)
.await
.unwrap();
pool.execute(
"CREATE TABLE user_smtp_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT 0,
email TEXT NOT NULL,
smtp_server TEXT NOT NULL,
smtp_port INTEGER NOT NULL,
username TEXT,
password TEXT,
use_tls BOOLEAN NOT NULL DEFAULT 1,
from_email TEXT,
from_name TEXT DEFAULT 'Silmätaivas Alerts',
subject_template TEXT,
body_template TEXT,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);",
)
.await
.unwrap();
pool
}
async fn create_user(pool: &SqlitePool) -> i64 {
let repo = UserRepository { db: pool };
let user = repo.create_user(None, Some(UserRole::User)).await.unwrap();
user.id
}
#[tokio::test]
async fn test_create_and_get_ntfy_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
let _settings = repo
.create(NtfySettingsInput {
user_id,
enabled: true,
topic: "topic1".to_string(),
server_url: "https://ntfy.sh".to_string(),
priority: 3,
title_template: Some("title".to_string()),
message_template: Some("msg".to_string()),
})
.await
.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
assert_eq!(fetched.topic, "topic1");
assert_eq!(fetched.server_url, "https://ntfy.sh");
assert_eq!(fetched.priority, 3);
assert_eq!(fetched.title_template, Some("title".to_string()));
assert_eq!(fetched.message_template, Some("msg".to_string()));
}
#[tokio::test]
async fn test_update_ntfy_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
let _settings = repo
.create(NtfySettingsInput {
user_id,
enabled: true,
topic: "topic1".to_string(),
server_url: "https://ntfy.sh".to_string(),
priority: 3,
title_template: None,
message_template: None,
})
.await
.unwrap();
let updated = repo
.update(
_settings.id,
NtfySettingsInput {
user_id,
enabled: false,
topic: "topic2".to_string(),
server_url: "https://ntfy2.sh".to_string(),
priority: 4,
title_template: Some("t2".to_string()),
message_template: Some("m2".to_string()),
},
)
.await
.unwrap();
assert!(!updated.enabled);
assert_eq!(updated.topic, "topic2");
assert_eq!(updated.server_url, "https://ntfy2.sh");
assert_eq!(updated.priority, 4);
assert_eq!(updated.title_template, Some("t2".to_string()));
assert_eq!(updated.message_template, Some("m2".to_string()));
}
#[tokio::test]
async fn test_delete_ntfy_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
let _settings = repo
.create(NtfySettingsInput {
user_id,
enabled: true,
topic: "topic1".to_string(),
server_url: "https://ntfy.sh".to_string(),
priority: 3,
title_template: None,
message_template: None,
})
.await
.unwrap();
repo.delete(_settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
}
#[tokio::test]
async fn test_create_and_get_smtp_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
let _settings = repo
.create(SmtpSettingsInput {
user_id,
enabled: true,
email: "test@example.com".to_string(),
smtp_server: "smtp.example.com".to_string(),
smtp_port: 587,
username: Some("user".to_string()),
password: Some("pass".to_string()),
use_tls: true,
from_email: Some("from@example.com".to_string()),
from_name: Some("Alerts".to_string()),
subject_template: Some("subj".to_string()),
body_template: Some("body".to_string()),
})
.await
.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
assert_eq!(fetched.email, "test@example.com");
assert_eq!(fetched.smtp_server, "smtp.example.com");
assert_eq!(fetched.smtp_port, 587);
assert_eq!(fetched.username, Some("user".to_string()));
assert_eq!(fetched.password, Some("pass".to_string()));
assert!(fetched.use_tls);
assert_eq!(fetched.from_email, Some("from@example.com".to_string()));
assert_eq!(fetched.from_name, Some("Alerts".to_string()));
assert_eq!(fetched.subject_template, Some("subj".to_string()));
assert_eq!(fetched.body_template, Some("body".to_string()));
}
#[tokio::test]
async fn test_update_smtp_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
let _settings = repo
.create(SmtpSettingsInput {
user_id,
enabled: true,
email: "test@example.com".to_string(),
smtp_server: "smtp.example.com".to_string(),
smtp_port: 587,
username: None,
password: None,
use_tls: true,
from_email: None,
from_name: None,
subject_template: None,
body_template: None,
})
.await
.unwrap();
let updated = repo
.update(
_settings.id,
SmtpSettingsInput {
user_id,
enabled: false,
email: "other@example.com".to_string(),
smtp_server: "smtp2.example.com".to_string(),
smtp_port: 465,
username: Some("u2".to_string()),
password: Some("p2".to_string()),
use_tls: false,
from_email: Some("f2@example.com".to_string()),
from_name: Some("N2".to_string()),
subject_template: Some("s2".to_string()),
body_template: Some("b2".to_string()),
},
)
.await
.unwrap();
assert!(!updated.enabled);
assert_eq!(updated.email, "other@example.com");
assert_eq!(updated.smtp_server, "smtp2.example.com");
assert_eq!(updated.smtp_port, 465);
assert_eq!(updated.username, Some("u2".to_string()));
assert_eq!(updated.password, Some("p2".to_string()));
assert!(!updated.use_tls);
assert_eq!(updated.from_email, Some("f2@example.com".to_string()));
assert_eq!(updated.from_name, Some("N2".to_string()));
assert_eq!(updated.subject_template, Some("s2".to_string()));
assert_eq!(updated.body_template, Some("b2".to_string()));
}
#[tokio::test]
async fn test_delete_smtp_settings() {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
let _settings = repo
.create(SmtpSettingsInput {
user_id,
enabled: true,
email: "test@example.com".to_string(),
smtp_server: "smtp.example.com".to_string(),
smtp_port: 587,
username: None,
password: None,
use_tls: true,
from_email: None,
from_name: None,
subject_template: None,
body_template: None,
})
.await
.unwrap();
repo.delete(_settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
}
}
|